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 
31 		while ((event = xcb_poll_for_event (c))) {
32 			bool handled = false;
33 			uint8_t response_type = event->response_type & ~0x80;
34 
35 			switch (response_type) {
36 			case XCB_EXPOSE: {
37 				handled = true;
38 				break;
39 			}
40 			case XCB_KEY_PRESS: {
41 				handled = true;
42 
43 				xcb_key_press_event_t *kp = (xcb_key_press_event_t *)event;
44 				if (kp->detail == 24 || kp->detail == 9) {
45 					printf("Exit due to keypress\n");
46 					need_exit = true;
47 				}
48 
49 				break;
50 			}
51 			}
52 
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.
56 
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 			}
67 
68 			free(event);
69 		}
70 
71 		if (s_num_frames && framenum >= s_num_frames)
72 			need_exit = true;
73 
74 		if (need_exit)
75 			break;
76 
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 }
82 
main_x11()83 void main_x11()
84 {
85 	Display* dpy = XOpenDisplay(NULL);
86 	FAIL_IF(!dpy, "Failed to connect to the X server");
87 
88 	xcb_connection_t *c = XGetXCBConnection(dpy);
89 
90 	/* Acquire event queue ownership */
91 	XSetEventQueueOwner(dpy, XCBOwnsEventQueue);
92 
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;
96 
97 	/* Create the window */
98 
99 	uint32_t width;
100 	uint32_t height;
101 
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 	}
109 
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 	};
117 
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);
130 
131 	if (s_fullscreen)
132 	{
133 		const char *net_wm_state = "_NET_WM_STATE";
134 		const char *net_wm_state_fullscreen = "_NET_WM_STATE_FULLSCREEN";
135 
136 		xcb_intern_atom_cookie_t cookie = xcb_intern_atom(c, 0, strlen(net_wm_state), net_wm_state);
137 		xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(c, cookie, 0);
138 
139 		xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(c, 0, strlen(net_wm_state_fullscreen), net_wm_state_fullscreen);
140 		xcb_intern_atom_reply_t* reply2 = xcb_intern_atom_reply(c, cookie2, 0);
141 
142 		xcb_change_property(c, XCB_PROP_MODE_REPLACE, window, reply->atom, XCB_ATOM_ATOM , 32, 1, (void*)&reply2->atom);
143 	}
144 
145 	xcb_map_window (c, window);
146 	xcb_flush (c);
147 
148 	main_loop(dpy, c, window, width, height);
149 
150 	xcb_flush(c);
151 	xcb_unmap_window(c, window);
152 	xcb_destroy_window(c, window);
153 
154 	XCloseDisplay(dpy);
155 }
156