dca59c875551acbba527cb31e68f4e4cdcd7761a
[android/external-libkmsxx.git] / kmscube / cube-x11.cpp
2 #include <X11/Xlib-xcb.h>
3 #include <X11/Xlibint.h>
5 #include "cube.h"
6 #include "cube-egl.h"
7 #include "cube-gles2.h"
9 #include <kms++util/kms++util.h>
11 using namespace std;
13 static void main_loop(Display* dpy, xcb_connection_t *c, xcb_window_t window, uint32_t width, uint32_t height)
14 {
15         EglState egl(dpy);
16         EglSurface surface(egl, (void*)(uintptr_t)window);
17         GlScene scene;
19         scene.set_viewport(width, height);
21         unsigned framenum = 0;
23         surface.make_current();
24         surface.swap_buffers();
26         bool need_exit = false;
28         xcb_generic_event_t *event;
29         while (true) {
31                 while ((event = xcb_poll_for_event (c))) {
32                         bool handled = false;
33                         uint8_t response_type = event->response_type & ~0x80;
35                         switch (response_type) {
36                         case XCB_EXPOSE: {
37                                 handled = true;
38                                 break;
39                         }
40                         case XCB_KEY_PRESS: {
41                                 handled = true;
43                                 xcb_key_press_event_t *kp = (xcb_key_press_event_t *)event;
44                                 if (kp->detail == 24) {
45                                         printf("Exit due to keypress\n");
46                                         need_exit = true;
47                                 }
49                                 break;
50                         }
51                         }
53                         if (!handled) {
54                                 // Check if a custom XEvent constructor was registered in xlib for this event type, and call it discarding the constructed XEvent if any.
55                                 // XESetWireToEvent might be used by libraries to intercept messages from the X server e.g. the OpenGL lib waiting for DRI2 events.
57                                 XLockDisplay(dpy);
58                                 Bool (*proc)(Display*, XEvent*, xEvent*) = XESetWireToEvent(dpy, response_type, NULL);
59                                 if (proc) {
60                                         XESetWireToEvent(dpy, response_type, proc);
61                                         XEvent dummy;
62                                         event->sequence = LastKnownRequestProcessed(dpy);
63                                         proc(dpy, &dummy, (xEvent*)event);
64                                 }
65                                 XUnlockDisplay(dpy);
66                         }
68                         free(event);
69                 }
71                 if (s_num_frames && framenum >= s_num_frames)
72                         need_exit = true;
74                 if (need_exit)
75                         break;
77                 // this should be in XCB_EXPOSE, but we don't get the event after swaps...
78                 scene.draw(framenum++);
79                 surface.swap_buffers();
80         }
81 }
83 void main_x11()
84 {
85         Display* dpy = XOpenDisplay(NULL);
86         FAIL_IF(!dpy, "Failed to connect to the X server");
88         xcb_connection_t *c = XGetXCBConnection(dpy);
90         /* Acquire event queue ownership */
91         XSetEventQueueOwner(dpy, XCBOwnsEventQueue);
93         /* Get the first screen */
94         const xcb_setup_t      *setup  = xcb_get_setup (c);
95         xcb_screen_t           *screen = xcb_setup_roots_iterator (setup).data;
97         /* Create the window */
99         uint32_t width;
100         uint32_t height;
102         if (s_fullscreen) {
103                 width = screen->width_in_pixels;
104                 height = screen->height_in_pixels;
105         } else {
106                 width = 600;
107                 height = 600;
108         }
110         const uint32_t xcb_window_attrib_mask = XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK;
111         const uint32_t xcb_window_attrib_list[] = {
112                 // OVERRIDE_REDIRECT
113                 0,
114                 // EVENT_MASK
115                 XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS,
116         };
118         xcb_window_t window = xcb_generate_id (c);
119         xcb_create_window (c,                    /* Connection          */
120                            XCB_COPY_FROM_PARENT,          /* depth (same as root)*/
121                            window,                        /* window Id           */
122                            screen->root,                  /* parent window       */
123                            0, 0,                          /* x, y                */
124                            width, height,                 /* width, height       */
125                            0,                             /* border_width        */
126                            XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
127                            screen->root_visual,           /* visual              */
128                            xcb_window_attrib_mask,
129                            xcb_window_attrib_list);
131 #if 0 // Doesn't work
132         if (s_fullscreen) {
134                 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(c, 0, 12, "_NET_WM_STATE");
135                 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(c, cookie, 0);
137                 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(c, 0, 24, "_NET_WM_STATE_FULLSCREEN");
138                 xcb_intern_atom_reply_t* reply2 = xcb_intern_atom_reply(c, cookie2, 0);
140                 xcb_change_property(c, XCB_PROP_MODE_REPLACE, window, reply->atom, XCB_ATOM_ATOM , 32, 1, (void*)&reply2->atom);
141         }
142 #endif
144         xcb_map_window (c, window);
145         xcb_flush (c);
147         main_loop(dpy, c, window, width, height);
149         xcb_flush(c);
150         xcb_unmap_window(c, window);
151         xcb_destroy_window(c, window);
153         XCloseDisplay(dpy);