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