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_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);
40 if(app_init() == -1) {
45 while(XPending(dpy)) {
52 app_reshape(win_width, win_height);
59 XDestroyWindow(dpy, win);
69 void app_swap_buffers(void)
73 static Window create_window(const char *title, int xsz, int ysz)
78 XSetWindowAttributes xattr;
80 if(!XMatchVisualInfo(dpy, xscr, 24, TrueColor, &vinf)) {
81 fprintf(stderr, "no suitable visual found\n");
85 xattr.background_pixel = BlackPixel(dpy, xscr);
86 xattr.colormap = XCreateColormap(dpy, root_win, vinf.visual, AllocNone);
88 if(!(win = XCreateWindow(dpy, root_win, 0, 0, xsz, ysz, 0, 24, InputOutput,
89 vinf.visual, CWBackPixel | CWColormap, &xattr))) {
90 fprintf(stderr, "failed to create window\n");
94 XSelectInput(dpy, win, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
95 ButtonMotionMask | ExposureMask | StructureNotifyMask);
97 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
99 if(XStringListToTextProperty((char**)&title, 1, &txprop)) {
100 XSetWMName(dpy, win, &txprop);
101 XSetWMIconName(dpy, win, &txprop);
105 XMapWindow(dpy, win);
111 static void handle_event(XEvent *ev)
124 case ConfigureNotify:
125 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
126 win_width = ev->xconfigure.width;
127 win_height = ev->xconfigure.height;
134 if((sym = XLookupKeysym(&ev->xkey, 0)) && (key = translate_keysym(sym))) {
135 app_keyboard(key, ev->type == KeyPress);
141 app_mouse(ev->xbutton.button - Button1, ev->type == ButtonPress, ev->xbutton.x, ev->xbutton.y);
145 if(ev->xclient.message_type == xa_wm_proto) {
146 if(ev->xclient.data.l[0] == xa_wm_del_win) {
157 static int translate_keysym(KeySym sym)
159 if(sym < 128) return sym;
162 case XK_Escape: return 27;
163 case XK_Tab: return '\t';
164 case XK_Return: return '\n';
165 case XK_BackSpace: return '\b';
166 case XK_Delete: return KEY_DEL;
167 case XK_Home: return KEY_HOME;
168 case XK_End: return KEY_END;
169 case XK_Page_Up: return KEY_PGUP;
170 case XK_Page_Down: return KEY_PGDOWN;