initial commit
[vktest3] / src / main_x11.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5 #include "app.h"
6 #include "vk.h"
7
8 static Window create_window(const char *title, int xsz, int ysz);
9 static void handle_event(XEvent *ev);
10 static int translate_keysym(KeySym sym);
11
12 static Display *dpy;
13 static Window win, root_win;
14 static int xscr;
15 static Atom xa_wm_proto, xa_wm_del_win;
16 static int reshape_pending, quit;
17 static int win_width, win_height, win_mapped;
18
19 int main(int argc, char **argv)
20 {
21         XEvent ev;
22
23         if(!(dpy = XOpenDisplay(0))) {
24                 fprintf(stderr, "failed to connect to the X server\n");
25                 return 1;
26         }
27         xscr = DefaultScreen(dpy);
28         root_win = RootWindow(dpy, xscr);
29         xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", False);
30         xa_wm_del_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
31
32         if(!(win = create_window("vkray", 1280, 800))) {
33                 return 1;
34         }
35         vk_init_xwin(dpy, win);
36
37         if(app_init() == -1) {
38                 goto end;
39         }
40
41         for(;;) {
42                 while(XPending(dpy)) {
43                         XNextEvent(dpy, &ev);
44                         handle_event(&ev);
45                         if(quit) goto end;
46                 }
47
48                 if(reshape_pending) {
49                         app_reshape(win_width, win_height);
50                 }
51                 app_display();
52         }
53
54 end:
55         app_cleanup();
56         XDestroyWindow(dpy, win);
57         XCloseDisplay(dpy);
58         return 0;
59 }
60
61 void app_quit(void)
62 {
63         quit = 1;
64 }
65
66 void app_swap_buffers(void)
67 {
68 }
69
70 static Window create_window(const char *title, int xsz, int ysz)
71 {
72         Window win;
73         XVisualInfo vinf;
74         XTextProperty txprop;
75         XSetWindowAttributes xattr;
76
77         if(!XMatchVisualInfo(dpy, xscr, 24, TrueColor, &vinf)) {
78                 fprintf(stderr, "no suitable visual found\n");
79                 return 0;
80         }
81
82         xattr.background_pixel = BlackPixel(dpy, xscr);
83         xattr.colormap = XCreateColormap(dpy, root_win, vinf.visual, AllocNone);
84
85         if(!(win = XCreateWindow(dpy, root_win, 0, 0, xsz, ysz, 0, 24, InputOutput,
86                                         vinf.visual, CWBackPixel | CWColormap, &xattr))) {
87                 fprintf(stderr, "failed to create window\n");
88                 return 0;
89         }
90
91         XSelectInput(dpy, win, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
92                         ButtonMotionMask | ExposureMask | StructureNotifyMask);
93
94         XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
95
96         if(XStringListToTextProperty((char**)&title, 1, &txprop)) {
97                 XSetWMName(dpy, win, &txprop);
98                 XSetWMIconName(dpy, win, &txprop);
99                 XFree(txprop.value);
100         }
101
102         XMapWindow(dpy, win);
103
104         reshape_pending = 1;
105         return win;
106 }
107
108 static void handle_event(XEvent *ev)
109 {
110         KeySym sym;
111         int key;
112
113         switch(ev->type) {
114         case MapNotify:
115                 win_mapped = 1;
116                 break;
117         case UnmapNotify:
118                 win_mapped = 0;
119                 break;
120
121         case ConfigureNotify:
122                 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
123                         win_width = ev->xconfigure.width;
124                         win_height = ev->xconfigure.height;
125                         reshape_pending = 1;
126                 }
127                 break;
128
129         case KeyPress:
130         case KeyRelease:
131                 if((sym = XLookupKeysym(&ev->xkey, 0)) && (key = translate_keysym(sym))) {
132                         app_keyboard(key, ev->type == KeyPress);
133                 }
134                 break;
135
136         case ButtonPress:
137         case ButtonRelease:
138                 app_mouse(ev->xbutton.button - Button1, ev->type == ButtonPress, ev->xbutton.x, ev->xbutton.y);
139                 break;
140
141         case ClientMessage:
142                 if(ev->xclient.message_type == xa_wm_proto) {
143                         if(ev->xclient.data.l[0] == xa_wm_del_win) {
144                                 quit = 1;
145                         }
146                 }
147                 break;
148
149         default:
150                 break;
151         }
152 }
153
154 static int translate_keysym(KeySym sym)
155 {
156         if(sym < 128) return sym;
157
158         switch(sym) {
159         case XK_Escape: return 27;
160         case XK_Tab: return '\t';
161         case XK_Return: return '\n';
162         case XK_BackSpace: return '\b';
163         case XK_Delete: return KEY_DEL;
164         case XK_Home: return KEY_HOME;
165         case XK_End: return KEY_END;
166         case XK_Page_Up: return KEY_PGUP;
167         case XK_Page_Down: return KEY_PGDOWN;
168         default:
169                 break;
170         }
171
172         return 0;
173 }