]> git.sur5r.net Git - i3/i3/blob - src/layout.c
Implement Xinerama (workspaces have a specific screen)
[i3/i3] / src / layout.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * (c) 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <xcb/xcb.h>
16
17 #include "font.h"
18 #include "i3.h"
19 #include "xcb.h"
20 #include "table.h"
21 #include "util.h"
22 #include "xinerama.h"
23
24 /* All functions handling layout/drawing of window decorations */
25
26 /*
27  * (Re-)draws window decorations for a given Client
28  *
29  */
30 void decorate_window(xcb_connection_t *conn, Client *client) {
31         uint32_t mask = 0;
32         uint32_t values[3];
33         i3Font *font = load_font(conn, pattern);
34         uint32_t background_color,
35                  text_color,
36                  border_color;
37
38         if (client->container->currently_focused == client) {
39                 background_color = get_colorpixel(conn, client->frame, "#285577");
40                 text_color = get_colorpixel(conn, client->frame, "#ffffff");
41                 border_color = get_colorpixel(conn, client->frame, "#4c7899");
42         } else {
43                 background_color = get_colorpixel(conn, client->frame, "#222222");
44                 text_color = get_colorpixel(conn, client->frame, "#888888");
45                 border_color = get_colorpixel(conn, client->frame, "#333333");
46         }
47
48         /* Our plan is the following:
49            - Draw a rect around the whole client in background_color
50            - Draw two lines in a lighter color
51            - Draw the window’s title
52
53            Note that xcb_image_text apparently adds 1xp border around the font? Can anyone confirm this?
54          */
55
56         /* Draw a green rectangle around the window */
57         mask = XCB_GC_FOREGROUND;
58         values[0] = background_color;
59         xcb_change_gc(conn, client->titlegc, mask, values);
60
61         xcb_rectangle_t rect = {0, 0, client->rect.width, client->rect.height};
62         xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &rect);
63
64         /* Draw the lines */
65         /* TODO: this needs to be more beautiful somewhen. maybe stdarg + change_gc(gc, ...) ? */
66 #define DRAW_LINE(colorpixel, x, y, to_x, to_y) { \
67                 uint32_t draw_values[1]; \
68                 draw_values[0] = colorpixel; \
69                 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND, draw_values); \
70                 xcb_point_t points[] = {{x, y}, {to_x, to_y}}; \
71                 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 2, points); \
72         }
73
74         DRAW_LINE(border_color, 2, 0, client->rect.width, 0);
75         DRAW_LINE(border_color, 2, font->height + 3, 2 + client->rect.width, font->height + 3);
76
77         /* Draw the font */
78         mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
79
80         values[0] = text_color;
81         values[1] = background_color;
82         values[2] = font->id;
83
84         xcb_change_gc(conn, client->titlegc, mask, values);
85
86         /* TODO: utf8? */
87         char *label;
88         asprintf(&label, "(%08x) %.*s", client->frame, client->name_len, client->name);
89         xcb_void_cookie_t text_cookie = xcb_image_text_8_checked(conn, strlen(label), client->frame,
90                                         client->titlegc, 3 /* X */, font->height /* Y = baseline of font */, label);
91         check_error(conn, text_cookie, "Could not draw client's title");
92         free(label);
93 }
94
95 static void render_container(xcb_connection_t *connection, Container *container) {
96         Client *client;
97         i3Font *font = load_font(connection, pattern);
98
99         if (container->mode == MODE_DEFAULT) {
100                 int num_clients = 0;
101                 CIRCLEQ_FOREACH(client, &(container->clients), clients)
102                         num_clients++;
103                 printf("got %d clients in this default container.\n", num_clients);
104
105                 int current_client = 0;
106                 CIRCLEQ_FOREACH(client, &(container->clients), clients) {
107                         /* TODO: at the moment, every column/row is screen / num_cols. This
108                          * needs to be changed to "percentage of the screen" by
109                          * default and adjustable by the user if necessary.
110                          */
111
112                         /* Check if we changed client->x or client->y by updating it…
113                          * Note the bitwise OR instead of logical OR to force evaluation of both statements */
114                         if (client->force_reconfigure |
115                             (client->rect.x != (client->rect.x = container->x + (container->col * container->width))) |
116                             (client->rect.y != (client->rect.y = container->y + (container->row * container->height +
117                                           (container->height / num_clients) * current_client)))) {
118                                 printf("frame needs to be pushed to %dx%d\n", client->rect.x, client->rect.y);
119                                 /* Note: We can use a pointer to client->x like an array of uint32_ts
120                                    because it is followed by client->y by definition */
121                                 xcb_configure_window(connection, client->frame,
122                                                 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &(client->rect.x));
123                         }
124
125                         /* TODO: vertical default layout */
126                         if (client->force_reconfigure |
127                             (client->rect.width != (client->rect.width = container->width)) |
128                             (client->rect.height != (client->rect.height = container->height / num_clients))) {
129                                 xcb_configure_window(connection, client->frame,
130                                                 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
131                                                 &(client->rect.width));
132
133                                 /* Adjust the position of the child inside its frame.
134                                  * The coordinates of the child are relative to its frame, we
135                                  * add a border of 2 pixel to each value */
136                                 uint32_t mask = XCB_CONFIG_WINDOW_X |
137                                                 XCB_CONFIG_WINDOW_Y |
138                                                 XCB_CONFIG_WINDOW_WIDTH |
139                                                 XCB_CONFIG_WINDOW_HEIGHT;
140                                 uint32_t values[4] = {2,                                                   /* x */
141                                                       font->height + 2 + 2,                                /* y */
142                                                       client->rect.width - (2 + 2),                        /* width */
143                                                       client->rect.height - ((font->height + 2 + 2) + 2)}; /* height */
144
145                                 printf("child will be at %dx%d with size %dx%d\n",
146                                                 values[0], values[1], values[2], values[3]);
147
148                                 xcb_configure_window(connection, client->child, mask, values);
149                         }
150
151                         if (client->force_reconfigure)
152                                 client->force_reconfigure = false;
153
154                         current_client++;
155                 }
156         } else {
157                 /* TODO: Implement stacking */
158         }
159 }
160
161 void render_layout(xcb_connection_t *connection) {
162         int cols, rows;
163         i3Screen *screen;
164         TAILQ_FOREACH(screen, &virtual_screens, screens) {
165                 /* r_ws (rendering workspace) is just a shortcut to the Workspace being currently rendered */
166                 Workspace *r_ws = &(workspaces[screen->current_workspace]);
167
168                 printf("Rendering screen %d\n", screen->num);
169                 if (r_ws->fullscreen_client != NULL)
170                         /* This is easy: A client has entered fullscreen mode, so we don’t render at all */
171                         continue;
172                 int width = r_ws->rect.width;
173                 int height = r_ws->rect.height;
174
175                 printf("got %d rows and %d cols\n", r_ws->rows, r_ws->cols);
176                 printf("each of them therefore is %d px width and %d px height\n",
177                                 width / r_ws->cols, height / r_ws->rows);
178
179                 /* Go through the whole table and render what’s necessary */
180                 for (cols = 0; cols < r_ws->cols; cols++)
181                         for (rows = 0; rows < r_ws->rows; rows++) {
182                                 Container *container = r_ws->table[cols][rows];
183                                 printf("container has %d colspan, %d rowspan\n",
184                                                 container->colspan, container->rowspan);
185                                 /* Update position of the container */
186                                 container->row = rows;
187                                 container->col = cols;
188                                 container->x = r_ws->rect.x;
189                                 container->y = r_ws->rect.y;
190                                 container->width = (width / r_ws->cols) * container->colspan;
191                                 container->height = (height / r_ws->rows) * container->rowspan;
192
193                                 /* Render it */
194                                 render_container(connection, container);
195                         }
196         }
197
198         xcb_flush(connection);
199 }