kmscube: fix resource leaks & freeing
[android/external-libkmsxx.git] / kmscube / cube-x11.cpp
2 #include <X11/Xlib-xcb.h>
4 #include "cube.h"
5 #include "cube-egl.h"
6 #include "cube-gles2.h"
8 #include "test.h"
10 using namespace std;
12 void main_x11()
13 {
14         Display* dpy = XOpenDisplay(NULL);
16         xcb_connection_t *c = XGetXCBConnection(dpy);
18         /* Get the first screen */
19         const xcb_setup_t      *setup  = xcb_get_setup (c);
20         xcb_screen_t           *screen = xcb_setup_roots_iterator (setup).data;
22         /* Create the window */
24         uint32_t width;
25         uint32_t height;
27         if (s_fullscreen) {
28                 width = screen->width_in_pixels;
29                 height = screen->height_in_pixels;
30         } else {
31                 width = 600;
32                 height = 600;
33         }
35         const uint32_t xcb_window_attrib_mask = XCB_CW_EVENT_MASK;
36         const uint32_t xcb_window_attrib_list[] = {
37                 XCB_EVENT_MASK_EXPOSURE,
38         };
40         xcb_window_t window = xcb_generate_id (c);
41         xcb_create_window (c,                    /* Connection          */
42                            XCB_COPY_FROM_PARENT,          /* depth (same as root)*/
43                            window,                        /* window Id           */
44                            screen->root,                  /* parent window       */
45                            0, 0,                          /* x, y                */
46                            width, height,                 /* width, height       */
47                            0,                             /* border_width        */
48                            XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
49                            screen->root_visual,           /* visual              */
50                            xcb_window_attrib_mask,
51                            xcb_window_attrib_list);
54 #if 0 // Doesn't work
55         if (s_fullscreen) {
57                 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(c, 0, 12, "_NET_WM_STATE");
58                 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(c, cookie, 0);
60                 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(c, 0, 24, "_NET_WM_STATE_FULLSCREEN");
61                 xcb_intern_atom_reply_t* reply2 = xcb_intern_atom_reply(c, cookie2, 0);
63                 xcb_change_property(c, XCB_PROP_MODE_REPLACE, window, reply->atom, XCB_ATOM_ATOM , 32, 1, (void*)&reply2->atom);
64         }
65 #endif
67         xcb_map_window (c, window);
68         xcb_flush (c);
70         {
71                 EglState egl(dpy);
72                 EglSurface surface(egl, (void*)(uintptr_t)window);
73                 GlScene scene;
75                 scene.set_viewport(width, height);
77                 unsigned framenum = 0;
79                 surface.make_current();
80                 surface.swap_buffers();
82                 xcb_generic_event_t *event;
83                 while ( (event = xcb_poll_for_event (c)) ) {
85                         free(event);
87                         if (s_num_frames && framenum >= s_num_frames)
88                                 break;
90                         surface.make_current();
91                         scene.draw(framenum++);
92                         surface.swap_buffers();
93                 }
94         }
96         xcb_flush(c);
97         xcb_unmap_window(c, window);
98         xcb_destroy_window(c, window);
100         XCloseDisplay(dpy);