Change to the linux kernel coding style
[wmaker-crm.git] / src / wmspec.c
Commit [+]AuthorDateLineData
abc09951 kojima2001-09-12 21:43:42 +00001/* wmspec.c-- support for the wm-spec Hints
9aca0d5f dan2004-10-12 01:34:32 +00002 *
abc09951 kojima2001-09-12 21:43:42 +00003 * Window Maker window manager
9aca0d5f dan2004-10-12 01:34:32 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1998-2003 Alfredo K. Kojima
9aca0d5f dan2004-10-12 01:34:32 +00006 *
abc09951 kojima2001-09-12 21:43:42 +00007 * 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.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
9aca0d5f dan2004-10-12 01:34:32 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
abc09951 kojima2001-09-12 21:43:42 +000020 * USA.
21 */
22
140c48f1 dan2005-01-08 22:53:39 +000023/*
24 * TODO
25 * ----
26 *
27 * This file needs to be checked for all calls to XGetWindowProperty() and
28 * proper checks need to be made on the returned values. Only checking for
29 * return to be Success is not enough. -Dan
30 */
abc09951 kojima2001-09-12 21:43:42 +000031
32#include "wconfig.h"
33
9aca0d5f dan2004-10-12 01:34:32 +000034#ifdef NETWM_HINTS
35
abc09951 kojima2001-09-12 21:43:42 +000036#include <X11/Xlib.h>
37#include <X11/Xatom.h>
fa7a5db4 dan2006-01-22 18:05:00 +000038#include <X11/Xarch.h>
9aca0d5f dan2004-10-12 01:34:32 +000039#include <string.h>
abc09951 kojima2001-09-12 21:43:42 +000040
41#include "WindowMaker.h"
42#include "window.h"
43#include "screen.h"
9aca0d5f dan2004-10-12 01:34:32 +000044#include "workspace.h"
abc09951 kojima2001-09-12 21:43:42 +000045#include "framewin.h"
46#include "actions.h"
9aca0d5f dan2004-10-12 01:34:32 +000047#include "client.h"
48#include "wmspec.h"
49#include "icon.h"
50#include "stacking.h"
51#include "xinerama.h"
879b00a5 kojima2004-10-13 05:09:08 +000052#include "properties.h"
abc09951 kojima2001-09-12 21:43:42 +000053
9aca0d5f dan2004-10-12 01:34:32 +000054#ifdef DEBUG_WMSPEC
55#include <stdio.h>
56#endif
57
58/* Global variables */
59extern Atom _XA_WM_DELETE_WINDOW;
60extern Time LastTimestamp;
61extern WPreferences wPreferences;
62
63/* Root Window Properties */
abc09951 kojima2001-09-12 21:43:42 +000064static Atom net_supported;
65static Atom net_client_list;
66static Atom net_client_list_stacking;
67static Atom net_number_of_desktops;
68static Atom net_desktop_geometry;
69static Atom net_desktop_viewport;
70static Atom net_current_desktop;
9aca0d5f dan2004-10-12 01:34:32 +000071static Atom net_desktop_names;
abc09951 kojima2001-09-12 21:43:42 +000072static Atom net_active_window;
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020073static Atom net_workarea; /* XXX: not xinerama compatible */
abc09951 kojima2001-09-12 21:43:42 +000074static Atom net_supporting_wm_check;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020075static Atom net_virtual_roots; /* N/A */
76static Atom net_desktop_layout; /* XXX */
9aca0d5f dan2004-10-12 01:34:32 +000077static Atom net_showing_desktop;
78
79/* Other Root Window Messages */
80static Atom net_close_window;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020081static Atom net_moveresize_window; /* TODO */
82static Atom net_wm_moveresize; /* TODO */
9aca0d5f dan2004-10-12 01:34:32 +000083
84/* Application Window Properties */
70555308 kojima2004-10-16 22:05:04 +000085static Atom net_wm_name;
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020086static Atom net_wm_visible_name; /* TODO (unnecessary?) */
70555308 kojima2004-10-16 22:05:04 +000087static Atom net_wm_icon_name;
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020088static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
abc09951 kojima2001-09-12 21:43:42 +000089static Atom net_wm_desktop;
9aca0d5f dan2004-10-12 01:34:32 +000090static Atom net_wm_window_type;
91static Atom net_wm_window_type_desktop;
92static Atom net_wm_window_type_dock;
93static Atom net_wm_window_type_toolbar;
94static Atom net_wm_window_type_menu;
95static Atom net_wm_window_type_utility;
96static Atom net_wm_window_type_splash;
97static Atom net_wm_window_type_dialog;
98static Atom net_wm_window_type_normal;
abc09951 kojima2001-09-12 21:43:42 +000099static Atom net_wm_state;
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200100static Atom net_wm_state_modal; /* XXX: what is this?!? */
abc09951 kojima2001-09-12 21:43:42 +0000101static Atom net_wm_state_sticky;
102static Atom net_wm_state_maximized_vert;
103static Atom net_wm_state_maximized_horz;
104static Atom net_wm_state_shaded;
105static Atom net_wm_state_skip_taskbar;
106static Atom net_wm_state_skip_pager;
9aca0d5f dan2004-10-12 01:34:32 +0000107static Atom net_wm_state_hidden;
108static Atom net_wm_state_fullscreen;
109static Atom net_wm_state_above;
110static Atom net_wm_state_below;
111static Atom net_wm_allowed_actions;
112static Atom net_wm_action_move;
113static Atom net_wm_action_resize;
114static Atom net_wm_action_minimize;
115static Atom net_wm_action_shade;
116static Atom net_wm_action_stick;
117static Atom net_wm_action_maximize_horz;
118static Atom net_wm_action_maximize_vert;
119static Atom net_wm_action_fullscreen;
120static Atom net_wm_action_change_desktop;
121static Atom net_wm_action_close;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200122static Atom net_wm_strut; /* XXX: see net_workarea */
123static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
124static 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. */
9aca0d5f dan2004-10-12 01:34:32 +0000125static Atom net_wm_icon;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200126static Atom net_wm_pid; /* TODO */
127static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
9aca0d5f dan2004-10-12 01:34:32 +0000128
129/* Window Manager Protocols */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200130static Atom net_wm_ping; /* TODO */
9aca0d5f dan2004-10-12 01:34:32 +0000131
132static Atom utf8_string;
133
134typedef struct {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200135 char *name;
136 Atom *atom;
9aca0d5f dan2004-10-12 01:34:32 +0000137} atomitem_t;
138
139static atomitem_t atomNames[] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200140 {"_NET_SUPPORTED", &net_supported},
141 {"_NET_CLIENT_LIST", &net_client_list},
142 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
143 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
144 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
145 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
146 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
147 {"_NET_DESKTOP_NAMES", &net_desktop_names},
148 {"_NET_ACTIVE_WINDOW", &net_active_window},
149 {"_NET_WORKAREA", &net_workarea},
150 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
151 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
152 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
153 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
154
155 {"_NET_CLOSE_WINDOW", &net_close_window},
156 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
157 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
158
159 {"_NET_WM_NAME", &net_wm_name},
160 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
161 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
162 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
163 {"_NET_WM_DESKTOP", &net_wm_desktop},
164 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
165 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
166 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
167 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
168 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
169 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
170 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
171 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
172 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
173 {"_NET_WM_STATE", &net_wm_state},
174 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
175 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
176 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
177 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
178 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
179 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
180 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
181 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
182 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
183 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
184 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
185 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
186 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
187 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
188 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
189 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
190 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
191 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
192 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
193 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
194 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
195 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
196 {"_NET_WM_STRUT", &net_wm_strut},
197 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
198 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
199 {"_NET_WM_ICON", &net_wm_icon},
200 {"_NET_WM_PID", &net_wm_pid},
201 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
202
203 {"_NET_WM_PING", &net_wm_ping},
204
205 {"UTF8_STRING", &utf8_string},
9aca0d5f dan2004-10-12 01:34:32 +0000206};
abc09951 kojima2001-09-12 21:43:42 +0000207
9aca0d5f dan2004-10-12 01:34:32 +0000208#define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
abc09951 kojima2001-09-12 21:43:42 +0000209
9aca0d5f dan2004-10-12 01:34:32 +0000210#define _NET_WM_STATE_REMOVE 0
211#define _NET_WM_STATE_ADD 1
212#define _NET_WM_STATE_TOGGLE 2
abc09951 kojima2001-09-12 21:43:42 +0000213
9aca0d5f dan2004-10-12 01:34:32 +0000214#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
215#define _NET_WM_MOVERESIZE_SIZE_TOP 1
216#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
217#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
218#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
219#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
220#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
221#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200222#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
223#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
224#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
abc09951 kojima2001-09-12 21:43:42 +0000225
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200226static void observer(void *self, WMNotification * notif);
227static void wsobserver(void *self, WMNotification * notif);
abc09951 kojima2001-09-12 21:43:42 +0000228
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200229static void updateClientList(WScreen * scr);
230static void updateClientListStacking(WScreen * scr, WWindow *);
9aca0d5f dan2004-10-12 01:34:32 +0000231
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200232static void updateWorkspaceNames(WScreen * scr);
233static void updateCurrentWorkspace(WScreen * scr);
234static void updateWorkspaceCount(WScreen * scr);
abc09951 kojima2001-09-12 21:43:42 +0000235
236typedef struct NetData {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200237 WScreen *scr;
238 WReservedArea *strut;
239 WWindow **show_desktop;
abc09951 kojima2001-09-12 21:43:42 +0000240} NetData;
241
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200242static void setSupportedHints(WScreen * scr)
abc09951 kojima2001-09-12 21:43:42 +0000243{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200244 Atom atom[atomNr];
245 int i = 0;
246
247 /* set supported hints list */
248 /* XXX: extend this !!! */
249
250 atom[i++] = net_client_list;
251 atom[i++] = net_client_list_stacking;
252 atom[i++] = net_number_of_desktops;
253 atom[i++] = net_desktop_geometry;
254 atom[i++] = net_desktop_viewport;
255 atom[i++] = net_current_desktop;
256 atom[i++] = net_desktop_names;
257 atom[i++] = net_active_window;
258 atom[i++] = net_workarea;
259 atom[i++] = net_supporting_wm_check;
260 atom[i++] = net_showing_desktop;
9aca0d5f dan2004-10-12 01:34:32 +0000261#if 0
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200262 atom[i++] = net_wm_moveresize;
9aca0d5f dan2004-10-12 01:34:32 +0000263#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200264 atom[i++] = net_wm_desktop;
265 atom[i++] = net_wm_window_type;
266 atom[i++] = net_wm_window_type_desktop;
267 atom[i++] = net_wm_window_type_dock;
268 atom[i++] = net_wm_window_type_toolbar;
269 atom[i++] = net_wm_window_type_menu;
270 atom[i++] = net_wm_window_type_utility;
271 atom[i++] = net_wm_window_type_splash;
272 atom[i++] = net_wm_window_type_dialog;
273 atom[i++] = net_wm_window_type_normal;
274
275 atom[i++] = net_wm_state;
276 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
277 atom[i++] = net_wm_state_sticky;
278 atom[i++] = net_wm_state_shaded;
279 atom[i++] = net_wm_state_maximized_horz;
280 atom[i++] = net_wm_state_maximized_vert;
281 atom[i++] = net_wm_state_skip_taskbar;
282 atom[i++] = net_wm_state_skip_pager;
283 atom[i++] = net_wm_state_hidden;
284 atom[i++] = net_wm_state_fullscreen;
285 atom[i++] = net_wm_state_above;
286 atom[i++] = net_wm_state_below;
287
288 atom[i++] = net_wm_allowed_actions;
289 atom[i++] = net_wm_action_move;
290 atom[i++] = net_wm_action_resize;
291 atom[i++] = net_wm_action_minimize;
292 atom[i++] = net_wm_action_shade;
293 atom[i++] = net_wm_action_stick;
294 atom[i++] = net_wm_action_maximize_horz;
295 atom[i++] = net_wm_action_maximize_vert;
296 atom[i++] = net_wm_action_fullscreen;
297 atom[i++] = net_wm_action_change_desktop;
298 atom[i++] = net_wm_action_close;
299
300 atom[i++] = net_wm_strut;
301 atom[i++] = net_wm_icon_geometry;
302 atom[i++] = net_wm_icon;
303 atom[i++] = net_wm_handled_icons;
304
305 atom[i++] = net_wm_name;
306 atom[i++] = net_wm_icon_name;
307
308 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
309
310 /* set supporting wm hint */
311 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
312 PropModeReplace, (unsigned char *)&scr->info_window, 1);
313
314 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
315 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
9aca0d5f dan2004-10-12 01:34:32 +0000316}
317
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200318void wNETWMUpdateDesktop(WScreen * scr)
9aca0d5f dan2004-10-12 01:34:32 +0000319{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200320 long *views, sizes[2];
321 int count, i;
9aca0d5f dan2004-10-12 01:34:32 +0000322
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200323 if (scr->workspace_count == 0)
324 return;
9aca0d5f dan2004-10-12 01:34:32 +0000325
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200326 count = scr->workspace_count * 2;
327 views = wmalloc(sizeof(long) * count);
328 /*memset(views, 0, sizeof(long) * count); */
9aca0d5f dan2004-10-12 01:34:32 +0000329
330#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200331 sizes[0] = scr->workspaces[scr->current_workspace]->width;
332 sizes[1] = scr->workspaces[scr->current_workspace]->height;
9aca0d5f dan2004-10-12 01:34:32 +0000333#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200334 sizes[0] = scr->scr_width;
335 sizes[1] = scr->scr_height;
9aca0d5f dan2004-10-12 01:34:32 +0000336#endif
337
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200338 for (i = 0; i < scr->workspace_count; i++) {
9aca0d5f dan2004-10-12 01:34:32 +0000339#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200340 views[2 * i + 0] = scr->workspaces[i]->view_x;
341 views[2 * i + 1] = scr->workspaces[i]->view_y;
9aca0d5f dan2004-10-12 01:34:32 +0000342#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200343 views[2 * i + 0] = 0;
344 views[2 * i + 1] = 0;
9aca0d5f dan2004-10-12 01:34:32 +0000345#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200346 }
9aca0d5f dan2004-10-12 01:34:32 +0000347
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200348 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
349 PropModeReplace, (unsigned char *)sizes, 2);
9aca0d5f dan2004-10-12 01:34:32 +0000350
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200351 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
352 PropModeReplace, (unsigned char *)views, count);
9aca0d5f dan2004-10-12 01:34:32 +0000353
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200354 wfree(views);
9aca0d5f dan2004-10-12 01:34:32 +0000355}
356
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200357int wNETWMGetCurrentDesktopFromHint(WScreen * scr)
879b00a5 kojima2004-10-13 05:09:08 +0000358{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200359 int count;
360 unsigned char *prop;
361
362 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
363 if (prop) {
364 int desktop = *(long *)prop;
365 XFree(prop);
366 return desktop;
367 }
368 return -1;
879b00a5 kojima2004-10-13 05:09:08 +0000369}
370
140c48f1 dan2005-01-08 22:53:39 +0000371/*
372 * Find the best icon to be used by Window Maker for appicon/miniwindows.
373 * Currently the algorithm is to take the image with the size closest
374 * to icon_size x icon_size, but never bigger than that.
375 *
376 * This algorithm is very poorly implemented and needs to be redone (it can
377 * easily select images with very large widths and very small heights over
378 * square images, if the area of the former is closer to the desired one).
379 *
380 * The logic can also be changed to accept bigger images and scale them down.
381 */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200382static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
140c48f1 dan2005-01-08 22:53:39 +0000383{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200384 int size, wanted, d;
385 unsigned long i, distance;
386 unsigned long *icon;
387
388 /* better use only 75% of icon_size. For 64x64 this means 48x48
389 * This leaves room around the icon for the miniwindow title and
390 * results in better overall aesthetics -Dan */
391 wanted = wPreferences.icon_size * wPreferences.icon_size;
392
393 for (icon = NULL, distance = LONG_MAX, i = 0L; i < items - 1;) {
394 size = data[i] * data[i + 1];
395 if (size == 0)
396 break;
397 d = wanted - size;
398 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
399 distance = d;
400 icon = &data[i];
401 }
402 i += size + 2;
403 }
404
405 return icon;
140c48f1 dan2005-01-08 22:53:39 +0000406}
407
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200408static RImage *makeRImageFromARGBData(unsigned long *data)
140c48f1 dan2005-01-08 22:53:39 +0000409{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200410 int size, width, height, i;
411 RImage *image;
412 unsigned char *imgdata;
413 unsigned long pixel;
140c48f1 dan2005-01-08 22:53:39 +0000414
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200415 width = data[0];
416 height = data[1];
417 size = width * height;
140c48f1 dan2005-01-08 22:53:39 +0000418
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200419 if (size == 0)
420 return NULL;
140c48f1 dan2005-01-08 22:53:39 +0000421
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200422 image = RCreateImage(width, height, True);
140c48f1 dan2005-01-08 22:53:39 +0000423
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200424 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
425 pixel = data[i];
fa7a5db4 dan2006-01-22 18:05:00 +0000426#if BYTE_ORDER == BIG_ENDIAN
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200427 imgdata[2] = (pixel >> 24) & 0xff; /* A */
428 imgdata[1] = (pixel >> 16) & 0xff; /* R */
429 imgdata[0] = (pixel >> 8) & 0xff; /* G */
430 imgdata[3] = (pixel >> 0) & 0xff; /* B */
431#else /* Little endian */
432 imgdata[3] = (pixel >> 24) & 0xff; /* A */
433 imgdata[0] = (pixel >> 16) & 0xff; /* R */
434 imgdata[1] = (pixel >> 8) & 0xff; /* G */
435 imgdata[2] = (pixel >> 0) & 0xff; /* B */
436#endif /* endianness */
437
438 }
439
440 return image;
140c48f1 dan2005-01-08 22:53:39 +0000441}
442
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200443static void updateIconImage(WScreen * scr, WWindow * wwin)
9aca0d5f dan2004-10-12 01:34:32 +0000444{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200445 unsigned long *property, *data;
446 unsigned long items, rest;
447 Atom type;
448 int format;
449 RImage *image;
450
451 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
452 False, XA_CARDINAL, &type, &format, &items, &rest,
453 (unsigned char **)&property) != Success || !property) {
454 return;
455 }
456
457 if (type != XA_CARDINAL || format != 32 || items < 2) {
458 XFree(property);
459 return;
460 }
461
462 data = findBestIcon(property, items);
463 if (!data) {
464 XFree(property);
465 return;
466 }
467
468 image = makeRImageFromARGBData(data);
469
470 if (image) {
471 if (wwin->net_icon_image)
472 RReleaseImage(wwin->net_icon_image);
473 wwin->net_icon_image = image;
474 }
475
476 XFree(property);
9aca0d5f dan2004-10-12 01:34:32 +0000477}
abc09951 kojima2001-09-12 21:43:42 +0000478
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200479static void updateShowDesktop(WScreen * scr, Bool show)
9aca0d5f dan2004-10-12 01:34:32 +0000480{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200481 long foo;
abc09951 kojima2001-09-12 21:43:42 +0000482
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200483 foo = (show == True);
484 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
485 PropModeReplace, (unsigned char *)&foo, 1);
abc09951 kojima2001-09-12 21:43:42 +0000486}
487
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200488void wNETWMShowingDesktop(WScreen * scr, Bool show)
9aca0d5f dan2004-10-12 01:34:32 +0000489{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200490 if (show && scr->netdata->show_desktop == NULL) {
491 WWindow *tmp, **wins;
492 int i = 0;
493
494 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
495
496 tmp = scr->focused_window;
497 while (tmp) {
498 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
499
500 wins[i++] = tmp;
501 tmp->flags.skip_next_animation = 1;
502 tmp->flags.net_show_desktop = 1;
503 wIconifyWindow(tmp);
504 }
505
506 tmp = tmp->prev;
507 }
508 wins[i++] = NULL;
509
510 scr->netdata->show_desktop = wins;
511 updateShowDesktop(scr, True);
512 } else if (scr->netdata->show_desktop != NULL) {
513 /* FIXME: get rid of workspace flashing ! */
514 int ws = scr->current_workspace;
515 WWindow **tmp;
516 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
517 wDeiconifyWindow(*tmp);
518 (*tmp)->flags.net_show_desktop = 0;
519 }
520 if (ws != scr->current_workspace)
521 wWorkspaceChange(scr, ws);
522 wfree(scr->netdata->show_desktop);
523 scr->netdata->show_desktop = NULL;
524 updateShowDesktop(scr, False);
525 }
9aca0d5f dan2004-10-12 01:34:32 +0000526}
527
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200528void wNETWMInitStuff(WScreen * scr)
abc09951 kojima2001-09-12 21:43:42 +0000529{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200530 NetData *data;
531 int i;
9aca0d5f dan2004-10-12 01:34:32 +0000532
533#ifdef DEBUG_WMSPEC
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200534 printf("wNETWMInitStuff\n");
9aca0d5f dan2004-10-12 01:34:32 +0000535#endif
abc09951 kojima2001-09-12 21:43:42 +0000536
537#ifdef HAVE_XINTERNATOMS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200538 {
539 Atom atoms[atomNr];
540 char *names[atomNr];
541
542 for (i = 0; i < atomNr; ++i) {
543 names[i] = atomNames[i].name;
544 }
545 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
546 for (i = 0; i < atomNr; ++i) {
547 *atomNames[i].atom = atoms[i];
548 }
549 }
9aca0d5f dan2004-10-12 01:34:32 +0000550#else
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200551 for (i = 0; i < atomNr; i++) {
552 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
553 }
abc09951 kojima2001-09-12 21:43:42 +0000554#endif
9aca0d5f dan2004-10-12 01:34:32 +0000555
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200556 data = wmalloc(sizeof(NetData));
557 data->scr = scr;
558 data->strut = NULL;
559 data->show_desktop = NULL;
9aca0d5f dan2004-10-12 01:34:32 +0000560
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200561 scr->netdata = data;
9aca0d5f dan2004-10-12 01:34:32 +0000562
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200563 setSupportedHints(scr);
abc09951 kojima2001-09-12 21:43:42 +0000564
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200565 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
566 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
567 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
568 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
569 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
570 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
571 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
abc09951 kojima2001-09-12 21:43:42 +0000572
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200573 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
574 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
575 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
576 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
9aca0d5f dan2004-10-12 01:34:32 +0000577
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200578 updateClientList(scr);
579 updateClientListStacking(scr, NULL);
580 updateWorkspaceCount(scr);
581 updateWorkspaceNames(scr);
582 updateShowDesktop(scr, False);
9aca0d5f dan2004-10-12 01:34:32 +0000583
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200584 wScreenUpdateUsableArea(scr);
abc09951 kojima2001-09-12 21:43:42 +0000585}
586
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200587void wNETWMCleanup(WScreen * scr)
70555308 kojima2004-10-16 22:05:04 +0000588{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200589 int i;
70555308 kojima2004-10-16 22:05:04 +0000590
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200591 for (i = 0; i < atomNr; i++)
592 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
70555308 kojima2004-10-16 22:05:04 +0000593}
594
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200595void wNETWMUpdateActions(WWindow * wwin, Bool del)
abc09951 kojima2001-09-12 21:43:42 +0000596{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200597 Atom action[10]; /* nr of actions atoms defined */
598 int i = 0;
9aca0d5f dan2004-10-12 01:34:32 +0000599
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200600 if (del) {
601 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
602 return;
603 }
9aca0d5f dan2004-10-12 01:34:32 +0000604
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200605 if (IS_MOVABLE(wwin))
606 action[i++] = net_wm_action_move;
9aca0d5f dan2004-10-12 01:34:32 +0000607
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200608 if (IS_RESIZABLE(wwin))
609 action[i++] = net_wm_action_resize;
9aca0d5f dan2004-10-12 01:34:32 +0000610
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200611 if (!WFLAGP(wwin, no_miniaturizable))
612 action[i++] = net_wm_action_minimize;
9aca0d5f dan2004-10-12 01:34:32 +0000613
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200614 if (!WFLAGP(wwin, no_shadeable))
615 action[i++] = net_wm_action_shade;
9aca0d5f dan2004-10-12 01:34:32 +0000616
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200617 /* if (!WFLAGP(wwin, no_stickable)) */
618 action[i++] = net_wm_action_stick;
9aca0d5f dan2004-10-12 01:34:32 +0000619
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200620 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
621 if (IS_RESIZABLE(wwin))
622 action[i++] = net_wm_action_maximize_horz;
9aca0d5f dan2004-10-12 01:34:32 +0000623
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200624 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
625 if (IS_RESIZABLE(wwin))
626 action[i++] = net_wm_action_maximize_vert;
9aca0d5f dan2004-10-12 01:34:32 +0000627
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200628 /* if (!WFLAGP(wwin, no_fullscreen)) */
629 action[i++] = net_wm_action_fullscreen;
9aca0d5f dan2004-10-12 01:34:32 +0000630
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200631 /* if (!WFLAGP(wwin, no_change_desktop)) */
632 action[i++] = net_wm_action_change_desktop;
9aca0d5f dan2004-10-12 01:34:32 +0000633
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200634 if (!WFLAGP(wwin, no_closable))
635 action[i++] = net_wm_action_close;
9aca0d5f dan2004-10-12 01:34:32 +0000636
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200637 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
638 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
9aca0d5f dan2004-10-12 01:34:32 +0000639}
640
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200641void wNETWMUpdateWorkarea(WScreen * scr, WArea usableArea)
9aca0d5f dan2004-10-12 01:34:32 +0000642{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200643 long *area;
644 int count, i;
9aca0d5f dan2004-10-12 01:34:32 +0000645
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200646 /* XXX: not Xinerama compatible,
647 xinerama gets the largest available */
9aca0d5f dan2004-10-12 01:34:32 +0000648
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200649 if (!scr->netdata || scr->workspace_count == 0)
650 return;
9aca0d5f dan2004-10-12 01:34:32 +0000651
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200652 count = scr->workspace_count * 4;
653 area = wmalloc(sizeof(long) * count);
654 for (i = 0; i < scr->workspace_count; i++) {
655 area[4 * i + 0] = usableArea.x1;
656 area[4 * i + 1] = usableArea.y1;
657 area[4 * i + 2] = usableArea.x2 - usableArea.x1;
658 area[4 * i + 3] = usableArea.y2 - usableArea.y1;
659 }
abc09951 kojima2001-09-12 21:43:42 +0000660
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200661 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
662 PropModeReplace, (unsigned char *)area, count);
9aca0d5f dan2004-10-12 01:34:32 +0000663
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200664 wfree(area);
abc09951 kojima2001-09-12 21:43:42 +0000665}
666
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200667Bool wNETWMGetUsableArea(WScreen * scr, int head, WArea * area)
9aca0d5f dan2004-10-12 01:34:32 +0000668{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200669 WReservedArea *cur;
670 WMRect rect;
671
672 if (!scr->netdata || !scr->netdata->strut)
673 return False;
674
675 area->x1 = area->y1 = area->x2 = area->y2 = 0;
676
677 for (cur = scr->netdata->strut; cur; cur = cur->next) {
678 WWindow *wwin = wWindowFor(cur->window);
679 if (wWindowTouchesHead(wwin, head)) {
680 if (cur->area.x1 > area->x1)
681 area->x1 = cur->area.x1;
682 if (cur->area.y1 > area->y1)
683 area->y1 = cur->area.y1;
684 if (cur->area.x2 > area->x2)
685 area->x2 = cur->area.x2;
686 if (cur->area.y2 > area->y2)
687 area->y2 = cur->area.y2;
688 }
689 }
690
691 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
692 return False;
693
694 rect = wGetRectForHead(scr, head);
695
696 area->x1 = rect.pos.x + area->x1;
697 area->x2 = rect.pos.x + rect.size.width - area->x2;
698 area->y1 = rect.pos.y + area->y1;
699 area->y2 = rect.pos.y + rect.size.height - area->y2;
700
701 return True;
9aca0d5f dan2004-10-12 01:34:32 +0000702}
703
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200704static void updateClientList(WScreen * scr)
abc09951 kojima2001-09-12 21:43:42 +0000705{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200706 WWindow *wwin;
707 Window *windows;
708 int count;
709
710 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
711
712 count = 0;
713 wwin = scr->focused_window;
714 while (wwin) {
715 windows[count++] = wwin->client_win;
716 wwin = wwin->prev;
717 }
718 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
719 PropModeReplace, (unsigned char *)windows, count);
720
721 wfree(windows);
722 XFlush(dpy);
abc09951 kojima2001-09-12 21:43:42 +0000723}
724
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200725static void updateClientListStacking(WScreen * scr, WWindow * wwin_excl)
abc09951 kojima2001-09-12 21:43:42 +0000726{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200727 WWindow *wwin;
728 Window *client_list;
729 Window *client_list_reverse;
730 int client_count;
731 WCoreWindow *tmp;
732 WMBagIterator iter;
733 int i;
734
735 /* update client list */
736 i = scr->window_count + 1;
737 client_list = (Window *) wmalloc(sizeof(Window) * i);
738 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
739
740 client_count = 0;
741 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
742 while (tmp) {
743 wwin = wWindowFor(tmp->window);
744 /* wwin_excl is a window to exclude from the list
745 (e.g. it's now unmanaged) */
746 if (wwin && (wwin != wwin_excl))
747 client_list[client_count++] = wwin->client_win;
748 tmp = tmp->stacking->under;
749 }
750 }
751
752 for (i = 0; i < client_count; i++) {
753 Window w = client_list[client_count - i - 1];
754 client_list_reverse[i] = w;
755 }
756
757 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
758 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
759
760 wfree(client_list);
761 wfree(client_list_reverse);
762
763 XFlush(dpy);
abc09951 kojima2001-09-12 21:43:42 +0000764}
765
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200766static void updateWorkspaceCount(WScreen * scr)
767{ /* changeable */
768 long count;
abc09951 kojima2001-09-12 21:43:42 +0000769
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200770 count = scr->workspace_count;
9aca0d5f dan2004-10-12 01:34:32 +0000771
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200772 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
773 32, PropModeReplace, (unsigned char *)&count, 1);
abc09951 kojima2001-09-12 21:43:42 +0000774}
775
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200776static void updateCurrentWorkspace(WScreen * scr)
777{ /* changeable */
778 long count;
abc09951 kojima2001-09-12 21:43:42 +0000779
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200780 count = scr->current_workspace;
9aca0d5f dan2004-10-12 01:34:32 +0000781
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200782 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
783 PropModeReplace, (unsigned char *)&count, 1);
abc09951 kojima2001-09-12 21:43:42 +0000784}
785
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200786static void updateWorkspaceNames(WScreen * scr)
abc09951 kojima2001-09-12 21:43:42 +0000787{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200788 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
789 unsigned int i, len, curr_size;
790
791 pos = buf;
792 len = 0;
793 for (i = 0; i < scr->workspace_count; i++) {
794 curr_size = strlen(scr->workspaces[i]->name);
795 strcpy(pos, scr->workspaces[i]->name);
796 pos += (curr_size + 1);
797 len += (curr_size + 1);
798 }
799
800 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
801 PropModeReplace, (unsigned char *)buf, len);
abc09951 kojima2001-09-12 21:43:42 +0000802}
803
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200804static void updateFocusHint(WScreen * scr, WWindow * wwin)
805{ /* changeable */
806 Window window;
abc09951 kojima2001-09-12 21:43:42 +0000807
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200808 if (!scr->focused_window || !scr->focused_window->flags.focused)
809 window = None;
810 else
811 window = scr->focused_window->client_win;
9aca0d5f dan2004-10-12 01:34:32 +0000812
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200813 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
814 PropModeReplace, (unsigned char *)&window, 1);
abc09951 kojima2001-09-12 21:43:42 +0000815}
816
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200817static void updateWorkspaceHint(WWindow * wwin, Bool fake, Bool del)
abc09951 kojima2001-09-12 21:43:42 +0000818{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200819 long l;
820
821 if (del) {
822 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
823 } else {
824 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
825 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
826 32, PropModeReplace, (unsigned char *)&l, 1);
827 }
abc09951 kojima2001-09-12 21:43:42 +0000828}
829
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200830static void updateStateHint(WWindow * wwin, Bool changedWorkspace, Bool del)
831{ /* changeable */
832 if (del) {
833 if (!wwin->flags.net_state_from_client) {
834 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
835 }
836 } else {
837 Atom state[15]; /* nr of defined state atoms */
838 int i = 0;
839
840 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
841 updateWorkspaceHint(wwin, False, False);
842
843 if (IS_OMNIPRESENT(wwin))
844 state[i++] = net_wm_state_sticky;
845 if (wwin->flags.shaded)
846 state[i++] = net_wm_state_shaded;
847 if (wwin->flags.maximized & MAX_HORIZONTAL)
848 state[i++] = net_wm_state_maximized_horz;
849 if (wwin->flags.maximized & MAX_VERTICAL)
850 state[i++] = net_wm_state_maximized_vert;
851 if (WFLAGP(wwin, skip_window_list))
852 state[i++] = net_wm_state_skip_taskbar;
853 if (wwin->flags.net_skip_pager)
854 state[i++] = net_wm_state_skip_pager;
855 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
856 state[i++] = net_wm_state_hidden;
857 state[i++] = net_wm_state_skip_pager;
858
859 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
860 if (!IS_OMNIPRESENT(wwin))
861 updateWorkspaceHint(wwin, True, False);
862 state[i++] = net_wm_state_sticky;
863 }
864 }
865 if (WFLAGP(wwin, sunken))
866 state[i++] = net_wm_state_below;
867 if (WFLAGP(wwin, floating))
868 state[i++] = net_wm_state_above;
869 if (wwin->flags.fullscreen)
870 state[i++] = net_wm_state_fullscreen;
871
872 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
873 PropModeReplace, (unsigned char *)state, i);
874 }
abc09951 kojima2001-09-12 21:43:42 +0000875}
876
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200877static Bool updateStrut(WWindow * wwin, Bool adding)
abc09951 kojima2001-09-12 21:43:42 +0000878{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200879 WScreen *scr;
880 WReservedArea *area;
881 Bool hasState = False;
9aca0d5f dan2004-10-12 01:34:32 +0000882
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200883 scr = wwin->screen_ptr;
9aca0d5f dan2004-10-12 01:34:32 +0000884
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200885 if (adding) {
886 Atom type_ret;
887 int fmt_ret;
888 unsigned long nitems_ret;
889 unsigned long bytes_after_ret;
890 long *data = 0;
9aca0d5f dan2004-10-12 01:34:32 +0000891
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200892 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
893 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
894 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
9aca0d5f dan2004-10-12 01:34:32 +0000895
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200896 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
897 area->area.x1 = data[0];
898 area->area.x2 = data[1];
899 area->area.y1 = data[2];
900 area->area.y2 = data[3];
9aca0d5f dan2004-10-12 01:34:32 +0000901
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200902 area->window = wwin->client_win;
9aca0d5f dan2004-10-12 01:34:32 +0000903
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200904 area->next = scr->netdata->strut;
905 scr->netdata->strut = area;
9aca0d5f dan2004-10-12 01:34:32 +0000906
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200907 XFree(data);
908 hasState = True;
9aca0d5f dan2004-10-12 01:34:32 +0000909#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200910 /* just in case wm_window_type didn't set it already */
911 wwin->client_flags.virtual_stick = 1;
9aca0d5f dan2004-10-12 01:34:32 +0000912#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200913 }
914
915 } else {
916 /* deleting */
917 area = scr->netdata->strut;
918
919 if (area) {
920 if (area->window == wwin->client_win) {
921 scr->netdata->strut = area->next;
922 wfree(area);
923 hasState = True;
924 } else {
925 while (area->next && area->next->window != wwin->client_win)
926 area = area->next;
927
928 if (area->next) {
929 WReservedArea *next;
930
931 next = area->next->next;
932 wfree(area->next);
933 area->next = next;
934
935 hasState = True;
936 }
937 }
938 }
939 }
940
941 return hasState;
9aca0d5f dan2004-10-12 01:34:32 +0000942}
943
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200944static int getWindowLayer(WWindow * wwin)
9aca0d5f dan2004-10-12 01:34:32 +0000945{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200946 int layer = WMNormalLevel;
947
948 if (wwin->type == net_wm_window_type_desktop) {
949 layer = WMDesktopLevel;
950 } else if (wwin->type == net_wm_window_type_dock) {
951 layer = WMDockLevel;
952 } else if (wwin->type == net_wm_window_type_toolbar) {
953 layer = WMMainMenuLevel;
954 } else if (wwin->type == net_wm_window_type_menu) {
955 layer = WMSubmenuLevel;
956 } else if (wwin->type == net_wm_window_type_utility) {
957 } else if (wwin->type == net_wm_window_type_splash) {
958 } else if (wwin->type == net_wm_window_type_dialog) {
959 if (wwin->transient_for) {
960 WWindow *parent = wWindowFor(wwin->transient_for);
961 if (parent && parent->flags.fullscreen) {
962 layer = WMFullscreenLevel;
963 }
964 }
965 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
966 } else if (wwin->type == net_wm_window_type_normal) {
967 }
968
969 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
970 layer = WMSunkenLevel;
971 if (wwin->client_flags.floating && WMFloatingLevel > layer)
972 layer = WMFloatingLevel;
973
974 return layer;
9aca0d5f dan2004-10-12 01:34:32 +0000975}
976
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200977static void doStateAtom(WWindow * wwin, Atom state, int set, Bool init)
9aca0d5f dan2004-10-12 01:34:32 +0000978{
979
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200980 if (state == net_wm_state_sticky) {
981 if (set == _NET_WM_STATE_TOGGLE) {
982 set = !IS_OMNIPRESENT(wwin);
983 }
984 if (set != wwin->flags.omnipresent) {
985 wWindowSetOmnipresent(wwin, set);
986 }
987 } else if (state == net_wm_state_shaded) {
988 if (set == _NET_WM_STATE_TOGGLE) {
989 set = !wwin->flags.shaded;
990 }
991 if (init) {
992 wwin->flags.shaded = set;
993 } else {
994 if (set) {
995 wShadeWindow(wwin);
996 } else {
997 wUnshadeWindow(wwin);
998 }
999 }
1000 } else if (state == net_wm_state_skip_taskbar) {
1001 if (set == _NET_WM_STATE_TOGGLE) {
1002 set = !wwin->client_flags.skip_window_list;
1003 }
1004 wwin->client_flags.skip_window_list = set;
1005 } else if (state == net_wm_state_skip_pager) {
1006 if (set == _NET_WM_STATE_TOGGLE) {
1007 set = !wwin->flags.net_skip_pager;
1008 }
1009 wwin->flags.net_skip_pager = set;
1010 } else if (state == net_wm_state_maximized_vert) {
1011 if (set == _NET_WM_STATE_TOGGLE) {
1012 set = !(wwin->flags.maximized & MAX_VERTICAL);
1013 }
1014 if (init) {
1015 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
1016 } else {
1017 if (set) {
1018 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1019 } else {
1020 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1021 }
1022 }
1023 } else if (state == net_wm_state_maximized_horz) {
1024 if (set == _NET_WM_STATE_TOGGLE) {
1025 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1026 }
1027 if (init) {
1028 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1029 } else {
1030 if (set) {
1031 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1032 } else {
1033 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1034 }
1035 }
1036 } else if (state == net_wm_state_hidden) {
1037 if (set == _NET_WM_STATE_TOGGLE) {
1038 set = !(wwin->flags.miniaturized);
1039 }
1040 if (init) {
1041 wwin->flags.miniaturized = set;
1042 } else {
1043 if (set) {
1044 wIconifyWindow(wwin);
1045 } else {
1046 wDeiconifyWindow(wwin);
1047 }
1048 }
1049 } else if (state == net_wm_state_fullscreen) {
1050 if (set == _NET_WM_STATE_TOGGLE) {
1051 set = !(wwin->flags.fullscreen);
1052 }
1053 if (init) {
1054 wwin->flags.fullscreen = set;
1055 } else {
1056 if (set) {
1057 wFullscreenWindow(wwin);
1058 } else {
1059 wUnfullscreenWindow(wwin);
1060 }
1061 }
1062 } else if (state == net_wm_state_above) {
1063 if (set == _NET_WM_STATE_TOGGLE) {
1064 set = !(wwin->client_flags.floating);
1065 }
1066 if (init) {
1067 wwin->client_flags.floating = set;
1068 } else {
1069 wwin->client_flags.floating = set;
1070 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1071 }
1072
1073 } else if (state == net_wm_state_below) {
1074 if (set == _NET_WM_STATE_TOGGLE) {
1075 set = !(wwin->client_flags.sunken);
1076 }
1077 if (init) {
1078 wwin->client_flags.sunken = set;
1079 } else {
1080 wwin->client_flags.sunken = set;
1081 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1082 }
1083
1084 } else {
9aca0d5f dan2004-10-12 01:34:32 +00001085#ifdef DEBUG_WMSPEC
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001086 printf("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
9aca0d5f dan2004-10-12 01:34:32 +00001087#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001088 }
abc09951 kojima2001-09-12 21:43:42 +00001089}
1090
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001091static void removeIcon(WWindow * wwin)
9aca0d5f dan2004-10-12 01:34:32 +00001092{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001093 if (wwin->icon == NULL)
1094 return;
1095 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1096 XUnmapWindow(dpy, wwin->icon->core->window);
1097 RemoveFromStackList(wwin->icon->core);
1098 wIconDestroy(wwin->icon);
1099 wwin->icon = NULL;
1100 }
9aca0d5f dan2004-10-12 01:34:32 +00001101}
1102
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001103static Bool handleWindowType(WWindow * wwin, Atom type, int *layer)
9aca0d5f dan2004-10-12 01:34:32 +00001104{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001105 Bool ret = True;
1106
1107 if (type == net_wm_window_type_desktop) {
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_border = 1;
1112 wwin->client_flags.no_resizebar = 1;
1113 wwin->client_flags.no_shadeable = 1;
1114 wwin->client_flags.no_movable = 1;
1115 wwin->client_flags.omnipresent = 1;
1116 wwin->client_flags.skip_window_list = 1;
1117 wwin->client_flags.dont_move_off = 1;
1118 wwin->client_flags.no_appicon = 1;
1119 wwin->flags.net_skip_pager = 1;
1120 wwin->frame_x = 0;
1121 wwin->frame_y = 0;
9aca0d5f dan2004-10-12 01:34:32 +00001122#ifdef VIRTUAL_DESKTOP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001123 wwin->client_flags.virtual_stick = 1;
9aca0d5f dan2004-10-12 01:34:32 +00001124#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001125 } else if (type == net_wm_window_type_dock) {
1126 wwin->client_flags.no_titlebar = 1;
1127 wwin->client_flags.no_resizable = 1;
1128 wwin->client_flags.no_miniaturizable = 1;
1129 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1130 wwin->client_flags.no_resizebar = 1;
1131 wwin->client_flags.no_shadeable = 1;
1132 wwin->client_flags.no_movable = 1;
1133 wwin->client_flags.omnipresent = 1;
1134 wwin->client_flags.skip_window_list = 1;
1135 wwin->client_flags.dont_move_off = 1;
1136 wwin->flags.net_skip_pager = 1;
9aca0d5f dan2004-10-12 01:34:32 +00001137#ifdef VIRTUAL_DESKTOP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001138 wwin->client_flags.virtual_stick = 1;
9aca0d5f dan2004-10-12 01:34:32 +00001139#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001140 } else if (type == net_wm_window_type_toolbar) {
1141 wwin->client_flags.no_titlebar = 1;
1142 wwin->client_flags.no_resizable = 1;
1143 wwin->client_flags.no_miniaturizable = 1;
1144 wwin->client_flags.no_resizebar = 1;
1145 wwin->client_flags.no_shadeable = 1;
1146 wwin->client_flags.skip_window_list = 1;
1147 wwin->client_flags.dont_move_off = 1;
1148 wwin->client_flags.no_appicon = 1;
1149 } else if (type == net_wm_window_type_menu) {
1150 wwin->client_flags.no_titlebar = 1;
1151 wwin->client_flags.no_resizable = 1;
1152 wwin->client_flags.no_miniaturizable = 1;
1153 wwin->client_flags.no_resizebar = 1;
1154 wwin->client_flags.no_shadeable = 1;
1155 wwin->client_flags.skip_window_list = 1;
1156 wwin->client_flags.dont_move_off = 1;
1157 wwin->client_flags.no_appicon = 1;
1158 } else if (type == net_wm_window_type_utility) {
1159 wwin->client_flags.no_appicon = 1;
1160 } else if (type == net_wm_window_type_splash) {
1161 wwin->client_flags.no_titlebar = 1;
1162 wwin->client_flags.no_resizable = 1;
1163 wwin->client_flags.no_miniaturizable = 1;
1164 wwin->client_flags.no_resizebar = 1;
1165 wwin->client_flags.no_shadeable = 1;
1166 wwin->client_flags.no_movable = 1;
1167 wwin->client_flags.skip_window_list = 1;
1168 wwin->client_flags.dont_move_off = 1;
1169 wwin->client_flags.no_appicon = 1;
1170 wwin->flags.net_skip_pager = 1;
1171 } else if (type == net_wm_window_type_dialog) {
1172 /* These also seem a bad idea in our context -Dan
1173 // wwin->client_flags.skip_window_list = 1;
1174 // wwin->client_flags.no_appicon = 1;
1175 */
1176 } else if (type == net_wm_window_type_normal) {
1177 } else {
1178 ret = False;
1179 }
1180
1181 wwin->type = type;
1182 *layer = getWindowLayer(wwin);
1183
1184 return ret;
9aca0d5f dan2004-10-12 01:34:32 +00001185}
1186
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001187void wNETWMPositionSplash(WWindow * wwin, int *x, int *y, int width, int height)
9aca0d5f dan2004-10-12 01:34:32 +00001188{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001189 if (wwin->type == net_wm_window_type_splash) {
1190 WScreen *scr = wwin->screen_ptr;
1191 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1192 *x = rect.pos.x + (rect.size.width - width) / 2;
1193 *y = rect.pos.y + (rect.size.height - height) / 2;
1194 }
9aca0d5f dan2004-10-12 01:34:32 +00001195}
1196
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001197static void updateWindowType(WWindow * wwin)
9aca0d5f dan2004-10-12 01:34:32 +00001198{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001199 Atom type_ret;
1200 int fmt_ret;
1201 unsigned long nitems_ret;
1202 unsigned long bytes_after_ret;
1203 long *data = 0;
1204 int layer;
1205 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1206 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1207 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1208
1209 int i;
1210 Atom *type = (Atom *) data;
1211 for (i = 0; i < nitems_ret; ++i) {
1212 if (handleWindowType(wwin, type[i], &layer))
1213 break;
1214 }
1215 XFree(data);
1216 }
1217
1218 ChangeStackingLevel(wwin->frame->core, layer);
1219 wwin->frame->flags.need_texture_change = 1;
1220 wWindowConfigureBorders(wwin);
1221 wFrameWindowPaint(wwin->frame);
1222 wNETWMUpdateActions(wwin, False);
9aca0d5f dan2004-10-12 01:34:32 +00001223}
1224
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001225Bool wNETWMCheckClientHints(WWindow * wwin, int *layer, int *workspace)
9aca0d5f dan2004-10-12 01:34:32 +00001226{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001227 Atom type_ret;
1228 int fmt_ret;
1229 unsigned long nitems_ret;
1230 unsigned long bytes_after_ret;
1231 long *data = 0;
1232 Bool hasState = False;
1233 int i;
1234
1235 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1236 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1237 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1238
1239 long desktop = *data;
1240 XFree(data);
1241
1242 if (desktop == -1)
1243 wwin->client_flags.omnipresent = 1;
1244 else
1245 *workspace = desktop;
1246
1247 hasState = True;
1248 }
1249
1250 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1251 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1252 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1253
1254 Atom *state = (Atom *) data;
1255 for (i = 0; i < nitems_ret; ++i) {
1256 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1257 }
1258 XFree(data);
1259 hasState = True;
1260 }
1261
1262 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1263 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1264 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1265
1266 Atom *type = (Atom *) data;
1267 for (i = 0; i < nitems_ret; ++i) {
1268 if (handleWindowType(wwin, type[i], layer))
1269 break;
1270 }
1271 XFree(data);
1272 hasState = True;
1273 }
1274
1275 wNETWMUpdateActions(wwin, False);
1276 updateStrut(wwin, False);
1277 if (updateStrut(wwin, True)) {
1278 hasState = True;
1279 }
1280 wScreenUpdateUsableArea(wwin->screen_ptr);
1281
1282 return hasState;
9aca0d5f dan2004-10-12 01:34:32 +00001283}
1284
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001285static Bool updateNetIconInfo(WWindow * wwin)
1286{
9aca0d5f dan2004-10-12 01:34:32 +00001287
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001288 Atom type_ret;
1289 int fmt_ret;
1290 unsigned long nitems_ret;
1291 unsigned long bytes_after_ret;
1292 long *data = 0;
1293 Bool hasState = False;
1294 Bool old_state = wwin->flags.net_handle_icon;
1295
1296 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1297 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1298 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1299 long handled = *data;
1300 wwin->flags.net_handle_icon = (handled != 0);
1301 XFree(data);
1302 hasState = True;
1303
1304 } else
1305 wwin->flags.net_handle_icon = False;
1306
1307 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1308 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1309 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
9aca0d5f dan2004-10-12 01:34:32 +00001310
1311#ifdef NETWM_PROPER
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001312 if (wwin->flags.net_handle_icon)
9aca0d5f dan2004-10-12 01:34:32 +00001313#else
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001314 wwin->flags.net_handle_icon = True;
9aca0d5f dan2004-10-12 01:34:32 +00001315#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001316 {
1317 wwin->icon_x = data[0];
1318 wwin->icon_y = data[1];
1319 wwin->icon_w = data[2];
1320 wwin->icon_h = data[3];
1321 }
1322
1323 XFree(data);
1324 hasState = True;
1325
1326 } else
1327 wwin->flags.net_handle_icon = False;
1328
1329 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1330 if (wwin->flags.net_handle_icon) {
1331 removeIcon(wwin);
1332 } else {
1333 wwin->flags.miniaturized = False;
1334 wwin->flags.skip_next_animation = True;
1335 wIconifyWindow(wwin);
1336 }
1337 }
1338
1339 return hasState;
9aca0d5f dan2004-10-12 01:34:32 +00001340}
1341
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001342Bool wNETWMCheckInitialClientState(WWindow * wwin)
9aca0d5f dan2004-10-12 01:34:32 +00001343{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001344 Bool hasState = False;
9aca0d5f dan2004-10-12 01:34:32 +00001345
1346#ifdef DEBUG_WMSPEC
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001347 printf("CheckInitialClientState\n");
9aca0d5f dan2004-10-12 01:34:32 +00001348#endif
1349
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001350 wNETWMShowingDesktop(wwin->screen_ptr, False);
9aca0d5f dan2004-10-12 01:34:32 +00001351
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001352 hasState |= updateNetIconInfo(wwin);
9aca0d5f dan2004-10-12 01:34:32 +00001353
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001354 updateIconImage(wwin->screen_ptr, wwin);
9aca0d5f dan2004-10-12 01:34:32 +00001355
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001356 return hasState;
9aca0d5f dan2004-10-12 01:34:32 +00001357}
1358
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001359static void handleDesktopNames(XClientMessageEvent * event, WScreen * scr)
9aca0d5f dan2004-10-12 01:34:32 +00001360{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001361 unsigned long nitems_ret, bytes_after_ret;
1362 char *data, *names[32];
1363 int fmt_ret, i, n;
1364 Atom type_ret;
1365
1366 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1367 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1368 &bytes_after_ret, (unsigned char **)&data) != Success) {
1369 return;
1370 }
1371
1372 if (data == 0)
1373 return;
1374
1375 if (type_ret != utf8_string || fmt_ret != 8)
1376 return;
1377
1378 n = 0;
1379 names[n] = data;
1380 for (i = 0; i < nitems_ret; i++) {
1381 if (data[i] == 0) {
1382 n++;
1383 names[n] = &data[i];
1384 } else if (*names[n] == 0) {
1385 names[n] = &data[i];
1386 wWorkspaceRename(scr, n, names[n]);
1387 }
1388 }
9aca0d5f dan2004-10-12 01:34:32 +00001389}
1390
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001391Bool wNETWMProcessClientMessage(XClientMessageEvent * event)
9aca0d5f dan2004-10-12 01:34:32 +00001392{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001393 WScreen *scr;
1394 WWindow *wwin;
1395 Bool done = True;
9aca0d5f dan2004-10-12 01:34:32 +00001396
1397#ifdef DEBUG_WMSPEC
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001398 printf("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
9aca0d5f dan2004-10-12 01:34:32 +00001399#endif
1400
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001401 scr = wScreenForWindow(event->window);
1402 if (scr) {
1403 /* generic client messages */
1404 if (event->message_type == net_current_desktop) {
1405 wWorkspaceChange(scr, event->data.l[0]);
1406 } else if (event->message_type == net_number_of_desktops) {
1407 long value;
1408
1409 value = event->data.l[0];
1410 if (value > scr->workspace_count) {
1411 wWorkspaceMake(scr, value - scr->workspace_count);
1412 } else if (value < scr->workspace_count) {
1413 int i;
1414 Bool rebuild = False;
1415
1416 for (i = scr->workspace_count - 1; i >= value; i--) {
1417 if (!wWorkspaceDelete(scr, i)) {
1418 rebuild = True;
1419 break;
1420 }
1421 }
1422
1423 if (rebuild) {
1424 updateWorkspaceCount(scr);
1425 }
1426 }
1427 } else if (event->message_type == net_showing_desktop) {
1428 wNETWMShowingDesktop(scr, event->data.l[0]);
9aca0d5f dan2004-10-12 01:34:32 +00001429#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001430 } else if (event->message_type == net_desktop_viewport) {
1431 wWorkspaceSetViewport(scr, scr->current_workspace, event->data.l[0], event->data.l[1]);
9aca0d5f dan2004-10-12 01:34:32 +00001432#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001433 } else if (event->message_type == net_desktop_names) {
1434 handleDesktopNames(event, scr);
1435 } else {
1436 done = False;
1437 }
1438
1439 if (done)
1440 return True;
1441 }
1442
1443 /* window specific client messages */
1444
1445 wwin = wWindowFor(event->window);
1446 if (!wwin)
1447 return False;
1448
1449 if (event->message_type == net_active_window) {
1450 wNETWMShowingDesktop(scr, False);
1451 wMakeWindowVisible(wwin);
1452 } else if (event->message_type == net_close_window) {
1453 if (!WFLAGP(wwin, no_closable)) {
1454 if (wwin->protocols.DELETE_WINDOW)
1455 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1456 }
1457 } else if (event->message_type == net_wm_state) {
1458 int maximized = wwin->flags.maximized;
1459 long set = event->data.l[0];
9aca0d5f dan2004-10-12 01:34:32 +00001460
1461#ifdef DEBUG_WMSPEC
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001462 printf("net_wm_state set %d a1 %s a2 %s\n", set,
1463 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
9aca0d5f dan2004-10-12 01:34:32 +00001464#endif
1465
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001466 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1467 if (event->data.l[2])
1468 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1469
1470 if (wwin->flags.maximized != maximized) {
1471 if (!wwin->flags.maximized) {
1472 wwin->flags.maximized = maximized;
1473 wUnmaximizeWindow(wwin);
1474 } else {
1475 wMaximizeWindow(wwin, wwin->flags.maximized);
1476 }
1477 }
1478 updateStateHint(wwin, False, False);
1479 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1480 updateNetIconInfo(wwin);
1481 } else if (event->message_type == net_wm_desktop) {
1482 long desktop = event->data.l[0];
1483 if (desktop == -1) {
1484 wWindowSetOmnipresent(wwin, True);
1485 } else {
1486 if (IS_OMNIPRESENT(wwin))
1487 wWindowSetOmnipresent(wwin, False);
1488 wWindowChangeWorkspace(wwin, desktop);
1489 }
1490 } else if (event->message_type == net_wm_icon) {
1491 updateIconImage(scr, wwin);
1492 } else {
1493 done = False;
1494 }
1495
1496 return done;
9aca0d5f dan2004-10-12 01:34:32 +00001497}
1498
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001499Bool wNETWMCheckClientHintChange(WWindow * wwin, XPropertyEvent * event)
9aca0d5f dan2004-10-12 01:34:32 +00001500{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001501 Bool ret = True;
1502
1503 if (event->atom == net_wm_strut) {
1504 updateStrut(wwin, False);
1505 updateStrut(wwin, True);
1506 wScreenUpdateUsableArea(wwin->screen_ptr);
1507 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1508 updateNetIconInfo(wwin);
1509 } else if (event->atom == net_wm_window_type) {
1510 updateWindowType(wwin);
1511 } else if (event->atom == net_wm_name) {
1512 char *name = wNETWMGetWindowName(wwin->client_win);
1513 wWindowUpdateName(wwin, name);
1514 if (name)
1515 wfree(name);
1516 } else if (event->atom == net_wm_icon_name) {
1517 if (wwin->icon) {
1518 char *name = wNETWMGetIconName(wwin->client_win);
1519 wIconChangeTitle(wwin->icon, name);
1520 }
1521 } else {
1522 ret = False;
1523 }
1524
1525 return ret;
9aca0d5f dan2004-10-12 01:34:32 +00001526}
1527
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001528int wNETWMGetPidForWindow(Window window)
9aca0d5f dan2004-10-12 01:34:32 +00001529{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001530 Atom type_ret;
1531 int fmt_ret;
1532 unsigned long nitems_ret;
1533 unsigned long bytes_after_ret;
1534 long *data = 0;
1535 int pid;
1536
1537 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1538 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1539 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1540
1541 pid = *data;
1542 XFree(data);
1543 } else {
1544 pid = 0;
1545 }
1546
1547 return pid;
9aca0d5f dan2004-10-12 01:34:32 +00001548}
1549
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001550char *wNETWMGetWindowName(Window window)
70555308 kojima2004-10-16 22:05:04 +00001551{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001552 char *name;
1553 char *ret;
1554 int size;
1555
1556 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1557 if (name) {
1558 ret = wstrndup(name, size);
1559 XFree(name);
1560 } else
1561 ret = NULL;
1562 return ret;
70555308 kojima2004-10-16 22:05:04 +00001563}
1564
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001565char *wNETWMGetIconName(Window window)
70555308 kojima2004-10-16 22:05:04 +00001566{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001567 char *name;
1568 char *ret;
1569 int size;
1570
1571 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1572 if (name) {
1573 ret = wstrndup(name, size);
1574 XFree(name);
1575 } else
1576 ret = NULL;
1577 return ret;
70555308 kojima2004-10-16 22:05:04 +00001578}
1579
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001580static void observer(void *self, WMNotification * notif)
abc09951 kojima2001-09-12 21:43:42 +00001581{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001582 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1583 const char *name = WMGetNotificationName(notif);
1584 void *data = WMGetNotificationClientData(notif);
1585 NetData *ndata = (NetData *) self;
1586
1587 if (strcmp(name, WMNManaged) == 0 && wwin) {
1588 updateClientList(wwin->screen_ptr);
1589 updateClientListStacking(wwin->screen_ptr, NULL);
1590 updateStateHint(wwin, True, False);
1591
1592 updateStrut(wwin, False);
1593 updateStrut(wwin, True);
1594 wScreenUpdateUsableArea(wwin->screen_ptr);
1595 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1596 updateClientList(wwin->screen_ptr);
1597 updateClientListStacking(wwin->screen_ptr, wwin);
1598 updateWorkspaceHint(wwin, False, True);
1599 updateStateHint(wwin, False, True);
1600 wNETWMUpdateActions(wwin, True);
1601
1602 updateStrut(wwin, False);
1603 wScreenUpdateUsableArea(wwin->screen_ptr);
1604 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1605 updateClientListStacking(wwin->screen_ptr, NULL);
1606 updateStateHint(wwin, False, False);
1607 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1608 updateClientListStacking(wwin->screen_ptr, NULL);
1609 updateStateHint(wwin, False, False);
1610 } else if (strcmp(name, WMNChangedFocus) == 0) {
1611 updateFocusHint(ndata->scr, wwin);
1612 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1613 updateWorkspaceHint(wwin, False, False);
1614 updateStateHint(wwin, True, False);
1615 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1616 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1617 }
abc09951 kojima2001-09-12 21:43:42 +00001618}
1619
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001620static void wsobserver(void *self, WMNotification * notif)
abc09951 kojima2001-09-12 21:43:42 +00001621{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001622 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1623 const char *name = WMGetNotificationName(notif);
1624
1625 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1626 updateWorkspaceCount(scr);
1627 updateWorkspaceNames(scr);
1628 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1629 updateWorkspaceCount(scr);
1630 updateWorkspaceNames(scr);
1631 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1632 updateCurrentWorkspace(scr);
1633 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1634 updateWorkspaceNames(scr);
1635 }
abc09951 kojima2001-09-12 21:43:42 +00001636}
1637
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001638#endif /* NETWM_HINTS */