14
static const char *pictdir;
16
static Display *display;
17
static XtAppContext app;
18
static Drawable window, rootwindow;
19
static Colormap colormap;
21
static XColor white, black;
22
static Pixmap offscreen;
23
static XtIntervalId timer;
24
static GC stdgc, whitegc;
25
static int screensize;
27
static Widget toplevel, field;
34
popdown(Widget w, XtPointer client_data, XtPointer call_data) {
42
x11_popup(Widget dialog) {
45
XWindowAttributes tattr, pattr;
48
XtRealizeWidget(XtParent(dialog));
49
XtSetMappedWhenManaged(XtParent(dialog), FALSE);
50
XtManageChild(dialog);
53
XTranslateCoordinates(display, XtWindow(toplevel), rootwindow,
54
0, 0, &tx, &ty, &temp);
55
XGetWindowAttributes(display, XtWindow(toplevel), &tattr);
56
XGetWindowAttributes(display, XtWindow(dialog), &pattr);
57
px = tx + (tattr.width - pattr.width) / 2;
58
py = ty + (tattr.height - pattr.height) / 2;
59
XtVaSetValues(XtParent(dialog), XtNx, px, XtNy, py, NULL);
61
XtAddCallback(XtParent(dialog), XtNpopdownCallback,
62
(XtCallbackProc) popdown, NULL);
63
XtPopup(XtParent(dialog), XtGrabExclusive);
64
while (in_popup || XtAppPending(app))
65
XtAppProcessEvent(app, XtIMXEvent);
73
leave_window(Widget w, XtPointer client_data, XEvent *event, Boolean *b) {
83
enter_window(Widget w, XtPointer client_data, XEvent *event, Boolean *b) {
93
redraw_window(Widget w, XtPointer client_data, XEvent *event, Boolean *b) {
103
button_press(Widget w, XtPointer data, XEvent *event, Boolean *b) {
104
XButtonEvent *buttonevent = (XButtonEvent *) event;
110
Game_button_press(buttonevent->x, buttonevent->y);
114
button_release(Widget w, XtPointer data, XEvent *event, Boolean *b) {
115
XButtonEvent *buttonevent = (XButtonEvent *) event;
121
Game_button_release(buttonevent->x, buttonevent->y);
125
timer_tick(XtPointer client_data, XtIntervalId *timer_id) {
138
x11_set_cursor(MCursor *cursor) {
139
XDefineCursor(display, window, cursor->cursor);
143
x11_load_cursor(const char *name, int masked, MCursor **cursorp) {
147
unsigned width, height;
150
cursor = xalloc(sizeof *cursor);
152
sprintf(file, "%s/bitmaps/%s.xbm", pictdir, name);
153
i = XReadBitmapFile(display, rootwindow, file,
154
&width, &height, &bitmap, &xh, &yh);
155
if (i == BitmapOpenFailed)
156
fatal("cannot open %s", file);
157
if (masked == CURSOR_SEP_MASK) {
158
sprintf(file, "%s/bitmaps/%s_mask.xbm", pictdir, name);
159
i = XReadBitmapFile(display, rootwindow,
160
file, &width, &height, &mask, &xh, &yh);
161
if (i == BitmapOpenFailed)
162
fatal("cannot open %s", file);
166
cursor->cursor = XCreatePixmapCursor(display, bitmap, mask,
177
x11_load_picture(const char *name, int trans, Picture **pictp) {
181
XpmColorSymbol symbol;
184
unsigned long gcmask;
187
pict = xalloc(sizeof *pict);
189
gcmask = GCForeground | GCBackground | GCGraphicsExposures;
190
gcval.graphics_exposures = False;
191
attr.valuemask = XpmCloseness | XpmReturnPixels | XpmColormap |
193
attr.closeness = 65535;
194
attr.colormap = colormap;
198
symbol.value = "none";
199
XtVaGetValues(field, XtNbackground, &symbol.pixel, NULL);
200
attr.colorsymbols = &symbol;
202
attr.valuemask |= XpmColorSymbols;
204
sprintf(file, "%s/pixmaps/%s.xpm", pictdir, name);
205
i = XpmReadFileToPixmap(display, rootwindow, file, &pict->pix,
208
fatal("cannot open %s", file);
210
pict->gc = XCreateGC(display, offscreen, gcmask, &gcval);
212
XSetClipMask(display, pict->gc, mask);
213
pict->width = attr.width;
214
pict->height = attr.height;
220
x11_set_icon(Picture *icon) {
221
XtVaSetValues(toplevel, XtNiconPixmap, icon->pix,
222
XtNiconMask, icon->mask, NULL);
226
x11_picture_width(Picture *pict) {
227
return (pict->width);
231
x11_picture_height(Picture *pict) {
232
return (pict->height);
236
* Graphics operations
240
x11_graphics_init() {
242
unsigned long gcmask;
243
gcmask = GCGraphicsExposures;
244
gcval.graphics_exposures = False;
245
stdgc = XCreateGC(display, window, gcmask, &gcval);
246
XSetLineAttributes(display, stdgc, 2, LineSolid, CapRound, JoinMiter);
247
XSetBackground(display, stdgc, white.pixel);
248
XSetForeground(display, stdgc, black.pixel);
249
whitegc = XCreateGC(display, window, gcmask, &gcval);
250
XSetBackground(display, whitegc, white.pixel);
251
XSetForeground(display, whitegc, white.pixel);
252
offscreen = XCreatePixmap(display, rootwindow, screensize,
258
XFillRectangle(display, offscreen, whitegc, 0, 0,
259
screensize, screensize);
263
x11_refresh_window() {
264
XCopyArea(display, offscreen, window, stdgc, 0, 0,
265
screensize, screensize, 0, 0);
269
x11_draw_image(Picture *pict, int x, int y) {
270
XSetClipOrigin(display, pict->gc, x, y);
271
XCopyArea(display, pict->pix, offscreen, pict->gc, 0, 0,
272
pict->width, pict->height, x, y);
276
x11_draw_line(int x1, int y1, int x2, int y2) {
277
XDrawLine(display, offscreen, stdgc, x1, y1, x2, y2);
281
x11_draw_string(const char *str, int x, int y) {
282
XDrawString(display, offscreen, stdgc, x, y, str, strlen(str));
290
x11_start_timer(int ms) {
291
timer = XtAppAddTimeOut(app, ms, timer_tick, NULL);
298
XtRemoveTimeOut(timer);
299
timer = (XtIntervalId) 0;
319
x11_initialize(int *argc, char **argv) {
322
timer = (XtIntervalId) 0;
323
toplevel = XtAppInitialize(&app, "XBill", NULL, 0, argc, argv,
325
display = XtDisplay(toplevel);
327
if (stat(IMAGES, &stats) == 0)
334
x11_setup_resources() {
335
XrmDatabase database;
337
database = XrmGetDatabase(display);
338
XrmPutStringResource(&database, "*background", "#c4c4c4");
339
XrmPutStringResource(&database, "*foreground", "#000000");
340
XrmSetDatabase(display, database);
347
Dimension winwidth, winheight;
349
XtRealizeWidget(toplevel);
350
screen = XtScreen(toplevel);
351
depth = DefaultDepthOfScreen(screen);
352
rootwindow = RootWindowOfScreen(screen);
354
colormap = DefaultColormapOfScreen(screen);
355
white.pixel = WhitePixelOfScreen(screen);
356
XQueryColor(display, colormap, &white);
357
black.pixel = BlackPixelOfScreen(screen);
358
XQueryColor(display, colormap, &black);
360
XtVaGetValues(toplevel, XtNwidth, &winwidth, XtNheight, &winheight,
362
h.width = h.base_width = h.min_width = h.max_width = winwidth;
363
h.height = h.base_height = h.min_height = h.max_height = winheight;
364
h.width_inc = h.height_inc = 0;
365
h.flags = PSize|PMaxSize|PMinSize|PBaseSize|PResizeInc;
366
XSetWMNormalHints(display, XtWindow(toplevel), &h);
370
x11_add_event_handlers(Widget w) {
371
XtAddEventHandler(w, ButtonPressMask, FALSE, button_press, NULL);
372
XtAddEventHandler(w, ButtonReleaseMask, FALSE, button_release, NULL);
373
XtAddEventHandler(w, LeaveWindowMask, FALSE, leave_window, NULL);
374
XtAddEventHandler(w, EnterWindowMask, FALSE, enter_window, NULL);
375
XtAddEventHandler(w, ExposureMask, FALSE, redraw_window, NULL);
384
x11_set_drawingarea(Widget w, int size) {
386
window = XtWindow(w);