]> git.sur5r.net Git - i3/i3/blob - i3bar/src/xcb.c
Migrate to queue.h
[i3/i3] / i3bar / src / xcb.c
1 #include <xcb/xcb.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "xcb.h"
7 #include "outputs.h"
8 #include "workspaces.h"
9
10 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
11
12 uint32_t get_colorpixel(const char *s) {
13         char strings[3][3] = { { s[0], s[1], '\0'} ,
14                                { s[2], s[3], '\0'} ,
15                                { s[4], s[5], '\0'} };
16         uint8_t r = strtol(strings[0], NULL, 16);
17         uint8_t g = strtol(strings[1], NULL, 16);
18         uint8_t b = strtol(strings[2], NULL, 16);
19         return (r << 16 | g << 8 | b);
20 }
21
22 void handle_xcb_event(xcb_generic_event_t *event) {
23         switch (event->response_type & ~0x80) {
24                 case XCB_EXPOSE:
25                         draw_buttons();
26                         break;
27         }
28 }
29
30 int get_string_width(char *string) {
31         xcb_query_text_extents_cookie_t cookie;
32         xcb_query_text_extents_reply_t *reply;
33         xcb_generic_error_t *error;
34         int width;
35
36         cookie = xcb_query_text_extents(xcb_connection, xcb_font, strlen(string), (xcb_char2b_t *)string);
37         if ((reply= xcb_query_text_extents_reply(xcb_connection, cookie, &error)) == NULL) {
38                 printf("ERROR: Could not get text extents!");
39                 return 7;
40         }
41
42         width = reply->overall_width;
43         free(reply);
44         return width;
45 }
46
47 void init_xcb() {
48         /* FIXME: xcb_connect leaks Memory */
49         xcb_connection = xcb_connect(NULL, NULL);
50         if (xcb_connection_has_error(xcb_connection)) {
51                 printf("Cannot open display\n");
52                 exit(EXIT_FAILURE);
53         }
54         printf("Connected to xcb\n");
55
56         /* We have to request the atoms we need */
57         #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
58         #include "xcb_atoms.def"
59
60         xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
61         xcb_root = xcb_screens->root;
62
63         xcb_font = xcb_generate_id(xcb_connection);
64         char *fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
65         xcb_open_font(xcb_connection,
66                       xcb_font,
67                       strlen(fontname),
68                       fontname);
69
70         xcb_list_fonts_with_info_cookie_t cookie;
71         cookie = xcb_list_fonts_with_info(xcb_connection,
72                                           1,
73                                           strlen(fontname),
74                                           fontname);
75         xcb_list_fonts_with_info_reply_t *reply;
76         reply = xcb_list_fonts_with_info_reply(xcb_connection,
77                                                cookie,
78                                                NULL);
79         font_height = reply->font_ascent + reply->font_descent;
80         printf("Calculated Font-height: %d\n", font_height);
81
82
83         /* FIXME: Maybe we can push that further backwards */
84         get_atoms();
85 }
86
87 void clean_xcb() {
88         xcb_disconnect(xcb_connection);
89 }
90
91 void get_atoms() {
92         xcb_intern_atom_reply_t* reply;
93         #define ATOM_DO(name)   reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
94                                 atoms[name] = reply->atom; \
95                                 free(reply);
96
97         #include "xcb_atoms.def"
98         printf("Got Atoms\n");
99 }
100
101 void destroy_windows() {
102         i3_output *walk;
103         if (outputs == NULL) {
104                 return;
105         }
106         SLIST_FOREACH(walk, outputs, slist) {
107                 if (walk->bar == XCB_NONE) {
108                         continue;
109                 }
110                 xcb_destroy_window(xcb_connection, walk->bar);
111                 walk->bar = XCB_NONE;
112         }
113 }
114
115 void create_windows() {
116         uint32_t mask;
117         uint32_t values[2];
118
119         i3_output *walk;
120         SLIST_FOREACH(walk, outputs, slist) {
121                 if (!walk->active) {
122                         continue;
123                 }
124                 printf("Creating Window for output %s\n", walk->name);
125
126                 walk->bar = xcb_generate_id(xcb_connection);
127                 mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
128                 values[0] = xcb_screens->black_pixel;
129                 values[1] = XCB_EVENT_MASK_EXPOSURE;
130                 xcb_create_window(xcb_connection,
131                                   xcb_screens->root_depth,
132                                   walk->bar,
133                                   xcb_root,
134                                   walk->rect.x, walk->rect.y,
135                                   walk->rect.w, font_height + 6,
136                                   1,
137                                   XCB_WINDOW_CLASS_INPUT_OUTPUT,
138                                   xcb_screens->root_visual,
139                                   mask,
140                                   values);
141
142                 xcb_change_property(xcb_connection,
143                                     XCB_PROP_MODE_REPLACE,
144                                     walk->bar,
145                                     atoms[_NET_WM_WINDOW_TYPE],
146                                     atoms[ATOM],
147                                     32,
148                                     1,
149                                     (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
150
151                 walk->bargc = xcb_generate_id(xcb_connection);
152                 mask = XCB_GC_FONT;
153                 values[0] = xcb_font;
154                 xcb_create_gc(xcb_connection,
155                               walk->bargc,
156                               walk->bar,
157                               mask,
158                               values);
159
160                 xcb_map_window(xcb_connection, walk->bar);
161         }
162         xcb_flush(xcb_connection);
163 }
164
165 void draw_buttons() {
166         printf("Drawing Buttons...\n");
167         int i = 0;
168         i3_output *outputs_walk;
169         SLIST_FOREACH(outputs_walk, outputs, slist) {
170                 if (!outputs_walk->active) {
171                         printf("Output %s inactive, skipping...\n", outputs_walk->name);
172                         continue;
173                 }
174                 if (outputs_walk->bar == XCB_NONE) {
175                         create_windows();
176                 }
177                 uint32_t color = get_colorpixel("000000");
178                 xcb_change_gc(xcb_connection,
179                               outputs_walk->bargc,
180                               XCB_GC_FOREGROUND,
181                               &color);
182                 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, font_height + 6 };
183                 xcb_poly_fill_rectangle(xcb_connection,
184                                         outputs_walk->bar,
185                                         outputs_walk->bargc,
186                                         1,
187                                         &rect);
188                 i3_ws *ws_walk;
189                 TAILQ_FOREACH(ws_walk, outputs_walk->workspaces, tailq) {
190                         printf("Drawing Button for WS %s...\n", ws_walk->name);
191                         uint32_t color = get_colorpixel("240000");
192                         if (ws_walk->visible) {
193                                 color = get_colorpixel("480000");
194                         }
195                         if (ws_walk->urgent) {
196                                 printf("WS %s is urgent!\n", ws_walk->name);
197                                 color = get_colorpixel("002400");
198                         }
199                         xcb_change_gc(xcb_connection,
200                                       outputs_walk->bargc,
201                                       XCB_GC_FOREGROUND,
202                                       &color);
203                         xcb_change_gc(xcb_connection,
204                                       outputs_walk->bargc,
205                                       XCB_GC_BACKGROUND,
206                                       &color);
207                         xcb_rectangle_t rect = { i + 1, 1, ws_walk->name_width + 8, font_height + 4 };
208                         xcb_poly_fill_rectangle(xcb_connection,
209                                                 outputs_walk->bar,
210                                                 outputs_walk->bargc,
211                                                 1,
212                                                 &rect);
213                         color = get_colorpixel("FFFFFF");
214                         xcb_change_gc(xcb_connection,
215                                       outputs_walk->bargc,
216                                       XCB_GC_FOREGROUND,
217                                       &color);
218                         xcb_image_text_8(xcb_connection,
219                                          strlen(ws_walk->name),
220                                          outputs_walk->bar,
221                                          outputs_walk->bargc,
222                                          i + 5, font_height + 1,
223                                          ws_walk->name);
224                         i += 10 + ws_walk->name_width;
225                 }
226                 i = 0;
227         }
228         xcb_flush(xcb_connection);
229 }