]> git.sur5r.net Git - i3/i3/blob - i3bar/src/xcb.c
Print the workspace-names
[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 init_xcb() {
23         /* FIXME: xcb_connect leaks Memory */
24         xcb_connection = xcb_connect(NULL, NULL);
25         if (xcb_connection_has_error(xcb_connection)) {
26                 printf("Cannot open display\n");
27                 exit(EXIT_FAILURE);
28         }
29         printf("Connected to xcb\n");
30
31         /* We have to request the atoms we need */
32         #define ATOM_DO(name) atom_cookies[name] = xcb_intern_atom(xcb_connection, 0, strlen(#name), #name);
33         #include "xcb_atoms.def"
34
35         xcb_screens = xcb_setup_roots_iterator(xcb_get_setup(xcb_connection)).data;
36         xcb_root = xcb_screens->root;
37
38         /* FIXME: Maybe we can push that further backwards */
39         get_atoms();
40 }
41
42 void clean_xcb() {
43         xcb_disconnect(xcb_connection);
44 }
45
46 void get_atoms() {
47         xcb_intern_atom_reply_t* reply;
48         #define ATOM_DO(name)   reply = xcb_intern_atom_reply(xcb_connection, atom_cookies[name], NULL); \
49                                 atoms[name] = reply->atom; \
50                                 free(reply);
51
52         #include "xcb_atoms.def"
53         printf("Got Atoms\n");
54 }
55
56 void destroy_windows() {
57         i3_output *walk = outputs;
58         while(walk != NULL) {
59                 if (walk->bar == XCB_NONE) {
60                         continue;
61                 }
62                 xcb_destroy_window(xcb_connection, walk->bar);
63                 walk->bar = XCB_NONE;
64         }
65 }
66
67 void create_windows() {
68         uint32_t mask;
69         uint32_t values[2];
70
71         i3_output* walk = outputs;
72         while (walk != NULL) {
73                 if (!walk->active) {
74                         walk = walk->next;
75                         continue;
76                 }
77                 printf("Creating Window for output %s\n", walk->name);
78
79                 walk->bar = xcb_generate_id(xcb_connection);
80                 mask = XCB_CW_BACK_PIXEL;
81                 values[0] = xcb_screens->black_pixel;
82                 xcb_create_window(xcb_connection,
83                                   xcb_screens->root_depth,
84                                   walk->bar,
85                                   xcb_root,
86                                   walk->rect.x, walk->rect.y,
87                                   walk->rect.w, 20,
88                                   1,
89                                   XCB_WINDOW_CLASS_INPUT_OUTPUT,
90                                   xcb_screens->root_visual,
91                                   mask,
92                                   values);
93
94                 xcb_change_property(xcb_connection,
95                                     XCB_PROP_MODE_REPLACE,
96                                     walk->bar,
97                                     atoms[_NET_WM_WINDOW_TYPE],
98                                     atoms[ATOM],
99                                     32,
100                                     1,
101                                     (unsigned char*) &atoms[_NET_WM_WINDOW_TYPE_DOCK]);
102
103                 walk->bargc = xcb_generate_id(xcb_connection);
104                 xcb_create_gc(xcb_connection,
105                               walk->bargc,
106                               walk->bar,
107                               0,
108                               NULL);
109
110                 xcb_map_window(xcb_connection, walk->bar);
111                 walk = walk->next;
112         }
113         xcb_flush(xcb_connection);
114 }
115
116 void draw_buttons() {
117         printf("Drawing Buttons...\n");
118         i3_output *outputs_walk = outputs;
119         int i = 0;
120         xcb_font_t button_font = xcb_generate_id(xcb_connection);
121         char *fontname = "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-iso10646-1";
122         xcb_open_font(xcb_connection,
123                       button_font,
124                       strlen(fontname),
125                       fontname);
126         xcb_change_gc(xcb_connection,
127                       outputs_walk->bargc,
128                       XCB_GC_FONT,
129                       &button_font);
130         while (outputs_walk != NULL) {
131                 if (!outputs_walk->active) {
132                         printf("Output %s inactive, skipping...\n", outputs_walk->name);
133                         outputs_walk = outputs_walk->next;
134                         continue;
135                 }
136                 if (outputs_walk->bar == XCB_NONE) {
137                         create_windows();
138                 }
139                 uint32_t color = get_colorpixel("000000");
140                 xcb_change_gc(xcb_connection,
141                               outputs_walk->bargc,
142                               XCB_GC_FOREGROUND,
143                               &color);
144                 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, 20 };
145                 xcb_poly_fill_rectangle(xcb_connection,
146                                         outputs_walk->bar,
147                                         outputs_walk->bargc,
148                                         1,
149                                         &rect);
150                 i3_ws *ws_walk = workspaces;
151                 while (ws_walk != NULL) {
152                         if (ws_walk->output != outputs_walk) {
153                                 printf("WS %s on wrong output, skipping...\n", ws_walk->name);
154                                 ws_walk = ws_walk->next;
155                                 continue;
156                         }
157                         printf("Drawing Button for WS %s...\n", ws_walk->name);
158                         uint32_t color = get_colorpixel("240000");
159                         if (ws_walk->visible) {
160                                 color = get_colorpixel("480000");
161                         }
162                         if (ws_walk->urgent) {
163                                 color = get_colorpixel("002400");
164                         }
165                         xcb_change_gc(xcb_connection,
166                                       outputs_walk->bargc,
167                                       XCB_GC_FOREGROUND,
168                                       &color);
169                         xcb_change_gc(xcb_connection,
170                                       outputs_walk->bargc,
171                                       XCB_GC_BACKGROUND,
172                                       &color);
173                         xcb_rectangle_t rect = { i + 1, 1, 18, 18 };
174                         xcb_poly_fill_rectangle(xcb_connection,
175                                                 outputs_walk->bar,
176                                                 outputs_walk->bargc,
177                                                 1,
178                                                 &rect);
179                         color = get_colorpixel("FFFFFF");
180                         xcb_change_gc(xcb_connection,
181                                       outputs_walk->bargc,
182                                       XCB_GC_FOREGROUND,
183                                       &color);
184                         xcb_image_text_8(xcb_connection,
185                                          strlen(ws_walk->name),
186                                          outputs_walk->bar,
187                                          outputs_walk->bargc,
188                                          i + 3, 14,
189                                          ws_walk->name);
190                         i += 20;
191                         ws_walk = ws_walk->next;
192                 }
193                 outputs_walk = outputs_walk->next;
194                 i = 0;
195         }
196         xcb_flush(xcb_connection);
197 }