/* * Puts up a 250x250 window at +100+100 and has a yellow 50x50 square * stepping through it, one step per second. Clicking on the window * draws a filled blue cirle around the clicked point. */ #include #include #include typedef struct cs CS; struct cs { const char *str; LX_RGBPF col; int succ; } ; static LX_CONN *xc; static int scr; static LX_XID root; static LX_XID cmap; static int depth; static CS col_k; static CS col_w; static CS col_b; static CS col_y; static int pending; static LX_XID win; static struct timeval nexttick_when; static int nexttick_x; static int nexttick_y; static LX_SGC wingc; static int tick_block(void *arg __attribute__((__unused__))) { struct timeval now; int delta; gettimeofday(&now,0); while ( (now.tv_sec > nexttick_when.tv_sec) || ( (now.tv_sec == nexttick_when.tv_sec) && (now.tv_usec >= nexttick_when.tv_usec) ) ) { lx_ChangeSGC_va(xc,wingc,LX_GCV_Foreground(col_k.col.pixel),LX_GCV_END); lx_fill_rectangle(xc,win,lx_SGC_GC(xc,wingc),nexttick_x*50,nexttick_y*50,50,50); nexttick_x ++; if (nexttick_x >= 5) { nexttick_x = 0; nexttick_y ++; if (nexttick_y >= 5) nexttick_y = 0; } lx_ChangeSGC_va(xc,wingc,LX_GCV_Foreground(col_y.col.pixel),LX_GCV_END); lx_fill_rectangle(xc,win,lx_SGC_GC(xc,wingc),nexttick_x*50,nexttick_y*50,50,50); lx_end_batch(xc); nexttick_when.tv_sec ++; return(AIO_BLOCK_LOOP); } delta = (((nexttick_when.tv_sec - now.tv_sec) * 1000000) + (nexttick_when.tv_usec - now.tv_usec) + 999) / 1000; if (delta < 1) delta = 1; return(delta); } static void button_press(const LX_EVENT_ButtonPress *e) { if (e->eventw != win) return; lx_fill_arc(xc, win, lx_ChangeSGC_va(xc,wingc,LX_GCV_Foreground(col_b.col.pixel),LX_GCV_PUSH), e->eventx-20, e->eventy-20, 40, 40, 0, 360*64); lx_end_batch(xc); } static void handle_event(LX_CONN *conn __attribute__((__unused__)), const LX_EVENT *e) { if (e->type == LX_EV_ButtonPress) button_press(&e->u.ButtonPress); } static void alloc_done(void *cv) { CS *c; c = cv; pending --; if (pending > 0) return; win = lx_CreateWindow_va(xc,root,100,100,250,250,1,depth,LX_WCLASS_InputOutput,LX_VISUAL_CopyFromParent, LX_CWV_BackPixel(col_k.col.pixel), LX_CWV_BorderPixel(col_w.col.pixel), LX_CWV_EventMask(LX_EM_ButtonPress), LX_CWV_END); lx_MapWindow(xc,win); nexttick_x = 10; nexttick_y = 10; gettimeofday(&nexttick_when,0); aio_add_block(&tick_block,0); lx_set_event_handler(xc,&handle_event); } static void lookup_done(void *cv) { CS *c; c = cv; if (! c->succ) { printf("Can't look up %s\n",c->str); exit(1); } lx_op_callback(lx_AllocColor(xc,cmap,c->col.r,c->col.g,c->col.b,&c->col.pixel,0,0,0),&alloc_done,c,0); } static void start_colour_lookup(CS *c, const char *s) { pending ++; c->str = s; lx_op_callback(lx_lookup_color(xc,cmap,s,-1,&c->col.r,&c->col.g,&c->col.b,&c->succ),&lookup_done,c,0); } static void open_done(LX_CONN *conn, void *arg __attribute__((__unused__))) { xc = conn; scr = lx_default_screen(xc); root = lx_root(xc,scr); cmap = lx_root_colormap(xc,scr); depth = lx_root_depth(xc,scr); wingc = lx_CreateSGC_va(xc,root,LX_GCV_END); pending = 0; start_colour_lookup(&col_k,"#000000"); start_colour_lookup(&col_w,"#ffffff"); start_colour_lookup(&col_b,"#0000ff"); start_colour_lookup(&col_y,"#ffff00"); } int main(void); int main(void) { aio_poll_init(); lx_open(0,0,&open_done,0,0,0); aio_event_loop(); return(0); }