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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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