]> git.sur5r.net Git - i3/i3/blob - include/data.h
Fix unaligned memory access on sparc (Thanks David Coppa)
[i3/i3] / include / data.h
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * include/data.h: This file defines all data structures used by i3
11  *
12  */
13 #include <xcb/xcb.h>
14 #include <xcb/randr.h>
15 #include <xcb/xcb_atom.h>
16 #include <stdbool.h>
17
18 #ifndef _DATA_H
19 #define _DATA_H
20 #include "queue.h"
21
22 /*
23  * To get the big concept: There are helper structures like struct Colorpixel
24  * or struct Stack_Window. Everything which is also defined as type (see
25  * forward definitions) is considered to be a major structure, thus important.
26  *
27  * Let’s start from the biggest to the smallest:
28  *
29  * - An Output is a physical output on your graphics driver. Outputs which
30  *   are currently in use have (output->active == true). Each output has a
31  *   position and a mode. An output usually corresponds to one connected
32  *   screen (except if you are running multiple screens in clone mode).
33  *
34  * - Each Output contains Workspaces. The concept is known from various
35  *   other window managers.  Basically, a workspace is a specific set of
36  *   windows, usually grouped thematically (irc, www, work, …). You can switch
37  *   between these.
38  *
39  * - Each Workspace has a table, which is our layout abstraction. You manage
40  *   your windows by moving them around in your table. It grows as necessary.
41  *
42  * - Each cell of the table has a container, which can be in default or
43  *   stacking mode. In default mode, each client is given equally much space
44  *   in the container. In stacking mode, only one client is shown at a time,
45  *   but all the titlebars are rendered at the top.
46  *
47  * - Inside the container are clients, which is X11-speak for a window.
48  *
49  */
50
51 /* Forward definitions */
52 typedef struct Cell Cell;
53 typedef struct Font i3Font;
54 typedef struct Container Container;
55 typedef struct Client Client;
56 typedef struct Binding Binding;
57 typedef struct Workspace Workspace;
58 typedef struct Rect Rect;
59 typedef struct xoutput Output;
60
61 /******************************************************************************
62  * Helper types
63  *****************************************************************************/
64 typedef enum { D_LEFT, D_RIGHT, D_UP, D_DOWN } direction_t;
65
66 enum {
67         BIND_NONE = 0,
68         BIND_SHIFT = XCB_MOD_MASK_SHIFT,        /* (1 << 0) */
69         BIND_CONTROL = XCB_MOD_MASK_CONTROL,    /* (1 << 2) */
70         BIND_MOD1 = XCB_MOD_MASK_1,             /* (1 << 3) */
71         BIND_MOD2 = XCB_MOD_MASK_2,             /* (1 << 4) */
72         BIND_MOD3 = XCB_MOD_MASK_3,             /* (1 << 5) */
73         BIND_MOD4 = XCB_MOD_MASK_4,             /* (1 << 6) */
74         BIND_MOD5 = XCB_MOD_MASK_5,             /* (1 << 7) */
75         BIND_MODE_SWITCH = (1 << 8)
76 };
77
78 /**
79  * Stores a rectangle, for example the size of a window, the child window etc.
80  * It needs to be packed so that the compiler will not add any padding bytes.
81  * (it is used in src/ewmh.c for example)
82  *
83  * Note that x and y can contain signed values in some cases (for example when
84  * used for the coordinates of a window, which can be set outside of the
85  * visible area, but not when specifying the position of a workspace for the
86  * _NET_WM_WORKAREA hint). Not declaring x/y as int32_t saves us a lot of
87  * typecasts.
88  *
89  * Note that x and y can contain signed values in some cases (for example when
90  * used for the coordinates of a window, which can be set outside of the
91  * visible area, but not when specifying the position of a workspace for the
92  * _NET_WM_WORKAREA hint). Not declaring x/y as int32_t saves us a lot of
93  * typecasts.
94  *
95  */
96 struct Rect {
97         uint32_t x;
98         uint32_t y;
99         uint32_t width;
100         uint32_t height;
101 } __attribute__((packed));
102
103 /**
104  * Defines a position in the table
105  *
106  */
107 struct Cell {
108         int row;
109         int column;
110 };
111
112 /**
113  * Used for the cache of colorpixels.
114  *
115  */
116 struct Colorpixel {
117         uint32_t pixel;
118         char *hex;
119         SLIST_ENTRY(Colorpixel) colorpixels;
120 };
121
122 struct Cached_Pixmap {
123         xcb_pixmap_t id;
124
125         /* We’re going to paint on it, so a graphics context will be needed */
126         xcb_gcontext_t gc;
127
128         /* The rect with which the pixmap was created */
129         Rect rect;
130
131         /* The rect of the object to which this pixmap belongs. Necessary to
132          * find out when we need to re-create the pixmap. */
133         Rect *referred_rect;
134
135         xcb_drawable_t referred_drawable;
136 };
137
138 /**
139  * Contains data for the windows needed to draw the titlebars on in stacking
140  * mode
141  *
142  */
143 struct Stack_Window {
144         xcb_window_t window;
145         struct Cached_Pixmap pixmap;
146         Rect rect;
147
148         /** Backpointer to the container this stack window is in */
149         Container *container;
150
151         SLIST_ENTRY(Stack_Window) stack_windows;
152 };
153
154 struct Ignore_Event {
155         int sequence;
156         time_t added;
157
158         SLIST_ENTRY(Ignore_Event) ignore_events;
159 };
160
161 /**
162  * Emulates the behaviour of tables of libxcb-wm, which in libxcb 0.3.4
163  * suddenly vanished.
164  *
165  */
166 struct keyvalue_element {
167         uint32_t key;
168         void *value;
169         TAILQ_ENTRY(keyvalue_element) elements;
170 };
171
172 /******************************************************************************
173  * Major types
174  *****************************************************************************/
175
176 /**
177  * The concept of Workspaces is known from various other window
178  * managers. Basically, a workspace is a specific set of windows, usually
179  * grouped thematically (irc, www, work, …). You can switch between these.
180  *
181  */
182 struct Workspace {
183         /** Number of this workspace, starting from 0 */
184         int num;
185
186         /** Name of the workspace (in UTF-8) */
187         char *utf8_name;
188
189         /** Name of the workspace (in UCS-2) */
190         char *name;
191
192         /** Length of the workspace’s name (in glyphs) */
193         int name_len;
194
195         /** Width of the workspace’s name (in pixels) rendered in config.font */
196         int text_width;
197
198         /** x, y, width, height */
199         Rect rect;
200
201         /** table dimensions */
202         int cols;
203         /** table dimensions */
204         int rows;
205
206         /** These are stored here only while this workspace is _not_ shown
207          * (see show_workspace()) */
208         int current_row;
209         /** These are stored here only while this workspace is _not_ shown
210          * (see show_workspace()) */
211         int current_col;
212
213         /** Should clients on this workspace be automatically floating? */
214         bool auto_float;
215         /** Are the floating clients on this workspace currently hidden? */
216         bool floating_hidden;
217
218         /** The name of the RandR output this screen should be on */
219         char *preferred_output;
220
221         /** True if any client on this workspace has its urgent flag set */
222         bool urgent;
223
224         /** the client who is started in fullscreen mode on this workspace,
225          * NULL if there is none */
226         Client *fullscreen_client;
227
228         /** The focus stack contains the clients in the correct order of focus
229            so that the focus can be reverted correctly when a client is
230            closed */
231         SLIST_HEAD(focus_stack_head, Client) focus_stack;
232
233         /** This tail queue contains the floating clients in order of when
234          * they were first set to floating (new floating clients are just
235          * appended) */
236         TAILQ_HEAD(floating_clients_head, Client) floating_clients;
237
238         /** Backpointer to the output this workspace is on */
239         Output *output;
240
241         /** This is a two-dimensional dynamic array of
242          * Container-pointers. I’ve always wanted to be a three-star
243          * programmer :) */
244         Container ***table;
245
246         /** width_factor and height_factor contain the amount of space
247          * (percentage) a column/row has of all the space which is available
248          * for resized windows. This ensures that non-resized windows (newly
249          * opened, for example) have the same size as always */
250         float *width_factor;
251         float *height_factor;
252
253         TAILQ_ENTRY(Workspace) workspaces;
254 };
255
256 /**
257  * Holds a keybinding, consisting of a keycode combined with modifiers and the
258  * command which is executed as soon as the key is pressed (see src/command.c)
259  *
260  */
261 struct Binding {
262         /** Symbol the user specified in configfile, if any. This needs to be
263          * stored with the binding to be able to re-convert it into a keycode
264          * if the keyboard mapping changes (using Xmodmap for example) */
265         char *symbol;
266
267         /** Only in use if symbol != NULL. Gets set to the value to which the
268          * symbol got translated when binding. Useful for unbinding and
269          * checking which binding was used when a key press event comes in.
270          *
271          * This is an array of number_keycodes size. */
272         xcb_keycode_t *translated_to;
273
274         uint32_t number_keycodes;
275
276         /** Keycode to bind */
277         uint32_t keycode;
278
279         /** Bitmask consisting of BIND_MOD_1, BIND_MODE_SWITCH, … */
280         uint32_t mods;
281
282         /** Command, like in command mode */
283         char *command;
284
285         TAILQ_ENTRY(Binding) bindings;
286 };
287
288 /**
289  * Holds a command specified by an exec-line in the config (see src/config.c)
290  *
291  */
292 struct Autostart {
293         /** Command, like in command mode */
294         char *command;
295         TAILQ_ENTRY(Autostart) autostarts;
296 };
297
298 /**
299  * Holds an assignment for a given window class/title to a specific workspace
300  * (see src/config.c)
301  *
302  */
303 struct Assignment {
304         char *windowclass_title;
305         /** floating is true if this was an assignment to the special
306          * workspace "~".  Matching clients will be put into floating mode
307          * automatically. */
308         enum {
309                 ASSIGN_FLOATING_NO,   /* don’t float, but put on a workspace */
310                 ASSIGN_FLOATING_ONLY, /* float, but don’t assign on a workspace */
311                 ASSIGN_FLOATING       /* float and put on a workspace */
312         } floating;
313
314         /** The number of the workspace to assign to. */
315         int workspace;
316         TAILQ_ENTRY(Assignment) assignments;
317 };
318
319 /**
320  * Data structure for cached font information:
321  * - font id in X11 (load it once)
322  * - font height (multiple calls needed to get it)
323  *
324  */
325 struct Font {
326         /** The name of the font, that is what the pattern resolves to */
327         char *name;
328         /** A copy of the pattern to build a cache */
329         char *pattern;
330         /** The height of the font, built from font_ascent + font_descent */
331         int height;
332         /** The xcb-id for the font */
333         xcb_font_t id;
334
335         TAILQ_ENTRY(Font) fonts;
336 };
337
338 /**
339  * A client is X11-speak for a window.
340  *
341  */
342 struct Client {
343         /** initialized will be set to true if the client was fully
344          * initialized by manage_window() and all functions can be used
345          * normally */
346         bool initialized;
347
348         /** if you set a client to floating and set it back to managed, it
349          * does remember its old position and *tries* to get back there */
350         Cell old_position;
351
352         /** Backpointer. A client is inside a container */
353         Container *container;
354         /** Because dock clients don’t have a container, we have this
355          * workspace-backpointer */
356         Workspace *workspace;
357
358         /** x, y, width, height of the frame */
359         Rect rect;
360         /** Position in floating mode and in tiling mode are saved
361          * separately */
362         Rect floating_rect;
363         /** x, y, width, height of the child (relative to its frame) */
364         Rect child_rect;
365
366         /** contains the size calculated from the hints set by the window or 0
367          * if the client did not send any hints */
368         int proportional_height;
369         int proportional_width;
370
371         int base_height;
372         int base_width;
373
374         /** The amount of pixels which X will draw around the client. */
375         int border_width;
376
377         /** contains the minimum increment size as specified for the window
378          * (in pixels). */
379         int width_increment;
380         int height_increment;
381
382         /** Height which was determined by reading the _NET_WM_STRUT_PARTIAL
383          * top/bottom of the screen reservation */
384         int desired_height;
385
386         /** Name (= window title) */
387         char *name;
388         /** name_len stores the real string length (glyphs) of the window
389          * title if the client uses _NET_WM_NAME. Otherwise, it is set to -1
390          * to indicate that name should be just passed to X as 8-bit string
391          * and therefore will not be rendered correctly. This behaviour is to
392          * support legacy applications which do not set _NET_WM_NAME */
393         int name_len;
394         /** This will be set to true as soon as the first _NET_WM_NAME comes
395          * in. If set to true, legacy window names are ignored. */
396         bool uses_net_wm_name;
397
398         /** Holds the WM_CLASS (which consists of two strings, the instance
399          * and the class), useful for matching the client in commands */
400         char *window_class_instance;
401         char *window_class_class;
402
403         /** Holds the client’s mark, for vim-like jumping */
404         char *mark;
405
406         /** Holds the xcb_window_t (just an ID) for the leader window (logical
407          * parent for toolwindows and similar floating windows) */
408         xcb_window_t leader;
409
410         /** fullscreen is pretty obvious */
411         bool fullscreen;
412
413         /** floating? (= not in tiling layout) This cannot be simply a bool
414          * because we want to keep track of whether the status was set by the
415          * application (by setting WM_CLASS to tools for example) or by the
416          * user. The user’s choice overwrites automatic mode, of course. The
417          * order of the values is important because we check with >=
418          * FLOATING_AUTO_ON if a client is floating. */
419         enum { FLOATING_AUTO_OFF = 0, FLOATING_USER_OFF = 1, FLOATING_AUTO_ON = 2, FLOATING_USER_ON = 3 } floating;
420
421         /** Ensure TITLEBAR_TOP maps to 0 because we use calloc for
422          * initialization later */
423         enum { TITLEBAR_TOP = 0, TITLEBAR_LEFT, TITLEBAR_RIGHT, TITLEBAR_BOTTOM, TITLEBAR_OFF } titlebar_position;
424
425         /** Contains a bool specifying whether this window should not be drawn
426          * with the usual decorations */
427         bool borderless;
428
429         /** If a client is set as a dock, it is placed at the very bottom of
430          * the screen and its requested size is used */
431         bool dock;
432
433         /** True if the client set the urgency flag in its WM_HINTS property */
434         bool urgent;
435
436         /* After leaving fullscreen mode, a client needs to be reconfigured
437          * (configuration = setting X, Y, width and height). By setting the
438          * force_reconfigure flag, render_layout() will reconfigure the
439          * client. */
440         bool force_reconfigure;
441
442         /* When reparenting a window, an unmap-notify is sent. As we delete
443          * windows when they’re unmapped, we need to ignore that
444          * one. Therefore, this flag is set when reparenting. */
445         bool awaiting_useless_unmap;
446
447         /* XCB contexts */
448         xcb_window_t frame;             /**< Our window: The frame around the
449                                          * client */
450         xcb_gcontext_t titlegc;         /**< The titlebar’s graphic context
451                                          * inside the frame */
452         xcb_window_t child;             /**< The client’s window */
453
454         /** The following entry provides the necessary list pointers to use
455          * Client with LIST_* macros */
456         CIRCLEQ_ENTRY(Client) clients;
457         SLIST_ENTRY(Client) dock_clients;
458         SLIST_ENTRY(Client) focus_clients;
459         TAILQ_ENTRY(Client) floating_clients;
460 };
461
462 /**
463  * A container is either in default, stacking or tabbed mode. There is one for
464  * each cell of the table.
465  *
466  */
467 struct Container {
468         /* Those are speaking for themselves: */
469         Client *currently_focused;
470         int colspan;
471         int rowspan;
472
473         /* Position of the container inside our table */
474         int row;
475         int col;
476         /* Xinerama: X/Y of the container */
477         int x;
478         int y;
479         /* Width/Height of the container. Changeable by the user */
480         int width;
481         int height;
482
483         /* When in stacking mode, we draw the titlebars of each client onto a
484          * separate window */
485         struct Stack_Window stack_win;
486
487         /* Backpointer to the workspace this container is in */
488         Workspace *workspace;
489
490         /* Ensure MODE_DEFAULT maps to 0 because we use calloc for
491          * initialization later */
492         enum { MODE_DEFAULT = 0, MODE_STACK, MODE_TABBED } mode;
493
494         /* When in stacking, one can either have unlimited windows inside the
495          * container or set a limit for the rows or columns the stack window
496          * should display to use the screen more efficiently. */
497         enum { STACK_LIMIT_NONE = 0, STACK_LIMIT_COLS, STACK_LIMIT_ROWS } stack_limit;
498
499         /* The number of columns or rows to limit to, see stack_limit */
500         int stack_limit_value;
501
502         CIRCLEQ_HEAD(client_head, Client) clients;
503 };
504
505 /**
506  * An Output is a physical output on your graphics driver. Outputs which
507  * are currently in use have (output->active == true). Each output has a
508  * position and a mode. An output usually corresponds to one connected
509  * screen (except if you are running multiple screens in clone mode).
510  *
511  */
512 struct xoutput {
513         /** Output id, so that we can requery the output directly later */
514         xcb_randr_output_t id;
515         /** Name of the output */
516         char *name;
517
518         /** Whether the output is currently active (has a CRTC attached with a
519          * valid mode) */
520         bool active;
521
522         /** Internal flags, necessary for querying RandR screens (happens in
523          * two stages) */
524         bool changed;
525         bool to_be_disabled;
526
527         /** Current workspace selected on this virtual screen */
528         Workspace *current_workspace;
529
530         /** x, y, width, height */
531         Rect rect;
532
533         /** The bar window */
534         xcb_window_t bar;
535         xcb_gcontext_t bargc;
536
537         /** Contains all clients with _NET_WM_WINDOW_TYPE ==
538          * _NET_WM_WINDOW_TYPE_DOCK */
539         SLIST_HEAD(dock_clients_head, Client) dock_clients;
540
541         TAILQ_ENTRY(xoutput) outputs;
542 };
543
544 #endif