summaryrefslogtreecommitdiff
path: root/src/libxcwm/xcwm_internal.h
blob: cc183f80c72fae9c8e1eea3dbceb45cfbffe868c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* Copyright (c) 2012 Jess VanDerwalker <washu@sonic.net>
 *
 * xcwm_internal.h
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef _XCWM_INTERNAL_H_
#define _XCWM_INTERNAL_H_

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <xcb/xcb.h>
#include <xcb/xcb_image.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xcb_atom.h>
#include <xcwm/xcwm.h>

/**
 * Structure used to pass necessary data to xcwm_start_event_loop.
 */
typedef struct xcwm_event_connection {
    xcb_connection_t *conn;               /* Connection to listen to events on */
    xcwm_event_cb_t event_callback;       /* Function to call when event caught */
} xcwm_event_connection;

/**
 * Structure to hold WM_* atoms that we care about
 */
struct xcwm_wm_atoms_t {
    xcb_atom_t wm_delete_window_atom;
    xcb_atom_t wm_transient_for_atom;
    xcb_atom_t wm_state_atom;
    xcb_atom_t net_wm_window_type_splashscreen;
    xcb_ewmh_connection_t ewmh_conn;
};
typedef struct xcwm_wm_atoms_t xcwm_wm_atoms_t;

/**
 * Structure to hold connection data
 */
struct xcwm_context_t {
    xcb_connection_t *conn;
    int conn_screen;
    xcwm_window_t *root_window;
    int damage_event_mask;
    xcb_window_t wm_cm_window;
    xcwm_wm_atoms_t atoms;
};

/**
 *  Structure for holding data for a window
 */
struct xcwm_window_t {
    xcb_drawable_t window_id;
    xcwm_context_t *context;
    xcwm_window_type_t type;    /* The type of this window */
    struct xcwm_window_t *parent;
    struct xcwm_window_t *transient_for; /* Window this one is transient for */
    xcb_damage_damage_t damage;
    xcwm_rect_t bounds;
    xcwm_rect_t dmg_bounds;
    xcb_size_hints_t size_hints; /* WM_NORMAL_HINTS */
    char *name;         /* The name of the window */
    int wm_delete_set;  /* Flag for WM_DELETE_WINDOW, 1 if set */
    int override_redirect;
    int initial_damage;         /* Set to 1 for override-redirect windows */
    void *local_data;   /* Area for data client cares about */
    unsigned int opacity;
    xcb_pixmap_t composite_pixmap_id;
};

/**
 * Mutex lock supplied to client to lock event loop thread
 */
extern pthread_mutex_t event_thread_lock;


/* util.c */

/**
 * Return the given windows attributes reply. Caller must free memory
 * allocated for reply.
 * @param conn The windows connection.
 * @param window The window.
 * @return The window attributes reply. Null if the request fails.
 */
xcb_get_window_attributes_reply_t *
_xcwm_get_window_attributes(xcb_connection_t *conn, xcb_window_t window);

/**
 * Return the geometry of the window in a geometry reply. Caller must free
 * memory allocated for reply.
 * @param conn The windows connection.
 * @param window The window.
 * @return The window's geometry reply. Null if the request for reply fails.
 */
xcb_get_geometry_reply_t *
_xcwm_get_window_geometry(xcb_connection_t *conn, xcb_window_t window);

/**
 * Print out information about the existing windows attached to our
 * root. Most of this code is taken from src/manage.c from the i3 code
 * by Michael Stapelberg
 * @param conn a connection the the xserver.
 * @param window the window.
 * @return the geometry of the window
 */
void
_xcwm_write_all_children_window_info(xcb_connection_t *conn,
                                     xcb_window_t root);

/**
 * Write information about a window out to stdio.
 * TODO: Add the ability to pass in the stream to write to.
 * @param conn The connection with the window.
 * @param window The window.
 */
void
_xcwm_write_window_info(xcb_connection_t *conn, xcb_window_t window);

/**
 * Check the request cookie and determine if there is an error.
 * @param conn The connection the request was sent on.
 * @param cookie The cookie returned by the request.
 * @param msg the string to display if there is an error with the request.
 * @return int The number of the error code, if any. Otherwise zero.
 */
int
_xcwm_request_check(xcb_connection_t *conn, xcb_void_cookie_t cookie,
                    const char *msg);

/****************
* init.c
****************/

/**
 * Initializes an extension on the xserver.
 * @param conn The connection to the xserver.
 * @param extension_name The string specifying the name of the extension.
 * @return The reply structure
 */
xcb_query_extension_reply_t *
_xcwm_init_extension(xcb_connection_t *conn, const char *extension_name);

/**
 * Initializes the damage extension.
 * @param contxt  The context containing.
 */
