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);
13 static Window win, root_win;
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;
19 int main(int argc, char **argv)
23 if(!(dpy = XOpenDisplay(0))) {
24 fprintf(stderr, "failed to connect to the X server\n");
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);
32 if(!(win = create_window("vkray", 1280, 800))) {
35 vk_init_xwin(dpy, win);
37 if(app_init() == -1) {
42 while(XPending(dpy)) {
49 app_reshape(win_width, win_height);
56 XDestroyWindow(dpy, win);
66 void app_swap_buffers(void)
70 static Window create_window(const char *title, int xsz, int ysz)
75 XSetWindowAttributes xattr;
77 if(!XMatchVisualInfo(dpy, xscr, 24, TrueColor, &vinf)) {
78 fprintf(stderr, "no suitable visual found\n");
82 xattr.background_pixel = BlackPixel(dpy, xscr);
83 xattr.colormap = XCreateColormap(dpy, root_win, vinf.visual, AllocNone);
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");
91 XSelectInput(dpy, win, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
92 ButtonMotionMask | ExposureMask | StructureNotifyMask);
94 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
96 if(XStringListToTextProperty((char**)&title, 1, &txprop)) {
97 XSetWMName(dpy, win, &txprop);
98 XSetWMIconName(dpy, win, &txprop);
102 XMapWindow(dpy, win);
108 static void handle_event(XEvent *ev)
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;
131 if((sym = XLookupKeysym(&ev->xkey, 0)) && (key = translate_keysym(sym))) {
132 app_keyboard(key, ev->type == KeyPress);
138 app_mouse(ev->xbutton.button - Button1, ev->type == ButtonPress, ev->xbutton.x, ev->xbutton.y);
142 if(ev->xclient.message_type == xa_wm_proto) {
143 if(ev->xclient.data.l[0] == xa_wm_del_win) {
154 static int translate_keysym(KeySym sym)
156 if(sym < 128) return 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;