New Adwaita style which approximates the look of the default GTK theme.
[wmaker-crm.git] / src / wmspec.c
blobf6e74378b06af50ec09a65749891b5e85c017cd0
1 /* wmspec.c-- support for the wm-spec Hints
3 * Window Maker window manager
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * TODO
24 * ----
26 * This file needs to be checked for all calls to XGetWindowProperty() and
27 * proper checks need to be made on the returned values. Only checking for
28 * return to be Success is not enough. -Dan
31 #include "wconfig.h"
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35 #include <string.h>
37 #include <WINGs/WUtil.h>
38 #include "WindowMaker.h"
39 #include "window.h"
40 #include "screen.h"
41 #include "workspace.h"
42 #include "framewin.h"
43 #include "actions.h"
44 #include "client.h"
45 #include "appicon.h"
46 #include "wmspec.h"
47 #include "icon.h"
48 #include "stacking.h"
49 #include "xinerama.h"
50 #include "properties.h"
53 /* Root Window Properties */
54 static Atom net_supported;
55 static Atom net_client_list;
56 static Atom net_client_list_stacking;
57 static Atom net_number_of_desktops;
58 static Atom net_desktop_geometry;
59 static Atom net_desktop_viewport;
60 static Atom net_current_desktop;
61 static Atom net_desktop_names;
62 static Atom net_active_window;
63 static Atom net_workarea;
64 static Atom net_supporting_wm_check;
65 static Atom net_virtual_roots; /* N/A */
66 static Atom net_desktop_layout; /* XXX */
67 static Atom net_showing_desktop;
69 /* Other Root Window Messages */
70 static Atom net_close_window;
71 static Atom net_moveresize_window; /* TODO */
72 static Atom net_wm_moveresize; /* TODO */
74 /* Application Window Properties */
75 static Atom net_wm_name;
76 static Atom net_wm_visible_name; /* TODO (unnecessary?) */
77 static Atom net_wm_icon_name;
78 static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
79 static Atom net_wm_desktop;
80 static Atom net_wm_window_type;
81 static Atom net_wm_window_type_desktop;
82 static Atom net_wm_window_type_dock;
83 static Atom net_wm_window_type_toolbar;
84 static Atom net_wm_window_type_menu;
85 static Atom net_wm_window_type_utility;
86 static Atom net_wm_window_type_splash;
87 static Atom net_wm_window_type_dialog;
88 static Atom net_wm_window_type_dropdown_menu;
89 static Atom net_wm_window_type_popup_menu;
90 static Atom net_wm_window_type_tooltip;
91 static Atom net_wm_window_type_notification;
92 static Atom net_wm_window_type_combo;
93 static Atom net_wm_window_type_dnd;
94 static Atom net_wm_window_type_normal;
95 static Atom net_wm_state;
96 static Atom net_wm_state_modal; /* XXX: what is this?!? */
97 static Atom net_wm_state_sticky;
98 static Atom net_wm_state_maximized_vert;
99 static Atom net_wm_state_maximized_horz;
100 static Atom net_wm_state_shaded;
101 static Atom net_wm_state_skip_taskbar;
102 static Atom net_wm_state_skip_pager;
103 static Atom net_wm_state_hidden;
104 static Atom net_wm_state_fullscreen;
105 static Atom net_wm_state_above;
106 static Atom net_wm_state_below;
107 static Atom net_wm_allowed_actions;
108 static Atom net_wm_action_move;
109 static Atom net_wm_action_resize;
110 static Atom net_wm_action_minimize;
111 static Atom net_wm_action_shade;
112 static Atom net_wm_action_stick;
113 static Atom net_wm_action_maximize_horz;
114 static Atom net_wm_action_maximize_vert;
115 static Atom net_wm_action_fullscreen;
116 static Atom net_wm_action_change_desktop;
117 static Atom net_wm_action_close;
118 static Atom net_wm_strut;
119 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
120 static Atom net_wm_icon_geometry; /* FIXME: should work together with net_wm_handled_icons, gnome-panel-2.2.0.1 doesn't use _NET_WM_HANDLED_ICONS, thus present situation. */
121 static Atom net_wm_icon;
122 static Atom net_wm_pid; /* TODO */
123 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
124 static Atom net_wm_window_opacity;
126 static Atom net_frame_extents;
128 /* Window Manager Protocols */
129 static Atom net_wm_ping; /* TODO */
131 static Atom utf8_string;
133 typedef struct {
134 char *name;
135 Atom *atom;
136 } atomitem_t;
138 static atomitem_t atomNames[] = {
139 {"_NET_SUPPORTED", &net_supported},
140 {"_NET_CLIENT_LIST", &net_client_list},
141 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
142 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
143 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
144 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
145 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
146 {"_NET_DESKTOP_NAMES", &net_desktop_names},
147 {"_NET_ACTIVE_WINDOW", &net_active_window},
148 {"_NET_WORKAREA", &net_workarea},
149 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
150 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
151 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
152 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
154 {"_NET_CLOSE_WINDOW", &net_close_window},
155 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
156 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
158 {"_NET_WM_NAME", &net_wm_name},
159 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
160 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
161 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
162 {"_NET_WM_DESKTOP", &net_wm_desktop},
163 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
164 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
165 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
166 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
167 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
168 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
169 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
170 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
171 {"_NET_WM_WINDOW_TYPE_DROPDOWN_MENU", &net_wm_window_type_dropdown_menu},
172 {"_NET_WM_WINDOW_TYPE_POPUP_MENU", &net_wm_window_type_popup_menu},
173 {"_NET_WM_WINDOW_TYPE_TOOLTIP", &net_wm_window_type_tooltip},
174 {"_NET_WM_WINDOW_TYPE_NOTIFICATION", &net_wm_window_type_notification},
175 {"_NET_WM_WINDOW_TYPE_COMBO", &net_wm_window_type_combo},
176 {"_NET_WM_WINDOW_TYPE_DND", &net_wm_window_type_dnd},
177 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
178 {"_NET_WM_STATE", &net_wm_state},
179 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
180 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
181 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
182 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
183 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
184 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
185 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
186 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
187 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
188 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
189 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
190 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
191 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
192 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
193 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
194 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
195 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
196 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
197 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
198 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
199 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
200 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
201 {"_NET_WM_STRUT", &net_wm_strut},
202 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
203 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
204 {"_NET_WM_ICON", &net_wm_icon},
205 {"_NET_WM_PID", &net_wm_pid},
206 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
207 {"_NET_WM_WINDOW_OPACITY", &net_wm_window_opacity},
209 {"_NET_FRAME_EXTENTS", &net_frame_extents},
211 {"_NET_WM_PING", &net_wm_ping},
213 {"UTF8_STRING", &utf8_string},
216 #define _NET_WM_STATE_ADD 1
217 #define _NET_WM_STATE_TOGGLE 2
219 #if 0
221 * These constant provide information on the kind of window move/resize when
222 * it is initiated by the application instead of by WindowMaker. They are
223 * parameter for the client message _NET_WM_MOVERESIZE, as defined by the
224 * FreeDesktop wm-spec standard:
225 * http://standards.freedesktop.org/wm-spec/1.5/ar01s04.html
227 * Today, WindowMaker does not support this at all (the corresponding Atom
228 * is not added to the list in setSupportedHints), probably because there is
229 * nothing it needs to do about it, the application is assumed to know what
230 * it is doing, and WindowMaker won't get in the way.
232 * The definition of the constants (taken from the standard) are disabled to
233 * avoid a spurious warning (-Wunused-macros).
235 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
236 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
237 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
238 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
239 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
240 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
241 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
242 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
243 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
244 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
245 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
246 #endif
248 static void observer(void *self, WMNotification *notif);
249 static void wsobserver(void *self, WMNotification *notif);
251 static void updateClientList(WScreen *scr);
252 static void updateClientListStacking(WScreen *scr, WWindow *);
254 static void updateWorkspaceNames(WScreen *scr);
255 static void updateCurrentWorkspace(WScreen *scr);
256 static void updateWorkspaceCount(WScreen *scr);
257 static void wNETWMShowingDesktop(WScreen *scr, Bool show);
259 typedef struct NetData {
260 WScreen *scr;
261 WReservedArea *strut;
262 WWindow **show_desktop;
263 } NetData;
265 static void setSupportedHints(WScreen *scr)
267 Atom atom[wlengthof(atomNames)];
268 int i = 0;
270 /* set supported hints list */
271 /* XXX: extend this !!! */
273 atom[i++] = net_client_list;
274 atom[i++] = net_client_list_stacking;
275 atom[i++] = net_number_of_desktops;
276 atom[i++] = net_desktop_geometry;
277 atom[i++] = net_desktop_viewport;
278 atom[i++] = net_current_desktop;
279 atom[i++] = net_desktop_names;
280 atom[i++] = net_active_window;
281 atom[i++] = net_workarea;
282 atom[i++] = net_supporting_wm_check;
283 atom[i++] = net_showing_desktop;
284 #if 0
285 atom[i++] = net_wm_moveresize;
286 #endif
287 atom[i++] = net_wm_desktop;
288 atom[i++] = net_wm_window_type;
289 atom[i++] = net_wm_window_type_desktop;
290 atom[i++] = net_wm_window_type_dock;
291 atom[i++] = net_wm_window_type_toolbar;
292 atom[i++] = net_wm_window_type_menu;
293 atom[i++] = net_wm_window_type_utility;
294 atom[i++] = net_wm_window_type_splash;
295 atom[i++] = net_wm_window_type_dialog;
296 atom[i++] = net_wm_window_type_dropdown_menu;
297 atom[i++] = net_wm_window_type_popup_menu;
298 atom[i++] = net_wm_window_type_tooltip;
299 atom[i++] = net_wm_window_type_notification;
300 atom[i++] = net_wm_window_type_combo;
301 atom[i++] = net_wm_window_type_dnd;
302 atom[i++] = net_wm_window_type_normal;
304 atom[i++] = net_wm_state;
305 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
306 atom[i++] = net_wm_state_sticky;
307 atom[i++] = net_wm_state_shaded;
308 atom[i++] = net_wm_state_maximized_horz;
309 atom[i++] = net_wm_state_maximized_vert;
310 atom[i++] = net_wm_state_skip_taskbar;
311 atom[i++] = net_wm_state_skip_pager;
312 atom[i++] = net_wm_state_hidden;
313 atom[i++] = net_wm_state_fullscreen;
314 atom[i++] = net_wm_state_above;
315 atom[i++] = net_wm_state_below;
317 atom[i++] = net_wm_allowed_actions;
318 atom[i++] = net_wm_action_move;
319 atom[i++] = net_wm_action_resize;
320 atom[i++] = net_wm_action_minimize;
321 atom[i++] = net_wm_action_shade;
322 atom[i++] = net_wm_action_stick;
323 atom[i++] = net_wm_action_maximize_horz;
324 atom[i++] = net_wm_action_maximize_vert;
325 atom[i++] = net_wm_action_fullscreen;
326 atom[i++] = net_wm_action_change_desktop;
327 atom[i++] = net_wm_action_close;
329 atom[i++] = net_wm_strut;
330 atom[i++] = net_wm_icon_geometry;
331 atom[i++] = net_wm_icon;
332 atom[i++] = net_wm_handled_icons;
333 atom[i++] = net_wm_window_opacity;
335 atom[i++] = net_frame_extents;
337 atom[i++] = net_wm_name;
338 atom[i++] = net_wm_icon_name;
340 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
342 /* set supporting wm hint */
343 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
344 PropModeReplace, (unsigned char *)&scr->info_window, 1);
346 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
347 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
350 void wNETWMUpdateDesktop(WScreen *scr)
352 long *views, sizes[2];
353 int count, i;
355 if (scr->workspace_count == 0)
356 return;
358 count = scr->workspace_count * 2;
359 views = wmalloc(sizeof(long) * count);
360 sizes[0] = scr->scr_width;
361 sizes[1] = scr->scr_height;
363 for (i = 0; i < scr->workspace_count; i++) {
364 views[2 * i + 0] = 0;
365 views[2 * i + 1] = 0;
368 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
369 PropModeReplace, (unsigned char *)sizes, 2);
371 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
372 PropModeReplace, (unsigned char *)views, count);
374 wfree(views);
377 int wNETWMGetCurrentDesktopFromHint(WScreen *scr)
379 int count;
380 unsigned char *prop;
382 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
383 if (prop) {
384 int desktop = *(long *)prop;
385 XFree(prop);
386 return desktop;
388 return -1;
392 * Find the best icon to be used by Window Maker for appicon/miniwindows.
393 * Currently the algorithm is to take the image with the size closest
394 * to icon_size x icon_size, but never bigger than that.
396 * This algorithm is very poorly implemented and needs to be redone (it can
397 * easily select images with very large widths and very small heights over
398 * square images, if the area of the former is closer to the desired one).
400 * The logic can also be changed to accept bigger images and scale them down.
402 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
404 int size, wanted, d;
405 unsigned long i, distance;
406 unsigned long *icon;
408 /* better use only 75% of icon_size. For 64x64 this means 48x48
409 * This leaves room around the icon for the miniwindow title and
410 * results in better overall aesthetics -Dan */
411 wanted = wPreferences.icon_size * wPreferences.icon_size;
413 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
414 size = data[i] * data[i + 1];
415 if (size == 0)
416 break;
417 d = wanted - size;
418 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
419 distance = d;
420 icon = &data[i];
422 i += size + 2;
425 return icon;
428 static RImage *makeRImageFromARGBData(unsigned long *data)
430 int size, width, height, i;
431 RImage *image;
432 unsigned char *imgdata;
433 unsigned long pixel;
435 width = data[0];
436 height = data[1];
437 size = width * height;
439 if (size == 0)
440 return NULL;
442 image = RCreateImage(width, height, True);
444 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
445 pixel = data[i];
446 imgdata[3] = (pixel >> 24) & 0xff; /* A */
447 imgdata[0] = (pixel >> 16) & 0xff; /* R */
448 imgdata[1] = (pixel >> 8) & 0xff; /* G */
449 imgdata[2] = (pixel >> 0) & 0xff; /* B */
452 return image;
455 RImage *get_window_image_from_x11(Window window)
457 RImage *image;
458 Atom type;
459 int format;
460 unsigned long items, rest;
461 unsigned long *property, *data;
463 /* Get the icon from X11 Window */
464 if (XGetWindowProperty(dpy, window, net_wm_icon, 0L, LONG_MAX,
465 False, XA_CARDINAL, &type, &format, &items, &rest,
466 (unsigned char **)&property) != Success || !property)
467 return NULL;
469 if (type != XA_CARDINAL || format != 32 || items < 2) {
470 XFree(property);
471 return NULL;
474 /* Find the best icon */
475 data = findBestIcon(property, items);
476 if (!data) {
477 XFree(property);
478 return NULL;
481 /* Save the best icon in the X11 icon */
482 image = makeRImageFromARGBData(data);
484 XFree(property);
486 /* Resize the image to the correct value */
487 image = wIconValidateIconSize(image, wPreferences.icon_size);
489 return image;
492 static void updateIconImage(WWindow *wwin)
494 /* Remove the icon image from X11 */
495 if (wwin->net_icon_image)
496 RReleaseImage(wwin->net_icon_image);
498 /* Save the icon in the X11 icon */
499 wwin->net_icon_image = get_window_image_from_x11(wwin->client_win);
501 /* Refresh the Window Icon */
502 if (wwin->icon)
503 wIconUpdate(wwin->icon);
505 /* Refresh the application icon */
506 WApplication *app = wApplicationOf(wwin->main_window);
507 if (app && app->app_icon) {
508 wIconUpdate(app->app_icon->icon);
509 wAppIconPaint(app->app_icon);
513 static void updateWindowOpacity(WWindow *wwin)
515 Atom type;
516 int format;
517 unsigned long items, rest;
518 unsigned long *property;
520 if (!wwin->frame)
521 return;
523 /* We don't care about this ourselves, but other programs need us to copy
524 * this to the frame window. */
525 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_opacity, 0L, 1L,
526 False, XA_CARDINAL, &type, &format, &items, &rest,
527 (unsigned char **)&property) != Success)
528 return;
530 if (type == None) {
531 XDeleteProperty(dpy, wwin->frame->core->window, net_wm_window_opacity);
532 } else if (type == XA_CARDINAL && format == 32 && items == 1 && property) {
533 XChangeProperty(dpy, wwin->frame->core->window, net_wm_window_opacity,
534 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)property, 1L);
537 if (property)
538 XFree(property);
541 static void updateShowDesktop(WScreen *scr, Bool show)
543 long foo;
545 foo = (show == True);
546 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
547 PropModeReplace, (unsigned char *)&foo, 1);
550 static void wNETWMShowingDesktop(WScreen *scr, Bool show)
552 if (show && scr->netdata->show_desktop == NULL) {
553 WWindow *tmp, **wins;
554 int i = 0;
556 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
558 tmp = scr->focused_window;
559 while (tmp) {
560 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
562 wins[i++] = tmp;
563 tmp->flags.skip_next_animation = 1;
564 tmp->flags.net_show_desktop = 1;
565 wIconifyWindow(tmp);
568 tmp = tmp->prev;
570 wins[i++] = NULL;
572 scr->netdata->show_desktop = wins;
573 updateShowDesktop(scr, True);
574 } else if (scr->netdata->show_desktop != NULL) {
575 /* FIXME: get rid of workspace flashing ! */
576 int ws = scr->current_workspace;
577 WWindow **tmp;
578 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
579 wDeiconifyWindow(*tmp);
580 (*tmp)->flags.net_show_desktop = 0;
582 if (ws != scr->current_workspace)
583 wWorkspaceChange(scr, ws);
584 wfree(scr->netdata->show_desktop);
585 scr->netdata->show_desktop = NULL;
586 updateShowDesktop(scr, False);
590 void wNETWMInitStuff(WScreen *scr)
592 NetData *data;
593 int i;
595 #ifdef DEBUG_WMSPEC
596 wmessage("wNETWMInitStuff");
597 #endif
599 #ifdef HAVE_XINTERNATOMS
601 Atom atoms[wlengthof(atomNames)];
602 char *names[wlengthof(atomNames)];
604 for (i = 0; i < wlengthof(atomNames); ++i)
605 names[i] = atomNames[i].name;
607 XInternAtoms(dpy, &names[0], wlengthof(atomNames), False, atoms);
608 for (i = 0; i < wlengthof(atomNames); ++i)
609 *atomNames[i].atom = atoms[i];
612 #else
613 for (i = 0; i < wlengthof(atomNames); i++)
614 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
615 #endif
617 data = wmalloc(sizeof(NetData));
618 data->scr = scr;
619 data->strut = NULL;
620 data->show_desktop = NULL;
622 scr->netdata = data;
624 setSupportedHints(scr);
626 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
627 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
628 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
629 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
630 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
631 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
632 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
634 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
635 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
636 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
637 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
639 updateClientList(scr);
640 updateClientListStacking(scr, NULL);
641 updateWorkspaceCount(scr);
642 updateWorkspaceNames(scr);
643 updateShowDesktop(scr, False);
645 wScreenUpdateUsableArea(scr);
648 void wNETWMCleanup(WScreen *scr)
650 int i;
652 for (i = 0; i < wlengthof(atomNames); i++)
653 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
656 void wNETWMUpdateActions(WWindow *wwin, Bool del)
658 Atom action[10]; /* nr of actions atoms defined */
659 int i = 0;
661 if (del) {
662 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
663 return;
666 if (IS_MOVABLE(wwin))
667 action[i++] = net_wm_action_move;
669 if (IS_RESIZABLE(wwin))
670 action[i++] = net_wm_action_resize;
672 if (!WFLAGP(wwin, no_miniaturizable))
673 action[i++] = net_wm_action_minimize;
675 if (!WFLAGP(wwin, no_shadeable))
676 action[i++] = net_wm_action_shade;
678 /* if (!WFLAGP(wwin, no_stickable)) */
679 action[i++] = net_wm_action_stick;
681 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
682 if (IS_RESIZABLE(wwin))
683 action[i++] = net_wm_action_maximize_horz;
685 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
686 if (IS_RESIZABLE(wwin))
687 action[i++] = net_wm_action_maximize_vert;
689 /* if (!WFLAGP(wwin, no_fullscreen)) */
690 action[i++] = net_wm_action_fullscreen;
692 /* if (!WFLAGP(wwin, no_change_desktop)) */
693 action[i++] = net_wm_action_change_desktop;
695 if (!WFLAGP(wwin, no_closable))
696 action[i++] = net_wm_action_close;
698 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
699 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
702 void wNETWMUpdateWorkarea(WScreen *scr)
704 WArea total_usable;
705 int nb_workspace;
707 if (!scr->netdata) {
708 /* If the _NET_xxx were not initialised, it not necessary to do anything */
709 return;
712 if (!scr->usableArea) {
713 /* If we don't have any info, we fall back on using the complete screen area */
714 total_usable.x1 = 0;
715 total_usable.y1 = 0;
716 total_usable.x2 = scr->scr_width;
717 total_usable.y2 = scr->scr_height;
719 } else {
720 int i;
723 * the _NET_WORKAREA is supposed to contain the total area of the screen that
724 * is usable, so we merge the areas from all xrandr sub-screens
726 total_usable = scr->usableArea[0];
728 for (i = 1; i < wXineramaHeads(scr); i++) {
729 /* The merge is not subtle because _NET_WORKAREA does not need more */
730 if (scr->usableArea[i].x1 < total_usable.x1)
731 total_usable.x1 = scr->usableArea[i].x1;
733 if (scr->usableArea[i].y1 < total_usable.y1)
734 total_usable.y1 = scr->usableArea[i].y1;
736 if (scr->usableArea[i].x2 > total_usable.x2)
737 total_usable.x2 = scr->usableArea[i].x2;
739 if (scr->usableArea[i].y2 > total_usable.y2)
740 total_usable.y2 = scr->usableArea[i].y2;
745 /* We are expected to repeat the information for each workspace */
746 if (scr->workspace_count == 0)
747 nb_workspace = 1;
748 else
749 nb_workspace = scr->workspace_count;
752 long property_value[nb_workspace * 4];
753 int i;
755 for (i = 0; i < nb_workspace; i++) {
756 property_value[4 * i + 0] = total_usable.x1;
757 property_value[4 * i + 1] = total_usable.y1;
758 property_value[4 * i + 2] = total_usable.x2 - total_usable.x1;
759 property_value[4 * i + 3] = total_usable.y2 - total_usable.y1;
762 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32, PropModeReplace,
763 (unsigned char *) property_value, nb_workspace * 4);
767 Bool wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
769 WReservedArea *cur;
770 WMRect rect;
772 if (!scr->netdata || !scr->netdata->strut)
773 return False;
775 area->x1 = area->y1 = area->x2 = area->y2 = 0;
777 for (cur = scr->netdata->strut; cur; cur = cur->next) {
778 WWindow *wwin = wWindowFor(cur->window);
779 if (wWindowTouchesHead(wwin, head)) {
780 if (cur->area.x1 > area->x1)
781 area->x1 = cur->area.x1;
782 if (cur->area.y1 > area->y1)
783 area->y1 = cur->area.y1;
784 if (cur->area.x2 > area->x2)
785 area->x2 = cur->area.x2;
786 if (cur->area.y2 > area->y2)
787 area->y2 = cur->area.y2;
791 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
792 return False;
794 rect = wGetRectForHead(scr, head);
796 area->x1 = rect.pos.x + area->x1;
797 area->x2 = rect.pos.x + rect.size.width - area->x2;
798 area->y1 = rect.pos.y + area->y1;
799 area->y2 = rect.pos.y + rect.size.height - area->y2;
801 return True;
804 static void updateClientList(WScreen *scr)
806 WWindow *wwin;
807 Window *windows;
808 int count;
810 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
812 count = 0;
813 wwin = scr->focused_window;
814 while (wwin) {
815 windows[count++] = wwin->client_win;
816 wwin = wwin->prev;
818 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
819 PropModeReplace, (unsigned char *)windows, count);
821 wfree(windows);
822 XFlush(dpy);
825 static void updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
827 WWindow *wwin;
828 Window *client_list, *client_list_reverse;
829 int client_count, i;
830 WCoreWindow *tmp;
831 WMBagIterator iter;
833 /* update client list */
834 i = scr->window_count + 1;
835 client_list = (Window *) wmalloc(sizeof(Window) * i);
836 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
838 client_count = 0;
839 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
840 while (tmp) {
841 wwin = wWindowFor(tmp->window);
842 /* wwin_excl is a window to exclude from the list
843 (e.g. it's now unmanaged) */
844 if (wwin && (wwin != wwin_excl))
845 client_list[client_count++] = wwin->client_win;
846 tmp = tmp->stacking->under;
850 for (i = 0; i < client_count; i++) {
851 Window w = client_list[client_count - i - 1];
852 client_list_reverse[i] = w;
855 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
856 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
858 wfree(client_list);
859 wfree(client_list_reverse);
861 XFlush(dpy);
864 static void updateWorkspaceCount(WScreen *scr)
865 { /* changeable */
866 long count;
868 count = scr->workspace_count;
870 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
871 32, PropModeReplace, (unsigned char *)&count, 1);
874 static void updateCurrentWorkspace(WScreen *scr)
875 { /* changeable */
876 long count;
878 count = scr->current_workspace;
880 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
881 PropModeReplace, (unsigned char *)&count, 1);
884 static void updateWorkspaceNames(WScreen *scr)
886 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
887 unsigned int i, len, curr_size;
889 pos = buf;
890 len = 0;
891 for (i = 0; i < scr->workspace_count; i++) {
892 curr_size = strlen(scr->workspaces[i]->name);
893 strcpy(pos, scr->workspaces[i]->name);
894 pos += (curr_size + 1);
895 len += (curr_size + 1);
898 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
899 PropModeReplace, (unsigned char *)buf, len);
902 static void updateFocusHint(WScreen *scr)
903 { /* changeable */
904 Window window;
906 if (!scr->focused_window || !scr->focused_window->flags.focused)
907 window = None;
908 else
909 window = scr->focused_window->client_win;
911 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
912 PropModeReplace, (unsigned char *)&window, 1);
915 static void updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
917 long l;
919 if (del) {
920 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
921 } else {
922 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
923 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
924 32, PropModeReplace, (unsigned char *)&l, 1);
928 static void updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del)
929 { /* changeable */
930 if (del) {
931 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
932 } else {
933 Atom state[15]; /* nr of defined state atoms */
934 int i = 0;
936 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
937 updateWorkspaceHint(wwin, False, False);
939 if (IS_OMNIPRESENT(wwin))
940 state[i++] = net_wm_state_sticky;
941 if (wwin->flags.shaded)
942 state[i++] = net_wm_state_shaded;
943 if (wwin->flags.maximized & MAX_HORIZONTAL)
944 state[i++] = net_wm_state_maximized_horz;
945 if (wwin->flags.maximized & MAX_VERTICAL)
946 state[i++] = net_wm_state_maximized_vert;
947 if (WFLAGP(wwin, skip_window_list))
948 state[i++] = net_wm_state_skip_taskbar;
949 if (wwin->flags.net_skip_pager)
950 state[i++] = net_wm_state_skip_pager;
951 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
952 state[i++] = net_wm_state_hidden;
953 state[i++] = net_wm_state_skip_pager;
955 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
956 if (!IS_OMNIPRESENT(wwin))
957 updateWorkspaceHint(wwin, True, False);
958 state[i++] = net_wm_state_sticky;
961 if (WFLAGP(wwin, sunken))
962 state[i++] = net_wm_state_below;
963 if (WFLAGP(wwin, floating))
964 state[i++] = net_wm_state_above;
965 if (wwin->flags.fullscreen)
966 state[i++] = net_wm_state_fullscreen;
968 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
969 PropModeReplace, (unsigned char *)state, i);
973 static Bool updateStrut(WScreen *scr, Window w, Bool adding)
975 WReservedArea *area;
976 Bool hasState = False;
978 if (adding) {
979 Atom type_ret;
980 int fmt_ret;
981 unsigned long nitems_ret, bytes_after_ret;
982 long *data = NULL;
984 if ((XGetWindowProperty(dpy, w, net_wm_strut, 0, 4, False,
985 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
986 &bytes_after_ret, (unsigned char **)&data) == Success && data) ||
987 ((XGetWindowProperty(dpy, w, net_wm_strut_partial, 0, 12, False,
988 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
989 &bytes_after_ret, (unsigned char **)&data) == Success && data))) {
991 /* XXX: This is strictly incorrect in the case of net_wm_strut_partial...
992 * Discard the start and end properties from the partial strut and treat it as
993 * a (deprecated) strut.
994 * This means we are marking the whole width or height of the screen as
995 * reserved, which is not necessarily what the strut defines. However for the
996 * purposes of determining placement or maximization it's probably good enough.
998 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
999 area->area.x1 = data[0];
1000 area->area.x2 = data[1];
1001 area->area.y1 = data[2];
1002 area->area.y2 = data[3];
1004 area->window = w;
1006 area->next = scr->netdata->strut;
1007 scr->netdata->strut = area;
1009 XFree(data);
1010 hasState = True;
1012 } else {
1013 /* deleting */
1014 area = scr->netdata->strut;
1016 if (area) {
1017 if (area->window == w) {
1018 scr->netdata->strut = area->next;
1019 wfree(area);
1020 hasState = True;
1021 } else {
1022 while (area->next && area->next->window != w)
1023 area = area->next;
1025 if (area->next) {
1026 WReservedArea *next;
1028 next = area->next->next;
1029 wfree(area->next);
1030 area->next = next;
1032 hasState = True;
1038 return hasState;
1041 static int getWindowLayer(WWindow *wwin)
1043 int layer = WMNormalLevel;
1045 if (wwin->type == net_wm_window_type_desktop) {
1046 layer = WMDesktopLevel;
1047 } else if (wwin->type == net_wm_window_type_dock) {
1048 layer = WMDockLevel;
1049 } else if (wwin->type == net_wm_window_type_toolbar) {
1050 layer = WMMainMenuLevel;
1051 } else if (wwin->type == net_wm_window_type_menu) {
1052 layer = WMSubmenuLevel;
1053 } else if (wwin->type == net_wm_window_type_utility) {
1054 } else if (wwin->type == net_wm_window_type_splash) {
1055 } else if (wwin->type == net_wm_window_type_dialog) {
1056 if (wwin->transient_for) {
1057 WWindow *parent = wWindowFor(wwin->transient_for);
1058 if (parent && parent->flags.fullscreen)
1059 layer = WMFullscreenLevel;
1061 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
1062 } else if (wwin->type == net_wm_window_type_dropdown_menu) {
1063 layer = WMSubmenuLevel;
1064 } else if (wwin->type == net_wm_window_type_popup_menu) {
1065 layer = WMSubmenuLevel;
1066 } else if (wwin->type == net_wm_window_type_tooltip) {
1067 } else if (wwin->type == net_wm_window_type_notification) {
1068 layer = WMPopUpLevel;
1069 } else if (wwin->type == net_wm_window_type_combo) {
1070 layer = WMSubmenuLevel;
1071 } else if (wwin->type == net_wm_window_type_dnd) {
1072 } else if (wwin->type == net_wm_window_type_normal) {
1075 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
1076 layer = WMSunkenLevel;
1077 if (wwin->client_flags.floating && WMFloatingLevel > layer)
1078 layer = WMFloatingLevel;
1080 return layer;
1083 static void doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
1085 if (state == net_wm_state_sticky) {
1086 if (set == _NET_WM_STATE_TOGGLE)
1087 set = !IS_OMNIPRESENT(wwin);
1089 if (set != wwin->flags.omnipresent)
1090 wWindowSetOmnipresent(wwin, set);
1092 } else if (state == net_wm_state_shaded) {
1093 if (set == _NET_WM_STATE_TOGGLE)
1094 set = !wwin->flags.shaded;
1096 if (init) {
1097 wwin->flags.shaded = set;
1098 } else {
1099 if (set)
1100 wShadeWindow(wwin);
1101 else
1102 wUnshadeWindow(wwin);
1104 } else if (state == net_wm_state_skip_taskbar) {
1105 if (set == _NET_WM_STATE_TOGGLE)
1106 set = !wwin->client_flags.skip_window_list;
1108 wwin->client_flags.skip_window_list = set;
1109 } else if (state == net_wm_state_skip_pager) {
1110 if (set == _NET_WM_STATE_TOGGLE)
1111 set = !wwin->flags.net_skip_pager;
1113 wwin->flags.net_skip_pager = set;
1114 } else if (state == net_wm_state_maximized_vert) {
1115 if (set == _NET_WM_STATE_TOGGLE)
1116 set = !(wwin->flags.maximized & MAX_VERTICAL);
1118 if (init) {
1119 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
1120 } else {
1121 if (set)
1122 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL,
1123 wGetHeadForWindow(wwin));
1124 else
1125 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL,
1126 wGetHeadForWindow(wwin));
1128 } else if (state == net_wm_state_maximized_horz) {
1129 if (set == _NET_WM_STATE_TOGGLE)
1130 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1132 if (init) {
1133 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1134 } else {
1135 if (set)
1136 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL,
1137 wGetHeadForWindow(wwin));
1138 else
1139 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL,
1140 wGetHeadForWindow(wwin));
1142 } else if (state == net_wm_state_hidden) {
1143 if (set == _NET_WM_STATE_TOGGLE)
1144 set = !(wwin->flags.miniaturized);
1146 if (init) {
1147 wwin->flags.miniaturized = set;
1148 } else {
1149 if (set)
1150 wIconifyWindow(wwin);
1151 else
1152 wDeiconifyWindow(wwin);
1154 } else if (state == net_wm_state_fullscreen) {
1155 if (set == _NET_WM_STATE_TOGGLE)
1156 set = !(wwin->flags.fullscreen);
1158 if (init) {
1159 wwin->flags.fullscreen = set;
1160 } else {
1161 if (set)
1162 wFullscreenWindow(wwin);
1163 else
1164 wUnfullscreenWindow(wwin);
1166 } else if (state == net_wm_state_above) {
1167 if (set == _NET_WM_STATE_TOGGLE)
1168 set = !(wwin->client_flags.floating);
1170 if (init) {
1171 wwin->client_flags.floating = set;
1172 } else {
1173 wwin->client_flags.floating = set;
1174 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1177 } else if (state == net_wm_state_below) {
1178 if (set == _NET_WM_STATE_TOGGLE)
1179 set = !(wwin->client_flags.sunken);
1181 if (init) {
1182 wwin->client_flags.sunken = set;
1183 } else {
1184 wwin->client_flags.sunken = set;
1185 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1188 } else {
1189 #ifdef DEBUG_WMSPEC
1190 wmessage("doStateAtom unknown atom %s set %d", XGetAtomName(dpy, state), set);
1191 #endif
1195 static void removeIcon(WWindow *wwin)
1197 if (wwin->icon == NULL)
1198 return;
1199 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1200 XUnmapWindow(dpy, wwin->icon->core->window);
1201 RemoveFromStackList(wwin->icon->core);
1202 wIconDestroy(wwin->icon);
1203 wwin->icon = NULL;
1207 static Bool handleWindowType(WWindow *wwin, Atom type, int *layer)
1209 Bool ret = True;
1211 if (type == net_wm_window_type_desktop) {
1212 wwin->client_flags.no_titlebar = 1;
1213 wwin->client_flags.no_resizable = 1;
1214 wwin->client_flags.no_miniaturizable = 1;
1215 wwin->client_flags.no_border = 1;
1216 wwin->client_flags.no_resizebar = 1;
1217 wwin->client_flags.no_shadeable = 1;
1218 wwin->client_flags.no_movable = 1;
1219 wwin->client_flags.omnipresent = 1;
1220 wwin->client_flags.skip_window_list = 1;
1221 wwin->client_flags.skip_switchpanel = 1;
1222 wwin->client_flags.dont_move_off = 1;
1223 wwin->client_flags.no_appicon = 1;
1224 wwin->flags.net_skip_pager = 1;
1225 wwin->frame_x = 0;
1226 wwin->frame_y = 0;
1227 } else if (type == net_wm_window_type_dock) {
1228 wwin->client_flags.no_titlebar = 1;
1229 wwin->client_flags.no_resizable = 1;
1230 wwin->client_flags.no_miniaturizable = 1;
1231 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1232 wwin->client_flags.no_resizebar = 1;
1233 wwin->client_flags.no_shadeable = 1;
1234 wwin->client_flags.no_movable = 1;
1235 wwin->client_flags.omnipresent = 1;
1236 wwin->client_flags.skip_window_list = 1;
1237 wwin->client_flags.skip_switchpanel = 1;
1238 wwin->client_flags.dont_move_off = 1;
1239 wwin->flags.net_skip_pager = 1;
1240 } else if (type == net_wm_window_type_toolbar ||
1241 type == net_wm_window_type_menu) {
1242 wwin->client_flags.no_resizable = 1;
1243 wwin->client_flags.no_miniaturizable = 1;
1244 wwin->client_flags.no_resizebar = 1;
1245 wwin->client_flags.no_shadeable = 1;
1246 wwin->client_flags.skip_window_list = 1;
1247 wwin->client_flags.skip_switchpanel = 1;
1248 wwin->client_flags.dont_move_off = 1;
1249 wwin->client_flags.no_appicon = 1;
1250 } else if (type == net_wm_window_type_dropdown_menu ||
1251 type == net_wm_window_type_popup_menu ||
1252 type == net_wm_window_type_combo) {
1253 wwin->client_flags.no_titlebar = 1;
1254 wwin->client_flags.no_resizable = 1;
1255 wwin->client_flags.no_miniaturizable = 1;
1256 wwin->client_flags.no_resizebar = 1;
1257 wwin->client_flags.no_shadeable = 1;
1258 wwin->client_flags.skip_window_list = 1;
1259 wwin->client_flags.skip_switchpanel = 1;
1260 wwin->client_flags.dont_move_off = 1;
1261 wwin->client_flags.no_appicon = 1;
1262 } else if (type == net_wm_window_type_utility) {
1263 wwin->client_flags.no_appicon = 1;
1264 } else if (type == net_wm_window_type_splash) {
1265 wwin->client_flags.no_titlebar = 1;
1266 wwin->client_flags.no_resizable = 1;
1267 wwin->client_flags.no_miniaturizable = 1;
1268 wwin->client_flags.no_resizebar = 1;
1269 wwin->client_flags.no_shadeable = 1;
1270 wwin->client_flags.no_movable = 1;
1271 wwin->client_flags.skip_window_list = 1;
1272 wwin->client_flags.skip_switchpanel = 1;
1273 wwin->client_flags.dont_move_off = 1;
1274 wwin->client_flags.no_appicon = 1;
1275 wwin->flags.net_skip_pager = 1;
1276 } else if (type == net_wm_window_type_dialog) {
1277 /* These also seem a bad idea in our context -Dan
1278 // wwin->client_flags.skip_window_list = 1;
1279 // wwin->client_flags.no_appicon = 1;
1281 } else if (wwin->type == net_wm_window_type_tooltip) {
1282 wwin->client_flags.no_titlebar = 1;
1283 wwin->client_flags.no_resizable = 1;
1284 wwin->client_flags.no_miniaturizable = 1;
1285 wwin->client_flags.no_resizebar = 1;
1286 wwin->client_flags.no_shadeable = 1;
1287 wwin->client_flags.no_movable = 1;
1288 wwin->client_flags.skip_window_list = 1;
1289 wwin->client_flags.skip_switchpanel = 1;
1290 wwin->client_flags.dont_move_off = 1;
1291 wwin->client_flags.no_appicon = 1;
1292 wwin->client_flags.no_focusable = 1;
1293 wwin->flags.net_skip_pager = 1;
1294 } else if (wwin->type == net_wm_window_type_notification) {
1295 wwin->client_flags.no_titlebar = 1;
1296 wwin->client_flags.no_resizable = 1;
1297 wwin->client_flags.no_miniaturizable = 1;
1298 wwin->client_flags.no_border = 1;
1299 wwin->client_flags.no_resizebar = 1;
1300 wwin->client_flags.no_shadeable = 1;
1301 wwin->client_flags.no_movable = 1;
1302 wwin->client_flags.omnipresent = 1;
1303 wwin->client_flags.skip_window_list = 1;
1304 wwin->client_flags.skip_switchpanel = 1;
1305 wwin->client_flags.dont_move_off = 1;
1306 wwin->client_flags.no_hide_others= 1;
1307 wwin->client_flags.no_appicon = 1;
1308 wwin->client_flags.no_focusable = 1;
1309 wwin->flags.net_skip_pager = 1;
1310 } else if (wwin->type == net_wm_window_type_dnd) {
1311 wwin->client_flags.no_titlebar = 1;
1312 wwin->client_flags.no_resizable = 1;
1313 wwin->client_flags.no_miniaturizable = 1;
1314 wwin->client_flags.no_border = 1;
1315 wwin->client_flags.no_resizebar = 1;
1316 wwin->client_flags.no_shadeable = 1;
1317 wwin->client_flags.no_movable = 1;
1318 wwin->client_flags.skip_window_list = 1;
1319 wwin->client_flags.skip_switchpanel = 1;
1320 wwin->client_flags.dont_move_off = 1;
1321 wwin->client_flags.no_appicon = 1;
1322 wwin->flags.net_skip_pager = 1;
1323 } else if (type == net_wm_window_type_normal) {
1324 } else {
1325 ret = False;
1328 /* Restore decoration if the user has enabled the
1329 * IgnoreDecorationChanges option */
1330 if (WFLAGP(wwin, ignore_decoration_changes)) {
1331 wwin->client_flags.no_titlebar = 0;
1332 wwin->client_flags.no_resizable = 0;
1333 wwin->client_flags.no_miniaturizable = 0;
1334 wwin->client_flags.no_resizebar = 0;
1335 wwin->client_flags.no_border = 0;
1336 wwin->client_flags.no_movable = 0;
1339 wwin->type = type;
1340 *layer = getWindowLayer(wwin);
1342 return ret;
1345 void wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1347 if (wwin->type == net_wm_window_type_splash) {
1348 WScreen *scr = wwin->screen_ptr;
1349 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1350 *x = rect.pos.x + (rect.size.width - width) / 2;
1351 *y = rect.pos.y + (rect.size.height - height) / 2;
1355 static void updateWindowType(WWindow *wwin)
1357 Atom type_ret;
1358 int fmt_ret, layer;
1359 unsigned long nitems_ret, bytes_after_ret;
1360 long *data = NULL;
1362 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1363 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1364 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1366 int i;
1367 Atom *type = (Atom *) data;
1368 for (i = 0; i < nitems_ret; ++i) {
1369 if (handleWindowType(wwin, type[i], &layer))
1370 break;
1372 XFree(data);
1375 if (wwin->frame != NULL) {
1376 ChangeStackingLevel(wwin->frame->core, layer);
1377 wwin->frame->flags.need_texture_change = 1;
1378 wWindowConfigureBorders(wwin);
1379 wFrameWindowPaint(wwin->frame);
1380 wNETWMUpdateActions(wwin, False);
1384 void wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1386 Atom type_ret;
1387 int fmt_ret, i;
1388 unsigned long nitems_ret, bytes_after_ret;
1389 long *data = NULL;
1391 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1392 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1393 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1395 long desktop = *data;
1396 XFree(data);
1398 if (desktop == -1)
1399 wwin->client_flags.omnipresent = 1;
1400 else
1401 *workspace = desktop;
1404 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1405 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1406 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1408 Atom *state = (Atom *) data;
1409 for (i = 0; i < nitems_ret; ++i)
1410 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1412 XFree(data);
1415 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1416 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1417 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1419 Atom *type = (Atom *) data;
1420 for (i = 0; i < nitems_ret; ++i) {
1421 if (handleWindowType(wwin, type[i], layer))
1422 break;
1424 XFree(data);
1427 wNETWMUpdateActions(wwin, False);
1428 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1429 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1431 wScreenUpdateUsableArea(wwin->screen_ptr);
1434 static Bool updateNetIconInfo(WWindow *wwin)
1436 Atom type_ret;
1437 int fmt_ret;
1438 unsigned long nitems_ret, bytes_after_ret;
1439 long *data = NULL;
1440 Bool hasState = False;
1441 Bool old_state = wwin->flags.net_handle_icon;
1443 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1444 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1445 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1446 long handled = *data;
1447 wwin->flags.net_handle_icon = (handled != 0);
1448 XFree(data);
1449 hasState = True;
1451 } else {
1452 wwin->flags.net_handle_icon = False;
1455 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1456 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1457 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1459 #ifdef NETWM_PROPER
1460 if (wwin->flags.net_handle_icon)
1461 #else
1462 wwin->flags.net_handle_icon = True;
1463 #endif
1465 wwin->icon_x = data[0];
1466 wwin->icon_y = data[1];
1467 wwin->icon_w = data[2];
1468 wwin->icon_h = data[3];
1471 XFree(data);
1472 hasState = True;
1474 } else {
1475 wwin->flags.net_handle_icon = False;
1478 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1479 if (wwin->flags.net_handle_icon) {
1480 removeIcon(wwin);
1481 } else {
1482 wwin->flags.miniaturized = False;
1483 wwin->flags.skip_next_animation = True;
1484 wIconifyWindow(wwin);
1488 return hasState;
1491 void wNETWMCheckInitialClientState(WWindow *wwin)
1493 #ifdef DEBUG_WMSPEC
1494 wmessage("wNETWMCheckInitialClientState");
1495 #endif
1497 wNETWMShowingDesktop(wwin->screen_ptr, False);
1499 updateWindowType(wwin);
1500 updateNetIconInfo(wwin);
1501 updateIconImage(wwin);
1504 void wNETWMCheckInitialFrameState(WWindow *wwin)
1506 #ifdef DEBUG_WMSPEC
1507 wmessage("wNETWMCheckInitialFrameState");
1508 #endif
1510 updateWindowOpacity(wwin);
1513 static void handleDesktopNames(WScreen *scr)
1515 unsigned long nitems_ret, bytes_after_ret;
1516 char *data, *names[32];
1517 int fmt_ret, i, n;
1518 Atom type_ret;
1520 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1521 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1522 &bytes_after_ret, (unsigned char **)&data) != Success)
1523 return;
1525 if (data == NULL)
1526 return;
1528 if (type_ret != utf8_string || fmt_ret != 8)
1529 return;
1531 n = 0;
1532 names[n] = data;
1533 for (i = 0; i < nitems_ret; i++) {
1534 if (data[i] == 0) {
1535 n++;
1536 names[n] = &data[i];
1537 } else if (*names[n] == 0) {
1538 names[n] = &data[i];
1539 wWorkspaceRename(scr, n, names[n]);
1544 Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
1546 WScreen *scr;
1547 WWindow *wwin;
1549 #ifdef DEBUG_WMSPEC
1550 wmessage("processClientMessage type %s", XGetAtomName(dpy, event->message_type));
1551 #endif
1553 scr = wScreenForWindow(event->window);
1554 if (scr) {
1555 /* generic client messages */
1556 if (event->message_type == net_current_desktop) {
1557 wWorkspaceChange(scr, event->data.l[0]);
1558 return True;
1560 } else if (event->message_type == net_number_of_desktops) {
1561 long value;
1563 value = event->data.l[0];
1564 if (value > scr->workspace_count) {
1565 wWorkspaceMake(scr, value - scr->workspace_count);
1566 } else if (value < scr->workspace_count) {
1567 int i;
1568 Bool rebuild = False;
1570 for (i = scr->workspace_count - 1; i >= value; i--) {
1571 if (!wWorkspaceDelete(scr, i)) {
1572 rebuild = True;
1573 break;
1577 if (rebuild)
1578 updateWorkspaceCount(scr);
1580 return True;
1582 } else if (event->message_type == net_showing_desktop) {
1583 wNETWMShowingDesktop(scr, event->data.l[0]);
1584 return True;
1586 } else if (event->message_type == net_desktop_names) {
1587 handleDesktopNames(scr);
1588 return True;
1592 /* window specific client messages */
1594 wwin = wWindowFor(event->window);
1595 if (!wwin)
1596 return False;
1598 if (event->message_type == net_active_window) {
1600 * Satisfy a client's focus request only if
1601 * - request comes from a pager, or
1602 * - it's explicitly allowed in Advanced Options, or
1603 * - giving the client the focus does not cause a change in
1604 * the active workspace (XXX: or the active head if Xinerama)
1606 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace /* No workspace change */
1607 || event->data.l[0] == 2 /* Requested by pager */
1608 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1609 wNETWMShowingDesktop(scr, False);
1610 wMakeWindowVisible(wwin);
1612 return True;
1614 } else if (event->message_type == net_close_window) {
1615 if (!WFLAGP(wwin, no_closable)) {
1616 if (wwin->protocols.DELETE_WINDOW)
1617 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
1618 w_global.timestamp.last_event);
1620 return True;
1622 } else if (event->message_type == net_wm_state) {
1623 int maximized = wwin->flags.maximized;
1624 long set = event->data.l[0];
1626 #ifdef DEBUG_WMSPEC
1627 wmessage("net_wm_state set %ld a1 %s a2 %s", set,
1628 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1629 #endif
1631 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1632 if (event->data.l[2])
1633 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1635 if (wwin->flags.maximized != maximized) {
1636 if (!wwin->flags.maximized) {
1637 wwin->flags.maximized = maximized;
1638 wUnmaximizeWindow(wwin);
1639 } else {
1640 wMaximizeWindow(wwin, wwin->flags.maximized,
1641 wGetHeadForWindow(wwin));
1644 updateStateHint(wwin, False, False);
1645 return True;
1647 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1648 updateNetIconInfo(wwin);
1649 return True;
1651 } else if (event->message_type == net_wm_desktop) {
1652 long desktop = event->data.l[0];
1654 if (desktop == -1) {
1655 wWindowSetOmnipresent(wwin, True);
1656 } else {
1657 if (IS_OMNIPRESENT(wwin))
1658 wWindowSetOmnipresent(wwin, False);
1659 wWindowChangeWorkspace(wwin, desktop);
1661 return True;
1664 return False;
1667 void wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1669 #ifdef DEBUG_WMSPEC
1670 wmessage("clientHintChange type %s", XGetAtomName(dpy, event->atom));
1671 #endif
1673 if (event->atom == net_wm_strut || event->atom == net_wm_strut_partial) {
1674 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1675 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1676 wScreenUpdateUsableArea(wwin->screen_ptr);
1677 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1678 updateNetIconInfo(wwin);
1679 } else if (event->atom == net_wm_window_type) {
1680 updateWindowType(wwin);
1681 } else if (event->atom == net_wm_name) {
1682 char *name = wNETWMGetWindowName(wwin->client_win);
1683 wWindowUpdateName(wwin, name);
1684 if (name)
1685 wfree(name);
1686 } else if (event->atom == net_wm_icon_name) {
1687 if (wwin->icon) {
1688 wIconChangeTitle(wwin->icon, wwin);
1689 wIconPaint(wwin->icon);
1691 } else if (event->atom == net_wm_icon) {
1692 updateIconImage(wwin);
1693 } else if (event->atom == net_wm_window_opacity) {
1694 updateWindowOpacity(wwin);
1698 int wNETWMGetPidForWindow(Window window)
1700 Atom type_ret;
1701 int fmt_ret;
1702 unsigned long nitems_ret, bytes_after_ret;
1703 long *data = NULL;
1704 int pid;
1706 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1707 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1708 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1709 pid = *data;
1710 XFree(data);
1711 } else {
1712 pid = 0;
1715 return pid;
1718 char *wNETWMGetWindowName(Window window)
1720 char *name;
1721 char *ret;
1722 int size;
1724 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1725 if (name) {
1726 ret = wstrndup(name, size);
1727 XFree(name);
1728 } else {
1729 ret = NULL;
1732 return ret;
1735 char *wNETWMGetIconName(Window window)
1737 char *name;
1738 char *ret;
1739 int size;
1741 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1742 if (name) {
1743 ret = wstrndup(name, size);
1744 XFree(name);
1745 } else {
1746 ret = NULL;
1749 return ret;
1752 static void observer(void *self, WMNotification *notif)
1754 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1755 const char *name = WMGetNotificationName(notif);
1756 void *data = WMGetNotificationClientData(notif);
1757 NetData *ndata = (NetData *) self;
1759 if (strcmp(name, WMNManaged) == 0 && wwin) {
1760 updateClientList(wwin->screen_ptr);
1761 updateClientListStacking(wwin->screen_ptr, NULL);
1762 updateStateHint(wwin, True, False);
1764 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1765 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1766 wScreenUpdateUsableArea(wwin->screen_ptr);
1767 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1768 updateClientList(wwin->screen_ptr);
1769 updateClientListStacking(wwin->screen_ptr, wwin);
1770 updateWorkspaceHint(wwin, False, True);
1771 updateStateHint(wwin, False, True);
1772 wNETWMUpdateActions(wwin, True);
1774 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1775 wScreenUpdateUsableArea(wwin->screen_ptr);
1776 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1777 updateClientListStacking(wwin->screen_ptr, NULL);
1778 updateStateHint(wwin, False, False);
1779 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1780 updateClientListStacking(wwin->screen_ptr, NULL);
1781 updateStateHint(wwin, False, False);
1782 } else if (strcmp(name, WMNChangedFocus) == 0) {
1783 updateFocusHint(ndata->scr);
1784 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1785 updateWorkspaceHint(wwin, False, False);
1786 updateStateHint(wwin, True, False);
1787 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1788 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1792 static void wsobserver(void *self, WMNotification *notif)
1794 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1795 const char *name = WMGetNotificationName(notif);
1797 /* Parameter not used, but tell the compiler that it is ok */
1798 (void) self;
1800 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1801 updateWorkspaceCount(scr);
1802 updateWorkspaceNames(scr);
1803 wNETWMUpdateWorkarea(scr);
1804 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1805 updateWorkspaceCount(scr);
1806 updateWorkspaceNames(scr);
1807 wNETWMUpdateWorkarea(scr);
1808 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1809 updateCurrentWorkspace(scr);
1810 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1811 updateWorkspaceNames(scr);
1815 void wNETFrameExtents(WWindow *wwin)
1817 long extents[4] = { 0, 0, 0, 0 };
1819 /* The extents array describes dimensions which are not
1820 * part of the client window. In our case that means
1821 * widths of the border and heights of the titlebar and resizebar.
1823 * Index 0 = left
1824 * 1 = right
1825 * 2 = top
1826 * 3 = bottom
1828 if (wwin->frame->titlebar)
1829 extents[2] = wwin->frame->titlebar->height;
1830 if (wwin->frame->resizebar)
1831 extents[3] = wwin->frame->resizebar->height;
1832 if (HAS_BORDER(wwin)) {
1833 extents[0] += wwin->screen_ptr->frame_border_width;
1834 extents[1] += wwin->screen_ptr->frame_border_width;
1835 extents[2] += wwin->screen_ptr->frame_border_width;
1836 extents[3] += wwin->screen_ptr->frame_border_width;
1839 XChangeProperty(dpy, wwin->client_win, net_frame_extents, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) extents, 4);
1842 void wNETCleanupFrameExtents(WWindow *wwin)
1844 XDeleteProperty(dpy, wwin->client_win, net_frame_extents);