9 #include "workspaces.h"
12 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
14 uint32_t get_colorpixel(const char *s) {
15 char strings[3][3] = { { s[0], s[1], '\0'} ,
17 { s[4], s[5], '\0'} };
18 uint8_t r = strtol(strings[0], NULL, 16);
19 uint8_t g = strtol(strings[1], NULL, 16);
20 uint8_t b = strtol(strings[2], NULL, 16);
21 return (r << 16 | g << 8 | b);
24 void handle_button(xcb_button_press_event_t *event) {
27 xcb_window_t bar = event->event;
28 SLIST_FOREACH(walk, outputs, slist) {
29 if (walk->bar == bar) {
35 printf("Unknown Bar klicked!\n");
39 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
40 if (cur_ws->visible) {
46 printf("No Workspace active?\n");
50 int32_t x = event->event_x;
52 printf("Got Button %d\n", event->detail);
54 switch (event->detail) {
56 TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
57 printf("x = %d\n", x);
58 if (x < cur_ws->name_width + 10) {
61 x -= cur_ws->name_width + 10;
68 if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
69 cur_ws = TAILQ_FIRST(walk->workspaces);
71 cur_ws = TAILQ_NEXT(cur_ws, tailq);
75 if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
76 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
78 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
84 snprintf(buffer, 50, "%d", cur_ws->num);
85 i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
88 void handle_xcb_event(xcb_generic_event_t *event) {
89 switch (event->response_type & ~0x80) {
93 case XCB_BUTTON_PRESS:
94 handle_button((xcb_button_press_event_t*) event);
99 int get_string_width(char *string) {
100 xcb_query_text_extents_cookie_t cookie;
101 xcb_query_text_extents_reply_t *reply;
102 xcb_generic_error_t *error;
105 cookie = xcb_query_text_extents(xcb_connection, xcb_font, strlen(string), (xcb_char2b_t*) string);
106 if ((reply= xcb_query_text_extents_reply(xcb_connection, cookie, &error)) == NULL) {
107 printf("ERROR: Could not get text extents!");
111 width = reply->overall_width;
117 /* FIXME: xcb_connect leaks Memory */
118 xcb_connection = xcb_connect(NULL, NULL);
119 if (xcb_connection_has_error(xcb_connection)) {
120 printf("Cannot open display\n");
123 printf("Connected to xcb\n");
125 /* We have to request the atoms we need */
126 #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
127 #include "xcb_atoms.def"
129 xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
130 xcb_root = xcb_screens->root;
132 xcb_font = xcb_generate_id(xcb_connection);
133 char *fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
134 xcb_open_font(xcb_connection,
139 xcb_list_fonts_with_info_cookie_t cookie;
140 cookie = xcb_list_fonts_with_info(xcb_connection,
144 xcb_list_fonts_with_info_reply_t *reply;
145 reply = xcb_list_fonts_with_info_reply(xcb_connection,
148 font_height = reply->font_ascent + reply->font_descent;
149 printf("Calculated Font-height: %d\n", font_height);
152 /* FIXME: Maybe we can push that further backwards */
157 xcb_disconnect(xcb_connection);
161 xcb_intern_atom_reply_t *reply;
162 #define ATOM_DO(name) reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
163 atoms[name] = reply->atom; \
166 #include "xcb_atoms.def"
167 printf("Got Atoms\n");
170 void destroy_windows() {
172 if (outputs == NULL) {
175 SLIST_FOREACH(walk, outputs, slist) {
176 if (walk->bar == XCB_NONE) {
179 xcb_destroy_window(xcb_connection, walk->bar);
180 walk->bar = XCB_NONE;
184 void create_windows() {
189 SLIST_FOREACH(walk, outputs, slist) {
193 printf("Creating Window for output %s\n", walk->name);
195 walk->bar = xcb_generate_id(xcb_connection);
196 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
197 values[0] = xcb_screens->black_pixel;
198 values[1] = XCB_EVENT_MASK_EXPOSURE |
199 XCB_EVENT_MASK_BUTTON_PRESS;
200 xcb_create_window(xcb_connection,
201 xcb_screens->root_depth,
204 walk->rect.x, walk->rect.y,
205 walk->rect.w, font_height + 6,
207 XCB_WINDOW_CLASS_INPUT_OUTPUT,
208 xcb_screens->root_visual,
212 xcb_change_property(xcb_connection,
213 XCB_PROP_MODE_REPLACE,
215 atoms[_NET_WM_WINDOW_TYPE],
219 (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
221 walk->bargc = xcb_generate_id(xcb_connection);
223 values[0] = xcb_font;
224 xcb_create_gc(xcb_connection,
230 xcb_map_window(xcb_connection, walk->bar);
232 xcb_flush(xcb_connection);
235 void draw_buttons() {
236 printf("Drawing Buttons...\n");
238 i3_output *outputs_walk;
239 SLIST_FOREACH(outputs_walk, outputs, slist) {
240 if (!outputs_walk->active) {
241 printf("Output %s inactive, skipping...\n", outputs_walk->name);
244 if (outputs_walk->bar == XCB_NONE) {
247 uint32_t color = get_colorpixel("000000");
248 xcb_change_gc(xcb_connection,
252 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
253 xcb_poly_fill_rectangle(xcb_connection,
259 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
260 printf("Drawing Button for WS %s at x = %d\n", ws_walk->name, i);
261 uint32_t color = get_colorpixel("240000");
262 if (ws_walk->visible) {
263 color = get_colorpixel("480000");
265 if (ws_walk->urgent) {
266 printf("WS %s is urgent!\n", ws_walk->name);
267 color = get_colorpixel("002400");
269 xcb_change_gc(xcb_connection,
273 xcb_change_gc(xcb_connection,
277 xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
278 xcb_poly_fill_rectangle(xcb_connection,
283 color = get_colorpixel("FFFFFF");
284 xcb_change_gc(xcb_connection,
288 xcb_image_text_8(xcb_connection,
289 strlen(ws_walk->name),
292 i + 5, font_height + 1,
294 i += 10 + ws_walk->name_width;
298 xcb_flush(xcb_connection);