int main(int argc, char **argv) { XEvent evt; Colormap colormap; struct timeval tv; int screenNumber; long eventMask; unsigned long white; unsigned long black; Status rc; int iters; int opt; int draw_lines; int draw_rects; int draw_stipples; int draw_fonts; int draw_image; int zero_counters; int scroll_type; char image_file[256]; char proxy_app[256]; char msg[4096]; // set some defaults g_winWidth = 640; g_winHeight = 480; iters = 5000; draw_lines = 1; draw_rects = 1; draw_stipples = 1; draw_fonts = 1; draw_image = 1; g_delay_dur = 1000; scroll_type = SCROLL_SMOOTH1; zero_counters = 0; strcpy(image_file, "yosemite.bmp"); strcpy(msg, "To be or not to be!"); // process cmd line args opterr = 0; while ((opt = getopt(argc, argv, "lrsg:c:f:i:d:o:z:")) != -1) { switch (opt) { case 'g': if (sscanf(optarg, "%dx%d", &g_winWidth, &g_winHeight) != 2) { fprintf(stderr, "\nerror: invalid geometry specified\n\n"); usage(); return -1; } break; case 'c': if (sscanf(optarg, "%d", &iters) != 1) { fprintf(stderr, "\nerror: invalid count specified\n\n"); usage(); return -1; } break; case 'l': draw_lines = 1; draw_rects = 0; draw_stipples = 0; draw_fonts = 0; draw_image = 0; break; case 'r': draw_rects = 1; draw_lines = 0; draw_stipples = 0; draw_fonts = 0; draw_image = 0; break; case 's': draw_stipples = 1; draw_lines = 0; draw_rects = 0; draw_fonts = 0; draw_image = 0; break; case 'f': if (strlen(optarg) <= 0) { fprintf(stderr, "\nerror: -f option requires an argument\n\n"); usage(); return -1; } draw_fonts = 1; strncpy(msg, optarg, 4096); draw_lines = 0; draw_rects = 0; draw_stipples = 0; draw_image = 0; break; case 'i': if (strlen(optarg) <= 0) { fprintf(stderr, "\nerror: -i option requires an argument\n\n"); usage(); return -1; } draw_image = 1; strncpy(image_file, optarg, 255); draw_lines = 0; draw_rects = 0; draw_stipples = 0; draw_fonts = 0; break; case 'h': usage(); return 0; break; case 'v': printf("xdemo Ver 1.0\n"); return 0; break; case 'd': if (sscanf(optarg, "%d", &g_delay_dur) != 1) { fprintf(stderr, "\nerror: -d option requires an argument\n\n"); usage(); return -1; } break; case 'z': if (strlen(optarg) <= 0) { fprintf(stderr, "\nerror: invalid proxy application specified\n\n"); usage(); return -1; } strcpy(proxy_app, optarg); printf("##### LK_TODO: proxy_app=%s\n", proxy_app); zero_counters = 1; break; case 'o': if (strcmp(optarg, "jump") == 0) { scroll_type = SCROLL_JUMP; } else if (strcmp(optarg, "smooth1") == 0) { scroll_type = SCROLL_SMOOTH1; } else if (strcmp(optarg, "smooth2") == 0) { scroll_type = SCROLL_SMOOTH2; } else if (strcmp(optarg, "smooth3") == 0) { scroll_type = SCROLL_SMOOTH3; } else if (strcmp(optarg, "smooth4") == 0) { scroll_type = SCROLL_SMOOTH4; } else { fprintf(stderr, "\ninvalid scroll type specified\n\n"); usage(); return -1; } break; default: usage(); return -1; } } // must have at least one operation if ((!draw_lines) && (!draw_rects) && (!draw_stipples) && (!draw_fonts) && (!draw_image)) { usage(); return -1; } g_disp = XOpenDisplay(NULL); if (!g_disp) { dprint("error opening X display\n"); exit(-1); } screenNumber = DefaultScreen(g_disp); white = WhitePixel(g_disp, screenNumber); black = BlackPixel(g_disp, screenNumber); g_win = XCreateSimpleWindow(g_disp, DefaultRootWindow(g_disp), 50, 50, // origin g_winWidth, g_winHeight, // size 0, black, // border white ); // backgd XMapWindow(g_disp, g_win); //eventMask = StructureNotifyMask | MapNotify | VisibilityChangeMask; eventMask = StructureNotifyMask | VisibilityChangeMask; XSelectInput(g_disp, g_win, eventMask); g_gc = XCreateGC(g_disp, g_win, 0, // mask of values NULL ); // array of values #if 0 do { dprint("about to call XNextEvent(...)\n"); XNextEvent(g_disp, &evt);// calls XFlush dprint("returned from XNextEvent(...)\n"); } //while(evt.type != MapNotify); while(evt.type != VisibilityNotify); #endif // get access to the screen's color map colormap = DefaultColormap(g_disp, screenNumber); // alloc red color rc = XAllocNamedColor(g_disp, colormap, "red", &g_colors[0], &g_colors[0]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'red' color.\n"); exit(1); } rc = XAllocNamedColor(g_disp, colormap, "green", &g_colors[1], &g_colors[1]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'green' color.\n"); exit(1); } rc = XAllocNamedColor(g_disp, colormap, "blue", &g_colors[2], &g_colors[2]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'blue' color.\n"); exit(1); } rc = XAllocNamedColor(g_disp, colormap, "yellow", &g_colors[3], &g_colors[3]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'yellow' color.\n"); exit(1); } rc = XAllocNamedColor(g_disp, colormap, "orange", &g_colors[4], &g_colors[4]); if (rc == 0) { printf("XAllocNamedColor - failed to allocated 'orange' color.\n"); exit(1); } if (zero_counters) { signal_tcp_proxy(proxy_app); } if (draw_lines) { start_timer(&tv); drawLines(iters); printf("drew %d lines in %d ms\n", iters, time_elapsed_ms(tv)); } if (draw_rects) { start_timer(&tv); drawRectangles(iters); printf("drew %d rects in %d ms\n", iters, time_elapsed_ms(tv)); } if (draw_stipples) { start_timer(&tv); // LK_TODO } if (draw_fonts) { start_timer(&tv); drawFont(iters, msg); printf("drew %d strings in %d ms\n", iters, time_elapsed_ms(tv)); } if (draw_image) { start_timer(&tv); drawBMP(image_file, scroll_type); printf("drew BMP in %d ms\n", time_elapsed_ms(tv)); } if (zero_counters) { signal_tcp_proxy(proxy_app); } eventMask = ButtonPressMask|ButtonReleaseMask; XSelectInput(g_disp, g_win, eventMask); do { XNextEvent(g_disp, &evt); // calls XFlush() } while(evt.type != ButtonRelease); XDestroyWindow(g_disp, g_win); XCloseDisplay(g_disp); return 0; }
int main (int argc, char *argv[]) { int x, y; struct input_event ev[2]; struct input_event ev_button[2]; struct input_event ev_sync; if (argc < 2) { printf("Usage: %s serial-device [uinput-device]\n",argv[0]); exit(-1); } open_serial_port (argv[1]); // Open serial port set_serial_ops (); //configure serial port optionS // configure uinput if (argc > 3) setup_uinput_dev(argv[2]); else setup_uinput(); // Try 5 times to initialize screen: int n = 1; printf ("Attempting to initialize screen...\n"); while (initialize_panel ()) { n++; if (n > 5) { printf ("Too many failures, exiting\n"); exit (1); } } // input sync signal: memset (&ev_sync, 0, sizeof (struct input_event)); ev_sync.type = EV_SYN; ev_sync.code = 0; ev_sync.value = 0; // button press signals: memset (&ev_button, 0, sizeof (ev_button)); ev_button[0].type = EV_KEY; ev_button[0].code = BTN_LEFT; ev_button[0].value = 0; ev_button[1].type = EV_KEY; ev_button[1].code = BTN_LEFT; ev_button[1].value = 1; struct timeval tv_start_click; struct timeval tv_current; char click_state = 0; char first_click = 0; unsigned char buffer[4]; while (1) { // Should have select timeout, because finger down garantees many results.. struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; fd_set serial; FD_ZERO (&serial); FD_SET (fd_serial, &serial); // Use select to use timeout... if (select(fd_serial+1,&serial,NULL,NULL,&tv) < 1) { first_click=0; click_state =0; if (write(fd_uinput, &ev_button[click_state],sizeof (struct input_event)) < 0) die ("error: write"); // Sync if (write (fd_uinput, &ev_sync, sizeof (struct input_event)) < 0) die ("error state"); continue; } read (fd_serial, &buffer, sizeof (buffer)); if ((buffer[0] < 0xFD) || (buffer[3] != 0xFF)) { // Correct missed packets... if ((buffer[1] >= 0xFD) && (buffer[1] < 0xFF)) read(fd_serial, &buffer,3); if ((buffer[2] >= 0xFD) && (buffer[2] < 0xFF)) read(fd_serial, &buffer,2); if ((buffer[3] >= 0xFD) && (buffer[3] < 0xFF)) read(fd_serial, &buffer,1); continue; // make sure its a valid position command. } x = (int) ((buffer[1]) * 1024.0 / 0x5F); // max is 5F y = (int) ((buffer[2]) * 1024.0 / 0x48); // max is 48 int old_click_state = click_state; if (buffer[0] == 0xFD) click_state = 0; else click_state = 1; // If this is the first panel event, track time for no-drag timer if (click_state != old_click_state && click_state == 1) { first_click = 1; gettimeofday(&tv_start_click,NULL); } else first_click=0; // load X,Y into input_events memset (ev, 0, sizeof (ev)); //resets object ev[0].type = EV_ABS; ev[0].code = ABS_X; ev[0].value = x; ev[1].type = EV_ABS; ev[1].code = ABS_Y; ev[1].value = y; // send X,Y gettimeofday(&tv_current,NULL); // Only move to posision of click for first while - prevents accidental dragging. if (time_elapsed_ms(&tv_start_click,&tv_current,500) || first_click) { if (write (fd_uinput, &ev[0], sizeof (struct input_event)) < 0) die ("error: write"); if (write (fd_uinput, &ev[1], sizeof (struct input_event)) < 0) die ("error: write"); } // clicking if (click_state != old_click_state) { if (write(fd_uinput, &ev_button[click_state],sizeof (struct input_event)) < 0) die ("error: write"); } // Sync if (write (fd_uinput, &ev_sync, sizeof (struct input_event)) < 0) die ("error: write"); } // while 1 if (ioctl (fd_uinput, UI_DEV_DESTROY) < 0) die ("error: ioctl"); close (fd_uinput); return 0; }