xlib_playground

Xlib playground for experiments.
Log | Files | Refs

x.c (3318B)


      1 #include <stdio.h>
      2 #include <stdint.h>
      3 
      4 #include <X11/Xlib.h>
      5 
      6 #include "main.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 draw_object(const struct Object *op)
     84 {
     85 	if (op->shape == SRECTANGLE) {
     86 		x_draw_rectangle(op->color,
     87 						 op->p.x, op->p.y,
     88 						 op->body.rectangle.w,
     89 						 op->body.rectangle.h);
     90 	} else {
     91 		fprintf(stderr, "draw_object: Drawing object other than rectangle is"
     92 						 "not implemented yet.\n");
     93 	}
     94 }
     95 
     96 void
     97 draw_object_scroll(const struct Object *op, int sd)
     98 {
     99 	if (op->shape == SRECTANGLE) {
    100 		x_draw_rectangle(op->color,
    101 						 op->p.x - sd, op->p.y,
    102 						 op->body.rectangle.w,
    103 						 op->body.rectangle.h);
    104 	} else {
    105 		fprintf(stderr, "draw_object_scroll: Drawing object other than"
    106 						"rectangle is not implemented yet.\n");
    107 	}
    108 }
    109 
    110 void
    111 x_flush(void)
    112 {
    113 	XFlush(display);
    114 }
    115 
    116 void
    117 x_clean_up(void)
    118 {
    119 	XCloseDisplay(display);
    120 }
    121 
    122 int
    123 x_next_event(char *c)
    124 {
    125 	XEvent            event;
    126 	XWindowAttributes wattr;
    127 
    128 	XNextEvent(display, &event);
    129 	if (c == NULL)
    130 		return NOEVENT;
    131 	switch (event.type) {
    132 	case Expose:
    133 		XGetWindowAttributes(display, window, &wattr);
    134 		win_width = wattr.width;
    135 		win_height = wattr.height;
    136 		return XEXPOSE;
    137 		break;
    138 	case ClientMessage:
    139 		if ((Atom) event.xclient.data.l[0] == wm_delete_window) {
    140 			return XWINDEL;
    141 		}
    142 		break;
    143 	case KeyPress:
    144 		*c = XLookupKeysym(&event.xkey, 0);
    145 		return XKEYPRESS;
    146 		break;
    147 	case KeyRelease:
    148 		*c = XLookupKeysym(&event.xkey, 0);
    149 		return XKEYRELEASE;
    150 		break;
    151 	default:
    152 		break;
    153 	}
    154 	return NOEVENT;
    155 }
    156 
    157 int
    158 x_pending(void)
    159 {
    160 	return XPending(display);
    161 }
    162 
    163 void
    164 x_get_win_wh(int *w, int *h)
    165 {
    166 	*w = win_width;
    167 	*h = win_height;
    168 }