[1] | 1 | /* |
---|
| 2 | * Demonstration program for Nano-X graphics. |
---|
| 3 | */ |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | #define MWINCLUDECOLORS |
---|
| 8 | #include <microwin/nano-X.h> |
---|
| 9 | #include <GL/gl.h> |
---|
| 10 | #include <GL/nglx.h> |
---|
| 11 | #include "ui.h" |
---|
| 12 | |
---|
| 13 | static GR_WINDOW_ID w1; /* id for large window */ |
---|
| 14 | static GR_GC_ID gc1; /* graphics context for text */ |
---|
| 15 | |
---|
| 16 | void errorcatcher(); /* routine to handle errors */ |
---|
| 17 | |
---|
| 18 | void tkSwapBuffers(void) |
---|
| 19 | { |
---|
| 20 | nglXSwapBuffers(w1); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | int |
---|
| 24 | ui_loop(int argc,char **argv, const char *name) |
---|
| 25 | { |
---|
| 26 | GR_EVENT event; /* current event */ |
---|
| 27 | GR_IMAGE_ID id = 0; |
---|
| 28 | NGLXContext cx; |
---|
| 29 | int width, height, k; |
---|
| 30 | |
---|
| 31 | if (GrOpen() < 0) { |
---|
| 32 | fprintf(stderr, "cannot open graphics\n"); |
---|
| 33 | exit(1); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | width = 400; |
---|
| 37 | height = 300; |
---|
| 38 | |
---|
| 39 | GrSetErrorHandler(errorcatcher); |
---|
| 40 | |
---|
| 41 | w1 = GrNewWindow(GR_ROOT_WINDOW_ID, 10, 10, width, height, 4, BLACK, WHITE); |
---|
| 42 | |
---|
| 43 | GrSelectEvents(w1, GR_EVENT_MASK_CLOSE_REQ|GR_EVENT_MASK_EXPOSURE|GR_EVENT_MASK_KEY_DOWN); |
---|
| 44 | |
---|
| 45 | GrMapWindow(w1); |
---|
| 46 | |
---|
| 47 | gc1 = GrNewGC(); |
---|
| 48 | |
---|
| 49 | GrSetGCForeground(gc1, WHITE); |
---|
| 50 | |
---|
| 51 | cx = nglXCreateContext(NULL, 0); |
---|
| 52 | nglXMakeCurrent(w1, cx); |
---|
| 53 | |
---|
| 54 | init(); |
---|
| 55 | reshape(width, height); |
---|
| 56 | |
---|
| 57 | while (1) { |
---|
| 58 | GrCheckNextEvent(&event); |
---|
| 59 | switch(event.type) { |
---|
| 60 | case GR_EVENT_TYPE_CLOSE_REQ: |
---|
| 61 | GrFreeImage(id); |
---|
| 62 | GrClose(); |
---|
| 63 | exit(0); |
---|
| 64 | case GR_EVENT_TYPE_EXPOSURE: |
---|
| 65 | break; |
---|
| 66 | case GR_EVENT_TYPE_KEY_DOWN: |
---|
| 67 | { |
---|
| 68 | GR_EVENT_KEYSTROKE *kp = &event.keystroke; |
---|
| 69 | /* XXX: nanoX special keys are totally bugged ! */ |
---|
| 70 | switch(kp->ch) { |
---|
| 71 | case 81: |
---|
| 72 | k = KEY_LEFT; |
---|
| 73 | break; |
---|
| 74 | case 83: |
---|
| 75 | k = KEY_RIGHT; |
---|
| 76 | break; |
---|
| 77 | case 82: |
---|
| 78 | k = KEY_UP; |
---|
| 79 | break; |
---|
| 80 | case 84: |
---|
| 81 | k = KEY_DOWN; |
---|
| 82 | break; |
---|
| 83 | default: |
---|
| 84 | k = kp->ch; |
---|
| 85 | break; |
---|
| 86 | } |
---|
| 87 | key(k, 0); |
---|
| 88 | } |
---|
| 89 | break; |
---|
| 90 | default: |
---|
| 91 | idle(); |
---|
| 92 | break; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | return 0; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /* |
---|
| 101 | * Here on an unrecoverable error. |
---|
| 102 | */ |
---|
| 103 | void |
---|
| 104 | errorcatcher(code, name, id) |
---|
| 105 | GR_ERROR code; /* error code */ |
---|
| 106 | GR_FUNC_NAME name; /* function name which failed */ |
---|
| 107 | GR_ID id; /* resource id */ |
---|
| 108 | { |
---|
| 109 | GrClose(); |
---|
| 110 | fprintf(stderr, "DEMO ERROR: code %d, function %s, resource id %d\n", |
---|
| 111 | code, name, id); |
---|
| 112 | exit(1); |
---|
| 113 | } |
---|