xlib_playground

Xlib playground for experiments.
Log | Files | Refs

x.c (2648B)


      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 	if (c == NULL)
    101 		return NOEVENT;
    102 	switch (event.type) {
    103 	case Expose:
    104 		XGetWindowAttributes(display, window, &wattr);
    105 		win_width = wattr.width;
    106 		win_height = wattr.height;
    107 		return XEXPOSE;
    108 		break;
    109 	case ClientMessage:
    110 		if ((Atom) event.xclient.data.l[0] == wm_delete_window) {
    111 			return XWINDEL;
    112 		}
    113 		break;
    114 	case KeyPress:
    115 		*c = XLookupKeysym(&event.xkey, 0);
    116 		return XKEYPRESS;
    117 		break;
    118 	case KeyRelease:
    119 		*c = XLookupKeysym(&event.xkey, 0);
    120 		return XKEYRELEASE;
    121 		break;
    122 	default:
    123 		break;
    124 	}
    125 	return NOEVENT;
    126 }
    127 
    128 int
    129 x_pending(void)
    130 {
    131 	return XPending(display);
    132 }
    133 
    134 void
    135 x_get_win_wh(int *w, int *h)
    136 {
    137 	*w = win_width;
    138 	*h = win_height;
    139 }