]> git.sur5r.net Git - i3/i3/blob - i3bar/src/xcb.c
Draw the workspace-buttons
[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         while (outputs_walk != NULL) {
121                 if (!outputs_walk->active) {
122                         printf("Output %s inactive, skipping...\n", outputs_walk->name);
123                         outputs_walk = outputs_walk->next;
124                         continue;
125                 }
126                 if (outputs_walk->bar == XCB_NONE) {
127                         create_windows();
128                 }
129                 uint32_t color = get_colorpixel("000000");
130                 xcb_change_gc(xcb_connection,
131                               outputs_walk->bargc,
132                               XCB_GC_FOREGROUND,
133                               &color);
134                 xcb_rectangle_t rect = { 0, 0, outputs_walk->rect.w, 20 };
135                 xcb_poly_fill_rectangle(xcb_connection,
136                                         outputs_walk->bar,
137                                         outputs_walk->bargc,
138                                         1,
139                                         &rect);
140                 i3_ws *ws_walk = workspaces;
141                 while (ws_walk != NULL) {
142                         if (ws_walk->output != outputs_walk) {
143                                 printf("WS %s on wrong output, skipping...\n", ws_walk->name);
144                                 ws_walk = ws_walk->next;
145                                 continue;
146                         }
147                         printf("Drawing Button for WS %s...\n", ws_walk->name);
148                         uint32_t color = get_colorpixel("240000");
149                         if (ws_walk->visible) {
150                                 color = get_colorpixel("480000");
151                         }
152                         if (ws_walk->urgent) {
153                                 color = get_colorpixel("002400");
154                         }
155                         xcb_change_gc(xcb_connection,
156                                       outputs_walk->bargc,
157                                       XCB_GC_FOREGROUND,
158                                       &color);
159                         xcb_change_gc(xcb_connection,
160                                       outputs_walk->bargc,
161                                       XCB_GC_BACKGROUND,
162                                       &color);
163                         xcb_rectangle_t rect = { i + 1, 1, 18, 18 };
164                         xcb_poly_fill_rectangle(xcb_connection,
165                                                 outputs_walk->bar,
166                                                 outputs_walk->bargc,
167                                                 1,
168                                                 &rect);
169                         i += 20;
170                         ws_walk = ws_walk->next;
171                 }
172                 outputs_walk = outputs_walk->next;
173                 i = 0;
174         }
175         xcb_flush(xcb_connection);
176 }