9 #include "workspaces.h"
13 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
15 uint32_t get_colorpixel(const char *s) {
16 char strings[3][3] = { { s[0], s[1], '\0'} ,
18 { s[4], s[5], '\0'} };
19 uint8_t r = strtol(strings[0], NULL, 16);
20 uint8_t g = strtol(strings[1], NULL, 16);
21 uint8_t b = strtol(strings[2], NULL, 16);
22 return (r << 16 | g << 8 | b);
25 void handle_button(xcb_button_press_event_t *event) {
28 xcb_window_t bar = event->event;
29 SLIST_FOREACH(walk, outputs, slist) {
30 if (walk->bar == bar) {
36 printf("Unknown Bar klicked!\n");
40 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
41 if (cur_ws->visible) {
47 printf("No Workspace active?\n");
51 int32_t x = event->event_x;
53 printf("Got Button %d\n", event->detail);
55 switch (event->detail) {
57 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
58 printf("x = %d\n", x);
59 if (x < cur_ws->name_width + 10) {
62 x -= cur_ws->name_width + 10;
69 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
70 cur_ws = TAILQ_FIRST(walk->workspaces);
72 cur_ws = TAILQ_NEXT(cur_ws, tailq);
76 if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
77 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
79 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
85 snprintf(buffer, 50, "%d", cur_ws->num);
86 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
89 void handle_xcb_event(xcb_generic_event_t *event) {
90 switch (event->response_type & ~0x80) {
94 case XCB_BUTTON_PRESS:
95 handle_button((xcb_button_press_event_t*) event);
100 int get_string_width(char *string) {
101 xcb_query_text_extents_cookie_t cookie;
102 xcb_query_text_extents_reply_t *reply;
103 xcb_generic_error_t *error;
106 cookie = xcb_query_text_extents(xcb_connection, xcb_font, strlen(string), (xcb_char2b_t*) string);
107 if ((reply= xcb_query_text_extents_reply(xcb_connection, cookie, &error)) == NULL) {
108 printf("ERROR: Could not get text extents!");
112 width = reply->overall_width;
118 /* FIXME: xcb_connect leaks Memory */
119 xcb_connection = xcb_connect(NULL, NULL);
120 if (xcb_connection_has_error(xcb_connection)) {
121 printf("Cannot open display\n");
124 printf("Connected to xcb\n");
126 /* We have to request the atoms we need */
127 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
128 #include "xcb_atoms.def"
130 xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
131 xcb_root = xcb_screens->root;
133 xcb_font = xcb_generate_id(xcb_connection);
134 char *fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
135 xcb_open_font(xcb_connection,
140 xcb_list_fonts_with_info_cookie_t cookie;
141 cookie = xcb_list_fonts_with_info(xcb_connection,
145 xcb_list_fonts_with_info_reply_t *reply;
146 reply = xcb_list_fonts_with_info_reply(xcb_connection,
149 font_height = reply->font_ascent + reply->font_descent;
151 printf("Calculated Font-height: %d\n", font_height);
153 /* FIXME: Maybe we can push that further backwards */
158 xcb_disconnect(xcb_connection);
162 xcb_intern_atom_reply_t *reply;
163 #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
164 atoms[name] = reply->atom; \
167 #include "xcb_atoms.def"
168 printf("Got Atoms\n");
171 void destroy_windows() {
173 if (outputs == NULL) {
176 SLIST_FOREACH(walk, outputs, slist) {
177 if (walk->bar == XCB_NONE) {
180 xcb_destroy_window(xcb_connection, walk->bar);
181 walk->bar = XCB_NONE;
185 void create_windows() {
190 SLIST_FOREACH(walk, outputs, slist) {
194 printf("Creating Window for output %s\n", walk->name);
196 walk->bar = xcb_generate_id(xcb_connection);
197 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
198 values[0] = xcb_screens->black_pixel;
199 values[1] = XCB_EVENT_MASK_EXPOSURE |
200 XCB_EVENT_MASK_BUTTON_PRESS;
201 xcb_create_window(xcb_connection,
202 xcb_screens->root_depth,
205 walk->rect.x, walk->rect.y,
206 walk->rect.w, font_height + 6,
208 XCB_WINDOW_CLASS_INPUT_OUTPUT,
209 xcb_screens->root_visual,
213 xcb_change_property(xcb_connection,
214 XCB_PROP_MODE_REPLACE,
216 atoms[_NET_WM_WINDOW_TYPE],
220 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
222 walk->bargc = xcb_generate_id(xcb_connection);
224 values[0] = xcb_font;
225 xcb_create_gc(xcb_connection,
231 xcb_map_window(xcb_connection, walk->bar);
233 xcb_flush(xcb_connection);
237 printf("Drawing Bars...\n");
239 i3_output *outputs_walk;
240 SLIST_FOREACH(outputs_walk, outputs, slist) {
241 if (!outputs_walk->active) {
242 printf("Output %s inactive, skipping...\n", outputs_walk->name);
245 if (outputs_walk->bar == XCB_NONE) {
248 uint32_t color = get_colorpixel("000000");
249 xcb_change_gc(xcb_connection,
253 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
254 xcb_poly_fill_rectangle(xcb_connection,
259 if (statusline != NULL) {
260 printf("Printing statusline!\n");
261 xcb_change_gc(xcb_connection,
265 color = get_colorpixel("FFFFFF");
266 xcb_change_gc(xcb_connection,
271 xcb_image_text_8(xcb_connection,
275 outputs_walk->rect.w - get_string_width(statusline) - 4,
280 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
281 printf("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
282 uint32_t color = get_colorpixel("240000");
283 if (ws_walk->visible) {
284 color = get_colorpixel("480000");
286 if (ws_walk->urgent) {
287 printf("WS %s is urgent!\n", ws_walk->name);
288 color = get_colorpixel("002400");
290 xcb_change_gc(xcb_connection,
294 xcb_change_gc(xcb_connection,
298 xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
299 xcb_poly_fill_rectangle(xcb_connection,
304 color = get_colorpixel("FFFFFF");
305 xcb_change_gc(xcb_connection,
309 xcb_image_text_8(xcb_connection,
310 strlen(ws_walk->name),
313 i + 5, font_height + 1,
315 i += 10 + ws_walk->name_width;
320 xcb_flush(xcb_connection);