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))) {
35 if(app_init() == -1) {
40 while(XPending(dpy)) {
47 app_reshape(win_width, win_height);
54 XDestroyWindow(dpy, win);
64 void app_swap_buffers(void)
68 static Window create_window(const char *title, int xsz, int ysz)
73 XSetWindowAttributes xattr;
75 if(!XMatchVisualInfo(dpy, xscr, 24, TrueColor, &vinf)) {
76 fprintf(stderr, "no suitable visual found\n");
80 xattr.background_pixel = BlackPixel(dpy, xscr);
81 xattr.colormap = XCreateColormap(dpy, root_win, vinf.visual, AllocNone);
83 if(!(win = XCreateWindow(dpy, root_win, 0, 0, xsz, ysz, 0, 24, InputOutput,
84 vinf.visual, CWBackPixel | CWColormap, &xattr))) {
85 fprintf(stderr, "failed to create window\n");
89 XSelectInput(dpy, win, KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
90 ButtonMotionMask | ExposureMask | StructureNotifyMask);
92 XSetWMProtocols(dpy, win, &xa_wm_del_win, 1);
94 if(XStringListToTextProperty((char**)&title, 1, &txprop)) {
95 XSetWMName(dpy, win, &txprop);
96 XSetWMIconName(dpy, win, &txprop);
100 XMapWindow(dpy, win);
106 static void handle_event(XEvent *ev)
119 case ConfigureNotify:
120 if(ev->xconfigure.width != win_width || ev->xconfigure.height != win_height) {
121 win_width = ev->xconfigure.width;
122 win_height = ev->xconfigure.height;
129 if((sym = XLookupKeysym(&ev->xkey, 0)) && (key = translate_keysym(sym))) {
130 app_keyboard(key, ev->type == KeyPress);
136 app_mouse(ev->xbutton.button - Button1, ev->type == ButtonPress, ev->xbutton.x, ev->xbutton.y);
140 if(ev->xclient.message_type == xa_wm_proto) {
141 if(ev->xclient.data.l[0] == xa_wm_del_win) {
152 static int translate_keysym(KeySym sym)
154 if(sym < 128) return sym;
157 case XK_Escape: return 27;
158 case XK_Tab: return '\t';
159 case XK_Return: return '\n';
160 case XK_BackSpace: return '\b';
161 case XK_Delete: return KEY_DEL;
162 case XK_Home: return KEY_HOME;
163 case XK_End: return KEY_END;
164 case XK_Page_Up: return KEY_PGUP;
165 case XK_Page_Down: return KEY_PGDOWN;