]> git.sur5r.net Git - i3/i3/blob - i3bar/src/xcb.c
Cleanup some Memory Leaks
[i3/i3] / i3bar / src / xcb.c
1 #include <xcb/xcb.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <i3/ipc.h>
6
7 #include "xcb.h"
8 #include "outputs.h"
9 #include "workspaces.h"
10 #include "common.h"
11 #include "ipc.h"
12
13 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
14
15 uint32_t get_colorpixel(const char *s) {
16     char strings[3][3] = { { s[0], s[1], '\0'} ,
17                            { s[2], s[3], '\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);
23 }
24
25 void handle_button(xcb_button_press_event_t *event) {
26     i3_ws *cur_ws;
27     i3_output *walk;
28     xcb_window_t bar = event->event;
29     SLIST_FOREACH(walk, outputs, slist) {
30         if (walk->bar == bar) {
31             break;
32         }
33     }
34
35     if (walk == NULL) {
36         printf("Unknown Bar klicked!\n");
37         return;
38     }
39
40     TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
41         if (cur_ws->visible) {
42             break;
43         }
44     }
45
46     if (cur_ws == NULL) {
47         printf("No Workspace active?\n");
48         return;
49     }
50
51     int32_t x = event->event_x;
52
53     printf("Got Button %d\n", event->detail);
54
55     switch (event->detail) {
56         case 1:
57             TAILQ_FOREACH(cur_ws, walk->workspaces, tailq) {
58                 printf("x = %d\n", x);
59                 if (x < cur_ws->name_width + 10) {
60                     break;
61                 }
62                 x -= cur_ws->name_width + 10;
63             }
64             if (cur_ws == NULL) {
65                 return;
66             }
67             break;
68         case 4:
69             if (cur_ws == TAILQ_LAST(walk->workspaces, ws_head)) {
70                 cur_ws = TAILQ_FIRST(walk->workspaces);
71             } else {
72                 cur_ws = TAILQ_NEXT(cur_ws, tailq);
73             }
74             break;
75         case 5:
76             if (cur_ws == TAILQ_FIRST(walk->workspaces)) {
77                 cur_ws = TAILQ_LAST(walk->workspaces, ws_head);
78             } else {
79                 cur_ws = TAILQ_PREV(cur_ws, ws_head, tailq);
80             }
81             break;
82     }
83
84     char buffer[50];
85     snprintf(buffer, 50, "%d", cur_ws->num);
86     i3_send_msg(I3_IPC_MESSAGE_TYPE_COMMAND, buffer);
87 }
88
89 void handle_xcb_event(xcb_generic_event_t *event) {
90     switch (event->response_type & ~0x80) {
91         case XCB_EXPOSE:
92             draw_bars();
93             break;
94         case XCB_BUTTON_PRESS:
95             handle_button((xcb_button_press_event_t*) event);
96             break;
97     }
98 }
99
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;
104     int width;
105
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!");
109         return 7;
110     }
111
112     width = reply->overall_width;
113     free(reply);
114     return width;
115 }
116
117 void init_xcb() {
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");
122         exit(EXIT_FAILURE);
123     }
124     printf("Connected to xcb\n");
125
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"
129
130     xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
131     xcb_root = xcb_screens->root;
132
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,
136                   xcb_font,
137                   strlen(fontname),
138                   fontname);
139
140     xcb_list_fonts_with_info_cookie_t cookie;
141     cookie = xcb_list_fonts_with_info(xcb_connection,
142                                       1,
143                                       strlen(fontname),
144                                       fontname);
145     xcb_list_fonts_with_info_reply_t *reply;
146     reply = xcb_list_fonts_with_info_reply(xcb_connection,
147                                            cookie,
148                                            NULL);
149     font_height = reply->font_ascent + reply->font_descent;
150     FREE(reply);
151     printf("Calculated Font-height: %d\n", font_height);
152
153     /* FIXME: Maybe we can push that further backwards */
154     get_atoms();
155 }
156
157 void clean_xcb() {
158     xcb_disconnect(xcb_connection);
159 }
160
161 void get_atoms() {
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; \
165         free(reply);
166
167     #include "xcb_atoms.def"
168     printf("Got Atoms\n");
169 }
170
171 void destroy_windows() {
172     i3_output *walk;
173     if (outputs == NULL) {
174         return;
175     }
176     SLIST_FOREACH(walk, outputs, slist) {
177         if (walk->bar == XCB_NONE) {
178             continue;
179         }
180         xcb_destroy_window(xcb_connection, walk->bar);
181         walk->bar = XCB_NONE;
182     }
183 }
184
185 void create_windows() {
186     uint32_t mask;
187     uint32_t values[2];
188
189     i3_output *walk;
190     SLIST_FOREACH(walk, outputs, slist) {
191         if (!walk->active) {
192             continue;
193         }
194         printf("Creating Window for output %s\n", walk->name);
195
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,
203                           walk->bar,
204                           xcb_root,
205                           walk->rect.x, walk->rect.y,
206                           walk->rect.w, font_height + 6,
207                           1,
208                           XCB_WINDOW_CLASS_INPUT_OUTPUT,
209                           xcb_screens->root_visual,
210                           mask,
211                           values);
212
213         xcb_change_property(xcb_connection,
214                             XCB_PROP_MODE_REPLACE,
215                             walk->bar,
216                             atoms[_NET_WM_WINDOW_TYPE],
217                             atoms[ATOM],
218                             32,
219                             1,
220                             (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
221
222         walk->bargc = xcb_generate_id(xcb_connection);
223         mask = XCB_GC_FONT;
224         values[0] = xcb_font;
225         xcb_create_gc(xcb_connection,
226                       walk->bargc,
227                       walk->bar,
228                       mask,
229                       values);
230
231         xcb_map_window(xcb_connection, walk->bar);
232     }
233     xcb_flush(xcb_connection);
234 }
235
236 void draw_bars() {
237     printf("Drawing Bars...\n");
238     int i = 0;
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);
243             continue;
244         }
245         if (outputs_walk->bar == XCB_NONE) {
246             create_windows();
247         }
248         uint32_t color = get_colorpixel("000000");
249         xcb_change_gc(xcb_connection,
250                       outputs_walk->bargc,
251                       XCB_GC_FOREGROUND,
252                       &color);
253         xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
254         xcb_poly_fill_rectangle(xcb_connection,
255                                 outputs_walk->bar,
256                                 outputs_walk->bargc,
257                                 1,
258                                 &rect);
259         if (statusline != NULL) {
260             printf("Printing statusline!\n");
261             xcb_change_gc(xcb_connection,
262                           outputs_walk->bargc,
263                           XCB_GC_BACKGROUND,
264                           &color);
265             color = get_colorpixel("FFFFFF");
266             xcb_change_gc(xcb_connection,
267                           outputs_walk->bargc,
268                           XCB_GC_FOREGROUND,
269                           &color);
270
271             xcb_image_text_8(xcb_connection,
272                              strlen(statusline),
273                              outputs_walk->bar,
274                              outputs_walk->bargc,
275                              outputs_walk->rect.w - get_string_width(statusline) - 4,
276                              font_height + 1,
277                              statusline);
278         }
279         i3_ws *ws_walk;
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");
285             }
286             if (ws_walk->urgent) {
287                 printf("WS %s is urgent!\n", ws_walk->name);
288                 color = get_colorpixel("002400");
289             }
290             xcb_change_gc(xcb_connection,
291                           outputs_walk->bargc,
292                           XCB_GC_FOREGROUND,
293                           &color);
294             xcb_change_gc(xcb_connection,
295                           outputs_walk->bargc,
296                           XCB_GC_BACKGROUND,
297                           &color);
298             xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
299             xcb_poly_fill_rectangle(xcb_connection,
300                                     outputs_walk->bar,
301                                     outputs_walk->bargc,
302                                     1,
303                                     &rect);
304             color = get_colorpixel("FFFFFF");
305             xcb_change_gc(xcb_connection,
306                           outputs_walk->bargc,
307                           XCB_GC_FOREGROUND,
308                           &color);
309             xcb_image_text_8(xcb_connection,
310                              strlen(ws_walk->name),
311                              outputs_walk->bar,
312                              outputs_walk->bargc,
313                              i + 5, font_height + 1,
314                              ws_walk->name);
315             i += 10 + ws_walk->name_width;
316         }
317
318         i = 0;
319     }
320     xcb_flush(xcb_connection);
321 }