void
_xcwm_init_damage(xcwm_context_t *contxt);

/**
 * Initializes the composite extension on the context containg
 * the root window.
 * @param contxt The contxt containing the root window
 */
void
_xcwm_init_composite(xcwm_context_t *contxt);

/**
 * Initialize the xfixes extension.
 * @param contxt The context
 */
void
_xcwm_init_xfixes(xcwm_context_t *contxt);

/****************
* event_loop.c
****************/

/**
 * Stops the thread running the event loop.
 * @return 0 on success, otherwise zero.
 */
int
_xcwm_event_stop_loop(void);

/****************
* context_list.c
****************/

/**
 * A structure (doubly linked list) to hold
 * pointers to the contexts
 */
typedef struct _xcwm_window_node {
    struct xcwm_window_t *window;   /**< Pointer to a window */
    struct _xcwm_window_node * next; /**< Pointer to the next window node */
    struct _xcwm_window_node * prev; /**< Pointer to the previous window node */
} _xcwm_window_node;

/* this is the head pointer */
extern _xcwm_window_node *_xcwm_window_list_head;

/**
 * Add a newly created window to the context_list.
 * @param window The window to be added to the linked list
 * @return Pointer to window added to the list.
 */
xcwm_window_t *
_xcwm_add_window(xcwm_window_t *window);

/**
 * Remove a window from the context_list using the window's id.
 * @param window_id The window_id of the window which should
 * be removed from the context_list
 */
void
_xcwm_remove_window_node(xcb_window_t window_id);

/**
 * Find a window in the doubly linked list using its window_id.
 * @param window_id The window_id of the window
 * @return Pointer to window (if found), NULL if not found.
 */
xcwm_window_t *
_xcwm_get_window_node_by_window_id(xcb_window_t window_id);

/****************
* window.c
****************/

/**
 * Create a new window
 * @param context The context the window was created in.
 * @param new_window ID of the window being created.
 * @param parent ID of the new window's parent.
 * @return Pointer to new window. NULL if window already exists.
 */
xcwm_window_t *
_xcwm_window_create(xcwm_context_t *context, xcb_window_t new_window,
                    xcb_window_t parent);

/**
 * Destroy the damage object associated with the window and
 * remove the window from the list of managed windows. Memory allocated
 * to the window must be removed with a call to _xcwm_window_release().
 * @param conn The connection to xserver
 * @param window The window being removed
 * @return Pointer to the window that was removed from the list, NULL if
 * window isn't being managed
 */
xcwm_window_t *
_xcwm_window_remove(xcb_connection_t *conn,
                    xcb_window_t window);
/**
 * Release the window and free its memory. Call after client has done
 * necessary clean up of the window on its side after the window has
 * been closed and a XCWM_DESTROY event has been received for the
 * window.
 * @param window The window to release.
 */
void
_xcwm_window_release(xcwm_window_t *window);

/**
 * Resize the window to given x, y, width and height.
 * @param conn The connection
 * @param window The id of window to resize
 * @param x The new x position of window, relative to root.
 * @param y The new y position of window, relative to root.
 * @param width The new width
 * @param height The new height
 */
void
_xcwm_resize_window(xcb_connection_t *conn, xcb_window_t window,
                    int x, int y, int width, int height);


/****************
 * atoms.c
 ****************/

/**
 * Get the values for the WM_* atoms that we need, as well as checking
 * to make sure another window manager is not running on the
 * connection.
 * @param context The context
 * @return Returns 0 on success, otherwise XCB error code.
 */
int
_xcwm_atoms_init(xcwm_context_t *context);

/**
 * Get and set the initial values ICCCM/EWMH atoms for the given
 * window.
 * @param window The window to get and set atoms values for.
 */
void
_xcwm_atoms_init_window(xcwm_window_t *window);

/**
 * Set the WM_DELETE_WINDOW atom for the window.
 * @param window The window.
 */
void
_xcwm_atoms_set_wm_delete(xcwm_window_t *window);

/**
 * Clean up any atom data necessary.
 * @param context The context to clean up.
 */
void
_xcwm_atoms_release(xcwm_context_t *context);

/**
 * Set the ICCCM WM_STATE and EWMH _NET_WM_STATE for the given
 * window to the state seen by the window manager.
 * @param window The window to set the state on
 * @param state The new state.
 */
void
_xcwm_atoms_set_wm_state(xcwm_window_t *window, xcwm_window_state_t state);

/**
 * Note change of a property atom, and look up corresponding event type
 * @param window The property atom which has changed
 * @param event The corresponding event type
 * @return 0 if there is no corresponding event
 */
int
_xcwm_atom_change_to_event(xcb_atom_t atom, xcwm_window_t *window, xcwm_event_type_t *event);


#endif  /* _XCWM_INTERNAL_H_ */