7 static Window create_window(const char *title, int xsz, int ysz);
8 static void handle_event(XEvent *ev);
9 static int translate_keysym(KeySym sym);
12 static Window win, root_win;
14 static Atom xa_wm_proto, xa_wm_del_win;
15 static int reshape_pending, quit;
16 static int win_width, win_height, win_mapped;
18 int main(int argc, char **argv)
22 if(!(dpy = XOpenDisplay(0))) {
23 fprintf(stderr, "failed to connect to the X server\n");
26 xscr = DefaultScreen(dpy);
27 root_win = RootWindow(dpy, xscr);
28 xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", False);
29 xa_wm_del_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
31 if(!(win = create_window("vkray", 1280, 800))) {
36 while(XPending(dpy)) {
43 app_reshape(win_width, win_height);
49 XDestroyWindow(dpy, win);
59 void app_swap_buffers(void)
63 static Window create_window(const char *title, int xsz, int ysz)
68 XSetWindowAttributes xattr;
70 if(!XMatchVisualInfo(dpy, xscr, 24, TrueColor, &vinf)) {
71 fprintf(stderr, "no suitable visual found\n");
75 xattr.background_pixel = BlackPixel(dpy, xscr);
76 xattr.colormap = XCreateColormap(dpy, root_win, vinf.visual, AllocNone);
78 if(!(win = XCreateWindow(dpy, root_win, 0, 0, xsz, ysz, 0, 24, InputOutput,
79 vinf.visual, CWBackPixel | CWColormap, &xattr))) {
80 fprintf(stderr, "failed to create window\n");
84 XSelectInput(dpy, win, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
85 ButtonMotionMask | ExposureMask | StructureNotifyMask);
87 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
89 if(XStringListToTextProperty((char**)&title, 1, &txprop)) {
90 XSetWMName(dpy, win, &txprop);
91 XSetWMIconName(dpy, win, &txprop);
101 static void handle_event(XEvent *ev)
114 case ConfigureNotify:
115 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
116 win_width = ev->xconfigure.width;
117 win_height = ev->xconfigure.height;
124 if((sym = XLookupKeysym(&ev->xkey, 0)) && (key = translate_keysym(sym))) {
125 app_keyboard(key, ev->type == KeyPress);
131 app_mouse(ev->xbutton.button - Button1, ev->type == ButtonPress, ev->xbutton.x, ev->xbutton.y);
135 if(ev->xclient.message_type == xa_wm_proto) {
136 if(ev->xclient.data.l[0] == xa_wm_del_win) {
147 static int translate_keysym(KeySym sym)
149 if(sym < 128) return sym;
152 case XK_Escape: return 27;
153 case XK_Tab: return '\t';
154 case XK_Return: return '\n';
155 case XK_BackSpace: return '\b';
156 case XK_Delete: return KEY_DEL;
157 case XK_Home: return KEY_HOME;
158 case XK_End: return KEY_END;
159 case XK_Page_Up: return KEY_PGUP;
160 case XK_Page_Down: return KEY_PGDOWN;