Variable workspace_count moved to the workspace object in the global namespace
[wmaker-crm.git] / src / wmspec.c
blob836d8f75f03fc77e2bcf2523a35910ff2ba0c264
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 <X11/Xarch.h>
36 #include <string.h>
38 #include <WINGs/WUtil.h>
39 #include "WindowMaker.h"
40 #include "window.h"
41 #include "screen.h"
42 #include "workspace.h"
43 #include "framewin.h"
44 #include "actions.h"
45 #include "client.h"
46 #include "appicon.h"
47 #include "wmspec.h"
48 #include "icon.h"
49 #include "stacking.h"
50 #include "xinerama.h"
51 #include "properties.h"
54 /* Root Window Properties */
55 static Atom net_supported;
56 static Atom net_client_list;
57 static Atom net_client_list_stacking;
58 static Atom net_number_of_desktops;
59 static Atom net_desktop_geometry;
60 static Atom net_desktop_viewport;
61 static Atom net_current_desktop;
62 static Atom net_desktop_names;
63 static Atom net_active_window;
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_normal;
89 static Atom net_wm_state;
90 static Atom net_wm_state_modal; /* XXX: what is this?!? */
91 static Atom net_wm_state_sticky;
92 static Atom net_wm_state_maximized_vert;
93 static Atom net_wm_state_maximized_horz;
94 static Atom net_wm_state_shaded;
95 static Atom net_wm_state_skip_taskbar;
96 static Atom net_wm_state_skip_pager;
97 static Atom net_wm_state_hidden;
98 static Atom net_wm_state_fullscreen;
99 static Atom net_wm_state_above;
100 static Atom net_wm_state_below;
101 static Atom net_wm_allowed_actions;
102 static Atom net_wm_action_move;
103 static Atom net_wm_action_resize;
104 static Atom net_wm_action_minimize;
105 static Atom net_wm_action_shade;
106 static Atom net_wm_action_stick;
107 static Atom net_wm_action_maximize_horz;
108 static Atom net_wm_action_maximize_vert;
109 static Atom net_wm_action_fullscreen;
110 static Atom net_wm_action_change_desktop;
111 static Atom net_wm_action_close;
112 static Atom net_wm_strut; /* XXX: see net_workarea */
113 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
114 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. */
115 static Atom net_wm_icon;
116 static Atom net_wm_pid; /* TODO */
117 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
119 static Atom net_frame_extents;
121 /* Window Manager Protocols */
122 static Atom net_wm_ping; /* TODO */
124 static Atom utf8_string;
126 typedef struct {
127 char *name;
128 Atom *atom;
129 } atomitem_t;
131 static atomitem_t atomNames[] = {
132 {"_NET_SUPPORTED", &net_supported},
133 {"_NET_CLIENT_LIST", &net_client_list},
134 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
135 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
136 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
137 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
138 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
139 {"_NET_DESKTOP_NAMES", &net_desktop_names},
140 {"_NET_ACTIVE_WINDOW", &net_active_window},
141 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
142 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
143 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
144 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
146 {"_NET_CLOSE_WINDOW", &net_close_window},
147 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
148 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
150 {"_NET_WM_NAME", &net_wm_name},
151 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
152 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
153 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
154 {"_NET_WM_DESKTOP", &net_wm_desktop},
155 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
156 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
157 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
158 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
159 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
160 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
161 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
162 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
163 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
164 {"_NET_WM_STATE", &net_wm_state},
165 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
166 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
167 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
168 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
169 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
170 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
171 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
172 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
173 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
174 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
175 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
176 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
177 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
178 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
179 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
180 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
181 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
182 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
183 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
184 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
185 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
186 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
187 {"_NET_WM_STRUT", &net_wm_strut},
188 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
189 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
190 {"_NET_WM_ICON", &net_wm_icon},
191 {"_NET_WM_PID", &net_wm_pid},
192 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
194 {"_NET_FRAME_EXTENTS", &net_frame_extents},
196 {"_NET_WM_PING", &net_wm_ping},
198 {"UTF8_STRING", &utf8_string},
201 #define atomNr (sizeof(atomNames)/sizeof(atomNames[0]))
203 #define _NET_WM_STATE_REMOVE 0
204 #define _NET_WM_STATE_ADD 1
205 #define _NET_WM_STATE_TOGGLE 2
207 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
208 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
209 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
210 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
211 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
212 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
213 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
214 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
215 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
216 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
217 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
219 static void observer(void *self, WMNotification *notif);
220 static void wsobserver(void *self, WMNotification *notif);
222 static void updateClientList(WScreen *scr);
223 static void updateClientListStacking(WScreen *scr, WWindow *);
225 static void updateWorkspaceNames(WScreen *scr);
226 static void updateCurrentWorkspace(WScreen *scr);
227 static void updateWorkspaceCount(WScreen *scr);
228 static void wNETWMShowingDesktop(WScreen *scr, Bool show);
230 typedef struct NetData {
231 WScreen *scr;
232 WReservedArea *strut;
233 WWindow **show_desktop;
234 } NetData;
236 static void setSupportedHints(WScreen *scr)
238 Atom atom[atomNr];
239 int i = 0;
241 /* set supported hints list */
242 /* XXX: extend this !!! */
244 atom[i++] = net_client_list;
245 atom[i++] = net_client_list_stacking;
246 atom[i++] = net_number_of_desktops;
247 atom[i++] = net_desktop_geometry;
248 atom[i++] = net_desktop_viewport;
249 atom[i++] = net_current_desktop;
250 atom[i++] = net_desktop_names;
251 atom[i++] = net_active_window;
252 atom[i++] = net_supporting_wm_check;
253 atom[i++] = net_showing_desktop;
254 #if 0
255 atom[i++] = net_wm_moveresize;
256 #endif
257 atom[i++] = net_wm_desktop;
258 atom[i++] = net_wm_window_type;
259 atom[i++] = net_wm_window_type_desktop;
260 atom[i++] = net_wm_window_type_dock;
261 atom[i++] = net_wm_window_type_toolbar;
262 atom[i++] = net_wm_window_type_menu;
263 atom[i++] = net_wm_window_type_utility;
264 atom[i++] = net_wm_window_type_splash;
265 atom[i++] = net_wm_window_type_dialog;
266 atom[i++] = net_wm_window_type_normal;
268 atom[i++] = net_wm_state;
269 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
270 atom[i++] = net_wm_state_sticky;
271 atom[i++] = net_wm_state_shaded;
272 atom[i++] = net_wm_state_maximized_horz;
273 atom[i++] = net_wm_state_maximized_vert;
274 atom[i++] = net_wm_state_skip_taskbar;
275 atom[i++] = net_wm_state_skip_pager;
276 atom[i++] = net_wm_state_hidden;
277 atom[i++] = net_wm_state_fullscreen;
278 atom[i++] = net_wm_state_above;
279 atom[i++] = net_wm_state_below;
281 atom[i++] = net_wm_allowed_actions;
282 atom[i++] = net_wm_action_move;
283 atom[i++] = net_wm_action_resize;
284 atom[i++] = net_wm_action_minimize;
285 atom[i++] = net_wm_action_shade;
286 atom[i++] = net_wm_action_stick;
287 atom[i++] = net_wm_action_maximize_horz;
288 atom[i++] = net_wm_action_maximize_vert;
289 atom[i++] = net_wm_action_fullscreen;
290 atom[i++] = net_wm_action_change_desktop;
291 atom[i++] = net_wm_action_close;
293 atom[i++] = net_wm_strut;
294 atom[i++] = net_wm_icon_geometry;
295 atom[i++] = net_wm_icon;
296 atom[i++] = net_wm_handled_icons;
298 atom[i++] = net_frame_extents;
300 atom[i++] = net_wm_name;
301 atom[i++] = net_wm_icon_name;
303 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
305 /* set supporting wm hint */
306 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
307 PropModeReplace, (unsigned char *)&scr->info_window, 1);
309 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
310 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
313 void wNETWMUpdateDesktop(WScreen *scr)
315 long *views, sizes[2];
316 int count, i;
318 if (w_global.workspace.count == 0)
319 return;
321 count = w_global.workspace.count * 2;
322 views = wmalloc(sizeof(long) * count);
323 /*memset(views, 0, sizeof(long) * count); */
324 sizes[0] = scr->scr_width;
325 sizes[1] = scr->scr_height;
327 for (i = 0; i < w_global.workspace.count; i++) {
328 views[2 * i + 0] = 0;
329 views[2 * i + 1] = 0;
332 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
333 PropModeReplace, (unsigned char *)sizes, 2);
335 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
336 PropModeReplace, (unsigned char *)views, count);
338 wfree(views);
341 int wNETWMGetCurrentDesktopFromHint(WScreen *scr)
343 int count;
344 unsigned char *prop;
346 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
347 if (prop) {
348 int desktop = *(long *)prop;
349 XFree(prop);
350 return desktop;
352 return -1;
356 * Find the best icon to be used by Window Maker for appicon/miniwindows.
357 * Currently the algorithm is to take the image with the size closest
358 * to icon_size x icon_size, but never bigger than that.
360 * This algorithm is very poorly implemented and needs to be redone (it can
361 * easily select images with very large widths and very small heights over
362 * square images, if the area of the former is closer to the desired one).
364 * The logic can also be changed to accept bigger images and scale them down.
366 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
368 int size, wanted, d;
369 unsigned long i, distance;
370 unsigned long *icon;
372 /* better use only 75% of icon_size. For 64x64 this means 48x48
373 * This leaves room around the icon for the miniwindow title and
374 * results in better overall aesthetics -Dan */
375 wanted = wPreferences.icon_size * wPreferences.icon_size;
377 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
378 size = data[i] * data[i + 1];
379 if (size == 0)
380 break;
381 d = wanted - size;
382 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
383 distance = d;
384 icon = &data[i];
386 i += size + 2;
389 return icon;
392 static RImage *makeRImageFromARGBData(unsigned long *data)
394 int size, width, height, i;
395 RImage *image;
396 unsigned char *imgdata;
397 unsigned long pixel;
399 width = data[0];
400 height = data[1];
401 size = width * height;
403 if (size == 0)
404 return NULL;
406 image = RCreateImage(width, height, True);
408 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
409 pixel = data[i];
410 imgdata[3] = (pixel >> 24) & 0xff; /* A */
411 imgdata[0] = (pixel >> 16) & 0xff; /* R */
412 imgdata[1] = (pixel >> 8) & 0xff; /* G */
413 imgdata[2] = (pixel >> 0) & 0xff; /* B */
416 return image;
419 RImage *get_window_image_from_x11(Window window)
421 RImage *image;
422 Atom type;
423 int format;
424 unsigned long items, rest;
425 unsigned long *property, *data;
427 /* Get the icon from X11 Window */
428 if (XGetWindowProperty(dpy, window, net_wm_icon, 0L, LONG_MAX,
429 False, XA_CARDINAL, &type, &format, &items, &rest,
430 (unsigned char **)&property) != Success || !property)
431 return NULL;
433 if (type != XA_CARDINAL || format != 32 || items < 2) {
434 XFree(property);
435 return NULL;
438 /* Find the best icon */
439 data = findBestIcon(property, items);
440 if (!data) {
441 XFree(property);
442 return NULL;
445 /* Save the best icon in the X11 icon */
446 image = makeRImageFromARGBData(data);
448 XFree(property);
450 /* Resize the image to the correct value */
451 image = wIconValidateIconSize(image, wPreferences.icon_size);
453 return image;
456 static void updateIconImage(WWindow *wwin)
458 /* Remove the icon image from X11 */
459 if (wwin->net_icon_image)
460 RReleaseImage(wwin->net_icon_image);
462 /* Save the icon in the X11 icon */
463 wwin->net_icon_image = get_window_image_from_x11(wwin->client_win);
465 /* Refresh the Window Icon */
466 if (wwin->icon)
467 wIconUpdate(wwin->icon);
469 /* Refresh the application icon */
470 WApplication *app = wApplicationOf(wwin->main_window);
471 if (app && app->app_icon) {
472 wIconUpdate(app->app_icon->icon);
473 wAppIconPaint(app->app_icon);
477 static void updateShowDesktop(WScreen *scr, Bool show)
479 long foo;
481 foo = (show == True);
482 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
483 PropModeReplace, (unsigned char *)&foo, 1);
486 static void wNETWMShowingDesktop(WScreen *scr, Bool show)
488 if (show && scr->netdata->show_desktop == NULL) {
489 WWindow *tmp, **wins;
490 int i = 0;
492 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
494 tmp = scr->focused_window;
495 while (tmp) {
496 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
498 wins[i++] = tmp;
499 tmp->flags.skip_next_animation = 1;
500 tmp->flags.net_show_desktop = 1;
501 wIconifyWindow(tmp);
504 tmp = tmp->prev;
506 wins[i++] = NULL;
508 scr->netdata->show_desktop = wins;
509 updateShowDesktop(scr, True);
510 } else if (scr->netdata->show_desktop != NULL) {
511 /* FIXME: get rid of workspace flashing ! */
512 int ws = scr->current_workspace;
513 WWindow **tmp;
514 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
515 wDeiconifyWindow(*tmp);
516 (*tmp)->flags.net_show_desktop = 0;
518 if (ws != scr->current_workspace)
519 wWorkspaceChange(scr, ws);
520 wfree(scr->netdata->show_desktop);
521 scr->netdata->show_desktop = NULL;
522 updateShowDesktop(scr, False);
526 void wNETWMInitStuff(WScreen *scr)
528 NetData *data;
529 int i;
531 #ifdef DEBUG_WMSPEC
532 wmessage("wNETWMInitStuff");
533 #endif
535 #ifdef HAVE_XINTERNATOMS
537 Atom atoms[atomNr];
538 char *names[atomNr];
540 for (i = 0; i < atomNr; ++i)
541 names[i] = atomNames[i].name;
543 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
544 for (i = 0; i < atomNr; ++i)
545 *atomNames[i].atom = atoms[i];
548 #else
549 for (i = 0; i < atomNr; i++)
550 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
551 #endif
553 data = wmalloc(sizeof(NetData));
554 data->scr = scr;
555 data->strut = NULL;
556 data->show_desktop = NULL;
558 scr->netdata = data;
560 setSupportedHints(scr);
562 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
563 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
564 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
565 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
566 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
567 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
568 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
570 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
571 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
572 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
573 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
575 updateClientList(scr);
576 updateClientListStacking(scr, NULL);
577 updateWorkspaceCount(scr);
578 updateWorkspaceNames(scr);
579 updateShowDesktop(scr, False);
581 wScreenUpdateUsableArea(scr);
584 void wNETWMCleanup(WScreen *scr)
586 int i;
588 for (i = 0; i < atomNr; i++)
589 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
592 void wNETWMUpdateActions(WWindow *wwin, Bool del)
594 Atom action[10]; /* nr of actions atoms defined */
595 int i = 0;
597 if (del) {
598 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
599 return;
602 if (IS_MOVABLE(wwin))
603 action[i++] = net_wm_action_move;
605 if (IS_RESIZABLE(wwin))
606 action[i++] = net_wm_action_resize;
608 if (!WFLAGP(wwin, no_miniaturizable))
609 action[i++] = net_wm_action_minimize;
611 if (!WFLAGP(wwin, no_shadeable))
612 action[i++] = net_wm_action_shade;
614 /* if (!WFLAGP(wwin, no_stickable)) */
615 action[i++] = net_wm_action_stick;
617 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
618 if (IS_RESIZABLE(wwin))
619 action[i++] = net_wm_action_maximize_horz;
621 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
622 if (IS_RESIZABLE(wwin))
623 action[i++] = net_wm_action_maximize_vert;
625 /* if (!WFLAGP(wwin, no_fullscreen)) */
626 action[i++] = net_wm_action_fullscreen;
628 /* if (!WFLAGP(wwin, no_change_desktop)) */
629 action[i++] = net_wm_action_change_desktop;
631 if (!WFLAGP(wwin, no_closable))
632 action[i++] = net_wm_action_close;
634 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
635 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
638 Bool wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
640 WReservedArea *cur;
641 WMRect rect;
643 if (!scr->netdata || !scr->netdata->strut)
644 return False;
646 area->x1 = area->y1 = area->x2 = area->y2 = 0;
648 for (cur = scr->netdata->strut; cur; cur = cur->next) {
649 WWindow *wwin = wWindowFor(cur->window);
650 if (wWindowTouchesHead(wwin, head)) {
651 if (cur->area.x1 > area->x1)
652 area->x1 = cur->area.x1;
653 if (cur->area.y1 > area->y1)
654 area->y1 = cur->area.y1;
655 if (cur->area.x2 > area->x2)
656 area->x2 = cur->area.x2;
657 if (cur->area.y2 > area->y2)
658 area->y2 = cur->area.y2;
662 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
663 return False;
665 rect = wGetRectForHead(scr, head);
667 area->x1 = rect.pos.x + area->x1;
668 area->x2 = rect.pos.x + rect.size.width - area->x2;
669 area->y1 = rect.pos.y + area->y1;
670 area->y2 = rect.pos.y + rect.size.height - area->y2;
672 return True;
675 static void updateClientList(WScreen *scr)
677 WWindow *wwin;
678 Window *windows;
679 int count;
681 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
683 count = 0;
684 wwin = scr->focused_window;
685 while (wwin) {
686 windows[count++] = wwin->client_win;
687 wwin = wwin->prev;
689 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
690 PropModeReplace, (unsigned char *)windows, count);
692 wfree(windows);
693 XFlush(dpy);
696 static void updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
698 WWindow *wwin;
699 Window *client_list, *client_list_reverse;
700 int client_count, i;
701 WCoreWindow *tmp;
702 WMBagIterator iter;
704 /* update client list */
705 i = scr->window_count + 1;
706 client_list = (Window *) wmalloc(sizeof(Window) * i);
707 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
709 client_count = 0;
710 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
711 while (tmp) {
712 wwin = wWindowFor(tmp->window);
713 /* wwin_excl is a window to exclude from the list
714 (e.g. it's now unmanaged) */
715 if (wwin && (wwin != wwin_excl))
716 client_list[client_count++] = wwin->client_win;
717 tmp = tmp->stacking->under;
721 for (i = 0; i < client_count; i++) {
722 Window w = client_list[client_count - i - 1];
723 client_list_reverse[i] = w;
726 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
727 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
729 wfree(client_list);
730 wfree(client_list_reverse);
732 XFlush(dpy);
735 static void updateWorkspaceCount(WScreen *scr)
736 { /* changeable */
737 long count;
739 count = w_global.workspace.count;
741 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
742 32, PropModeReplace, (unsigned char *)&count, 1);
745 static void updateCurrentWorkspace(WScreen *scr)
746 { /* changeable */
747 long count;
749 count = scr->current_workspace;
751 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
752 PropModeReplace, (unsigned char *)&count, 1);
755 static void updateWorkspaceNames(WScreen *scr)
757 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
758 unsigned int i, len, curr_size;
760 pos = buf;
761 len = 0;
762 for (i = 0; i < w_global.workspace.count; i++) {
763 curr_size = strlen(scr->workspaces[i]->name);
764 strcpy(pos, scr->workspaces[i]->name);
765 pos += (curr_size + 1);
766 len += (curr_size + 1);
769 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
770 PropModeReplace, (unsigned char *)buf, len);
773 static void updateFocusHint(WScreen *scr, WWindow *wwin)
774 { /* changeable */
775 Window window;
777 if (!scr->focused_window || !scr->focused_window->flags.focused)
778 window = None;
779 else
780 window = scr->focused_window->client_win;
782 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
783 PropModeReplace, (unsigned char *)&window, 1);
786 static void updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
788 long l;
790 if (del) {
791 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
792 } else {
793 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
794 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
795 32, PropModeReplace, (unsigned char *)&l, 1);
799 static void updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del)
800 { /* changeable */
801 if (del) {
802 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
803 } else {
804 Atom state[15]; /* nr of defined state atoms */
805 int i = 0;
807 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
808 updateWorkspaceHint(wwin, False, False);
810 if (IS_OMNIPRESENT(wwin))
811 state[i++] = net_wm_state_sticky;
812 if (wwin->flags.shaded)
813 state[i++] = net_wm_state_shaded;
814 if (wwin->flags.maximized & MAX_HORIZONTAL)
815 state[i++] = net_wm_state_maximized_horz;
816 if (wwin->flags.maximized & MAX_VERTICAL)
817 state[i++] = net_wm_state_maximized_vert;
818 if (WFLAGP(wwin, skip_window_list))
819 state[i++] = net_wm_state_skip_taskbar;
820 if (wwin->flags.net_skip_pager)
821 state[i++] = net_wm_state_skip_pager;
822 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
823 state[i++] = net_wm_state_hidden;
824 state[i++] = net_wm_state_skip_pager;
826 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
827 if (!IS_OMNIPRESENT(wwin))
828 updateWorkspaceHint(wwin, True, False);
829 state[i++] = net_wm_state_sticky;
832 if (WFLAGP(wwin, sunken))
833 state[i++] = net_wm_state_below;
834 if (WFLAGP(wwin, floating))
835 state[i++] = net_wm_state_above;
836 if (wwin->flags.fullscreen)
837 state[i++] = net_wm_state_fullscreen;
839 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
840 PropModeReplace, (unsigned char *)state, i);
844 static Bool updateStrut(WScreen *scr, Window w, Bool adding)
846 WReservedArea *area;
847 Bool hasState = False;
849 if (adding) {
850 Atom type_ret;
851 int fmt_ret;
852 unsigned long nitems_ret, bytes_after_ret;
853 long *data = NULL;
855 if ((XGetWindowProperty(dpy, w, net_wm_strut, 0, 4, False,
856 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
857 &bytes_after_ret, (unsigned char **)&data) == Success && data) ||
858 ((XGetWindowProperty(dpy, w, net_wm_strut_partial, 0, 12, False,
859 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
860 &bytes_after_ret, (unsigned char **)&data) == Success && data))) {
862 /* XXX: This is strictly incorrect in the case of net_wm_strut_partial...
863 * Discard the start and end properties from the partial strut and treat it as
864 * a (deprecated) strut.
865 * This means we are marking the whole width or height of the screen as
866 * reserved, which is not necessarily what the strut defines. However for the
867 * purposes of determining placement or maximization it's probably good enough.
869 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
870 area->area.x1 = data[0];
871 area->area.x2 = data[1];
872 area->area.y1 = data[2];
873 area->area.y2 = data[3];
875 area->window = w;
877 area->next = scr->netdata->strut;
878 scr->netdata->strut = area;
880 XFree(data);
881 hasState = True;
883 } else {
884 /* deleting */
885 area = scr->netdata->strut;
887 if (area) {
888 if (area->window == w) {
889 scr->netdata->strut = area->next;
890 wfree(area);
891 hasState = True;
892 } else {
893 while (area->next && area->next->window != w)
894 area = area->next;
896 if (area->next) {
897 WReservedArea *next;
899 next = area->next->next;
900 wfree(area->next);
901 area->next = next;
903 hasState = True;
909 return hasState;
912 static int getWindowLayer(WWindow *wwin)
914 int layer = WMNormalLevel;
916 if (wwin->type == net_wm_window_type_desktop) {
917 layer = WMDesktopLevel;
918 } else if (wwin->type == net_wm_window_type_dock) {
919 layer = WMDockLevel;
920 } else if (wwin->type == net_wm_window_type_toolbar) {
921 layer = WMMainMenuLevel;
922 } else if (wwin->type == net_wm_window_type_menu) {
923 layer = WMSubmenuLevel;
924 } else if (wwin->type == net_wm_window_type_utility) {
925 } else if (wwin->type == net_wm_window_type_splash) {
926 } else if (wwin->type == net_wm_window_type_dialog) {
927 if (wwin->transient_for) {
928 WWindow *parent = wWindowFor(wwin->transient_for);
929 if (parent && parent->flags.fullscreen)
930 layer = WMFullscreenLevel;
932 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
933 } else if (wwin->type == net_wm_window_type_normal) {
936 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
937 layer = WMSunkenLevel;
938 if (wwin->client_flags.floating && WMFloatingLevel > layer)
939 layer = WMFloatingLevel;
941 return layer;
944 static void doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
946 if (state == net_wm_state_sticky) {
947 if (set == _NET_WM_STATE_TOGGLE)
948 set = !IS_OMNIPRESENT(wwin);
950 if (set != wwin->flags.omnipresent)
951 wWindowSetOmnipresent(wwin, set);
953 } else if (state == net_wm_state_shaded) {
954 if (set == _NET_WM_STATE_TOGGLE)
955 set = !wwin->flags.shaded;
957 if (init) {
958 wwin->flags.shaded = set;
959 } else {
960 if (set)
961 wShadeWindow(wwin);
962 else
963 wUnshadeWindow(wwin);
965 } else if (state == net_wm_state_skip_taskbar) {
966 if (set == _NET_WM_STATE_TOGGLE)
967 set = !wwin->client_flags.skip_window_list;
969 wwin->client_flags.skip_window_list = set;
970 } else if (state == net_wm_state_skip_pager) {
971 if (set == _NET_WM_STATE_TOGGLE)
972 set = !wwin->flags.net_skip_pager;
974 wwin->flags.net_skip_pager = set;
975 } else if (state == net_wm_state_maximized_vert) {
976 if (set == _NET_WM_STATE_TOGGLE)
977 set = !(wwin->flags.maximized & MAX_VERTICAL);
979 if (init) {
980 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
981 } else {
982 if (set)
983 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
984 else
985 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
987 } else if (state == net_wm_state_maximized_horz) {
988 if (set == _NET_WM_STATE_TOGGLE)
989 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
991 if (init) {
992 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
993 } else {
994 if (set)
995 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
996 else
997 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
999 } else if (state == net_wm_state_hidden) {
1000 if (set == _NET_WM_STATE_TOGGLE)
1001 set = !(wwin->flags.miniaturized);
1003 if (init) {
1004 wwin->flags.miniaturized = set;
1005 } else {
1006 if (set)
1007 wIconifyWindow(wwin);
1008 else
1009 wDeiconifyWindow(wwin);
1011 } else if (state == net_wm_state_fullscreen) {
1012 if (set == _NET_WM_STATE_TOGGLE)
1013 set = !(wwin->flags.fullscreen);
1015 if (init) {
1016 wwin->flags.fullscreen = set;
1017 } else {
1018 if (set)
1019 wFullscreenWindow(wwin);
1020 else
1021 wUnfullscreenWindow(wwin);
1023 } else if (state == net_wm_state_above) {
1024 if (set == _NET_WM_STATE_TOGGLE)
1025 set = !(wwin->client_flags.floating);
1027 if (init) {
1028 wwin->client_flags.floating = set;
1029 } else {
1030 wwin->client_flags.floating = set;
1031 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1034 } else if (state == net_wm_state_below) {
1035 if (set == _NET_WM_STATE_TOGGLE)
1036 set = !(wwin->client_flags.sunken);
1038 if (init) {
1039 wwin->client_flags.sunken = set;
1040 } else {
1041 wwin->client_flags.sunken = set;
1042 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1045 } else {
1046 #ifdef DEBUG_WMSPEC
1047 wmessage("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1048 #endif
1052 static void removeIcon(WWindow *wwin)
1054 if (wwin->icon == NULL)
1055 return;
1056 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1057 XUnmapWindow(dpy, wwin->icon->core->window);
1058 RemoveFromStackList(wwin->icon->core);
1059 wIconDestroy(wwin->icon);
1060 wwin->icon = NULL;
1064 static Bool handleWindowType(WWindow *wwin, Atom type, int *layer)
1066 Bool ret = True;
1068 if (type == net_wm_window_type_desktop) {
1069 wwin->client_flags.no_titlebar = 1;
1070 wwin->client_flags.no_resizable = 1;
1071 wwin->client_flags.no_miniaturizable = 1;
1072 wwin->client_flags.no_border = 1;
1073 wwin->client_flags.no_resizebar = 1;
1074 wwin->client_flags.no_shadeable = 1;
1075 wwin->client_flags.no_movable = 1;
1076 wwin->client_flags.omnipresent = 1;
1077 wwin->client_flags.skip_window_list = 1;
1078 wwin->client_flags.skip_switchpanel = 1;
1079 wwin->client_flags.dont_move_off = 1;
1080 wwin->client_flags.no_appicon = 1;
1081 wwin->flags.net_skip_pager = 1;
1082 wwin->frame_x = 0;
1083 wwin->frame_y = 0;
1084 } else if (type == net_wm_window_type_dock) {
1085 wwin->client_flags.no_titlebar = 1;
1086 wwin->client_flags.no_resizable = 1;
1087 wwin->client_flags.no_miniaturizable = 1;
1088 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1089 wwin->client_flags.no_resizebar = 1;
1090 wwin->client_flags.no_shadeable = 1;
1091 wwin->client_flags.no_movable = 1;
1092 wwin->client_flags.omnipresent = 1;
1093 wwin->client_flags.skip_window_list = 1;
1094 wwin->client_flags.skip_switchpanel = 1;
1095 wwin->client_flags.dont_move_off = 1;
1096 wwin->flags.net_skip_pager = 1;
1097 } else if (type == net_wm_window_type_toolbar) {
1098 wwin->client_flags.no_titlebar = 1;
1099 wwin->client_flags.no_resizable = 1;
1100 wwin->client_flags.no_miniaturizable = 1;
1101 wwin->client_flags.no_resizebar = 1;
1102 wwin->client_flags.no_shadeable = 1;
1103 wwin->client_flags.skip_window_list = 1;
1104 wwin->client_flags.skip_switchpanel = 1;
1105 wwin->client_flags.dont_move_off = 1;
1106 wwin->client_flags.no_appicon = 1;
1107 } else if (type == net_wm_window_type_menu) {
1108 wwin->client_flags.no_titlebar = 1;
1109 wwin->client_flags.no_resizable = 1;
1110 wwin->client_flags.no_miniaturizable = 1;
1111 wwin->client_flags.no_resizebar = 1;
1112 wwin->client_flags.no_shadeable = 1;
1113 wwin->client_flags.skip_window_list = 1;
1114 wwin->client_flags.skip_switchpanel = 1;
1115 wwin->client_flags.dont_move_off = 1;
1116 wwin->client_flags.no_appicon = 1;
1117 } else if (type == net_wm_window_type_utility) {
1118 wwin->client_flags.no_appicon = 1;
1119 } else if (type == net_wm_window_type_splash) {
1120 wwin->client_flags.no_titlebar = 1;
1121 wwin->client_flags.no_resizable = 1;
1122 wwin->client_flags.no_miniaturizable = 1;
1123 wwin->client_flags.no_resizebar = 1;
1124 wwin->client_flags.no_shadeable = 1;
1125 wwin->client_flags.no_movable = 1;
1126 wwin->client_flags.skip_window_list = 1;
1127 wwin->client_flags.skip_switchpanel = 1;
1128 wwin->client_flags.dont_move_off = 1;
1129 wwin->client_flags.no_appicon = 1;
1130 wwin->flags.net_skip_pager = 1;
1131 } else if (type == net_wm_window_type_dialog) {
1132 /* These also seem a bad idea in our context -Dan
1133 // wwin->client_flags.skip_window_list = 1;
1134 // wwin->client_flags.no_appicon = 1;
1136 } else if (type == net_wm_window_type_normal) {
1137 } else {
1138 ret = False;
1141 wwin->type = type;
1142 *layer = getWindowLayer(wwin);
1144 return ret;
1147 void wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1149 if (wwin->type == net_wm_window_type_splash) {
1150 WScreen *scr = wwin->screen_ptr;
1151 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1152 *x = rect.pos.x + (rect.size.width - width) / 2;
1153 *y = rect.pos.y + (rect.size.height - height) / 2;
1157 static void updateWindowType(WWindow *wwin)
1159 Atom type_ret;
1160 int fmt_ret, layer;
1161 unsigned long nitems_ret, bytes_after_ret;
1162 long *data = NULL;
1164 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1165 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1166 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1168 int i;
1169 Atom *type = (Atom *) data;
1170 for (i = 0; i < nitems_ret; ++i) {
1171 if (handleWindowType(wwin, type[i], &layer))
1172 break;
1174 XFree(data);
1177 if (wwin->frame != NULL) {
1178 ChangeStackingLevel(wwin->frame->core, layer);
1179 wwin->frame->flags.need_texture_change = 1;
1180 wWindowConfigureBorders(wwin);
1181 wFrameWindowPaint(wwin->frame);
1182 wNETWMUpdateActions(wwin, False);
1186 void wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1188 Atom type_ret;
1189 int fmt_ret, i;
1190 unsigned long nitems_ret, bytes_after_ret;
1191 long *data = NULL;
1193 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1194 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1195 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1197 long desktop = *data;
1198 XFree(data);
1200 if (desktop == -1)
1201 wwin->client_flags.omnipresent = 1;
1202 else
1203 *workspace = desktop;
1206 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1207 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1208 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1210 Atom *state = (Atom *) data;
1211 for (i = 0; i < nitems_ret; ++i)
1212 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1214 XFree(data);
1217 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1218 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1219 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1221 Atom *type = (Atom *) data;
1222 for (i = 0; i < nitems_ret; ++i) {
1223 if (handleWindowType(wwin, type[i], layer))
1224 break;
1226 XFree(data);
1229 wNETWMUpdateActions(wwin, False);
1230 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1231 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1233 wScreenUpdateUsableArea(wwin->screen_ptr);
1236 static Bool updateNetIconInfo(WWindow *wwin)
1238 Atom type_ret;
1239 int fmt_ret;
1240 unsigned long nitems_ret, bytes_after_ret;
1241 long *data = NULL;
1242 Bool hasState = False;
1243 Bool old_state = wwin->flags.net_handle_icon;
1245 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1246 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1247 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1248 long handled = *data;
1249 wwin->flags.net_handle_icon = (handled != 0);
1250 XFree(data);
1251 hasState = True;
1253 } else {
1254 wwin->flags.net_handle_icon = False;
1257 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1258 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1259 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1261 #ifdef NETWM_PROPER
1262 if (wwin->flags.net_handle_icon)
1263 #else
1264 wwin->flags.net_handle_icon = True;
1265 #endif
1267 wwin->icon_x = data[0];
1268 wwin->icon_y = data[1];
1269 wwin->icon_w = data[2];
1270 wwin->icon_h = data[3];
1273 XFree(data);
1274 hasState = True;
1276 } else {
1277 wwin->flags.net_handle_icon = False;
1280 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1281 if (wwin->flags.net_handle_icon) {
1282 removeIcon(wwin);
1283 } else {
1284 wwin->flags.miniaturized = False;
1285 wwin->flags.skip_next_animation = True;
1286 wIconifyWindow(wwin);
1290 return hasState;
1293 void wNETWMCheckInitialClientState(WWindow *wwin)
1295 #ifdef DEBUG_WMSPEC
1296 wmessage("wNETWMCheckInitialClientState");
1297 #endif
1299 wNETWMShowingDesktop(wwin->screen_ptr, False);
1301 updateNetIconInfo(wwin);
1302 updateIconImage(wwin);
1305 static void handleDesktopNames(XClientMessageEvent *event, WScreen *scr)
1307 unsigned long nitems_ret, bytes_after_ret;
1308 char *data, *names[32];
1309 int fmt_ret, i, n;
1310 Atom type_ret;
1312 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1313 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1314 &bytes_after_ret, (unsigned char **)&data) != Success)
1315 return;
1317 if (data == NULL)
1318 return;
1320 if (type_ret != utf8_string || fmt_ret != 8)
1321 return;
1323 n = 0;
1324 names[n] = data;
1325 for (i = 0; i < nitems_ret; i++) {
1326 if (data[i] == 0) {
1327 n++;
1328 names[n] = &data[i];
1329 } else if (*names[n] == 0) {
1330 names[n] = &data[i];
1331 wWorkspaceRename(scr, n, names[n]);
1336 Bool wNETWMProcessClientMessage(XClientMessageEvent *event)
1338 WScreen *scr;
1339 WWindow *wwin;
1340 Bool done = True;
1342 #ifdef DEBUG_WMSPEC
1343 wmessage("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1344 #endif
1346 scr = wScreenForWindow(event->window);
1347 if (scr) {
1348 /* generic client messages */
1349 if (event->message_type == net_current_desktop) {
1350 wWorkspaceChange(scr, event->data.l[0]);
1351 } else if (event->message_type == net_number_of_desktops) {
1352 long value;
1354 value = event->data.l[0];
1355 if (value > w_global.workspace.count) {
1356 wWorkspaceMake(scr, value - w_global.workspace.count);
1357 } else if (value < w_global.workspace.count) {
1358 int i;
1359 Bool rebuild = False;
1361 for (i = w_global.workspace.count - 1; i >= value; i--) {
1362 if (!wWorkspaceDelete(scr, i)) {
1363 rebuild = True;
1364 break;
1368 if (rebuild)
1369 updateWorkspaceCount(scr);
1371 } else if (event->message_type == net_showing_desktop) {
1372 wNETWMShowingDesktop(scr, event->data.l[0]);
1373 } else if (event->message_type == net_desktop_names) {
1374 handleDesktopNames(event, scr);
1375 } else {
1376 done = False;
1379 if (done)
1380 return True;
1383 /* window specific client messages */
1385 wwin = wWindowFor(event->window);
1386 if (!wwin)
1387 return False;
1389 if (event->message_type == net_active_window) {
1391 * Satisfy a client's focus request only if
1392 * - request comes from a pager, or
1393 * - it's explicitly allowed in Advanced Options, or
1394 * - giving the client the focus does not cause a change in
1395 * the active workspace (XXX: or the active head if Xinerama)
1397 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace /* No workspace change */
1398 || event->data.l[0] == 2 /* Requested by pager */
1399 || WFLAGP(wwin, focus_across_wksp) /* Explicitly allowed */) {
1400 wNETWMShowingDesktop(scr, False);
1401 wMakeWindowVisible(wwin);
1403 } else if (event->message_type == net_close_window) {
1404 if (!WFLAGP(wwin, no_closable)) {
1405 if (wwin->protocols.DELETE_WINDOW)
1406 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
1407 w_global.timestamp.last_event);
1409 } else if (event->message_type == net_wm_state) {
1410 int maximized = wwin->flags.maximized;
1411 long set = event->data.l[0];
1413 #ifdef DEBUG_WMSPEC
1414 wmessage("net_wm_state set %ld a1 %s a2 %s\n", set,
1415 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1416 #endif
1418 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1419 if (event->data.l[2])
1420 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1422 if (wwin->flags.maximized != maximized) {
1423 if (!wwin->flags.maximized) {
1424 wwin->flags.maximized = maximized;
1425 wUnmaximizeWindow(wwin);
1426 } else {
1427 wMaximizeWindow(wwin, wwin->flags.maximized);
1430 updateStateHint(wwin, False, False);
1431 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1432 updateNetIconInfo(wwin);
1433 } else if (event->message_type == net_wm_desktop) {
1434 long desktop = event->data.l[0];
1435 if (desktop == -1) {
1436 wWindowSetOmnipresent(wwin, True);
1437 } else {
1438 if (IS_OMNIPRESENT(wwin))
1439 wWindowSetOmnipresent(wwin, False);
1440 wWindowChangeWorkspace(wwin, desktop);
1442 } else {
1443 done = False;
1446 return done;
1449 void wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1451 #ifdef DEBUG_WMSPEC
1452 wmessage("clientHintChange type %s\n", XGetAtomName(dpy, event->atom));
1453 #endif
1455 if (event->atom == net_wm_strut || event->atom == net_wm_strut_partial) {
1456 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1457 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1458 wScreenUpdateUsableArea(wwin->screen_ptr);
1459 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1460 updateNetIconInfo(wwin);
1461 } else if (event->atom == net_wm_window_type) {
1462 updateWindowType(wwin);
1463 } else if (event->atom == net_wm_name) {
1464 char *name = wNETWMGetWindowName(wwin->client_win);
1465 wWindowUpdateName(wwin, name);
1466 if (name)
1467 wfree(name);
1468 } else if (event->atom == net_wm_icon_name) {
1469 if (wwin->icon) {
1470 wIconChangeTitle(wwin->icon, wwin);
1471 wIconPaint(wwin->icon);
1473 } else if (event->atom == net_wm_icon) {
1474 updateIconImage(wwin);
1478 int wNETWMGetPidForWindow(Window window)
1480 Atom type_ret;
1481 int fmt_ret;
1482 unsigned long nitems_ret, bytes_after_ret;
1483 long *data = NULL;
1484 int pid;
1486 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1487 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1488 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1489 pid = *data;
1490 XFree(data);
1491 } else {
1492 pid = 0;
1495 return pid;
1498 char *wNETWMGetWindowName(Window window)
1500 char *name;
1501 char *ret;
1502 int size;
1504 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1505 if (name) {
1506 ret = wstrndup(name, size);
1507 XFree(name);
1508 } else {
1509 ret = NULL;
1512 return ret;
1515 char *wNETWMGetIconName(Window window)
1517 char *name;
1518 char *ret;
1519 int size;
1521 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1522 if (name) {
1523 ret = wstrndup(name, size);
1524 XFree(name);
1525 } else {
1526 ret = NULL;
1529 return ret;
1532 static void observer(void *self, WMNotification *notif)
1534 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1535 const char *name = WMGetNotificationName(notif);
1536 void *data = WMGetNotificationClientData(notif);
1537 NetData *ndata = (NetData *) self;
1539 if (strcmp(name, WMNManaged) == 0 && wwin) {
1540 updateClientList(wwin->screen_ptr);
1541 updateClientListStacking(wwin->screen_ptr, NULL);
1542 updateStateHint(wwin, True, False);
1544 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1545 updateStrut(wwin->screen_ptr, wwin->client_win, True);
1546 wScreenUpdateUsableArea(wwin->screen_ptr);
1547 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1548 updateClientList(wwin->screen_ptr);
1549 updateClientListStacking(wwin->screen_ptr, wwin);
1550 updateWorkspaceHint(wwin, False, True);
1551 updateStateHint(wwin, False, True);
1552 wNETWMUpdateActions(wwin, True);
1554 updateStrut(wwin->screen_ptr, wwin->client_win, False);
1555 wScreenUpdateUsableArea(wwin->screen_ptr);
1556 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1557 updateClientListStacking(wwin->screen_ptr, NULL);
1558 updateStateHint(wwin, False, False);
1559 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1560 updateClientListStacking(wwin->screen_ptr, NULL);
1561 updateStateHint(wwin, False, False);
1562 } else if (strcmp(name, WMNChangedFocus) == 0) {
1563 updateFocusHint(ndata->scr, wwin);
1564 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1565 updateWorkspaceHint(wwin, False, False);
1566 updateStateHint(wwin, True, False);
1567 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1568 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1572 static void wsobserver(void *self, WMNotification *notif)
1574 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1575 const char *name = WMGetNotificationName(notif);
1577 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1578 updateWorkspaceCount(scr);
1579 updateWorkspaceNames(scr);
1580 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1581 updateWorkspaceCount(scr);
1582 updateWorkspaceNames(scr);
1583 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1584 updateCurrentWorkspace(scr);
1585 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1586 updateWorkspaceNames(scr);
1590 void wNETFrameExtents(WWindow *wwin)
1592 long extents[4] = { 0, 0, 0, 0 };
1594 /* The extents array describes dimensions which are not
1595 * part of the client window. In our case that means
1596 * widths of the border and heights of the titlebar and resizebar.
1598 * Index 0 = left
1599 * 1 = right
1600 * 2 = top
1601 * 3 = bottom
1603 if (wwin->frame->titlebar)
1604 extents[2] = wwin->frame->titlebar->height;
1605 if (wwin->frame->resizebar)
1606 extents[3] = wwin->frame->resizebar->height;
1607 if (HAS_BORDER(wwin)) {
1608 extents[0] += wwin->screen_ptr->frame_border_width;
1609 extents[1] += wwin->screen_ptr->frame_border_width;
1610 extents[2] += wwin->screen_ptr->frame_border_width;
1611 extents[3] += wwin->screen_ptr->frame_border_width;
1614 XChangeProperty(dpy, wwin->client_win, net_frame_extents, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) extents, 4);
1617 void wNETCleanupFrameExtents(WWindow *wwin)
1619 XDeleteProperty(dpy, wwin->client_win, net_frame_extents);