OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [demos/] [nanox/] [demo5.c] - Blame information for rev 673

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Demonstration program for off screen drawing with Nano-X. Based on demo.c
3
 */
4
#include <stdio.h>
5
#include <stdlib.h>
6
#define MWINCLUDECOLORS
7
#include "nano-X.h"
8
 
9
/*
10
 * Definitions to make it easy to define cursors
11
 */
12
#define _       ((unsigned) 0)          /* off bits */
13
#define X       ((unsigned) 1)          /* on bits */
14
#define MASK(a,b,c,d,e,f,g) \
15
        (((((((((((((a * 2) + b) * 2) + c) * 2) + d) * 2) \
16
        + e) * 2) + f) * 2) + g) << 9)
17
 
18
#define W2_WIDTH        70
19
#define W2_HEIGHT       40
20
 
21
 
22
static  GR_WINDOW_ID    w1;             /* id for large window */
23
static  GR_WINDOW_ID    w2;             /* id for small window */
24
static  GR_WINDOW_ID    w3;             /* id for third window */
25
static  GR_WINDOW_ID    w4;             /* id for grabbable window */
26
static  GR_WINDOW_ID    w5;             /* id for testing enter/exit window */
27
static  GR_WINDOW_ID    p1;             /* off screen pixmap */
28
static  GR_GC_ID        gc1;            /* graphics context for text */
29
static  GR_GC_ID        gc2;            /* graphics context for rectangle */
30
static  GR_GC_ID        gc3;            /* graphics context for circles */
31
static  GR_GC_ID        gc4;            /* graphics context for lines */
32
static  GR_COORD        begxpos;        /* beginning x position */
33
static  GR_COORD        xpos;           /* x position for text drawing */
34
static  GR_COORD        ypos;           /* y position for text drawing */
35
static  GR_COORD        linexpos;       /* x position for line drawing */
36
static  GR_COORD        lineypos;       /* y position for line drawing */
37
static  GR_COORD        xorxpos;        /* x position for xor line */
38
static  GR_COORD        xorypos;        /* y position for xor line */
39
static  GR_BOOL         lineok;         /* ok to draw line */
40
static  GR_SCREEN_INFO  si;             /* information about screen */
41
 
42
void do_buttondown();
43
void do_buttonup();
44
void do_motion();
45
void do_keystroke();
46
void do_exposure();
47
void do_focusin();
48
void do_focusout();
49
void do_enter();
50
void do_exit();
51
void do_idle();
52
 
