#include #include #include "chess.h" /* keycodes */ #define XK_MISCELLANY #include struct { Display *dpy; int scr; Window win; GC gc; int width, height; KeyCode quit_code; } boardWindow; /* large portions of this sporked from a free example I found - therefore its * copyright status is indeterminate */ void input(char *buf, size_t len) { /* this is just ANSI input since cairo is still beyond me for the time * being */ if(feof(inputstream)) { if(inputstream != stdin && !feof(stdin)) inputstream = stdin; else { strncpy(out, "EOF reached on input", sizeof(out)); output(); exit(ERROR); } } while(1) { fgets(buf, len, inputstream); if(strncmp(buf, "//", 2)) break; } if(strchr(buf, '\n')) *strchr(buf, '\n') = '\0'; } static void board_create() { boardWindow.dpy = XOpenDisplay(0); if(!boardWindow.dpy) { fprintf(stderr, "Failed to open display\n"); exit(ERROR); } } static void board_redraw() { int i, x, y, xpos, ypos; cairo_surface_t *surface; cairo_t *cr; Visual *visual; int square_size; visual = DefaultVisual(boardWindow.dpy, DefaultScreen(boardWindow.dpy)); surface = cairo_xlib_surface_create(boardWindow.dpy, boardWindow.win, visual, boardWindow.width, boardWindow.height); cr = cairo_create(surface); cairo_set_source_rgb(cr, 255, 0, 0); cairo_rectangle(cr, 0, 0, boardWindow.width, boardWindow.height); cairo_fill(cr); square_size = boardWindow.width / 9; for(i = 0; i < 64; i++) { x = i % 8; y = i / 8; xpos = x * square_size + square_size / 2; ypos = y * square_size + square_size / 2; if((x % 2) ^ (y % 2)) { cairo_set_source_rgb(cr, 0, 0, 0); cairo_rectangle(cr, xpos, ypos, square_size, square_size); cairo_fill(cr); } else { cairo_set_source_rgb(cr, 255, 255, 255); cairo_rectangle(cr, xpos, ypos, square_size, square_size); cairo_fill(cr); } } cairo_move_to(cr, 20, square_size * 9); cairo_set_source_rgb(cr, 0, 0, 0); cairo_show_text(cr, out); if(cairo_status(cr)) { printf("Cairo is unhappy: %s\n", cairo_status_to_string(cairo_status(cr))); exit(ERROR); } cairo_destroy(cr); cairo_surface_destroy(surface); } void output_init(void) { Window root; memset(&boardWindow, 0, sizeof(boardWindow)); board_create(); boardWindow.width = 360; boardWindow.height = 440; root = DefaultRootWindow(boardWindow.dpy); boardWindow.scr = DefaultScreen(boardWindow.dpy); boardWindow.win = XCreateSimpleWindow(boardWindow.dpy, root, 0, 0, boardWindow.width, boardWindow.height, 0, BlackPixel(boardWindow.dpy, boardWindow.scr), BlackPixel(boardWindow.dpy, boardWindow.scr)); boardWindow.quit_code = XKeysymToKeycode(boardWindow.dpy, XStringToKeysym("Q")); XSelectInput(boardWindow.dpy, boardWindow.win, KeyPressMask|StructureNotifyMask|ExposureMask); XMapWindow(boardWindow.dpy, boardWindow.win); board_redraw(&boardWindow); } void output(void) { XEvent xev; board_redraw(); while(true) { putchar('.'); XNextEvent(boardWindow.dpy, &xev); switch(xev.type) { case KeyPress: { XKeyEvent *kev = &xev.xkey; if(kev->keycode == boardWindow.quit_code) { exit(QUIT); } else if(kev->keycode == XKeysymToKeycode(boardWindow.dpy, XK_Return)) return; } break; case ConfigureNotify: { XConfigureEvent *cev = &xev.xconfigure; boardWindow.width = cev->width; boardWindow.height = cev->height; } break; case Expose: { XExposeEvent *eev = &xev.xexpose; if(eev->count == 0) { board_redraw(&boardWindow); } } break; } } putchar('\n'); }