1 // SPDX-FileCopyrightText: Copyright 2012 Intel Corporation
2 // SPDX-License-Identifier: BSD-2-Clause
6 #include "wcore_error.h"
8 #include "x11_display.h"
9 #include "x11_window.h"
12 x11_winddow_get_depth(xcb_connection_t
*conn
,
13 const xcb_screen_t
*screen
,
16 for (xcb_depth_iterator_t depth
=
17 xcb_screen_allowed_depths_iterator(screen
);
19 xcb_depth_next(&depth
))
21 for (xcb_visualtype_iterator_t visual
=
22 xcb_depth_visuals_iterator (depth
.data
);
24 xcb_visualtype_next(&visual
))
26 if (visual
.data
->visual_id
== id
)
27 return depth
.data
->depth
;
35 get_xcb_screen(const xcb_setup_t
*setup
, int screen
)
37 xcb_screen_iterator_t iter
;
39 iter
= xcb_setup_roots_iterator(setup
);
40 for (; iter
.rem
; --screen
, xcb_screen_next(&iter
))
48 x11_window_init(struct x11_window
*self
,
49 struct x11_display
*dpy
,
50 xcb_visualid_t visual_id
,
54 xcb_colormap_t colormap
= 0;
55 xcb_window_t window
= 0;
58 xcb_connection_t
*conn
= dpy
->xcb
;
60 const xcb_setup_t
*setup
= xcb_get_setup(conn
);
62 wcore_errorf(WAFFLE_ERROR_UNKNOWN
, "xcb_get_setup() failed");
66 const xcb_screen_t
*screen
= get_xcb_screen(setup
, dpy
->screen
);
68 wcore_errorf(WAFFLE_ERROR_UNKNOWN
, "failed to get xcb screen");
72 colormap
= xcb_generate_id(conn
);
73 window
= xcb_generate_id(conn
);
74 if (colormap
<= 0 || window
<= 0) {
75 wcore_errorf(WAFFLE_ERROR_UNKNOWN
, "xcb_generate_id() failed");
79 xcb_void_cookie_t colormap_cookie
= xcb_create_colormap_checked(
81 XCB_COLORMAP_ALLOC_NONE
,
86 const uint32_t event_mask
= XCB_EVENT_MASK_BUTTON_PRESS
87 | XCB_EVENT_MASK_EXPOSURE
88 | XCB_EVENT_MASK_KEY_PRESS
;
90 // Please keep attrib_mask sorted the same as attrib_list.
91 const uint32_t attrib_mask
= XCB_CW_BORDER_PIXEL
95 // XCB requires that attrib_list be sorted in the same order as
97 const uint32_t attrib_list
[] = {
103 xcb_void_cookie_t create_cookie
= xcb_create_window_checked(
105 x11_winddow_get_depth(conn
, screen
, visual_id
),
107 screen
->root
, // parent
111 XCB_WINDOW_CLASS_INPUT_OUTPUT
,
117 xcb_generic_error_t
*error
;
118 error
= xcb_request_check(conn
, colormap_cookie
);
120 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
121 "xcb_create_colormap() failed on visual_id=0x%x with "
122 "error=0x%x\n", visual_id
, error
->error_code
);
125 error
= xcb_request_check(conn
, create_cookie
);
127 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
128 "xcb_create_window_checked() failed: error=0x%x",
141 xcb_free_colormap(conn
, colormap
);
144 xcb_destroy_window(conn
, window
);
151 x11_window_teardown(struct x11_window
*self
)
153 xcb_void_cookie_t cookie
;
154 xcb_generic_error_t
*error
;
159 cookie
= xcb_destroy_window_checked(self
->display
->xcb
, self
->xcb
);
160 error
= xcb_request_check(self
->display
->xcb
, cookie
);
163 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
164 "xcb_destroy_window_checked() failed: error=0x%x",
172 x11_window_show(struct x11_window
*self
)
174 xcb_void_cookie_t cookie
;
175 xcb_generic_error_t
*error
;
177 cookie
= xcb_map_window_checked(self
->display
->xcb
, self
->xcb
);
178 error
= xcb_request_check(self
->display
->xcb
, cookie
);
181 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
182 "xcb_map_window_checked() failed: error=0x%x",
190 x11_window_resize(struct x11_window
*self
, int32_t width
, int32_t height
)
192 xcb_void_cookie_t cookie
;
193 xcb_generic_error_t
*error
;
195 cookie
= xcb_configure_window(
196 self
->display
->xcb
, self
->xcb
,
197 XCB_CONFIG_WINDOW_WIDTH
| XCB_CONFIG_WINDOW_HEIGHT
,
198 (uint32_t[]){width
, height
});
200 error
= xcb_request_check(self
->display
->xcb
, cookie
);
202 wcore_errorf(WAFFLE_ERROR_UNKNOWN
,
203 "xcb_configure_window() failed to resize width, "
204 "height: error=0x%x\n", error
->error_code
);