wmaker: fix stacking order of dock and fullscreen
[wmaker-crm.git] / src / wmspec.c
blob0bfc8648dce06863d4ecc84d4acd95e8f6ea351e
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_REMOVE 0
217 #define _NET_WM_STATE_ADD 1
218 #define _NET_WM_STATE_TOGGLE 2
220 #if 0
222 * These constant provide information on the kind of window move/resize when
223 * it is initiated by the application instead of by WindowMaker. They are
224 * parameter for the client message _NET_WM_MOVERESIZE, as defined by the
225 * FreeDesktop wm-spec standard:
226 * http://standards.freedesktop.org/wm-spec/1.5/ar01s04.html
228 * Today, WindowMaker does not support this at all (the corresponding Atom
229 * is not added to the list in setSupportedHints), probably because there is
230 * nothing it needs to do about it, the application is assumed to know what
231 * it is doing, and WindowMaker won't get in the way.
233 * The definition of the constants (taken from the standard) are disabled to
234 * avoid a spurious warning (-Wunused-macros).
236 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
237 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
238 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
239 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
240 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
241 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
242 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
243 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
244 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
245 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
246 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
247 #endif
249 static void observer(void *self, WMNotification *notif);
250 static void wsobserver(void *self, WMNotification *notif);
252 static void updateClientList(WScreen *scr);
253 static void updateClientListStacking(WScreen *scr, WWindow *);
255 static void updateWorkspaceNames(WScreen *scr);
256 static void updateCurrentWorkspace(WScreen *scr);
257 static void updateWorkspaceCount(WScreen *scr);
258 static void wNETWMShowingDesktop(WScreen *scr, Bool show);
260 typedef struct NetData {
261 WScreen *scr;
262 WReservedArea *strut;
263 WWindow **show_desktop;
264 } NetData;
266 static void setSupportedHints(WScreen *scr)
268 Atom atom[wlengthof(atomNames)];
269 int i = 0;
271 /* set supported hints list */
272 /* XXX: extend this !!! */
274 atom[i++] = net_client_list;
275 atom[i++] = net_client_list_stacking;
276 atom[i++] = net_number_of_desktops;
277 atom[i++] = net_desktop_geometry;
278 atom[i++] = net_desktop_viewport;
279 atom[i++] = net_current_desktop;
280 atom[i++] = net_desktop_names;
281 atom[i++] = net_active_window;
282 atom[i++] = net_workarea;
283 atom[i++] = net_supporting_wm_check;
284 atom[i++] = net_showing_desktop;
285 #if 0
286 atom[i++] = net_wm_moveresize;
287 #endif
288 atom[i++] = net_wm_desktop;
289 atom[i++] = net_wm_window_type;
290 atom[i++] = net_wm_window_type_desktop;
291 atom[i++] = net_wm_window_type_dock;
292 atom[i++] = net_wm_window_type_toolbar;
293 atom[i++] = net_wm_window_type_menu;
294 atom[i++] = net_wm_window_type_utility;
295 atom[i++] = net_wm_window_type_splash;
296 atom[i++] = net_wm_window_type_dialog;
297 atom[i++] = net_wm_window_type_dropdown_menu;
298 atom[i++] = net_wm_window_type_popup_menu;
299 atom[i++] = net_wm_window_type_tooltip;
300 atom[i++] = net_wm_window_type_notification;
301 atom[i++] = net_wm_window_type_combo;
302 atom[i++] = net_wm_window_type_dnd;
303 atom[i++] = net_wm_window_type_normal;
305 atom[i++] = net_wm_state;
306 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
307 atom[i++] = net_wm_state_sticky;
308 atom[i++] = net_wm_state_shaded;
309 atom[i++] = net_wm_state_maximized_horz;
310 atom[i++] = net_wm_state_maximized_vert;
311 atom[i++] = net_wm_state_skip_taskbar;
312 atom[i++] = net_wm_state_skip_pager;
313 atom[i++] = net_wm_state_hidden;
314 atom[i++] = net_wm_state_fullscreen;
315 atom[i++] = net_wm_state_above;
316 atom[i++] = net_wm_state_below;
318 atom[i++] = net_wm_allowed_actions;
319 atom[i++] = net_wm_action_move;
320 atom[i++] = net_wm_action_resize;
321 atom[i++] = net_wm_action_minimize;
322 atom[i++] = net_wm_action_shade;
323 atom[i++] = net_wm_action_stick;
324 atom[i++] = net_wm_action_maximize_horz;
325 atom[i++] = net_wm_action_maximize_vert;
326 atom[i++] = net_wm_action_fullscreen;
327 atom[i++] = net_wm_action_change_desktop;
328 atom[i++] = net_wm_action_close;
330 atom[i++] = net_wm_strut;
331 atom[i++] = net_wm_icon_geometry;
332 atom[i++] = net_wm_icon;
333 atom[i++] = net_wm_handled_icons;
334 atom[i++] = net_wm_window_opacity;
336 atom[i++] = net_frame_extents;
338 atom[i++] = net_wm_name;
339 atom[i++] = net_wm_icon_name;
341 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
343 /* set supporting wm hint */
344 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
345 PropModeReplace, (unsigned char *)&scr->info_window, 1);
347 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
348 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
351 void wNETWMUpdateDesktop(WScreen *scr)
353 long *views, sizes[2];
354 int count, i;
356 if (scr->workspace_count == 0)
357 return;
359 count = scr->workspace_count * 2;
360 views = wmalloc(sizeof(long) * count);
361 sizes[0] = scr->scr_width;
362 sizes[1] = scr->scr_height;
364 for (i = 0; i < scr->workspace_count; i++) {
365 views[2 * i + 0] = 0;
366 views[2 * i + 1] = 0;
369 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
370 PropModeReplace, (unsigned char *)sizes, 2);
372 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
373 PropModeReplace, (unsigned char *)views, count);
375 wfree(views);
378 int wNETWMGetCurrentDesktopFromHint(WScreen *scr)
380 int count;
381 unsigned char *prop;
383 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
384 if (prop) {
385 int desktop = *(long *)prop;
386 XFree(prop);
387 return desktop;
389 return -1;
393 * Find the best icon to be used by Window Maker for appicon/miniwindows.
394 * Currently the algorithm is to take the image with the size closest
395 * to icon_size x icon_size, but never bigger than that.
397 * This algorithm is very poorly implemented and needs to be redone (it can
398 * easily select images with very large widths and very small heights over
399 * square images, if the area of the former is closer to the desired one).
401 * The logic can also be changed to accept bigger images and scale them down.
403 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
405 int size, wanted, d;
406 unsigned long i, distance;
407 unsigned long *icon;
409 /* better use only 75% of icon_size. For 64x64 this means 48x48
410 * This leaves room around the icon for the miniwindow title and
411 * results in better overall aesthetics -Dan */
412 wanted = wPreferences.icon_size * wPreferences.icon_size;
414 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
415 size = data[i] * data[i + 1];
416 if (size == 0)
417 break;
418 d = wanted - size;
419 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
420 distance = d;
421 icon = &data[i];
423 i += size + 2;
426 return icon;
429 static RImage *makeRImageFromARGBData(unsigned long *data)
431 int size, width, height, i;
432 RImage *image;
433 unsigned char *imgdata;
434 unsigned long pixel;
436 width = data[0];
437 height = data[1];
438 size = width * height;
440 if (size == 0)
441 return NULL;
443 image = RCreateImage(width, height, True);
445 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
446 pixel = data[i];
447 imgdata[3] = (pixel >> 24) & 0xff; /* A */
448 imgdata[0] = (pixel >> 16) & 0xff; /* R */
449 imgdata[1] = (pixel >> 8) & 0xff; /* G */
450 imgdata[2] = (pixel >> 0) & 0xff; /* B */
453 return image;
456 RImage *get_window_image_from_x11(Window window)
458 RImage *image;
459 Atom type;
460 int format;
461 unsigned long items, rest;
462 unsigned long *property, *data;
464 /* Get the icon from X11 Window */
465 if (XGetWindowProperty(dpy, window, net_wm_icon, 0L, LONG_MAX,
466 False, XA_CARDINAL, &type, &format, &items, &rest,
467 (unsigned char **)&property) != Success || !property)
468 return NULL;
470 if (type != XA_CARDINAL || format != 32 || items < 2) {
471 XFree(property);
472 return NULL;
475 /* Find the best icon */
476 data = findBestIcon(property, items);
477 if (!data) {
478 XFree(property);
479 return NULL;
482 /* Save the best icon in the X11 icon */
483 image = makeRImageFromARGBData(data);
485 XFree(property);
487 /* Resize the image to the correct value */
488 image = wIconValidateIconSize(image, wPreferences.icon_size);
490 return image;
493 static void updateIconImage(WWindow *wwin)
495 /* Remove the icon image from X11 */
496 if (wwin->net_icon_image)
497 RReleaseImage(wwin->net_icon_image);
499 /* Save the icon in the X11 icon */
500 wwin->net_icon_image = get_window_image_from_x11(wwin->client_win);
502 /* Refresh the Window Icon */
503 if (wwin->icon)
504 wIconUpdate(wwin->icon);
506 /* Refresh the application icon */
507 WApplication *app = wApplicationOf(wwin->main_window);
508 if (app && app->app_icon) {
509 wIconUpdate(app->app_icon->icon);
510 wAppIconPaint(app->app_icon);
514 static void updateWindowOpacity(WWindow *wwin)
516 Atom type;
517 int format;
518 unsigned long items, rest;
519 unsigned long *property;
521 if (!wwin->frame)
522 return;
524 /* We don't care about this ourselves, but other programs need us to copy
525 * this to the frame window. */
526 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_opacity, 0L, 1L,
527 False, XA_CARDINAL, &type, &format, &items, &rest,
528 (unsigned char **)&property) != Success)
529 return;
531 if (type == None) {
532 XDeleteProperty(dpy, wwin->frame->core->window, net_wm_window_opacity);
533 } else if (type == XA_CARDINAL && format == 32 && items == 1 && property) {
534 XChangeProperty(dpy, wwin->frame->core->window, net_wm_window_opacity,
535 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)property, 1L);
538 if (property)
539 XFree(property);
542 static void updateShowDesktop(WScreen *scr, Bool show)
544 long foo;
546 foo = (show == True);
547 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
548 PropModeReplace, (unsigned char *)&foo, 1);
551 static void wNETWMShowingDesktop(WScreen *scr, Bool show)
553 if (show && scr->netdata->show_desktop == NULL) {
554 WWindow *tmp, **wins;
555 int i = 0;
557 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
559 tmp = scr->focused_window;
560 while (tmp) {
561 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
563 wins[i++] = tmp;
564 tmp->flags.skip_next_animation = 1;
565 tmp->flags.net_show_desktop = 1;
566 wIconifyWindow(tmp);
569 tmp = tmp->prev;
571 wins[i++] = NULL;
573 scr->netdata->show_desktop = wins;
574 updateShowDesktop(scr, True);
575 } else if (scr->netdata->show_desktop != NULL) {
576 /* FIXME: get rid of workspace flashing ! */
577 int ws = scr->current_workspace;
578 WWindow **tmp;
579 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
580 wDeiconifyWindow(*tmp);
581 (*tmp)->flags.net_show_desktop = 0;
583 if (ws != scr->current_workspace)
584 wWorkspaceChange(scr, ws);
585 wfree(scr->netdata->show_desktop);
586 scr->netdata->show_desktop = NULL;
587 updateShowDesktop(scr, False);
591 void wNETWMInitStuff(WScreen *scr)
593 NetData *data;
594 int i;
596 #ifdef DEBUG_WMSPEC
597 wmessage("wNETWMInitStuff");
598 #endif
600 #ifdef HAVE_XINTERNATOMS
602 Atom atoms[wlengthof(atomNames)];
603 char *names[wlengthof(atomNames)];
605 for (i = 0; i < wlengthof(atomNames); ++i)
606 names[i] = atomNames[i].name;
608 XInternAtoms(dpy, &names[0], wlengthof(atomNames), False, atoms);
609 for (i = 0; i < wlengthof(atomNames); ++i)
610 *atomNames[i].atom = atoms[i];
613 #else
614 for (i = 0; i < wlengthof(atomNames); i++)
615 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
616 #endif
618 data = wmalloc(sizeof(NetData));
619 data->scr = scr;
620 data->strut = NULL;
621 data->show_desktop = NULL;
623 scr->netdata = data;
625 setSupportedHints(scr);
627 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
628 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
629 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
630 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
631 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
632 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
633 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
635 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
636 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
637 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
638 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
640 updateClientList(scr);
641 updateClientListStacking(scr, NULL);
642 updateWorkspaceCount(scr);
643 updateWorkspaceNames(scr);
644 updateShowDesktop(scr, False);
646 wScreenUpdateUsableArea(scr);
649 void wNETWMCleanup(WScreen *scr)
651 int i;
653 for (i = 0; i < wlengthof(atomNames); i++)
654 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
657 void wNETWMUpdateActions(WWindow *wwin, Bool del)
659 Atom action[10]; /* nr of actions atoms defined */
660 int i = 0;
662 if (del) {
663 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
664 return;
667 if (IS_MOVABLE(wwin))
668 action[i++] = net_wm_action_move;
670 if (IS_RESIZABLE(wwin))
671 action[i++] = net_wm_action_resize;
673 if (!WFLAGP(wwin, no_miniaturizable))
674 action[i++] = net_wm_action_minimize;
676 if (!WFLAGP(wwin, no_shadeable))
677 action[i++] = net_wm_action_shade;
679 /* if (!WFLAGP(wwin, no_stickable)) */
680 action[i++] = net_wm_action_stick;
682 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
683 if (IS_RESIZABLE(wwin))
684 action[i++] = net_wm_action_maximize_horz;
686 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
687 if (IS_RESIZABLE(wwin))
688 action[i++] = net_wm_action_maximize_vert;
690 /* if (!WFLAGP(wwin, no_fullscreen)) */
691 action[i++] = net_wm_action_fullscreen;
693 /* if (!WFLAGP(wwin, no_change_desktop)) */
694 action[i++] = net_wm_action_change_desktop;
696 if (!WFLAGP(wwin, no_closable))
697 action[i++] = net_wm_action_close;
699 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
700 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
703 void wNETWMUpdateWorkarea(WScreen *scr)
705 WArea total_usable;
706 int nb_workspace;
708 if (!scr->netdata) {
709 /* If the _NET_xxx were not initialised, it not necessary to do anything */
710 return;
713 if (!scr->usableArea) {
714 /* If we don't have any info, we fall back on using the complete screen area */
715 total_usable.x1 = 0;
716 total_usable.y1 = 0;
717 total_usable.x2 = scr->scr_width;
718 total_usable.y2 = scr->scr_height;
720 } else {
721 int i;
724 * the _NET_WORKAREA is supposed to contain the total area of the screen that
725 * is usable, so we merge the areas from all xrandr sub-screens
727 total_usable = scr->usableArea[0];
729 for (i = 1; i < wXineramaHeads(scr); i++) {
730 /* The merge is not subtle because _NET_WORKAREA does not need more */
731 if (scr->usableArea[i].x1 < total_usable.x1)
732 total_usable.x1 = scr->usableArea[i].x1;
734 if (scr->usableArea[i].y1 < total_usable.y1)
735 total_usable.y1 = scr->usableArea[i].y1;
737 if (scr->usableArea[i].x2 > total_usable.x2)
738 total_usable.x2 = scr->usableArea[i].x2;
740 if (scr->usableArea[i].y2 > total_usable.y2)
741 total_usable.y2 = scr->usableArea[i].y2;
746 /* We are expected to repeat the information for each workspace */
747 if (scr->workspace_count == 0)
748 nb_workspace = 1;
749 else
750 nb_workspace = scr->workspace_count;
753 long property_value[nb_workspace * 4];
754 int i;
756 for (i = 0; i < nb_workspace; i++) {
757 property_value[4 * i + 0] = total_usable.x1;
758 property_value[4 * i + 1] = total_usable.y1;
759 property_value[4 * i + 2] = total_usable.x2 - total_usable.x1;
760 property_value[4 * i + 3] = total_usable.y2 - total_usable.y1;
763 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32, PropModeReplace,
764 (unsigned char *) property_value, nb_workspace * 4);
768 Bool wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
770 WReservedArea *cur;
771 WMRect rect;
773 if (!scr->netdata || !scr->netdata->strut)
774 return False;
776 area->x1 = area->y1 = area->x2 = area->y2 = 0;
778 for (cur = scr->netdata->strut; cur; cur = cur->next) {
779 WWindow *wwin = wWindowFor(cur->window);
780 if (wWindowTouchesHead(wwin, head)) {
781 if (cur->area.x1 > area->x1)
782 area->x1 = cur->area.x1;
783 if (cur->area.y1 > area->y1)
784 area->y1 = cur->area.y1;
785 if (cur->area.x2 > area->x2)
786 area->x2 = cur->area.x2;
787 if (cur->area.y2 > area->y2)
788 area->y2 = cur->area.y2;
792 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
793 return False;
795 rect = wGetRectForHead(scr, head);
797 area->x1 = rect.pos.x + area->x1;
798 area->x2 = rect.pos.x + rect.size.width - area->x2;
799 area->y1 = rect.pos.y + area->y1;
800 area->y2 = rect.pos.y + rect.size.height - area->y2;
802 return True;
805 static void updateClientList(WScreen *scr)
807 WWindow *wwin;
808 Window *windows;
809 int count;
811 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
813 count = 0;
814 wwin = scr->focused_window;
815 while (wwin) {
816 windows[count++] = wwin->client_win;
817 wwin = wwin->prev;
819 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
820 PropModeReplace, (unsigned char *)windows, count);
822 wfree(windows);
823 XFlush(dpy);
826 static void updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
828 WWindow *wwin;
829 Window *client_list, *client_list_reverse;
830 int client_count, i;
831 WCoreWindow *tmp;
832 WMBagIterator iter;
834 /* update client list */
835 i = scr->window_count + 1;
836 client_list = (Window *) wmalloc(sizeof(Window) * i);
837 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
839 client_count = 0;
840 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
841 while (tmp) {
842 wwin = wWindowFor(tmp->window);
843 /* wwin_excl is a window to exclude from the list
844 (e.g. it's now unmanaged) */
845 if (wwin && (wwin != wwin_excl))
846 client_list[client_count++] = wwin->client_win;
847 tmp = tmp->stacking->under;
851 for (i = 0; i < client_count; i++) {
852 Window w = client_list[client_count - i - 1];
853 client_list_reverse[i] = w;
856 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
857 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
859 wfree(client_list);
860 wfree(client_list_reverse);
862 XFlush(dpy);
865 static void updateWorkspaceCount(WScreen *scr)
866 { /* changeable */
867 long count;
869 count = scr->workspace_count;
871 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
872 32, PropModeReplace, (unsigned char *)&count, 1);
875 static void updateCurrentWorkspace(WScreen *scr)
876 { /* changeable */
877 long count;
879 count = scr->current_workspace;
881 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
882 PropModeReplace, (unsigned char *)&count, 1);
885 static void updateWorkspaceNames(WScreen *scr)
887 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
888 unsigned int i, len, curr_size;
890 pos = buf;
891 len = 0;
892 for (i = 0; i < scr->workspace_count; i++) {
893 curr_size = strlen(scr->workspaces[i]->name);
894 strcpy(pos, scr->workspaces[i]->name);
895 pos += (curr_size + 1);
896 len += (curr_size + 1);
899 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
900 PropModeReplace, (unsigned char *)buf, len);
903 static void updateFocusHint(WScreen *scr)
904 { /* changeable */
905 Window window;
907 if (!scr->focused_window || !scr->focused_window->flags.focused)
908 window = None;
909 else
910 window = scr->focused_window->client_win;
912 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
913 PropModeReplace, (unsigned char *)&window, 1);
916 static void updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
918 long l;
920 if (del) {
921 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
922 } else {
923 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
924 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
925 32, PropModeReplace, (unsigned char *)&l, 1);
929 static void updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del)
930 { /* changeable */
931 if (del) {
932 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
933 } else {
934 Atom state[15]; /* nr of defined state atoms */
935 int i = 0;
937 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
938 updateWorkspaceHint(wwin, False, False);
940 if (IS_OMNIPRESENT(wwin))
941 state[i++] = net_wm_state_sticky;
942 if (wwin->flags.shaded)
943 state[i++] = net_wm_state_shaded;
944 if (wwin->flags.maximized & MAX_HORIZONTAL)
945 state[i++] = net_wm_state_maximized_horz;
946 if (wwin->flags.maximized & MAX_VERTICAL)
947 state[i++] = net_wm_state_maximized_vert;
948 if (WFLAGP(wwin, skip_window_list))
949 state[i++] = net_wm_state_skip_taskbar;
950 if (wwin->flags.net_skip_pager)
951 state[i++] = net_wm_state_skip_pager;
952 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
953 state[i++] = net_wm_state_hidden;
954 state[i++] = net_wm_state_skip_pager;
956 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
957 if (!IS_OMNIPRESENT(wwin))
958 updateWorkspaceHint(wwin, True, False);
959 state[i++] = net_wm_state_sticky;
962 if (WFLAGP(wwin, sunken))
963 state[i++] = net_wm_state_below;
964 if (WFLAGP(wwin, floating))
965 state[i++] = net_wm_state_above;
966 if (wwin->flags.fullscreen)
967 state[i++] = net_wm_state_fullscreen;
969 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
970 PropModeReplace, (unsigned char *)state, i);
974 static Bool updateStrut(WScreen *scr, Window w, Bool adding)
976 WReservedArea *area;
977 Bool hasState = False;
979 if (adding) {
980 Atom type_ret;
981 int fmt_ret;
982 unsigned long nitems_ret, bytes_after_ret;
983 long *data = NULL;
985 if ((XGetWindowProperty(dpy, w, net_wm_strut, 0, 4, False,
986 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
987 &bytes_after_ret, (unsigned char **)&data) == Success && data) ||
988 ((XGetWindowProperty(dpy, w, net_wm_strut_partial, 0, 12, False,
989 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
990 &bytes_after_ret, (unsigned char **)&data) == Success && data))) {
992 /* XXX: This is strictly incorrect in the case of net_wm_strut_partial...
993 * Discard the start and end properties from the partial strut and treat it as
994 * a (deprecated) strut.
995 * This means we are marking the whole width or height of the screen as
996 * reserved, which is not necessarily what the strut defines. However for the
997 * purposes of determining placement or maximization it's probably good enough.
999 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
1000 area->area.x1 = data[0];
1001 area->area.x2 = data[1];
1002 area->area.y1 = data[2];
1003 area->area.y2 = data[3];
1005 area->window = w;
1007 area->next = scr->netdata->strut;
1008 scr->netdata->strut = area;
1010 XFree(data);
1011 hasState = True;
1013 } else {
1014 /* deleting */
1015 area = scr->netdata->strut;
1017 if (area) {
1018 if (area->window == w) {
1019 scr->netdata->strut = area->next;
1020 wfree(area);
1021 hasState = True;
1022 } else {
1023 while (area->next && area->next->window != w)
1024 area = area->next;
1026 if (area->next) {
1027 WReservedArea *next;
1029 next = area->next->next;
1030 wfree(area->next);
1031 area->next = next;
1033 hasState = True;
1039 return hasState;
1042 static int getWindowLayer(WWindow *wwin)
1044 int layer = WMNormalLevel;
1046 if (wwin->type == net_wm_window_type_desktop) {
1047 layer = WMDesktopLevel;
1048 } else if (wwin->type == net_wm_window_type_dock) {
1049 layer = WMDockLevel;
1050 } else if (wwin->type == net_wm_window_type_toolbar) {
1051 layer = WMMainMenuLevel;
1052 } else if (wwin->type == net_wm_window_type_menu) {
1053 layer = WMSubmenuLevel;
1054 } else if (wwin->type == net_wm_window_type_utility) {
1055 } else if (wwin->type == net_wm_window_type_splash) {
1056 } else if (wwin->type == net_wm_window_type_dialog) {
1057 if (wwin->transient_for) {
1058 WWindow *parent = wWindowFor(wwin->transient_for);
1059 if (parent && parent->flags.fullscreen)
1060 layer = WMFullscreenLevel;
1062 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
1063 } else if (wwin->type == net_wm_window_type_dropdown_menu) {
1064 layer = WMSubmenuLevel;
1065 } else if (wwin->type == net_wm_window_type_popup_menu) {
1066 layer = WMSubmenuLevel;
1067 } else if (wwin->type == net_wm_window_type_tooltip) {
1068 } else if (wwin->type == net_wm_window_type_notification) {
1069 layer = WMPopUpLevel;
1070 } else if (wwin->type == net_wm_window_type_combo) {
1071 layer = WMSubmenuLevel;
1072 } else if (wwin->type == net_wm_window_type_dnd) {
1073 } else if (wwin->type == net_wm_window_type_normal) {
1076 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
1077 layer = WMSunkenLevel;
1078 if (wwin->client_flags.floating && WMFloatingLevel > layer)
1079 layer = WMFloatingLevel;
1081 return layer;
1084 static void doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
1086 if (state == net_wm_state_sticky) {
1087 if (set == _NET_WM_STATE_TOGGLE)
1088 set = !IS_OMNIPRESENT(wwin);
1090 if (set != wwin->flags.omnipresent)
1091 wWindowSetOmnipresent(wwin, set);
1093 } else if (state == net_wm_state_shaded) {
1094 if (set == _NET_WM_STATE_TOGGLE)
1095 set = !wwin->flags.shaded;
1097 if (init) {
1098 wwin->flags.shaded = set;
1099 } else {
1100 if (set)
1101 wShadeWindow(wwin);
1102 else
1103 wUnshadeWindow(wwin);
1105 } else if (state == net_wm_state_skip_taskbar) {
1106 if (set == _NET_WM_STATE_TOGGLE)
1107 set = !wwin->client_flags.skip_window_list;
1109 wwin->client_flags.skip_window_list = set;
1110 } else if (state == net_wm_state_skip_pager) {
1111 if (set == _NET_WM_STATE_TOGGLE)
1112 set = !wwin->flags.net_skip_pager;
1114 wwin->flags.net_skip_pager = set;
1115 } else if (state == net_wm_state_maximized_vert) {
1116 if (set == _NET_WM_STATE_TOGGLE)
1117 set = !(wwin->flags.maximized & MAX_VERTICAL);
1119 if (init) {
1120 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
1121 } else {
1122 if (set)
1123 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1124 else
1125 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1127 } else if (state == net_wm_state_maximized_horz) {
1128 if (set == _NET_WM_STATE_TOGGLE)
1129 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1131 if (init) {
1132 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1133 } else {
1134 if (set)
1135 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1136 else
1137 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1139 } else if (state == net_wm_state_hidden) {
1140 if (set == _NET_WM_STATE_TOGGLE)
1141 set = !(wwin->flags.miniaturized);
1143 if (init) {
1144 wwin->flags.miniaturized = set;
1145 } else {
1146 if (set)
1147 wIconifyWindow(wwin);
1148 else
1149 wDeiconifyWindow(wwin);
1151 } else if (state == net_wm_state_fullscreen) {
1152 if (set == _NET_WM_STATE_TOGGLE)
1153 set = !(wwin->flags.fullscreen);
1155 if (init) {
1156 wwin->flags.fullscreen = set;
1157 } else {
1158 if (set)
1159 wFullscreenWindow(wwin);
1160 else
1161 wUnfullscreenWindow(wwin);
1163 } else if (state == net_wm_state_above) {
1164 if (set == _NET_WM_STATE_TOGGLE)
1165 set = !(wwin->client_flags.floating);
1167 if (init) {
1168 wwin->client_flags.floating = set;
1169 } else {
1170 wwin->client_flags.floating = set;
1171 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1174 } else if (state == net_wm_state_below) {
1175 if (set == _NET_WM_STATE_TOGGLE)
1176 set = !(wwin->client_flags.sunken);
1178 if (init) {
1179 wwin->client_flags.sunken = set;
1180 } else {
1181 wwin->client_flags.sunken = set;
1182 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1185 } else {
1186 #ifdef DEBUG_WMSPEC
1187 wmessage("doStateAtom unknown atom %s set %d", XGetAtomName(dpy, state), set);
1188 #endif
1192 static void removeIcon(WWindow *wwin)
1194 if (wwin->icon == NULL)
1195 return;
1196 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1197 XUnmapWindow(dpy, wwin->icon->core->window);
1198 RemoveFromStackList(wwin->icon->core);
1199 wIconDestroy(wwin->icon);
1200 wwin->icon = NULL;
1204 static Bool handleWindowType(WWindow *wwin, Atom type, int *layer)
1206 Bool ret = True;
1208 if (type == net_wm_window_type_desktop) {
1209 wwin->client_flags.no_titlebar = 1;
1210 wwin->client_flags.no_resizable = 1;
1211 wwin->client_flags.no_miniaturizable = 1;
1212 wwin->client_flags.no_border = 1;
1213 wwin->client_flags.no_resizebar = 1;
1214 wwin->client_flags.no_shadeable = 1;
1215 wwin->client_flags.no_movable = 1;
1216 wwin->client_flags.omnipresent = 1;
1217 wwin->client_flags.skip_window_list = 1;
1218 wwin->client_flags.skip_switchpanel = 1;
1219 wwin->client_flags.dont_move_off = 1;
1220 wwin->client_flags.no_appicon = 1;
1221 wwin->flags.net_skip_pager = 1;
1222 wwin->frame_x = 0;
1223 wwin->frame_y = 0;
1224 } else if (type == net_wm_window_type_dock) {
1225 wwin->client_flags.no_titlebar = 1;
1226 wwin->client_flags.no_resizable = 1;
1227 wwin->client_flags.no_miniaturizable = 1;
1228 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1229 wwin->client_flags.no_resizebar = 1;
1230 wwin->client_flags.no_shadeable = 1;
1231 wwin->client_flags.no_movable = 1;
1232 wwin->client_flags.omnipresent = 1;
1233 wwin->client_flags.skip_window_list = 1;
1234 wwin->client_flags.skip_switchpanel = 1;
1235 wwin->client_flags.dont_move_off = 1;
1236 wwin->flags.net_skip_pager = 1;
1237 } else if (type == net_wm_window_type_toolbar ||
1238 type == net_wm_window_type_menu) {
1239 wwin->client_flags.no_resizable = 1;
1240 wwin->client_flags.no_miniaturizable = 1;
1241 wwin->client_flags.no_resizebar = 1;
1242 wwin->client_flags.no_shadeable = 1;
1243 wwin->client_flags.skip_window_list = 1;
1244 wwin->client_flags.skip_switchpanel = 1;
1245 wwin->client_flags.dont_move_off = 1;
1246 wwin->client_flags.no_appicon = 1;
1247 } else if (type == net_wm_window_type_dropdown_menu ||
1248 type == net_wm_window_type_popup_menu ||
1249 type == net_wm_window_type_combo) {
1250 wwin->client_flags.no_titlebar = 1;
1251 wwin->client_flags.no_resizable = 1;
1252 wwin->client_flags.no_miniaturizable = 1;
1253 wwin->client_flags.no_resizebar = 1;
1254 wwin->client_flags.no_shadeable = 1;
1255 wwin->client_flags.skip_window_list = 1;
1256 wwin->client_flags.skip_switchpanel = 1;
1257 wwin->client_flags.dont_move_off = 1;
1258 wwin->client_flags.no_appicon = 1;
1259 } else if (type == net_wm_window_type_utility) {
1260 wwin->client_flags.no_appicon = 1;
1261 } else if (type == net_wm_window_type_splash) {
1262 wwin->client_flags.no_titlebar = 1;
1263 wwin->client_flags.no_resizable = 1;
1264 wwin->client_flags.no_miniaturizable = 1;
1265 wwin->client_flags.no_resizebar = 1;
1266 wwin->client_flags.no_shadeable = 1;
1267 wwin->client_flags.no_movable = 1;
1268 wwin->client_flags.skip_window_list = 1;
1269 wwin->client_flags.skip_switchpanel = 1;
1270 wwin->client_flags.dont_move_off = 1;
1271 wwin->client_flags.no_appicon = 1;
1272 wwin->flags.net_skip_pager = 1;
1273 } else if (type == net_wm_window_type_dialog) {
1274 /* These also seem a bad idea in our context -Dan
1275 // wwin->client_flags.skip_window_list = 1;
1276 // wwin->client_flags.no_appicon = 1;
1278 } else if (wwin->type == net_wm_window_type_tooltip) {
1279 wwin->client_flags.no_titlebar = 1;
1280 wwin->client_flags.no_resizable = 1;
1281 wwin->client_flags.no_miniaturizable = 1;
1282 wwin->client_flags.no_resizebar = 1;
1283 wwin->client_flags.no_shadeable = 1;
1284 wwin->client_flags.no_movable = 1;
1285 wwin->client_flags.skip_window_list = 1;
1286 wwin->client_flags.skip_switchpanel = 1;
1287 wwin->client_flags.dont_move_off = 1;
1288 wwin->client_flags.no_appicon = 1;
1289 wwin->client_flags.no_focusable = 1;
1290 wwin->flags.net_skip_pager = 1;
1291 } else if (wwin->type == net_wm_window_type_notification) {
1292 wwin->client_flags.no_titlebar = 1;
1293 wwin->client_flags.no_resizable = 1;
1294 wwin->client_flags.no_miniaturizable = 1;
1295 wwin->client_flags.no_border = 1;
1296 wwin->client_flags.no_resizebar = 1;
1297 wwin->client_flags.no_shadeable = 1;
1298 wwin->client_flags.no_movable = 1;
1299 wwin->client_flags.omnipresent = 1;
1300 wwin->client_flags.skip_window_list = 1;
1301 wwin->client_flags.skip_switchpanel = 1;
1302 wwin->client_flags.dont_move_off = 1;
1303 wwin->client_flags.no_hide_others= 1;
1304 wwin->client_flags.no_appicon = 1;
1305 wwin->client_flags.no_focusable = 1;
1306 wwin->flags.net_skip_pager = 1;
1307 } else if (wwin->type == net_wm_window_type_dnd) {
1308 wwin->client_flags.no_titlebar = 1;
1309 wwin->client_flags.no_resizable = 1;
1310 wwin->client_flags.no_miniaturizable = 1;
1311 wwin->client_flags.no_border = 1;
1312 wwin->client_flags.no_resizebar = 1;
1313 wwin->client_flags.no_shadeable = 1;
1314 wwin->client_flags.no_movable = 1;
1315 wwin->client_flags.skip_window_list = 1;
1316 wwin->client_flags.skip_switchpanel = 1;
1317 wwin->client_flags.dont_move_off = 1;
1318 wwin->client_flags.no_appicon = 1;
1319 wwin->flags.net_skip_pager = 1;
1320 } else if (type == net_wm_window_type_normal) {
1321 } else {
1322 ret = False;
1325 wwin->type = type;
1326 *layer = getWindowLayer(wwin);
1328 return ret;
1331 void wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1333 if (wwin->type == net_wm_window_type_splash) {
1334 WScreen *scr = wwin->screen_ptr;
1335 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1336 *x = rect.pos.x + (rect.size.width - width) / 2;
1337 *y = rect.pos.y + (rect.size.height - height) / 2;
1341 static void updateWindowType(WWindow *wwin)
1343 Atom type_ret;
1344 int fmt_ret, layer;
1345 unsigned long nitems_ret, bytes_after_ret;
1346 long *data = NULL;
1348 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1349 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1350 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1352 int i;
1353 Atom *type = (Atom *) data;
1354 for (i = 0; i < nitems_ret; ++i) {
1355 if (handleWindowType(wwin, type[i], &layer))
1356 break;
1358 XFree(data);
1361 if (wwin->frame != NULL) {
1362 ChangeStackingLevel(wwin->frame->core, layer);
1363 wwin->frame->flags.need_texture_change = 1;
1364 wWindowConfigureBorders(wwin);
1365 wFrameWindowPaint(wwin->frame);
1366 wNETWMUpdateActions(wwin, False);
1370 void wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1372 Atom type_ret;
1373 int fmt_ret, i;
1374 unsigned long nitems_ret, bytes_after_ret;
1375 long *data = NULL;
1377 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1378 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1379 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1381 long desktop = *data;
1382 XFree(data);
1384 if (desktop == -1)
1385 wwin->client_flags.omnipresent = 1;
1386 else
1387 *workspace = desktop;
1390 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1391 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1392 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1394 Atom *state = (Atom *) data;
1395 for (i = 0; i < nitems_ret; ++i)
1396 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1398 XFree(data);
1401 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1402 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1403 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1405 Atom *type = (Atom *) data;
1406 for (i = 0; i < nitems_ret; ++i) {
1407 if (handleWindowType(wwin, type[i], layer))
1408 break;
1410 XFree(data);
1413 wNETWMUpdateActions(wwin, False);
1414 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1415 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1417 wScreenUpdateUsableArea(wwin->screen_ptr);
1420 static Bool updateNetIconInfo(WWindow *wwin)
1422 Atom type_ret;
1423 int fmt_ret;
1424 unsigned long nitems_ret, bytes_after_ret;
1425 long *data = NULL;
1426 Bool hasState = False;
1427 Bool old_state = wwin->flags.net_handle_icon;
1429 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1430 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1431 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1432 long handled = *data;
1433 wwin->flags.net_handle_icon = (handled != 0);
1434 XFree(data);
1435 hasState = True;
1437 } else {
1438 wwin->flags.net_handle_icon = False;
1441 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1442 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1443 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1445 #ifdef NETWM_PROPER
1446 if (wwin->flags.net_handle_icon)
1447 #else
1448 wwin->flags.net_handle_icon = True;
1449 #endif
1451 wwin->icon_x = data[0];
1452 wwin->icon_y = data[1];
1453 wwin->icon_w = data[2];
1454 wwin->icon_h = data[3];
1457 XFree(data);
1458 hasState = True;
1460 } else {
1461 wwin->flags.net_handle_icon = False;
1464 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1465 if (wwin->flags.net_handle_icon) {
1466 removeIcon(wwin);
1467 } else {
1468 wwin->flags.miniaturized = False;
1469 wwin->flags.skip_next_animation = True;
1470 wIconifyWindow(wwin);
1474 return hasState;
1477 void wNETWMCheckInitialClientState(WWindow *wwin)
1479 #ifdef DEBUG_WMSPEC
1480 wmessage("wNETWMCheckInitialClientState");
1481 #endif
1483 wNETWMShowingDesktop(wwin->screen_ptr, False);
1485 updateWindowType(wwin);
1486 updateNetIconInfo(wwin);
1487 updateIconImage(wwin);
1490 void wNETWMCheckInitialFrameState(WWindow *wwin)
1492 #ifdef DEBUG_WMSPEC
1493 wmessage("wNETWMCheckInitialFrameState");
1494 #endif
1496 updateWindowOpacity(wwin);
1499 static void handleDesktopNames(WScreen *scr)
1501 unsigned long nitems_ret, bytes_after_ret;
1502 char *data, *names[32];
1503 int fmt_ret, i, n;
1504 Atom type_ret;
1506 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1507 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1508 &bytes_after_ret, (unsigned char **)&data) != Success)
1509 return;
1511 if (data == NULL)
1512 return;
1514 if (type_ret != utf8_string || fmt_ret != 8)
1515 return;
1517 n = 0;
1518 names[n] = data;
1519 for (i = 0; i < nitems_ret; i++) {
1520 if (data[i] == 0) {
1521 n++;
1522 names[n] = &data[i];
1523 } else if (*names[n] == 0) {
1524 names[n] = &data[i];
1525 wWorkspaceRename(scr, n, names[n]);
1530 Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
1532 WScreen *scr;
1533 WWindow *wwin;
1535 #ifdef DEBUG_WMSPEC
1536 wmessage("processClientMessage type %s", XGetAtomName(dpy, event->message_type));
1537 #endif
1539 scr = wScreenForWindow(event->window);
1540 if (scr) {
1541 /* generic client messages */
1542 if (event->message_type == net_current_desktop) {
1543 wWorkspaceChange(scr, event->data.l[0]);
1544 return True;
1546 } else if (event->message_type == net_number_of_desktops) {
1547 long value;
1549 value = event->data.l[0];
1550 if (value > scr->workspace_count) {
1551 wWorkspaceMake(scr, value - scr->workspace_count);
1552 } else if (value < scr->workspace_count) {
1553 int i;
1554 Bool rebuild = False;
1556 for (i = scr->workspace_count - 1; i >= value; i--) {
1557 if (!wWorkspaceDelete(scr, i)) {
1558 rebuild = True;
1559 break;
1563 if (rebuild)
1564 updateWorkspaceCount(scr);
1566 return True;
1568 } else if (event->message_type == net_showing_desktop) {
1569 wNETWMShowingDesktop(scr, event->data.l[0]);
1570 return True;
1572 } else if (event->message_type == net_desktop_names) {
1573 handleDesktopNames(scr);
1574 return True;
1578 /* window specific client messages */
1580 wwin = wWindowFor(event->window);
1581 if (!wwin)
1582 return False;
1584 if (event->message_type == net_active_window) {
1586 * Satisfy a client's focus request only if
1587 * - request comes from a pager, or
1588 * - it's explicitly allowed in Advanced Options, or
1589 * - giving the client the focus does not cause a change in
1590 * the active workspace (XXX: or the active head if Xinerama)
1592 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace /* No workspace change */
1593 || event->data.l[0] == 2 /* Requested by pager */
1594 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1595 wNETWMShowingDesktop(scr, False);
1596 wMakeWindowVisible(wwin);
1598 return True;
1600 } else if (event->message_type == net_close_window) {
1601 if (!WFLAGP(wwin, no_closable)) {
1602 if (wwin->protocols.DELETE_WINDOW)
1603 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
1604 w_global.timestamp.last_event);
1606 return True;
1608 } else if (event->message_type == net_wm_state) {
1609 int maximized = wwin->flags.maximized;
1610 long set = event->data.l[0];
1612 #ifdef DEBUG_WMSPEC
1613 wmessage("net_wm_state set %ld a1 %s a2 %s", set,
1614 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1615 #endif
1617 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1618 if (event->data.l[2])
1619 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1621 if (wwin->flags.maximized != maximized) {
1622 if (!wwin->flags.maximized) {
1623 wwin->flags.maximized = maximized;
1624 wUnmaximizeWindow(wwin);
1625 } else {
1626 wMaximizeWindow(wwin, wwin->flags.maximized);
1629 updateStateHint(wwin, False, False);
1630 return True;
1632 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1633 updateNetIconInfo(wwin);
1634 return True;
1636 } else if (event->message_type == net_wm_desktop) {
1637 long desktop = event->data.l[0];
1639 if (desktop == -1) {
1640 wWindowSetOmnipresent(wwin, True);
1641 } else {
1642 if (IS_OMNIPRESENT(wwin))
1643 wWindowSetOmnipresent(wwin, False);
1644 wWindowChangeWorkspace(wwin, desktop);
1646 return True;
1649 return False;
1652 void wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1654 #ifdef DEBUG_WMSPEC
1655 wmessage("clientHintChange type %s", XGetAtomName(dpy, event->atom));
1656 #endif
1658 if (event->atom == net_wm_strut || event->atom == net_wm_strut_partial) {
1659 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1660 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1661 wScreenUpdateUsableArea(wwin->screen_ptr);
1662 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1663 updateNetIconInfo(wwin);
1664 } else if (event->atom == net_wm_window_type) {
1665 updateWindowType(wwin);
1666 } else if (event->atom == net_wm_name) {
1667 char *name = wNETWMGetWindowName(wwin->client_win);
1668 wWindowUpdateName(wwin, name);
1669 if (name)
1670 wfree(name);
1671 } else if (event->atom == net_wm_icon_name) {
1672 if (wwin->icon) {
1673 wIconChangeTitle(wwin->icon, wwin);
1674 wIconPaint(wwin->icon);
1676 } else if (event->atom == net_wm_icon) {
1677 updateIconImage(wwin);
1678 } else if (event->atom == net_wm_window_opacity) {
1679 updateWindowOpacity(wwin);
1683 int wNETWMGetPidForWindow(Window window)
1685 Atom type_ret;
1686 int fmt_ret;
1687 unsigned long nitems_ret, bytes_after_ret;
1688 long *data = NULL;
1689 int pid;
1691 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1692 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1693 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1694 pid = *data;
1695 XFree(data);
1696 } else {
1697 pid = 0;
1700 return pid;
1703 char *wNETWMGetWindowName(Window window)
1705 char *name;
1706 char *ret;
1707 int size;
1709 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1710 if (name) {
1711 ret = wstrndup(name, size);
1712 XFree(name);
1713 } else {
1714 ret = NULL;
1717 return ret;
1720 char *wNETWMGetIconName(Window window)
1722 char *name;
1723 char *ret;
1724 int size;
1726 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1727 if (name) {
1728 ret = wstrndup(name, size);
1729 XFree(name);
1730 } else {
1731 ret = NULL;
1734 return ret;
1737 static void observer(void *self, WMNotification *notif)
1739 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1740 const char *name = WMGetNotificationName(notif);
1741 void *data = WMGetNotificationClientData(notif);
1742 NetData *ndata = (NetData *) self;
1744 if (strcmp(name, WMNManaged) == 0 && wwin) {
1745 updateClientList(wwin->screen_ptr);
1746 updateClientListStacking(wwin->screen_ptr, NULL);
1747 updateStateHint(wwin, True, False);
1749 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1750 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1751 wScreenUpdateUsableArea(wwin->screen_ptr);
1752 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1753 updateClientList(wwin->screen_ptr);
1754 updateClientListStacking(wwin->screen_ptr, wwin);
1755 updateWorkspaceHint(wwin, False, True);
1756 updateStateHint(wwin, False, True);
1757 wNETWMUpdateActions(wwin, True);
1759 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1760 wScreenUpdateUsableArea(wwin->screen_ptr);
1761 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1762 updateClientListStacking(wwin->screen_ptr, NULL);
1763 updateStateHint(wwin, False, False);
1764 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1765 updateClientListStacking(wwin->screen_ptr, NULL);
1766 updateStateHint(wwin, False, False);
1767 } else if (strcmp(name, WMNChangedFocus) == 0) {
1768 updateFocusHint(ndata->scr);
1769 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1770 updateWorkspaceHint(wwin, False, False);
1771 updateStateHint(wwin, True, False);
1772 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1773 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1777 static void wsobserver(void *self, WMNotification *notif)
1779 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1780 const char *name = WMGetNotificationName(notif);
1782 /* Parameter not used, but tell the compiler that it is ok */
1783 (void) self;
1785 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1786 updateWorkspaceCount(scr);
1787 updateWorkspaceNames(scr);
1788 wNETWMUpdateWorkarea(scr);
1789 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1790 updateWorkspaceCount(scr);
1791 updateWorkspaceNames(scr);
1792 wNETWMUpdateWorkarea(scr);
1793 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1794 updateCurrentWorkspace(scr);
1795 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1796 updateWorkspaceNames(scr);
1800 void wNETFrameExtents(WWindow *wwin)
1802 long extents[4] = { 0, 0, 0, 0 };
1804 /* The extents array describes dimensions which are not
1805 * part of the client window. In our case that means
1806 * widths of the border and heights of the titlebar and resizebar.
1808 * Index 0 = left
1809 * 1 = right
1810 * 2 = top
1811 * 3 = bottom
1813 if (wwin->frame->titlebar)
1814 extents[2] = wwin->frame->titlebar->height;
1815 if (wwin->frame->resizebar)
1816 extents[3] = wwin->frame->resizebar->height;
1817 if (HAS_BORDER(wwin)) {
1818 extents[0] += wwin->screen_ptr->frame_border_width;
1819 extents[1] += wwin->screen_ptr->frame_border_width;
1820 extents[2] += wwin->screen_ptr->frame_border_width;
1821 extents[3] += wwin->screen_ptr->frame_border_width;
1824 XChangeProperty(dpy, wwin->client_win, net_frame_extents, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) extents, 4);
1827 void wNETCleanupFrameExtents(WWindow *wwin)
1829 XDeleteProperty(dpy, wwin->client_win, net_frame_extents);