x11: remove unreachable self checks
[mesa-waffle.git] / src / waffle / x11 / x11_window.c
blobcf44fd958925bb3bf0f29d3c936e04406f76937e
1 // Copyright 2012 Intel Corporation
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
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.
26 #include <assert.h>
28 #include "wcore_error.h"
30 #include "x11_display.h"
31 #include "x11_window.h"
33 static uint8_t
34 x11_winddow_get_depth(xcb_connection_t *conn,
35 const xcb_screen_t *screen,
36 xcb_visualid_t id)
38 for (xcb_depth_iterator_t depth =
39 xcb_screen_allowed_depths_iterator(screen);
40 depth.rem;
41 xcb_depth_next(&depth))
43 for (xcb_visualtype_iterator_t visual =
44 xcb_depth_visuals_iterator (depth.data);
45 visual.rem;
46 xcb_visualtype_next(&visual))
48 if (visual.data->visual_id == id)
49 return depth.data->depth;
53 return 0;
56 static xcb_screen_t *
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))
63 if (screen == 0)
64 return iter.data;
66 return NULL;
69 bool
70 x11_window_init(struct x11_window *self,
71 struct x11_display *dpy,
72 xcb_visualid_t visual_id,
73 int32_t width,
74 int32_t height)
76 xcb_colormap_t colormap = 0;
77 xcb_window_t window = 0;
79 bool ok = true;
80 xcb_connection_t *conn = dpy->xcb;
82 const xcb_setup_t *setup = xcb_get_setup(conn);
83 if (!setup){
84 wcore_errorf(WAFFLE_ERROR_UNKNOWN, "xcb_get_setup() failed");
85 goto error;
88 const xcb_screen_t *screen = get_xcb_screen(setup, dpy->screen);
89 if (!screen) {
90 wcore_errorf(WAFFLE_ERROR_UNKNOWN, "failed to get xcb screen");
91 goto error;
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");
98 goto error;
101 xcb_void_cookie_t colormap_cookie = xcb_create_colormap_checked(
102 conn,
103 XCB_COLORMAP_ALLOC_NONE,
104 colormap,
105 screen->root,
106 visual_id);
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
114 | XCB_CW_EVENT_MASK
115 | XCB_CW_COLORMAP;
117 // XCB requires that attrib_list be sorted in the same order as
118 // `enum xcb_cw_t`.
119 const uint32_t attrib_list[] = {
120 /* border_pixel */ 0,
121 event_mask,
122 colormap,
125 xcb_void_cookie_t create_cookie = xcb_create_window_checked(
126 conn,
127 x11_winddow_get_depth(conn, screen, visual_id),
128 window,
129 screen->root, // parent
130 0, 0, // x, y
131 width, height,
132 0, // border width
133 XCB_WINDOW_CLASS_INPUT_OUTPUT,
134 visual_id,
135 attrib_mask,
136 attrib_list);
138 // Check errors.
139 xcb_generic_error_t *error;
140 error = xcb_request_check(conn, colormap_cookie);
141 if (error) {
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);
145 goto error;
147 error = xcb_request_check(conn, create_cookie);
148 if (error) {
149 wcore_errorf(WAFFLE_ERROR_UNKNOWN,
150 "xcb_create_window_checked() failed: error=0x%x",
151 error->error_code);
152 goto error;
155 self->display = dpy;
156 self->xcb = window;
157 goto end;
159 error:
160 ok = false;
162 if (colormap)
163 xcb_free_colormap(conn, colormap);
165 if (window)
166 xcb_destroy_window(conn, window);
168 end:
169 return ok;
172 bool
173 x11_window_teardown(struct x11_window *self)
175 xcb_void_cookie_t cookie;
176 xcb_generic_error_t *error;
178 if (!self->xcb)
179 return true;
181 cookie = xcb_destroy_window_checked(self->display->xcb, self->xcb);
182 error = xcb_request_check(self->display->xcb, cookie);
184 if (error) {
185 wcore_errorf(WAFFLE_ERROR_UNKNOWN,
186 "xcb_destroy_window_checked() failed: error=0x%x",
187 error->error_code);
190 return !error;
193 bool
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);
202 if (error) {
203 wcore_errorf(WAFFLE_ERROR_UNKNOWN,
204 "xcb_map_window_checked() failed: error=0x%x",
205 error->error_code);
208 return !error;
211 bool
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);
223 if (error) {
224 wcore_errorf(WAFFLE_ERROR_UNKNOWN,
225 "xcb_configure_window() failed to resize width, "
226 "height: error=0x%x\n", error->error_code);
227 return false;
230 return true;