xlib_playground

Xlib playground for experiments.
Log | Files | Refs

draw.c (2668B)


      1 #include <stdio.h>
      2 #include <stdint.h>
      3 
      4 #include <X11/Xlib.h>
      5 
      6 #include "draw.h"
      7 
      8 Display          *display;
      9 Atom              wm_delete_window;
     10 GC gc;
     11 Window window;
     12 unsigned int win_width, win_height;
     13 
     14 /*
     15  * Creates a window
     16  * If succeeded, returns 0
     17  * If error occured, returns -1 
     18  */
     19 int
     20 x_setup_window(int x, int y,
     21                unsigned int w, unsigned int h,
     22 			   unsigned long bc, char *win_name)
     23 {
     24 	if ((display = XOpenDisplay(NULL)) == NULL){
     25 		fprintf(stderr, "ERROR: could not open display\n");
     26 		return -1;
     27 	}
     28 	window = XCreateSimpleWindow(
     29 	                    display,
     30 	                    XDefaultRootWindow(display),
     31 	                    x, y,
     32 	                    w, h,
     33 	                    0, 0,    // I don't need border?
     34 	                    bc);
     35 	win_width = w;
     36 	win_height = h;
     37 
     38 	XStoreName(display, window, win_name);
     39 	gc = XCreateGC(display, window, 0, NULL);
     40 
     41 	wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
     42 	XSetWMProtocols(display, window, &wm_delete_window, 1);
     43 
     44 	XSelectInput(display, window,
     45 	             ExposureMask | KeyPressMask | KeyReleaseMask);
     46 
     47 	XMapWindow(display, window);	
     48 	return 0;
     49 }
     50 
     51 /*
     52  * Clears the window with the background color.
     53  * I don't know what the return value of XClearArea means...
     54  */
     55 void
     56 x_clear_area(void)
     57 {
     58 	XClearArea(display, window,
     59 			   0, 0,
     60 			   win_width, win_height,
     61 			   False);
     62 }
     63 
     64 /*
     65  * Draws a string with specified color.
     66  */
     67 void
     68 x_draw_string(unsigned long color, int x, int y, const char *str, int length)
     69 {
     70 	XSetForeground(display, gc, color);
     71 	XDrawString(display, window, gc, x, y, str, length);
     72 }
     73 
     74 void
     75 x_draw_rectangle(unsigned long color, int x, int y,
     76                  unsigned int w, unsigned int h)
     77 {
     78 	XSetForeground(display, gc, color);
     79 	XDrawRectangle(display, window, gc, x, y, w, h);
     80 }
     81 
     82 void
     83 x_flush(void)
     84 {
     85 	XFlush(display);
     86 }
     87 
     88 void
     89 x_clean_up(void)
     90 {
     91 	XCloseDisplay(display);
     92 }
     93 
     94 int
     95 x_next_event(char *c)
     96 {
     97 	XEvent            event;
     98 	XWindowAttributes wattr;
     99 
    100 	XNextEvent(display, &event);
    101 	if (c == NULL)
    102 		return NOEVENT;
    103 	switch (event.type) {
    104 	case Expose:
    105 		XGetWindowAttributes(display, window, &wattr);
    106 		win_width = wattr.width;
    107 		win_height = wattr.height;
    108 		return XEXPOSE;
    109 		break;
    110 	case ClientMessage:
    111 		if ((Atom) event.xclient.data.l[0] == wm_delete_window) {
    112 			return XWINDEL;
    113 		}
    114 		break;
    115 	case KeyPress:
    116 		*c = XLookupKeysym(&event.xkey, 0);
    117 		return XKEYPRESS;
    118 		break;
    119 	case KeyRelease:
    120 		*c = XLookupKeysym(&event.xkey, 0);
    121 		return XKEYRELEASE;
    122 		break;
    123 	default:
    124 		break;
    125 	}
    126 	return NOEVENT;
    127 }
    128 
    129 int
    130 x_pending(void)
    131 {
    132 	return XPending(display);
    133 }
    134 
    135 void
    136 x_get_win_wh(int *w, int *h)
    137 {
    138 	*w = win_width;
    139 	*h = win_height;
    140 }