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