xlib_playground

Xlib playground for experiments.
Log | Files | Refs

x.c (2614B)


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