53
int
54
main(int argc,char **argv)
55
{
56
        GR_EVENT        event;          /* current event */
57
        GR_BITMAP       bitmap1fg[7];   /* bitmaps for first cursor */
58
        GR_BITMAP       bitmap1bg[7];
59
        GR_BITMAP       bitmap2fg[7];   /* bitmaps for second cursor */
60
        GR_BITMAP       bitmap2bg[7];
61
 
62
        if (GrOpen() < 0) {
63
                fprintf(stderr, "cannot open graphics\n");
64
                exit(1);
65
        }
66
 
67
        GrGetScreenInfo(&si);
68
 
69
        w1 = GrNewWindow(GR_ROOT_WINDOW_ID, 100, 50, si.cols - 120,
70
                si.rows - 60, 1, BROWN, WHITE);
71
        w2 = GrNewWindow(GR_ROOT_WINDOW_ID, 6, 6, W2_WIDTH, W2_HEIGHT, 2, GREEN,
72
                WHITE);
73
        w3 = GrNewWindow(GR_ROOT_WINDOW_ID, 250, 30, 80, 100, 1, LTGRAY,
74
                GREEN);
75
        w4 = GrNewWindow(GR_ROOT_WINDOW_ID, 350, 20, 200, 150, 5, BLACK, WHITE);
76
        w5 = GrNewWindow(GR_ROOT_WINDOW_ID, 11, 143, 209, 100, 1, BLUE, GREEN);
77
 
78
        p1 = GrNewPixmap(200,200,0);
79
 
80
        GrSelectEvents(w1, GR_EVENT_MASK_BUTTON_DOWN |
81
                GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_EXPOSURE |
82
                GR_EVENT_MASK_FOCUS_IN | GR_EVENT_MASK_FOCUS_OUT |
83
                GR_EVENT_MASK_CLOSE_REQ);
84
        GrSelectEvents(w2, GR_EVENT_MASK_BUTTON_UP | GR_EVENT_MASK_CLOSE_REQ);
85
        GrSelectEvents(w3, GR_EVENT_MASK_BUTTON_DOWN |
86
                GR_EVENT_MASK_MOUSE_MOTION | GR_EVENT_MASK_CLOSE_REQ);
87
        GrSelectEvents(w4, GR_EVENT_MASK_BUTTON_DOWN |
88
                GR_EVENT_MASK_BUTTON_UP | GR_EVENT_MASK_MOUSE_POSITION |
89
                GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_CLOSE_REQ);
90
        GrSelectEvents(w5, GR_EVENT_MASK_MOUSE_ENTER |
91
                GR_EVENT_MASK_MOUSE_EXIT | GR_EVENT_MASK_CLOSE_REQ);
92
        GrSelectEvents(GR_ROOT_WINDOW_ID, GR_EVENT_MASK_BUTTON_DOWN |
93
                        GR_EVENT_MASK_CLOSE_REQ);
94
 
95
        GrMapWindow(w1);
96
        GrMapWindow(w2);
97
        GrMapWindow(w3);
98
        GrMapWindow(w4);
99
        GrMapWindow(w5);
100
 
101
        gc1 = GrNewGC();
102
        gc2 = GrNewGC();
103
        gc3 = GrNewGC();
104
        gc4 = GrNewGC();
105
 
106
        GrSetGCForeground(gc1, RED);
107
        GrSetGCBackground(gc1, BROWN);
108
        GrSetGCForeground(gc2, MAGENTA);
109
        GrSetGCMode(gc4, GR_MODE_XOR);
110
 
111
        bitmap1fg[0] = MASK(_,_,_,X,_,_,_);
112
        bitmap1fg[1] = MASK(_,_,_,X,_,_,_);
113
        bitmap1fg[2] = MASK(_,_,_,X,_,_,_);
114
        bitmap1fg[3] = MASK(X,X,X,X,X,X,X);
115
        bitmap1fg[4] = MASK(_,_,_,X,_,_,_);
116
        bitmap1fg[5] = MASK(_,_,_,X,_,_,_);
117
        bitmap1fg[6] = MASK(_,_,_,X,_,_,_);
118
 
119
        bitmap1bg[0] = MASK(_,_,X,X,X,_,_);
120
        bitmap1bg[1] = MASK(_,_,X,X,X,_,_);
121
        bitmap1bg[2] = MASK(X,X,X,X,X,X,X);
122
        bitmap1bg[3] = MASK(X,X,X,X,X,X,X);
123
        bitmap1bg[4] = MASK(X,X,X,X,X,X,X);
124
        bitmap1bg[5] = MASK(_,_,X,X,X,_,_);
125
        bitmap1bg[6] = MASK(_,_,X,X,X,_,_);
126
 
127
        bitmap2fg[0] = MASK(_,_,X,X,X,_,_);
128
        bitmap2fg[1] = MASK(_,X,_,_,_,X,_);
129
        bitmap2fg[2] = MASK(X,_,_,_,_,_,X);
130
        bitmap2fg[3] = MASK(X,_,_,_,_,_,X);
131
        bitmap2fg[4] = MASK(_,X,_,_,_,X,_);
132
        bitmap2fg[5] = MASK(_,_,X,X,X,_,_);
133
 
134
        bitmap2bg[0] = MASK(_,_,X,X,X,_,_);
135
        bitmap2bg[1] = MASK(_,X,X,X,X,X,_);
136
        bitmap2bg[2] = MASK(X,X,X,X,X,X,X);
137
        bitmap2bg[3] = MASK(X,X,X,X,X,X,X);
138
        bitmap2bg[4] = MASK(_,X,X,X,X,X,_);
139
        bitmap2bg[5] = MASK(_,_,X,X,X,_,_);
140
 
141
        GrSetCursor(w1, 7, 7, 3, 3, WHITE, BLACK, bitmap1fg, bitmap1bg);
142
        GrSetCursor(w2, 7, 6, 3, 3, WHITE, BLACK, bitmap2fg, bitmap2bg);
143
 
144
        GrRect(GR_ROOT_WINDOW_ID, gc1, 0, 0, si.cols, si.rows);
145
 
146
 
147
        while (1) {
148
                GrGetNextEvent(&event);
149
 
150
                switch (event.type) {
151
                        case GR_EVENT_TYPE_BUTTON_DOWN:
152
                                do_buttondown(&event.button);
153
                                break;
154
 
155
                        case GR_EVENT_TYPE_BUTTON_UP:
156
                                do_buttonup(&event.button);
157
                                break;
158
 
159
                        case GR_EVENT_TYPE_MOUSE_POSITION:
160
                        case GR_EVENT_TYPE_MOUSE_MOTION:
161
                                do_motion(&event.mouse);
162
                                break;
163
 
164
                        case GR_EVENT_TYPE_KEY_DOWN:
165
                                do_keystroke(&event.keystroke);
166
                                break;
167
 
168
                        case GR_EVENT_TYPE_EXPOSURE:
169
                                do_exposure(&event.exposure);
170
                                break;
171
 
172
                        case GR_EVENT_TYPE_FOCUS_IN:
173
                                do_focusin(&event.general);
174
                                break;
175
 
176
                        case GR_EVENT_TYPE_FOCUS_OUT:
177
                                do_focusout(&event.general);
178
                                break;
179
 
180
                        case GR_EVENT_TYPE_MOUSE_ENTER:
181
                                do_enter(&event.general);
182
                                break;
183
 
184
                        case GR_EVENT_TYPE_MOUSE_EXIT:
185
                                do_exit(&event.general);
186
                                break;
187
 
188
                        case GR_EVENT_TYPE_CLOSE_REQ:
189
                                GrClose();
190
                                exit(0);
191
 
192
                        case GR_EVENT_TYPE_NONE:
193
                                do_idle();
194
                                break;
195
                }
196
        }
197
}
198
 
