2 * usage: .../gui-plan [<windowid>]
5 * off <segname>[/[<movfeat>]
6 * [t|f][i]on <segname>[[/<movfeat>] <movpos>]
7 * [t|f][i]det <segname>[[/<movfeat>] <movpos>]
19 #include "../layout/plan-data-format.h"
24 typedef struct MaskState MaskState;
26 int x, y, width, height;
30 typedef struct PosnState PosnState;
32 int x, y, width, height;
37 typedef struct SegmovfeatState SegmovfeatState;
38 struct SegmovfeatState {
39 SegmovfeatState *next;
40 int invert, det, trainown, posn, redraw_needed;
42 PosnState (*posns)[2/*i*/][2/*det*/];
43 /* posns[n_posns]=unknown if n_posns>1 */
48 static SegmovfeatState **state, *states_head;
50 static oop_source_sys *sys_events;
52 static int redraw_needed_count, expose_count;
53 static Pixmap bg_pixmap;
54 static unsigned long train_pixel, owned_pixel;
56 static const char *badcmdreport_data;
57 static size_t badcmdreport_recsz;
59 static void diex(const char *fn, const char *w) __attribute__((noreturn));
60 static void diex(const char *fn, const char *w) {
61 die("Xlib call failed: %s%s%s%s", fn,
62 (w)?" (":"", (w), (w)?")":"");
65 #define XCALL(f,w,al) do{ \
70 static void diexpm(const char *fn, const char *w, int xpmst)
71 __attribute__((noreturn));
72 static void diexpm(const char *fn, const char *w, int xpmst) {
73 die("Xpm call failed: %s%s%s%s: %s", fn,
74 (w)?" (":"", (w), (w)?")":"", XpmGetErrorString(xpmst));
77 #define XPMCALL(f,w,al) do{ \
79 xpmcall_xpmst= ((f) al); \
80 if (xpmcall_xpmst != XpmSuccess) \
81 diexpm(#f, (w), xpmcall_xpmst); \
84 void vbadcmd(ParseState *ps, const char *fmt, va_list al) {
85 fprintf(stderr,"gui-plan: incorrect input: `%.*s': ",
86 (int)badcmdreport_recsz, badcmdreport_data);
87 vfprintf(stderr,fmt,al);
92 static void *stdin_iferr(oop_source *evts, oop_read *stdin_read,
93 oop_rd_event evt, const char *errmsg, int errnoval,
94 const char *data, size_t recsz, void *cl_v) {
95 die("read stdin: %s", oop_rd_errmsg(stdin_read, evt,
96 errnoval, OOP_RD_STYLE_GETLINE));
100 static int lstrpdbsearch(const char *str, int l,
102 const void *items, int n_items,
104 int min, maxe, try, cmp;
105 const void *try_item;
106 const char *try_name;
111 if (min >= maxe) badcmd(0,"unknown %s `%.*s'",what,l,str);
112 try= min + (maxe - min) / 2;
113 try_item= (const char*)items + itemsz * try;
114 try_name= *(const char *const *)try_item;
115 cmp= lstrstrcmp(str, l, try_name ? try_name : "");
116 if (!cmp) return try;
117 if (cmp < 0) maxe= try;
122 static void *some_exception(oop_source *evts, int fd,
123 oop_event evt, void *cl_v) {
124 die("poll exception on fd %d",fd);
127 static int range_overlap(int x1, int width1, int x2, int width2) {
128 /* works for y's and heights too, obviously. */
132 if (rhs1 <= x2 || rhs2 <= x1) return 0;
136 static void xlib_expose(XExposeEvent *ev) {
139 expose_count= ev->count;
140 if (!ev->width || !ev->height) return;
142 for (fs= states_head;
145 if (fs->redraw_needed)
147 if (!range_overlap(fs->whole.x, fs->whole.width,
148 ev->x, ev->width)) continue;
149 if (!range_overlap(fs->whole.y, fs->whole.height,
150 ev->y, ev->height)) continue;
151 fs->redraw_needed= 1;
152 redraw_needed_count++;
156 static void redraw(SegmovfeatState *fs) {
160 if (fs->redraw_needed) {
161 fs->redraw_needed= 0;
162 redraw_needed_count--;
165 XCALL( XCopyArea, "redraw",
166 (d, bg_pixmap, w, fs->whole.gc,
167 fs->whole.x, fs->whole.y,
168 fs->whole.width, fs->whole.height,
169 fs->whole.x, fs->whole.y) );
170 if (fs->invert >= 0) {
171 src= &fs->posns[fs->posn][fs->invert][fs->det];
172 XCALL( XCopyArea, "redraw",
173 (d, src->pm, w, fs->whole.gc,
174 0,0, src->width, src->height,
177 if (fs->trainown && src && src->edge.x >= 0) {
178 gcv.foreground= fs->trainown>1 ? train_pixel : owned_pixel;
179 XCALL( XChangeGC, "train/own",
180 (d, src->edge.gc, GCForeground, &gcv) );
181 XCALL( XFillRectangle, "train/own",
183 src->edge.x, src->edge.y,
184 src->edge.width, src->edge.height) );
188 static void redraw_as_needed(void) {
191 for (fs= states_head;
194 if (fs->redraw_needed)
196 assert(!redraw_needed_count);
199 static Bool evpredicate_always(Display *d, XEvent *ev, XPointer a) {
203 static void xlib_process(void) {
208 xst= XCheckIfEvent(d,&ev,evpredicate_always,0);
210 if (!redraw_needed_count || expose_count)
217 case Expose: xlib_expose(&ev.xexpose); break;
218 case NoExpose: break;
219 default: die("unrequested event type %d\n",ev.type);
224 static void *xlib_readable(oop_source *evts, int fd,
225 oop_event evt, void *cl_v) {
230 static int thiswordeatonechar(ParseState *ps, int c) {
231 if (ps->thisword[0] == c) {
232 ps->thisword++; ps->lthisword--;
238 static void *stdin_ifok(oop_source *evts, oop_read *cl_read,
239 oop_rd_event evt, const char *errmsg, int errnoval,
240 const char *data, size_t recsz, void *cl_v) {
241 const char *slash, *movfeatname;
243 int invert, det, trainown, segment_ix, movfeat_ix, lmovfeatname;
245 const PlanSegmentData *segment_d;
246 const PlanSegmovfeatData *movfeat_d;
249 if (evt == OOP_RD_EOF)
252 if (evt != OOP_RD_OK)
253 return stdin_iferr(evts,cl_read,evt,errmsg,errnoval,data,recsz,cl_v);
255 badcmdreport_data= data;
256 badcmdreport_recsz= recsz;
262 if (!thiswordstrcmp(&ps,"off")) {
268 thiswordeatonechar(&ps,'t') ? 2 :
269 thiswordeatonechar(&ps,'f') ? 1 : 0;
270 invert= thiswordeatonechar(&ps,'i');
271 det= (!thiswordstrcmp(&ps,"on") ? 0 :
272 !thiswordstrcmp(&ps,"det") ? 1 :
273 (badcmd(&ps,"unknown command"),-1));
276 slash= memchr(ps.thisword, '/', ps.lthisword);
278 movfeatname= slash + 1;
279 lmovfeatname= (ps.thisword + ps.lthisword) - movfeatname;
280 ps.lthisword= slash - ps.thisword;
285 segment_ix= lstrpdbsearch(ps.thisword, ps.lthisword,
286 "segment", ui_plan_data.segments,
287 ui_plan_data.n_segments, sizeof(*segment_d));
288 segment_d= &ui_plan_data.segments[segment_ix];
290 movfeat_ix= lstrpdbsearch(movfeatname, lmovfeatname,
291 "movfeat", segment_d->movfeats,
292 segment_d->n_movfeats, sizeof(*movfeat_d));
293 movfeat_d= &segment_d->movfeats[movfeat_ix];
296 if (invert<0) badcmd(0,"off may not take movfeatpos");
297 ps_neednumber(&ps, &posn, 0, movfeat_d->n_posns-1, "movfeatpos");
299 if (invert>=0 && movfeat_d->n_posns > 1) {
300 posn= movfeat_d->n_posns;
308 fs= &state[segment_ix][movfeat_ix];
311 fs->trainown= trainown;
320 static void loadmask(MaskState *out, const PlanPixmapDataRef *ppd,
321 XGCValues *gcv, long gcv_mask) {
322 static XpmColorSymbol coloursymbols[2]= {
323 { (char*)"space", 0, 0 },
324 { (char*)"mark", 0, 1 }
327 XpmAttributes mattribs;
332 mattribs.valuemask= XpmDepth | XpmColorSymbols;
334 mattribs.colorsymbols= coloursymbols;
335 mattribs.numsymbols= sizeof(coloursymbols) / sizeof(*coloursymbols);
336 XPMCALL( XpmCreatePixmapFromData, "mask",
337 (d,w, (char**)ppd->d, &pm,0, &mattribs) );
338 out->width= mattribs.width;
339 out->height= mattribs.height;
341 gcv->clip_x_origin= out->x;
342 gcv->clip_y_origin= out->y;
344 out->gc= XCreateGC(d,w,
345 gcv_mask | GCClipXOrigin | GCClipYOrigin | GCClipMask,
347 XCALL( XFreePixmap, "mask", (d,pm) );
350 int main(int argc, const char *const *argv) {
353 XpmAttributes mattribs;
354 XWindowAttributes wattribs;
355 int segment_ix, movfeat_ix, posn, invert, det, oor;
359 const PlanSegmentData *segment_d;
360 const PlanSegmovfeatData *movfeat_d;
362 d= XOpenDisplay(0); if (!d) die("XOpenDisplay failed");
364 if ((arg= *++argv)) {
367 if (!strcmp(arg,"--sizes")) {
368 printf("%d\n%d\n", ui_plan_data.xsz, ui_plan_data.ysz);
369 if (ferror(stdout) || fflush(stdout)) diee("print stdout");
373 if (arg[0]=='-') die("invalid option(s)");
375 errno=0; w= strtoul(arg,&ep,0);
376 if (errno || ep==arg || *ep) die("bad windowid");
378 w= XCreateSimpleWindow(d, DefaultRootWindow(d),
379 0,0, ui_plan_data.xsz, ui_plan_data.ysz,
381 if (w == None) diex("XCreateSimpleWindow", "initial");
384 XCALL( XGetWindowAttributes, 0, (d,w,&wattribs) );
386 XPMCALL( XpmCreatePixmapFromData, "background",
387 (d,w, (char**)ui_plan_data.background, &bg_pixmap,0,0) );
389 XCALL( XSetWindowBackgroundPixmap, 0, (d,w,bg_pixmap) );
391 XCALL( XAllocNamedColor, "white",
392 (d, wattribs.colormap, "#ffffff",
394 train_pixel= colour.pixel;
396 XCALL( XAllocNamedColor, "owned",
397 (d, wattribs.colormap, "#a0a0a0",
399 owned_pixel= colour.pixel;
401 state= mmalloc(sizeof(*state) * ui_plan_data.n_segments);
402 for (segment_ix= 0, segment_d= ui_plan_data.segments;
403 segment_ix < ui_plan_data.n_segments;
404 segment_ix++, segment_d++) {
405 state[segment_ix]= fs=
406 mmalloc(sizeof(**state) * segment_d->n_movfeats);
407 for (movfeat_ix= 0, movfeat_d= segment_d->movfeats;
408 movfeat_ix < segment_d->n_movfeats;
409 movfeat_ix++, movfeat_d++, fs++) {
410 fs->next= states_head; states_head= fs;
414 fs->posn= movfeat_d->n_posns;
415 if (fs->posn==1) fs->posn= 0;
416 fs->redraw_needed= 0;
418 loadmask(&fs->whole, &movfeat_d->mask, &gcv, 0);
420 fs->posns= mmalloc(sizeof(*fs->posns)*(fs->posn+1));
421 for (posn= 0; posn <= fs->posn; posn++)
422 for (invert=0; invert<2; invert++)
423 for (det=0; det<2; det++) {
424 PosnState *ps= &fs->posns[posn][invert][det];
425 const PlanPixmapOnData *ppod=
426 posn < movfeat_d->n_posns
427 ? &movfeat_d->posns[posn] : 0;
428 const PlanPixmapDataRef *ppdr= ppod
429 ? &ppod->on[invert][det]
430 : &movfeat_d->unknown[invert][det];
433 mattribs.valuemask= 0;
434 XPMCALL( XpmCreatePixmapFromData, "main",
439 ps->width= mattribs.width;
440 ps->height= mattribs.height;
442 loadmask(&ps->edge, &ppod->pedge, &gcv, 0);
451 sys_events= oop_sys_new(); if (!sys_events) diee("oop_sys_new");
452 events= oop_sys_source(sys_events); assert(events);
454 rd= oop_rd_new_fd(events, 0, 0,0);
455 if (!rd) diee("oop_rd_new_fd");
456 oor= oop_rd_read(rd, OOP_RD_STYLE_GETLINE, 1024,
459 if (oor) diee("oop_rd_read");
461 events->on_fd(events, 0, OOP_EXCEPTION, some_exception, 0);
462 events->on_fd(events, ConnectionNumber(d), OOP_READ, xlib_readable, 0);
463 events->on_fd(events, ConnectionNumber(d), OOP_EXCEPTION, some_exception, 0);
465 XCALL( XSelectInput, 0, (d,w, ExposureMask) );
468 XCALL( XClearArea, "initial", (d,w, 0,0,0,0, True) );
470 XCALL( XMapWindow, 0, (d,w) );
475 oop_sys_run(sys_events);