5 #include <X11/keysym.h>
8 static void redraw(void);
9 static void reshape(int x, int y);
10 static void keydown(int key, int x, int y);
11 static void keyup(int key, int x, int y);
12 static void button(int bn, int st, int x, int y);
13 static void motion(int x, int y);
15 static int create_window(const char *title, int width, int height);
16 static void handle_event(XEvent *ev);
17 static KeySym translate_keysym(KeySym sym);
18 static void set_window_title(const char *title);
22 static Window win, root;
23 static Atom xa_wm_proto, xa_wm_del_win;
24 static int quit, mapped, win_width, win_height, modstate;
27 int main(int argc, char **argv)
29 if(!(dpy = XOpenDisplay(0))) {
30 fprintf(stderr, "failed to connect to the X server\n");
33 xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", False);
34 xa_wm_del_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
36 scr = DefaultScreen(dpy);
37 root = RootWindow(dpy, scr);
39 if(create_window("gph-gfx test", 800, 600) == -1) {
43 if(ggfx_init("test", GGFX_INIT_DEBUG) == -1) {
48 while(XPending(dpy)) {
65 static void redraw(void)
67 XSetForeground(dpy, gc, 0xff00);
68 XClearWindow(dpy, win);
72 static void reshape(int x, int y)
74 printf("reshape %dx%d\n", x, y);
77 static void keydown(int key, int x, int y)
86 static void keyup(int key, int x, int y)
90 static void button(int bn, int st, int x, int y)
94 static void motion(int x, int y)
98 static int create_window(const char *title, int width, int height)
100 XSetWindowAttributes xattr = {0};
102 unsigned int xattr_mask;
104 if(!XMatchVisualInfo(dpy, scr, 24, TrueColor, &vinf)) {
105 fprintf(stderr, "failed to find appropriate visual\n");
109 xattr.background_pixel = BlackPixel(dpy, scr);
110 xattr.colormap = XCreateColormap(dpy, root, vinf.visual, AllocNone);
111 xattr_mask = CWBackPixel | CWColormap | CWBackPixmap | CWBorderPixel;
112 if(!(win = XCreateWindow(dpy, root, 0, 0, width, height, 0, vinf.depth,
113 InputOutput, vinf.visual, xattr_mask, &xattr))) {
114 fprintf(stderr, "Failed to create window\n");
118 XSelectInput(dpy, win, ExposureMask | StructureNotifyMask | KeyPressMask |
119 KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
121 set_window_title(title);
122 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
123 XMapWindow(dpy, win);
125 gc = XCreateGC(dpy, win, 0, 0);
130 static KeySym translate_keysym(KeySym sym)
151 static void handle_event(XEvent *ev)
162 case ConfigureNotify:
163 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
164 win_width = ev->xconfigure.width;
165 win_height = ev->xconfigure.height;
166 reshape(ev->xconfigure.width, ev->xconfigure.height);
171 if(ev->xclient.message_type == xa_wm_proto) {
172 if(ev->xclient.data.l[0] == xa_wm_del_win) {
184 modstate = ev->xkey.state & (ShiftMask | ControlMask | Mod1Mask);
185 if(!(sym = XLookupKeysym(&ev->xkey, 0))) {
188 sym = translate_keysym(sym);
189 if(ev->type == KeyPress) {
190 keydown(sym, ev->xkey.x, ev->xkey.y);
192 keyup(sym, ev->xkey.x, ev->xkey.y);
198 modstate = ev->xbutton.state & (ShiftMask | ControlMask | Mod1Mask);
199 button(ev->xbutton.button - Button1, ev->type == ButtonPress,
200 ev->xbutton.x, ev->xbutton.y);
204 motion(ev->xmotion.x, ev->xmotion.y);
212 static void set_window_title(const char *title)
215 if(!XStringListToTextProperty((char**)&title, 1, &tprop)) {
218 XSetWMName(dpy, win, &tprop);