2 * usage: .../gui-plan [<windowid>]
5 * off <segname>[/[<movfeat>]
6 * [i]on <segname>[[/<movfeat>] <movpos>]
7 * [i]det <segname>[[/<movfeat>] <movpos>]
19 #include "../layout/plan-data-format.h"
24 typedef struct PosnState PosnState;
26 int x, y, width, height;
30 typedef struct MaskState MaskState;
32 int x, y, width, height;
36 typedef struct SegmovfeatState SegmovfeatState;
37 struct SegmovfeatState {
38 SegmovfeatState *next;
39 int invert, det, posn, redraw_needed;
41 PosnState (*posns)[2/*i*/][2/*det*/];
42 /* posns[n_posns]=unknown if n_posns>1 */
47 static SegmovfeatState **state, *states_head;
49 static oop_source_sys *sys_events;
51 static int redraw_needed_count, expose_count;
52 static Pixmap bg_pixmap;
54 static void diex(const char *fn, const char *w) __attribute__((noreturn));
55 static void diex(const char *fn, const char *w) {
56 die("Xlib call failed: %s%s%s%s", fn,
57 (w)?" (":"", (w), (w)?")":"");
60 #define XCALL(f,w,al) do{ \
65 static void diexpm(const char *fn, const char *w, int xpmst)
66 __attribute__((noreturn));
67 static void diexpm(const char *fn, const char *w, int xpmst) {
68 die("Xpm call failed: %s%s%s%s: %s", fn,
69 (w)?" (":"", (w), (w)?")":"", XpmGetErrorString(xpmst));
72 #define XPMCALL(f,w,al) do{ \
74 xpmcall_xpmst= ((f) al); \
75 if (xpmcall_xpmst != XpmSuccess) \
76 diexpm(#f, (w), xpmcall_xpmst); \
79 void vbadcmd(ParseState *ps, const char *fmt, va_list al) {
80 fprintf(stderr,"gui-plan: incorrect input: ");
81 vfprintf(stderr,fmt,al);
86 static void *stdin_iferr(oop_source *evts, oop_read *stdin_read,
87 oop_rd_event evt, const char *errmsg, int errnoval,
88 const char *data, size_t recsz, void *cl_v) {
89 die("read stdin: %s", oop_rd_errmsg(stdin_read, evt,
90 errnoval, OOP_RD_STYLE_GETLINE));
94 static int lstrpdbsearch(const char *str, int l,
96 const void *items, int n_items,
98 int min, maxe, try, cmp;
100 const char *try_name;
105 if (min >= maxe) badcmd(0,"unknown %s",what);
106 try= min + (maxe - min) / 2;
107 try_item= (const char*)items + itemsz * try;
108 try_name= *(const char *const *)try_item;
109 cmp= lstrstrcmp(str, l, try_name ? try_name : "");
110 if (!cmp) return try;
111 if (cmp < 0) maxe= try;
116 static void *some_exception(oop_source *evts, int fd,
117 oop_event evt, void *cl_v) {
118 die("poll exception on fd %d",fd);
121 static int range_overlap(int x1, int width1, int x2, int width2) {
122 /* works for y's and heights too, obviously. */
126 if (rhs1 <= x2 || rhs2 <= x1) return 0;
130 static void xlib_expose(XExposeEvent *ev) {
133 expose_count= ev->count;
134 if (!ev->width || !ev->height) return;
136 for (fs= states_head;
139 if (fs->redraw_needed)
141 if (!range_overlap(fs->mask.x, fs->mask.width,
142 ev->x, ev->width)) continue;
143 if (!range_overlap(fs->mask.y, fs->mask.height,
144 ev->y, ev->height)) continue;
145 fs->redraw_needed= 1;
146 redraw_needed_count++;
150 static void redraw(SegmovfeatState *fs) {
153 if (fs->redraw_needed) {
154 fs->redraw_needed= 0;
155 redraw_needed_count--;
157 if (fs->invert < 0) {
158 XCALL( XCopyArea, "redraw",
159 (d, bg_pixmap, w, fs->mask.gc,
160 fs->mask.x, fs->mask.y,
161 fs->mask.width, fs->mask.height,
162 fs->mask.x, fs->mask.y) );
164 src= &fs->posns[fs->posn][fs->invert][fs->det];
165 XCALL( XCopyArea, "redraw",
166 (d, src->pm, w, fs->mask.gc,
167 0,0, src->width, src->height,
172 static void redraw_as_needed(void) {
175 for (fs= states_head;
178 if (fs->redraw_needed)
180 assert(!redraw_needed_count);
183 static Bool evpredicate_always(Display *d, XEvent *ev, XPointer a) {
187 static void xlib_process(void) {
192 xst= XCheckIfEvent(d,&ev,evpredicate_always,0);
194 if (!redraw_needed_count || expose_count)
201 case Expose: xlib_expose(&ev.xexpose); break;
202 case NoExpose: break;
203 default: die("unrequested event type %d\n",ev.type);
208 static void *xlib_readable(oop_source *evts, int fd,
209 oop_event evt, void *cl_v) {
214 static void *stdin_ifok(oop_source *evts, oop_read *cl_read,
215 oop_rd_event evt, const char *errmsg, int errnoval,
216 const char *data, size_t recsz, void *cl_v) {
217 const char *slash, *movfeatname;
219 int invert, det, segment_ix, movfeat_ix, lmovfeatname;
221 const PlanSegmentData *segment_d;
222 const PlanSegmovfeatData *movfeat_d;
225 if (evt == OOP_RD_EOF)
228 if (evt != OOP_RD_OK)
229 return stdin_iferr(evts,cl_read,evt,errmsg,errnoval,data,recsz,cl_v);
235 if (!thiswordstrcmp(&ps,"off")) {
239 invert= (ps.thisword[0]=='i');
240 if (invert) { ps.thisword++; ps.lthisword--; }
241 det= (!thiswordstrcmp(&ps,"on") ? 0 :
242 !thiswordstrcmp(&ps,"det") ? 1 :
243 (badcmd(&ps,"unknown command"),-1));
246 slash= memchr(ps.thisword, '/', ps.lthisword);
248 movfeatname= slash + 1;
249 lmovfeatname= (ps.thisword + ps.lthisword) - movfeatname;
250 ps.lthisword= slash - ps.thisword;
255 segment_ix= lstrpdbsearch(ps.thisword, ps.lthisword,
256 "segment", ui_plan_data.segments,
257 ui_plan_data.n_segments, sizeof(*segment_d));
258 segment_d= &ui_plan_data.segments[segment_ix];
260 movfeat_ix= lstrpdbsearch(movfeatname, lmovfeatname,
261 "movfeat", segment_d->movfeats,
262 segment_d->n_movfeats, sizeof(*movfeat_d));
263 movfeat_d= &segment_d->movfeats[movfeat_ix];
266 if (invert<0) badcmd(0,"off may not take movfeatpos");
267 ps_neednumber(&ps, &posn, 0, movfeat_d->n_posns-1, "movfeatpos");
269 if (invert>=0 && movfeat_d->n_posns > 1) {
270 posn= movfeat_d->n_posns;
278 fs= &state[segment_ix][movfeat_ix];
289 int main(int argc, const char *const *argv) {
290 static XpmColorSymbol coloursymbols[2]= {
291 { (char*)"space", 0, 0 },
292 { (char*)"mark", 0, 1 }
298 XpmAttributes mattribs;
299 XWindowAttributes wattribs;
300 int segment_ix, movfeat_ix, posn, invert, det, oor;
302 const PlanSegmentData *segment_d;
303 const PlanSegmovfeatData *movfeat_d;
306 d= XOpenDisplay(0); if (!d) die("XOpenDisplay failed");
308 if ((arg= *++argv)) {
311 if (!strcmp(arg,"--sizes")) {
312 printf("%d\n%d\n", ui_plan_data.xsz, ui_plan_data.ysz);
313 if (ferror(stdout) || fflush(stdout)) diee("print stdout");
317 if (arg[0]=='-') die("invalid option(s)");
319 errno=0; w= strtoul(arg,&ep,0);
320 if (errno || ep==arg || *ep) die("bad windowid");
322 w= XCreateSimpleWindow(d, DefaultRootWindow(d),
323 0,0, ui_plan_data.xsz, ui_plan_data.ysz,
325 if (w == None) diex("XCreateSimpleWindow", "initial");
328 XCALL( XGetWindowAttributes, 0, (d,w,&wattribs) );
330 XPMCALL( XpmCreatePixmapFromData, "background",
331 (d,w, (char**)ui_plan_data.background, &bg_pixmap,0,0) );
333 XCALL( XSetWindowBackgroundPixmap, 0, (d,w,bg_pixmap) );
335 state= mmalloc(sizeof(*state) * ui_plan_data.n_segments);
336 for (segment_ix= 0, segment_d= ui_plan_data.segments;
337 segment_ix < ui_plan_data.n_segments;
338 segment_ix++, segment_d++) {
339 state[segment_ix]= fs=
340 mmalloc(sizeof(**state) * segment_d->n_movfeats);
341 for (movfeat_ix= 0, movfeat_d= segment_d->movfeats;
342 movfeat_ix < segment_d->n_movfeats;
343 movfeat_ix++, movfeat_d++, fs++) {
344 fs->next= states_head; states_head= fs;
347 fs->posn= movfeat_d->n_posns;
348 if (fs->posn==1) fs->posn= 0;
349 fs->mask.x= movfeat_d->mask.x;
350 fs->mask.y= movfeat_d->mask.y;
351 fs->redraw_needed= 0;
353 mattribs.valuemask= XpmDepth | XpmColorSymbols;
355 mattribs.colorsymbols= coloursymbols;
356 mattribs.numsymbols= sizeof(coloursymbols) / sizeof(*coloursymbols);
357 XPMCALL( XpmCreatePixmapFromData, "mask",
358 (d,w, (char**)movfeat_d->mask.d, &mask,0, &mattribs) );
359 fs->mask.width= mattribs.width;
360 fs->mask.height= mattribs.height;
362 gcv.clip_x_origin= fs->mask.x;
363 gcv.clip_y_origin= fs->mask.y;
365 fs->mask.gc= XCreateGC(d,w,
366 GCClipXOrigin | GCClipYOrigin | GCClipMask,
368 XCALL( XFreePixmap, "mask", (d,mask) );
370 fs->posns= mmalloc(sizeof(*fs->posns)*(fs->posn+1));
371 for (posn= 0; posn <= fs->posn; posn++)
372 for (invert=0; invert<2; invert++)
373 for (det=0; det<2; det++) {
374 PosnState *ps= &fs->posns[posn][invert][det];
375 const PlanPixmapDataRef *ppdr= posn < movfeat_d->n_posns
376 ? &movfeat_d->posns[posn].on[invert][det]
377 : &movfeat_d->unknown[invert][det];
380 mattribs.valuemask= 0;
381 XPMCALL( XpmCreatePixmapFromData, "main",
386 ps->width= mattribs.width;
387 ps->height= mattribs.height;
392 sys_events= oop_sys_new(); if (!sys_events) diee("oop_sys_new");
393 events= oop_sys_source(sys_events); assert(events);
395 rd= oop_rd_new_fd(events, 0, 0,0);
396 if (!rd) diee("oop_rd_new_fd");
397 oor= oop_rd_read(rd, OOP_RD_STYLE_GETLINE, 1024,
400 if (oor) diee("oop_rd_read");
402 events->on_fd(events, 0, OOP_EXCEPTION, some_exception, 0);
403 events->on_fd(events, ConnectionNumber(d), OOP_READ, xlib_readable, 0);
404 events->on_fd(events, ConnectionNumber(d), OOP_EXCEPTION, some_exception, 0);
406 XCALL( XSelectInput, 0, (d,w, ExposureMask) );
409 XCALL( XClearArea, "initial", (d,w, 0,0,0,0, True) );
411 XCALL( XMapWindow, 0, (d,w) );
416 oop_sys_run(sys_events);