199
 
200
/*
201
 * Here when a button is pressed.
202
 */
203
void
204
do_buttondown(bp)
205
        GR_EVENT_BUTTON *bp;
206
{
207
        GR_PIXELVAL     intable[W2_WIDTH * W2_HEIGHT];
208
        GR_PIXELVAL     outtable[W2_WIDTH * W2_HEIGHT * 6];
209
        GR_PIXELVAL     *inp;
210
        GR_PIXELVAL     *outp;
211
        GR_PIXELVAL     *oldinp;
212
        GR_COORD        row;
213
        GR_COORD        col;
214
 
215
        /*static int xx = 100;
216
        static int yy = 50;*/
217
 
218
        if (bp->wid == w3) {
219
                GrRaiseWindow(w3);
220
                GrReadArea(w2, 0, 0, W2_WIDTH, W2_HEIGHT, intable);
221
                inp = intable;
222
                outp = outtable;
223
                for (row = 0; row < W2_HEIGHT; row++) {
224
                        oldinp = inp;
225
                        for (col = 0; col < W2_WIDTH; col++) {
226
                                *outp++ = *inp;
227
                                *outp++ = *inp++;
228
                        }
229
                        inp = oldinp;
230
                        for (col = 0; col < W2_WIDTH; col++) {
231
                                *outp++ = *inp;
232
                                *outp++ = *inp++;
233
                        }
234
                        inp = oldinp;
235
                        for (col = 0; col < W2_WIDTH; col++) {
236
                                *outp++ = *inp;
237
                                *outp++ = *inp++;
238
                        }
239
                }
240
                GrArea(w1, gc1, 0, 0, W2_WIDTH * 2, W2_HEIGHT * 3, outtable,
241
                        MWPF_PIXELVAL);
242
                return;
243
        }
244
 
245
        if (bp->wid == w4) {
246
                /* Draw a line in the off screen pixmap. Won't be shown until the button
247
                 * is released
248
                 */
249
 
250
                GrLine(p1, gc1, 0,0, bp->x, bp->y);
251
 
252
                GrRaiseWindow(w4);
253
                linexpos = bp->x;
254
                lineypos = bp->y;
255
                xorxpos = bp->x;
256
                xorypos = bp->y;
257
                GrLine(w4, gc4, xorxpos, xorypos, linexpos, lineypos);
258
                lineok = GR_TRUE;
259
                return;
260
        }
261
 
262
        if (bp->wid != w1) {
263
                /*
264
                 * Cause a fatal error for testing if more than one
265
                 * button is pressed.
266
                 */
267
                if ((bp->buttons & -((int) bp->buttons)) != bp->buttons)
268
                        GrClearWindow(-1, 0);
269
                return;
270
        }
271
 
272
        GrRaiseWindow(w1);
273
        /*GrMoveWindow(w1, ++xx, yy);*/
274
 
275
        if (bp->buttons & GR_BUTTON_L) {
276
                GrClearWindow(w1, GR_TRUE);
277
                return;
278
        }
279
 
280
        begxpos = bp->x;
281
        xpos = bp->x;
282
        ypos = bp->y;
283
}
284
 
285
 
286
/*
287
 * Here when a button is released.
288
 */
289
void
290
do_buttonup(bp)
291
        GR_EVENT_BUTTON *bp;
292
{
293
        if (bp->wid == w4) {
294
                if (lineok) {
295
                        GrLine(w4, gc4, xorxpos, xorypos, linexpos, lineypos);
296
                        GrLine(w4, gc3, bp->x, bp->y, linexpos, lineypos);
297
                }
298
                lineok = GR_FALSE;
299
                GrCopyArea(w1, gc4, 200,200, 200,200, p1,0,0,MWROP_SRCCOPY);
300
                return;
301
        }
302
 
303
        if (bp->wid == w2) {
304
                GrClose();
305
                exit(0);
306
        }
307
}
308
 
309
 
310
/*
311
 * Here when the mouse has a motion event.
312
 */
