1 // Copyright 2012 Intel Corporation
3 // All rights reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
8 // - Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
11 // - Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "wcore_error.h"
30 #include "x11_display.h"
31 #include "x11_window.h"
34 x11_winddow_get_depth(xcb_connection_t
*conn
,
35 const xcb_screen_t
*screen
,
38 for (xcb_depth_iterator_t depth
=
39 xcb_screen_allowed_depths_iterator(screen
);
41 xcb_depth_next(&depth
))
43 for (xcb_visualtype_iterator_t visual
=
44 xcb_depth_visuals_iterator (depth
.data
);
46 xcb_visualtype_next(&visual
))
48 if (visual
.data
->visual_id
== id
)
49 return depth
.data
->depth
;
57 get_xcb_screen(const xcb_setup_t
*setup
, int screen
)
59 xcb_screen_iterator_t iter
;
61 iter
= xcb_setup_roots_iterator(setup
);
62 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
70 x11_window_init(struct x11_window
*self
,
71 struct x11_display
*dpy
,
72 xcb_visualid_t visual_id
,
76 xcb_colormap_t colormap
= 0;
77 xcb_window_t window
= 0;
80 xcb_connection_t
*conn
= dpy
->xcb
;
82 const xcb_setup_t
*setup
= xcb_get_setup(conn
);
84 wcore_errorf(WAFFLE_ERROR_UNKNOWN
, "xcb_get_setup() failed");
88 const xcb_screen_t
*screen
= get_xcb_screen(setup
, dpy
->screen
);
90 wcore_errorf(WAFFLE_ERROR_UNKNOWN
, "failed to get xcb screen");
94 colormap
= xcb_generate_id(conn
);
95 window
= xcb_generate_id(conn
);
96 if (colormap
<= 0 || window
<= 0) {
97 wcore_errorf(WAFFLE_ERROR_UNKNOWN
, "xcb_generate_id() failed");
101 xcb_void_cookie_t colormap_cookie
= xcb_create_colormap_checked(
103 XCB_COLORMAP_ALLOC_NONE
,
108 const uint32_t event_mask
= XCB_EVENT_MASK_BUTTON_PRESS
109 | XCB_EVENT_MASK_EXPOSURE
110 | XCB_EVENT_MASK_KEY_PRESS
;
112 // Please keep attrib_mask sorted the same as attrib_list.
113 const uint32_t attrib_mask
= XCB_CW_BORDER_PIXEL
117 // XCB requires that attrib_list be sorted in the same order as
119 const uint32_t attrib_list
[] = {
120 /* border_pixel */ 0,
125 xcb_void_cookie_t create_cookie
= xcb_create_window_checked(
127 x11_winddow_get_depth(conn
, screen
, visual_id
),
129 screen
->root
, // parent
133 XCB_WINDOW_CLASS_INPUT_OUTPUT
,
139 xcb_generic_error_t
*error
;
140 error
= xcb_request_check(conn
, colormap_cookie
);
142 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
143 "xcb_create_colormap() failed on visual_id=0x%x with "
144 "error=0x%x\n", visual_id
, error
->error_code
);
147 error
= xcb_request_check(conn
, create_cookie
);
149 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
150 "xcb_create_window_checked() failed: error=0x%x",
163 xcb_free_colormap(conn
, colormap
);
166 xcb_destroy_window(conn
, window
);
173 x11_window_teardown(struct x11_window
*self
)
175 xcb_void_cookie_t cookie
;
176 xcb_generic_error_t
*error
;
181 cookie
= xcb_destroy_window_checked(self
->display
->xcb
, self
->xcb
);
182 error
= xcb_request_check(self
->display
->xcb
, cookie
);
185 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
186 "xcb_destroy_window_checked() failed: error=0x%x",
194 x11_window_show(struct x11_window
*self
)
196 xcb_void_cookie_t cookie
;
197 xcb_generic_error_t
*error
;
199 cookie
= xcb_map_window_checked(self
->display
->xcb
, self
->xcb
);
200 error
= xcb_request_check(self
->display
->xcb
, cookie
);
203 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
204 "xcb_map_window_checked() failed: error=0x%x",
212 x11_window_resize(struct x11_window
*self
, int32_t width
, int32_t height
)
214 xcb_void_cookie_t cookie
;
215 xcb_generic_error_t
*error
;
217 cookie
= xcb_configure_window(
218 self
->display
->xcb
, self
->xcb
,
219 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
,
220 (uint32_t[]){width
, height
});
222 error
= xcb_request_check(self
->display
->xcb
, cookie
);
224 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
225 "xcb_configure_window() failed to resize width, "
226 "height: error=0x%x\n", error
->error_code
);