// check for events & delete the event queue static void check_event() { if (GrPeekEvent(&break_event)) { GrGetNextEventTimeout(&break_event, 1000); if (break_event.type != GR_EVENT_TYPE_TIMEOUT) { pz_event_handler(&break_event); // delete the rest of the event queue while ( break_event.type != GR_EVENT_TYPE_NONE ) GrGetNextEventTimeout(&break_event, 0); } } }
void run_game(nstate *state) { while(state->state == STATE_RUNNING) { GrGetNextEventTimeout(&state->event, timeout_delay(state)); handle_event(state); if(state->state == STATE_PAUSED) do_pause(state); if(state->state == STATE_RUNNING) do_update(state); } }
int main(int ac, char **av) { GR_EVENT event; GR_GC_ID gc; GR_WINDOW_ID pmap; GR_WINDOW_ID window; GR_WINDOW_INFO info; if (GrOpen() < 0) { fprintf(stderr, "cannot open graphics\n"); exit(1); } window = nxCreateAppWindow(&ac, &av, args); gc = GrNewGC(); GrSetGCForeground(gc, GrGetSysColor(GR_COLOR_WINDOW)); GrSetGCBackground(gc, GrGetSysColor(GR_COLOR_WINDOWTEXT)); GrSelectEvents(window, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_UPDATE | GR_EVENT_MASK_CLOSE_REQ); GrGetWindowInfo(window, &info); pmap = resize(info.width, info.height, 0); GrMapWindow(window); while (1) { GrGetNextEventTimeout(&event, 500L); switch (event.type) { case GR_EVENT_TYPE_EXPOSURE: case GR_EVENT_TYPE_TIMEOUT: draw_clock(0, 0, width, height, pmap, gc, window); break; case GR_EVENT_TYPE_CLOSE_REQ: GrClose(); exit(0); case GR_EVENT_TYPE_UPDATE: switch (event.update.utype) { case GR_UPDATE_SIZE: pmap = resize(event.update.width, event.update.height, pmap); break; } } } return 0; }
static void video_check_messages() { GR_EVENT event; int evtcap = 200; video_vol_delta = 0; while (GrPeekEvent(&event) && evtcap--) { GrGetNextEventTimeout(&event, 500); if (event.type != GR_EVENT_TYPE_TIMEOUT) { pz_event_handler(&event); } else { break; } } if (video_vol_delta != 0) { audio_adjustVolume(video_vol_delta); } }
static void mp3_event_handler() { GR_EVENT event; int evtcap = 200; vol_delta = 0; while (GrPeekEvent(&event) && evtcap--) { GrGetNextEventTimeout(&event, 1000); if (event.type != GR_EVENT_TYPE_TIMEOUT) { pz_event_handler(&event); } else { break; } } if (vol_delta != 0) { mp3_refresh_state(); } }
void generate_tone() { int nsamples; GR_EVENT event; int freq = 0; // struct audio_buf_info info; // char buf[20]; while(!generator_paused) { if(generator_freq != freq) { freq = generator_freq; nsamples = make_wave_buffer(generator_freq); //add wave types here - tbd } // we need to use ioctl to get available space and ensure we dont write too much to improve response // times while changing freq/notes. to be done // ioctl(dspz.dsp, SNDCTL_DSP_GETOSPACE, &info); // sprintf(buf, "f:%d, s/f: %d", info.fragstotal, info.fragsize); // sprintf(buf, "afs: %d, ab: %d", info.fragments, info.bytes); // generator_draw_text(buf); dsp_write( &dspz, wave_buffer, sizeof(short)*nsamples); // dont write block if(GrPeekEvent(&event)) { repeat: GrGetNextEventTimeout(&event, 1000); if (event.type != GR_EVENT_TYPE_TIMEOUT) { pz_event_handler(&event); if (GrPeekEvent(&event)) goto repeat; } } } if (generator_quit_app) // menu button in the middle of a play generator_button_menu();//get out of the app // better way to get out of the event handler - tbd }
void show_jpeg(int client, char *file, int show_time) { GR_EVENT event; /* current event */ GR_IMAGE_ID id = 0; GR_SIZE w = -1; GR_SIZE h = -1; GR_IMAGE_INFO info; GR_WINDOW_ID w1; /* id for large window */ GR_GC_ID gc1; /* graphics context for text */ GR_SCREEN_INFO si; int time_left; bool ever_exposed = false; #if !defined(HAVE_JPEG_SUPPORT) && !defined(HAVE_BMP_SUPPORT) && !defined(HAVE_GIF_SUPPORT) printf("Sorry, no image support compiled in\n"); exit(1); #endif GrGetScreenInfo(&si); printf("Loading image: %s\n", file); if (access(file, F_OK) < 0) { fdprintf(client, "Can't access \"%s\": %s\n", file, strerror(errno)); return; } id = GrLoadImageFromFile(file, 0); if (id) { GrGetImageInfo(id, &info); } else { // File exists, so why the error? int fd, len; char buf[64]; fdprintf(client, "Can't load %s\n", file); if ((fd = open(file, O_RDONLY)) >= 0) { len = read(fd, buf, 64); if (len != 64) { diag_printf("Short read? len = %d\n", len); } else { diag_dump_buf(buf, len); } close(fd); } else { diag_printf("Can't oopen \"%s\": %s\n", file, strerror(errno)); } return; } w = info.width; h = info.height; if ((si.rows < info.height) || (si.cols < info.width)) { // Preserve aspect ratio if (si.cols < info.width) { w = si.cols; h = (si.cols * info.height) / info.width; } if (si.rows < h) { w = (si.rows * w) / h; h = si.rows; } } printf("Create window - orig %dx%d => %dx%d\n", info.width, info.height, w, h); fdprintf(client, "<INFO> Display \"%s\" - orig %dx%d => %dx%d\n", file, info.width, info.height, w, h); w1 = GrNewWindow(GR_ROOT_WINDOW_ID, 10, 10, w, h, 4, BLACK, WHITE); GrSelectEvents(w1, GR_EVENT_MASK_CLOSE_REQ|GR_EVENT_MASK_EXPOSURE); GrMapWindow(w1); gc1 = GrNewGC(); GrSetGCForeground(gc1, WHITE); #define TO_MS 50 time_left = show_time * 1000; while (time_left > 0) { GrGetNextEventTimeout(&event, TO_MS); // milliseconds switch(event.type) { case GR_EVENT_TYPE_CLOSE_REQ: GrDestroyWindow(w1); GrFreeImage(id); return; /* no return*/ case GR_EVENT_TYPE_EXPOSURE: /*GrDrawImageFromFile(w1, gc1, 0, 0, w, h, argv[1],0);*/ GrDrawImageToFit(w1, gc1, 0, 0, w, h, id); ever_exposed = true; break; default: case GR_EVENT_TYPE_NONE: case GR_EVENT_TYPE_TIMEOUT: time_left -= TO_MS; if ((time_left < 0) && !ever_exposed) { // Things get real cranky if we delete the window too fast! time_left = TO_MS; } break; } } GrUnmapWindow(w1); GrDestroyWindow(w1); GrDestroyGC(gc1); GrFreeImage(id); }
int main(int ac, char **av) { GR_EVENT event; /* current event */ GR_BITMAP bitmap1fg[7]; /* bitmaps for first cursor */ GR_BITMAP bitmap1bg[7]; if (GrOpen() < 0) { fprintf(stderr, "cannot open graphics\n"); exit(1); } /* create window*/ w1 = GrNewWindowEx( GR_WM_PROPS_NOAUTOMOVE|GR_WM_PROPS_BORDER|GR_WM_PROPS_CAPTION| GR_WM_PROPS_CLOSEBOX, "nxclock", GR_ROOT_WINDOW_ID, 10, 10, CWIDTH, CHEIGHT, GrGetSysColor(GR_COLOR_WINDOW)); GrSelectEvents(w1, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_CLOSE_REQ); gc1 = GrNewGC(); gc2 = GrNewGC(); GrSetGCForeground(gc1, GrGetSysColor(GR_COLOR_WINDOW)); GrSetGCBackground(gc1, GrGetSysColor(GR_COLOR_WINDOWTEXT)); GrSetGCForeground(gc2, GrGetSysColor(GR_COLOR_WINDOWTEXT)); GrSetGCBackground(gc2, GrGetSysColor(GR_COLOR_WINDOW)); bitmap1bg[0] = MASK(_,_,X,X,X,_,_); bitmap1bg[1] = MASK(_,X,X,X,X,X,_); bitmap1bg[2] = MASK(X,X,X,X,X,X,X); bitmap1bg[3] = MASK(X,X,X,X,X,X,X); bitmap1bg[4] = MASK(X,X,X,X,X,X,X); bitmap1bg[5] = MASK(_,X,X,X,X,X,_); bitmap1bg[6] = MASK(_,_,X,X,X,_,_); bitmap1fg[0] = MASK(_,_,_,X,_,_,_); bitmap1fg[1] = MASK(_,X,_,X,_,X,_); bitmap1fg[2] = MASK(_,_,_,X,_,_,_); bitmap1fg[3] = MASK(X,_,_,X,X,_,X); bitmap1fg[4] = MASK(_,_,_,_,_,_,_); bitmap1fg[5] = MASK(_,X,_,_,_,X,_); bitmap1fg[6] = MASK(_,_,_,X,_,_,_); GrSetCursor(w1, 7, 7, 3, 3, WHITE, BLACK, bitmap1fg, bitmap1bg); GrMapWindow(w1); while (1) { GrGetNextEventTimeout(&event, 500L); switch (event.type) { case GR_EVENT_TYPE_EXPOSURE: do_exposure(&event.exposure); break; case GR_EVENT_TYPE_CLOSE_REQ: GrClose(); exit(0); case GR_EVENT_TYPE_TIMEOUT: do_clock(); break; } } }
int new_snake_window(int argc, char **argv) { GR_SCREEN_INFO si; fin = 0; snake_count = 0; game_speed = start_speed; GrGetScreenInfo(&si); /* Get screen info */ srand(time(0)); game_state = SNAKE_START; /* Make the window */ /*swindow = GrNewWindowEx(WM_PROPS, "nxsnake", GR_ROOT_WINDOW_ID, 10, 10, WWIDTH, WHEIGHT, BLACK);*/ swindow = pz_new_window (0,HEADER_TOPLINE+1,si.cols,si.rows-HEADER_TOPLINE-1, do_draw,nxsnake_handle_event); GrSelectEvents(swindow, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_KEY_DOWN|GR_EVENT_MASK_TIMER); nxsnake_timer = GrCreateTimer( swindow, 5 ); offscreen = pz_new_window (0,HEADER_TOPLINE+1,si.cols,si.rows-HEADER_TOPLINE-1, do_draw,nxsnake_handle_event); //offscreen = GrNewPixmap(WWIDTH, WHEIGHT, 0); do_draw(); GrMapWindow(swindow); GrMapWindow(offscreen); /* Draw the instructions into the buffer */ draw_string( (char *) welcome, 1); while(fin != 1) { GR_EVENT event; /* We start at 130ms, but it goes down every */ /* time a nibble is eaten */ /* If they get this far, then they rock! */ if (game_speed < 5) game_speed = 5; GrGetNextEventTimeout(&event, game_speed); switch(event.type) { case GR_EVENT_TYPE_EXPOSURE: case GR_EVENT_TYPE_KEY_DOWN: nxsnake_handle_event(&event); break; case GR_EVENT_TYPE_TIMER: handle_idle(); break; } } return(1); }
int main(int argc, char **argv) { GR_WINDOW_ID main_wid; GR_TIMEOUT timeout; int width, height; if (argc != 2) return (-1); if (GrOpen() == -1) return (-1); font = GrCreateFontEx(argv[1], 12, 12, 0); if (!font) printf("Unable to load %s\n", argv[1]); GrGetFontInfo(font, &finfo); printf("font_id = %d\n", font); printf("First char = %d, last char = %d\n", finfo.firstchar, finfo.lastchar); printf("Max width = %d, max height = %d\n", finfo.maxwidth, finfo.height); printf("baseline = ascent = %d, descent = %d\n", finfo.baseline, finfo.descent); printf("max ascent = %d, max descent = %d\n", finfo.maxascent, finfo.maxdescent); printf("linespacing = %d, fixed = %s\n", finfo.linespacing, finfo.fixed? "yes": "no"); // finfo.firstchar = 0; /* force display of undefined chars, test with jiskan24.pcf.gz*/ /* determine window metrics*/ width = (finfo.maxwidth + spacer) * line_width + 2 * border - spacer; if (width > 640) { line_width /= 2; lines *= 2; width = (finfo.maxwidth + 2) * line_width + 2 * border - spacer; } height = lines * (finfo.height + spacer) + 2 * border - spacer; chars_to_show = lines * line_width; /* create the main application window*/ main_wid = GrNewWindowEx(GR_WM_PROPS_APPWINDOW, "pcfdemo", GR_ROOT_WINDOW_ID, 0, 0, width, height, BLACK); GrSelectEvents(main_wid, GR_EVENT_MASK_EXPOSURE|GR_EVENT_MASK_CLOSE_REQ); GrMapWindow(main_wid); if (finfo.lastchar >= chars_to_show) timeout = 8 * 1000; else timeout = 0; while (1) { GR_EVENT event; GrGetNextEventTimeout(&event, timeout); if (event.type == GR_EVENT_TYPE_TIMEOUT) { first_char += chars_to_show; draw_string(main_wid); } if (event.type == GR_EVENT_TYPE_EXPOSURE) draw_string(main_wid); if(event.type == GR_EVENT_TYPE_CLOSE_REQ) { GrClose(); exit(0); } } }
int main(int argc, char *argv[]) { GR_CHAR *typelist, *p; GR_WINDOW_ID sid, wid; GR_EVENT event; int n = 0, mimetype = -1; if(GrOpen() < 0) { fprintf(stderr, "Couldn't connect to Nano-X server\n"); return 1; } sid = GrGetSelectionOwner(&typelist); if(!sid) { fprintf(stderr, "Clipboard is empty\n"); return 2; } if(!typelist) { fprintf(stderr, "GrGetSelectionOwner() returned an empty " "type list for window %d\n", sid); return 3; } fprintf(stderr, "Window %d owns the selection\n", sid); fprintf(stderr, "It claims to be able to supply data in the following " "types:\n%s\n", typelist); p = strtok(typelist, " "); do { if(!strncmp("text/plain", p, 10)) { mimetype = n; break; } n++; } while((p = strtok(NULL, " "))); if(mimetype == -1) { fprintf(stderr, "Type text/plain is not available\n"); return 4; } free(typelist); fprintf(stderr, "Type text/plain is available- requesting data\n"); wid = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, 1, 1, 0, 0, 0); if(!wid) { fprintf(stderr, "Couldn't get a window\n"); return 5; } GrSelectEvents(wid, GR_EVENT_MASK_CLIENT_DATA); GrRequestClientData(wid, sid, 0, mimetype); while(1) { GrGetNextEventTimeout(&event, 4000); switch(event.type) { case GR_EVENT_TYPE_CLIENT_DATA: if(got_client_data(&event)) return 0; break; case GR_EVENT_TYPE_TIMEOUT: fprintf(stderr, "Timed out waiting for data\n"); return 6; default: break; } } return 0; }