313
void
314
do_motion(mp)
315
        GR_EVENT_MOUSE  *mp;
316
{
317
        if (mp->wid == w4) {
318
                if (lineok) {
319
                        GrLine(w4, gc4, xorxpos, xorypos, linexpos, lineypos);
320
                        xorxpos = mp->x;
321
                        xorypos = mp->y;
322
                        GrLine(w4, gc4, xorxpos, xorypos, linexpos, lineypos);
323
                }
324
                return;
325
        }
326
 
327
        if (mp->wid == w3) {
328
                GrPoint(w3, gc3, mp->x, mp->y);
329
                return;
330
        }
331
}
332
 
333
 
334
/*
335
 * Here when a keyboard press occurs.
336
 */
337
void
338
do_keystroke(kp)
339
        GR_EVENT_KEYSTROKE      *kp;
340
{
341
        GR_SIZE         width;          /* width of character */
342
        GR_SIZE         height;         /* height of character */
343
        GR_SIZE         base;           /* height of baseline */
344
 
345
        if (kp->wid == w4) {
346
                if (lineok) {
347
                        GrLine(w4, gc4, xorxpos, xorypos, linexpos, lineypos);
348
                        lineok = GR_FALSE;
349
                }
350
                return;
351
        }
352
 
353
        GrGetGCTextSize(gc1, &kp->ch, 1, GR_TFASCII, &width, &height, &base);
354
        if ((kp->ch == '\r') || (kp->ch == '\n')) {
355
                xpos = begxpos;
356
                ypos += height;
357
                return;
358
        }
359
        if (kp->ch == '\b') {           /* assumes fixed width font!! */
360
                if (xpos <= begxpos)
361
                        return;
362
                xpos -= width;
363
                GrSetGCForeground(gc3, BROWN);
364
                GrFillRect(w1, gc3, xpos, ypos - height + base + 1,
365
                        width, height);
366
                return;
367
        }
368
        GrText(w1, gc1, xpos, ypos + base, &kp->ch, 1, 0);
369
        xpos += width;
370
}
371
 
372
 
373
/*
374
 * Here when an exposure event occurs.
375
 */
376
void
377
do_exposure(ep)
378
        GR_EVENT_EXPOSURE       *ep;
379
{
380
        GR_POINT        points[3];
381
 
382
        if (ep->wid != w1)
383
                return;
384
        points[0].x = 311;
385
        points[0].y = 119;
386
        points[1].x = 350;
387
        points[1].y = 270;
388
        points[2].x = 247;
389
        points[2].y = 147;
390
 
391
        GrFillRect(w1, gc2, 50, 50, 150, 200);
392
        GrFillPoly(w1, gc2, 3, points);
393
}
394
 
395
 
396
/*
397
 * Here when a focus in event occurs.
398
 */
399
void
400
do_focusin(gp)
401
        GR_EVENT_GENERAL        *gp;
402
{
403
        if (gp->wid != w1)
404
                return;
405
        GrSetBorderColor(w1, WHITE);
406
}
407
 
408
/*
409
 * Here when a focus out event occurs.
410
 */
411
void
412
do_focusout(gp)
413
        GR_EVENT_GENERAL        *gp;
414
{
415
        if (gp->wid != w1)
416
                return;
417
        GrSetBorderColor(w1, GRAY);
418
}
419
 
420
 
421
/*
422
 * Here when a enter window event occurs.
423
 */
424
void
425
do_enter(gp)
426
        GR_EVENT_GENERAL        *gp;
427
{
428
        if (gp->wid != w5)
429
                return;
430
        GrSetBorderColor(w5, WHITE);
431
        GrRaiseWindow(w5);
432
}
433
 
434
 
435
/*
436
 * Here when a exit window event occurs.
437
 */
438
void
439
do_exit(gp)
440
        GR_EVENT_GENERAL        *gp;
441
{
442
        if (gp->wid != w5)
443
                return;
444
        GrSetBorderColor(w5, GREEN);
445
        GrLowerWindow(w5);
446
}
447
 
448
 
449
/*
450
 * Here to do an idle task when nothing else is happening.
451
 * Just draw a randomly colored filled circle in the small window.
452
 */
453
void
454
do_idle()
455
{
456
        GR_COORD        x;
457
        GR_COORD        y;
458
        GR_SIZE         rx;
459
        GR_SIZE         ry;
460
        GR_COLOR        color;
461
 
462
        x = rand() % 70;
463
        y = rand() % 40;
464
        rx = (rand() % 10) + 5;
465
        ry = (rx * si.ydpcm) / si.xdpcm;        /* make it appear circular */
466
 
467
        color = rand() % si.ncolors;
468
 
469
        GrSetGCForeground(gc3, MWPALINDEX(color));
470
        GrFillEllipse(w2, gc3, x, y, rx, ry);
471
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.