Make wmaker XRandR aware
[wmaker-crm.git] / src / event.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/* event.c- event loop and handling
6830b057 dan2004-10-12 21:28:27 +00002 *
9af1c6c4 dan1998-10-21 14:43:47 +00003 * Window Maker window manager
6830b057 dan2004-10-12 21:28:27 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1997-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00006 *
9d2e6ef9 scottc1998-09-29 22:36:29 +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
6830b057 dan2004-10-12 21:28:27 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000020 * USA.
21 */
22
9d2e6ef9 scottc1998-09-29 22:36:29 +000023#include "wconfig.h"
24
c7868fa4
TT
Tamas TEVESZ2010-03-18 19:18:18 +010025#ifdef HAVE_INOTIFY
26#include <sys/inotify.h>
27#endif
28
9d2e6ef9 scottc1998-09-29 22:36:29 +000029#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33
34#include <X11/Xlib.h>
35#include <X11/Xutil.h>
36#ifdef SHAPE
eb87c409 kojima1999-10-20 03:25:06 +000037# include <X11/extensions/shape.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000038#endif
d4025072 id1999-05-17 20:21:43 +000039#ifdef XDND
40#include "xdnd.h"
9af1c6c4 dan1998-10-21 14:43:47 +000041#endif
42
c201e161
CM
Carlos R. Mafra2010-08-22 19:05:07 +020043#ifdef HAVE_XRANDR
44#include <X11/extensions/Xrandr.h>
45#endif
46
6830b057 dan2004-10-12 21:28:27 +000047#ifdef KEEP_XKB_LOCK_STATUS
48#include <X11/XKBlib.h>
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020049#endif /* KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +000050
51#include "WindowMaker.h"
52#include "window.h"
53#include "actions.h"
54#include "client.h"
55#include "funcs.h"
56#include "keybind.h"
57#include "application.h"
58#include "stacking.h"
59#include "defaults.h"
60#include "workspace.h"
61#include "dock.h"
62#include "framewin.h"
63#include "properties.h"
64#include "balloon.h"
a10214a5 kojima2002-11-28 22:04:07 +000065#include "xinerama.h"
15e37ff9 Carlos R. Mafra2009-10-12 00:08:21 +020066#include "wmspec.h"
9d2e6ef9 scottc1998-09-29 22:36:29 +000067
68/******** Global Variables **********/
69extern XContext wWinContext;
9aca0d5f dan2004-10-12 01:34:32 +000070extern XContext wVEdgeContext;
9d2e6ef9 scottc1998-09-29 22:36:29 +000071
72extern Cursor wCursor[WCUR_LAST];
73
74extern WShortKey wKeyBindings[WKBD_LAST];
75extern int wScreenCount;
76extern Time LastTimestamp;
77extern Time LastFocusChange;
78
79extern WPreferences wPreferences;
80
81#define MOD_MASK wPreferences.modifier_mask
82
59eb9475 dan1998-11-03 12:53:26 +000083extern Atom _XA_WM_COLORMAP_NOTIFY;
84
9d2e6ef9 scottc1998-09-29 22:36:29 +000085extern Atom _XA_WM_CHANGE_STATE;
86extern Atom _XA_WM_DELETE_WINDOW;
0af24483 richard2000-03-02 07:36:02 +000087extern Atom _XA_GNUSTEP_WM_ATTR;
9af1c6c4 dan1998-10-21 14:43:47 +000088extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
0af24483 richard2000-03-02 07:36:02 +000089extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
9d2e6ef9 scottc1998-09-29 22:36:29 +000090extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
9df0e167 kojima1999-04-03 03:51:17 +000091extern Atom _XA_WINDOWMAKER_COMMAND;
3f7110b1 Daniel Déchelotte2009-08-19 00:50:49 +020092extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
9d2e6ef9 scottc1998-09-29 22:36:29 +000093
9d2e6ef9 scottc1998-09-29 22:36:29 +000094#ifdef SHAPE
9af1c6c4 dan1998-10-21 14:43:47 +000095extern Bool wShapeSupported;
96extern int wShapeEventBase;
9d2e6ef9 scottc1998-09-29 22:36:29 +000097#endif
98
6830b057 dan2004-10-12 21:28:27 +000099#ifdef KEEP_XKB_LOCK_STATUS
2bce7940 id1999-05-23 19:33:19 +0000100extern int wXkbEventBase;
101#endif
102
c201e161
CM
Carlos R. Mafra2010-08-22 19:05:07 +0200103#ifdef HAVE_XRANDR
104extern Bool has_randr;
105extern int randr_event_base;
106#endif
107
9d2e6ef9 scottc1998-09-29 22:36:29 +0000108/* special flags */
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100109/*extern char WDelayedActionSet;*/
9d2e6ef9 scottc1998-09-29 22:36:29 +0000110
9d2e6ef9 scottc1998-09-29 22:36:29 +0000111/************ Local stuff ***********/
112
31f16389
CM
Carlos R. Mafra2009-08-23 15:15:29 +0200113static void saveTimestamp(XEvent *event);
114static void handleColormapNotify(XEvent *event);
115static void handleMapNotify(XEvent *event);
116static void handleUnmapNotify(XEvent *event);
117static void handleButtonPress(XEvent *event);
118static void handleExpose(XEvent *event);
119static void handleDestroyNotify(XEvent *event);
120static void handleConfigureRequest(XEvent *event);
121static void handleMapRequest(XEvent *event);
122static void handlePropertyNotify(XEvent *event);
123static void handleEnterNotify(XEvent *event);
124static void handleLeaveNotify(XEvent *event);
125static void handleExtensions(XEvent *event);
126static void handleClientMessage(XEvent *event);
127static void handleKeyPress(XEvent *event);
128static void handleFocusIn(XEvent *event);
129static void handleMotionNotify(XEvent *event);
130static void handleVisibilityNotify(XEvent *event);
59eb9475 dan1998-11-03 12:53:26 +0000131
9d2e6ef9 scottc1998-09-29 22:36:29 +0000132#ifdef SHAPE
31f16389 Carlos R. Mafra2009-08-23 15:15:29 +0200133static void handleShapeNotify(XEvent *event);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000134#endif
135
65aa3ae9
BJ
Brad Jorsch2010-04-08 17:01:24 -0400136#ifdef KEEP_XKB_LOCK_STATUS
137static void handleXkbIndicatorStateNotify(XEvent *event);
138#endif
139
9d2e6ef9 scottc1998-09-29 22:36:29 +0000140/* called from the signal handler */
141void NotifyDeadProcess(pid_t pid, unsigned char status);
142
143/* real dead process handler */
144static void handleDeadProcess(void *foo);
145
9d2e6ef9 scottc1998-09-29 22:36:29 +0000146typedef struct DeadProcesses {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200147 pid_t pid;
148 unsigned char exit_status;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000149} DeadProcesses;
150
151/* stack of dead processes */
152static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200153static int deadProcessPtr = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000154
155typedef struct DeathHandler {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200156 WDeathHandler *callback;
157 pid_t pid;
158 void *client_data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000159} DeathHandler;
160
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200161static WMArray *deathHandlers = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000162
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200163WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000164{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200165 DeathHandler *handler;
6830b057 dan2004-10-12 21:28:27 +0000166
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200167 handler = malloc(sizeof(DeathHandler));
168 if (!handler)
169 return 0;
3c046182 kojima1999-09-16 02:58:56 +0000170
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200171 handler->pid = pid;
172 handler->callback = callback;
173 handler->client_data = cdata;
3c046182 kojima1999-09-16 02:58:56 +0000174
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200175 if (!deathHandlers)
176 deathHandlers = WMCreateArrayWithDestructor(8, wfree);
3c046182 kojima1999-09-16 02:58:56 +0000177
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200178 WMAddToArray(deathHandlers, handler);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000179
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200180 return handler;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000181}
182
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200183void wDeleteDeathHandler(WMagicNumber id)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000184{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200185 DeathHandler *handler = (DeathHandler *) id;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000186
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200187 if (!handler || !deathHandlers)
188 return;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000189
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200190 /* array destructor will call wfree(handler) */
191 WMRemoveFromArray(deathHandlers, handler);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000192}
193
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200194void DispatchEvent(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000195{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200196 if (deathHandlers)
197 handleDeadProcess(NULL);
198
199 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
200 WCHANGE_STATE(WSTATE_EXITING);
201 /* received SIGTERM */
202 /*
203 * WMHandleEvent() can't be called from anything
204 * executed inside here, or we can get in a infinite
205 * recursive loop.
206 */
207 Shutdown(WSExitMode);
208
209 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
210 WCHANGE_STATE(WSTATE_RESTARTING);
211
212 Shutdown(WSRestartPreparationMode);
213 /* received SIGHUP */
214 Restart(NULL, True);
215 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
216 WCHANGE_STATE(WSTATE_NORMAL);
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100217 wDefaultsCheckDomains(NULL);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200218 }
219
220 /* for the case that all that is wanted to be dispatched is
221 * the stuff above */
222 if (!event)
223 return;
224
225 saveTimestamp(event);
226 switch (event->type) {
227 case MapRequest:
228 handleMapRequest(event);
229 break;
230
231 case KeyPress:
232 handleKeyPress(event);
233 break;
234
235 case MotionNotify:
236 handleMotionNotify(event);
237 break;
238
239 case ConfigureRequest:
240 handleConfigureRequest(event);
241 break;
242
243 case DestroyNotify:
244 handleDestroyNotify(event);
245 break;
246
247 case MapNotify:
248 handleMapNotify(event);
249 break;
250
251 case UnmapNotify:
252 handleUnmapNotify(event);
253 break;
254
255 case ButtonPress:
256 handleButtonPress(event);
257 break;
258
259 case Expose:
260 handleExpose(event);
261 break;
262
263 case PropertyNotify:
264 handlePropertyNotify(event);
265 break;
266
267 case EnterNotify:
268 handleEnterNotify(event);
269 break;
270
271 case LeaveNotify:
272 handleLeaveNotify(event);
273 break;
274
275 case ClientMessage:
276 handleClientMessage(event);
277 break;
278
279 case ColormapNotify:
280 handleColormapNotify(event);
281 break;
282
283 case MappingNotify:
284 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
285 XRefreshKeyboardMapping(&event->xmapping);
286 break;
287
288 case FocusIn:
289 handleFocusIn(event);
290 break;
291
292 case VisibilityNotify:
293 handleVisibilityNotify(event);
294 break;
295 default:
296 handleExtensions(event);
297 break;
298 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000299}
300
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100301#ifdef HAVE_INOTIFY
56d85687
RP
Rodney Padgett2008-08-09 11:39:24 +0100302/*
303 *----------------------------------------------------------------------
304 * inotifyHandleEvents-
305 * Check for inotify events
306 *
307 * Returns:
308 * After reading events for the given file descriptor (fd) and
309 * watch descriptor (wd)
310 *
311 * Side effects:
312 * Calls wDefaultsCheckDomains if config database is updated
313 *----------------------------------------------------------------------
314 */
8efc4903
CM
Carlos R. Mafra2009-09-04 15:02:00 +0200315/* allow 5 simultaneous events, with path + filenames up to 64 chars */
316#define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200317void inotifyHandleEvents(int fd, int wd)
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100318{
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100319 extern void wDefaultsCheckDomains(void *);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200320 ssize_t eventQLength, i = 0;
321 char buff[BUFF_SIZE] = { 0 };
322 /* Check config only once per read of the event queue */
323 int oneShotFlag = 0;
324
325 /*
326 * Read off the queued events
327 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
328 * not occur; the block is on Xevents, but a config file change will normally
329 * occur as a result of an Xevent - so the event queue should never have more than
330 * a few entries before a read().
331 */
332 eventQLength = read(fd, buff, BUFF_SIZE);
333
334 /* check what events occured */
335 /* Should really check wd here too, but for now we only have one watch! */
336 while (i < eventQLength) {
337 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
338
339 /*
340 * see inotify.h for event types.
341 */
342 if (pevent->mask & IN_DELETE_SELF) {
343 wwarning(_("the defaults database has been deleted!"
344 " Restart Window Maker to create the database" " with the default settings"));
345 close(fd);
346 }
347 if (pevent->mask & IN_UNMOUNT) {
348 wwarning(_("the unit containing the defaults database has"
349 " been unmounted. Setting --static mode." " Any changes will not be saved."));
350 close(fd);
351 wPreferences.flags.noupdates = 1;
352 }
353 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
354 fprintf(stdout, "wmaker: reading config files in defaults database.\n");
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100355 wDefaultsCheckDomains(NULL);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200356 }
357
358 /* move to next event in the buffer */
359 i += sizeof(struct inotify_event) + pevent->len;
360 }
f18567db Carlos R. Mafra2009-08-18 00:30:17 +0200361}
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100362#endif /* HAVE_INOTIFY */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000363
364/*
365 *----------------------------------------------------------------------
366 * EventLoop-
367 * Processes X and internal events indefinitely.
6830b057 dan2004-10-12 21:28:27 +0000368 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000369 * Returns:
370 * Never returns
6830b057 dan2004-10-12 21:28:27 +0000371 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000372 * Side effects:
373 * The LastTimestamp global variable is updated.
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100374 * Calls inotifyGetEvents if defaults database changes.
6830b057 dan2004-10-12 21:28:27 +0000375 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000376 */
31f16389 Carlos R. Mafra2009-08-23 15:15:29 +0200377void EventLoop(void)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000378{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200379 XEvent event;
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100380#ifdef HAVE_INOTIFY
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200381 extern int inotifyFD;
382 extern int inotifyWD;
383 struct timeval time;
384 fd_set rfds;
385 int retVal = 0;
386
387 if (inotifyFD < 0 || inotifyWD < 0)
388 retVal = -1;
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100389#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200390
391 for (;;) {
392
393 WMNextEvent(dpy, &event); /* Blocks here */
394 WMHandleEvent(&event);
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100395#ifdef HAVE_INOTIFY
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200396 if (retVal != -1) {
397 time.tv_sec = 0;
398 time.tv_usec = 0;
399 FD_ZERO(&rfds);
400 FD_SET(inotifyFD, &rfds);
401
402 /* check for available read data from inotify - don't block! */
403 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
404
405 if (retVal < 0) { /* an error has occured */
406 wwarning(_("select failed. The inotify instance will be closed."
407 " Changes to the defaults database will require"
408 " a restart to take effect."));
409 close(inotifyFD);
410 continue;
411 }
412 if (FD_ISSET(inotifyFD, &rfds))
413 inotifyHandleEvents(inotifyFD, inotifyWD);
414 }
c7868fa4 Tamas TEVESZ2010-03-18 19:18:18 +0100415#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200416 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000417}
418
9aca0d5f dan2004-10-12 01:34:32 +0000419/*
420 *----------------------------------------------------------------------
421 * ProcessPendingEvents --
422 * Processes the events that are currently pending (at the time
423 * this function is called) in the display's queue.
424 *
425 * Returns:
426 * After the pending events that were present at the function call
427 * are processed.
428 *
429 * Side effects:
6830b057 dan2004-10-12 21:28:27 +0000430 * Many -- whatever handling events may involve.
9aca0d5f dan2004-10-12 01:34:32 +0000431 *
432 *----------------------------------------------------------------------
433 */
31f16389 Carlos R. Mafra2009-08-23 15:15:29 +0200434void ProcessPendingEvents(void)
9aca0d5f dan2004-10-12 01:34:32 +0000435{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200436 XEvent event;
437 int count;
9aca0d5f dan2004-10-12 01:34:32 +0000438
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200439 XSync(dpy, False);
9aca0d5f dan2004-10-12 01:34:32 +0000440
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200441 /* Take a snapshot of the event count in the queue */
442 count = XPending(dpy);
9aca0d5f dan2004-10-12 01:34:32 +0000443
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200444 while (count > 0 && XPending(dpy)) {
445 WMNextEvent(dpy, &event);
446 WMHandleEvent(&event);
447 count--;
448 }
9aca0d5f dan2004-10-12 01:34:32 +0000449}
450
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200451Bool IsDoubleClick(WScreen * scr, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000452{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200453 if ((scr->last_click_time > 0) &&
454 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
455 && (event->xbutton.button == scr->last_click_button)
456 && (event->xbutton.window == scr->last_click_window)) {
457
458 scr->flags.next_click_is_not_double = 1;
459 scr->last_click_time = 0;
460 scr->last_click_window = event->xbutton.window;
461
462 return True;
463 }
464 return False;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000465}
466
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200467void NotifyDeadProcess(pid_t pid, unsigned char status)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000468{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200469 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
470 wwarning("stack overflow: too many dead processes");
471 return;
472 }
473 /* stack the process to be handled later,
474 * as this is called from the signal handler */
475 deadProcesses[deadProcessPtr].pid = pid;
476 deadProcesses[deadProcessPtr].exit_status = status;
477 deadProcessPtr++;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000478}
479
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200480static void handleDeadProcess(void *foo)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000481{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200482 DeathHandler *tmp;
483 int i;
484
485 for (i = 0; i < deadProcessPtr; i++) {
486 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
487 }
488
489 if (!deathHandlers) {
490 deadProcessPtr = 0;
491 return;
492 }
493
494 /* get the pids on the queue and call handlers */
495 while (deadProcessPtr > 0) {
496 deadProcessPtr--;
497
498 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
499 tmp = WMGetFromArray(deathHandlers, i);
500 if (!tmp)
501 continue;
502
503 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
504 (*tmp->callback) (tmp->pid,
505 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
506 wDeleteDeathHandler(tmp);
507 }
508 }
509 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000510}
511
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200512static void saveTimestamp(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000513{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200514 /*
515 * Never save CurrentTime as LastTimestamp because CurrentTime
516 * it's not a real timestamp (it's the 0L constant)
517 */
518
519 switch (event->type) {
520 case ButtonRelease:
521 case ButtonPress:
522 LastTimestamp = event->xbutton.time;
523 break;
524 case KeyPress:
525 case KeyRelease:
526 LastTimestamp = event->xkey.time;
527 break;
528 case MotionNotify:
529 LastTimestamp = event->xmotion.time;
530 break;
531 case PropertyNotify:
532 LastTimestamp = event->xproperty.time;
533 break;
534 case EnterNotify:
535 case LeaveNotify:
536 LastTimestamp = event->xcrossing.time;
537 break;
538 case SelectionClear:
539 LastTimestamp = event->xselectionclear.time;
540 break;
541 case SelectionRequest:
542 LastTimestamp = event->xselectionrequest.time;
543 break;
544 case SelectionNotify:
545 LastTimestamp = event->xselection.time;
d4025072 id1999-05-17 20:21:43 +0000546#ifdef XDND
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200547 wXDNDProcessSelection(event);
d4025072 id1999-05-17 20:21:43 +0000548#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200549 break;
550 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000551}
552
8018da81 Carlos R. Mafra2010-03-16 19:01:01 +0100553static int matchWindow(const void *item, const void *cdata)
90c77b1a dan2001-12-17 14:46:31 +0000554{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200555 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
90c77b1a dan2001-12-17 14:46:31 +0000556}
557
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200558static void handleExtensions(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000559{
6830b057 dan2004-10-12 21:28:27 +0000560#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200561 XkbEvent *xkbevent;
562 xkbevent = (XkbEvent *) event;
563#endif /*KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000564#ifdef SHAPE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200565 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
566 handleShapeNotify(event);
567 }
6830b057 dan2004-10-12 21:28:27 +0000568#endif
569#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200570 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
571 handleXkbIndicatorStateNotify(event);
572 }
573#endif /*KEEP_XKB_LOCK_STATUS */
c201e161
CM
Carlos R. Mafra2010-08-22 19:05:07 +0200574#ifdef HAVE_XRANDR
575 if (has_randr && event->type == (randr_event_base + RRScreenChangeNotify))
576 Restart(NULL,True);
577#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +0000578}
579
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200580static void handleMapRequest(XEvent * ev)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000581{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200582 WWindow *wwin;
583 WScreen *scr = NULL;
584 Window window = ev->xmaprequest.window;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000585
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200586 if ((wwin = wWindowFor(window))) {
587 if (wwin->flags.shaded) {
588 wUnshadeWindow(wwin);
589 }
590 /* deiconify window */
591 if (wwin->flags.miniaturized) {
592 wDeiconifyWindow(wwin);
593 } else if (wwin->flags.hidden) {
594 WApplication *wapp = wApplicationOf(wwin->main_window);
595 /* go to the last workspace that the user worked on the app */
596 if (wapp) {
597 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
598 }
599 wUnhideApplication(wapp, False, False);
600 }
601 return;
602 }
603
604 scr = wScreenForRootWindow(ev->xmaprequest.parent);
605
606 wwin = wManageWindow(scr, window);
607
608 /*
609 * This is to let the Dock know that the application it launched
610 * has already been mapped (eg: it has finished launching).
611 * It is not necessary for normally docked apps, but is needed for
612 * apps that were forcedly docked (like with dockit).
613 */
614 if (scr->last_dock) {
615 if (wwin && wwin->main_window != None && wwin->main_window != window)
616 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
617 else
618 wDockTrackWindowLaunch(scr->last_dock, window);
619 }
620
621 if (wwin) {
622 wClientSetState(wwin, NormalState, None);
623 if (wwin->flags.maximized) {
624 wMaximizeWindow(wwin, wwin->flags.maximized);
625 }
626 if (wwin->flags.shaded) {
627 wwin->flags.shaded = 0;
628 wwin->flags.skip_next_animation = 1;
629 wShadeWindow(wwin);
630 }
631 if (wwin->flags.miniaturized) {
632 wwin->flags.miniaturized = 0;
633 wwin->flags.skip_next_animation = 1;
634 wIconifyWindow(wwin);
635 }
636 if (wwin->flags.fullscreen) {
637 wwin->flags.fullscreen = 0;
638 wFullscreenWindow(wwin);
639 }
640 if (wwin->flags.hidden) {
641 WApplication *wapp = wApplicationOf(wwin->main_window);
642
643 wwin->flags.hidden = 0;
644 wwin->flags.skip_next_animation = 1;
645 if (wapp) {
646 wHideApplication(wapp);
647 }
648 }
649 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000650}
651
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200652static void handleDestroyNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000653{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200654 WWindow *wwin;
655 WApplication *app;
656 Window window = event->xdestroywindow.window;
657 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
658 int widx;
0261c326 dan1999-01-06 15:22:33 +0000659
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200660 wwin = wWindowFor(window);
661 if (wwin) {
662 wUnmanageWindow(wwin, False, True);
663 }
664
665 if (scr != NULL) {
666 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
667 WFakeGroupLeader *fPtr;
668
669 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
670 if (fPtr->retainCount > 0) {
671 fPtr->retainCount--;
672 if (fPtr->retainCount == 0 && fPtr->leader != None) {
673 XDestroyWindow(dpy, fPtr->leader);
674 fPtr->leader = None;
675 XFlush(dpy);
676 }
677 }
678 fPtr->origLeader = None;
679 }
680 }
681
682 app = wApplicationOf(window);
683 if (app) {
684 if (window == app->main_window) {
685 app->refcount = 0;
686 wwin = app->main_window_desc->screen_ptr->focused_window;
687 while (wwin) {
688 if (wwin->main_window == window) {
689 wwin->main_window = None;
690 }
691 wwin = wwin->prev;
692 }
693 }
694 wApplicationDestroy(app);
695 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000696}
697
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200698static void handleExpose(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000699{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200700 WObjDescriptor *desc;
701 XEvent ev;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000702
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200703 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
e7495baf dan1999-02-17 11:06:40 +0000704
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200705 if (XFindContext(dpy, event->xexpose.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
706 return;
707 }
6830b057 dan2004-10-12 21:28:27 +0000708
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200709 if (desc->handle_expose) {
710 (*desc->handle_expose) (desc, event);
711 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000712}
713
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200714static void executeButtonAction(WScreen * scr, XEvent * event, int action)
3bde6495 dan2001-04-27 23:41:17 +0000715{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200716 switch (action) {
717 case WA_SELECT_WINDOWS:
718 wUnselectWindows(scr);
719 wSelectWindows(scr, event);
720 break;
721 case WA_OPEN_APPMENU:
722 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
723 /* ugly hack */
724 if (scr->root_menu) {
725 if (scr->root_menu->brother->flags.mapped)
726 event->xbutton.window = scr->root_menu->brother->frame->core->window;
727 else
728 event->xbutton.window = scr->root_menu->frame->core->window;
729 }
730 break;
731 case WA_OPEN_WINLISTMENU:
732 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
733 if (scr->switch_menu) {
734 if (scr->switch_menu->brother->flags.mapped)
735 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
736 else
737 event->xbutton.window = scr->switch_menu->frame->core->window;
738 }
739 break;
740 default:
741 break;
742 }
3bde6495 dan2001-04-27 23:41:17 +0000743}
744
9d2e6ef9 scottc1998-09-29 22:36:29 +0000745/* bindable */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200746static void handleButtonPress(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000747{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200748 WObjDescriptor *desc;
749 WScreen *scr;
6830b057 dan2004-10-12 21:28:27 +0000750
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200751 scr = wScreenForRootWindow(event->xbutton.root);
6830b057 dan2004-10-12 21:28:27 +0000752
9d2e6ef9 scottc1998-09-29 22:36:29 +0000753#ifdef BALLOON_TEXT
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200754 wBalloonHide(scr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000755#endif
756
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200757 if (event->xbutton.window == scr->root_win) {
758 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
759 executeButtonAction(scr, event, wPreferences.mouse_button1);
760 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
761 executeButtonAction(scr, event, wPreferences.mouse_button2);
762 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
763 executeButtonAction(scr, event, wPreferences.mouse_button3);
764 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
765 wWorkspaceRelativeChange(scr, 1);
766 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
767 wWorkspaceRelativeChange(scr, -1);
768 }
769 }
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200770
771 desc = NULL;
772 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext, (XPointer *) & desc) == XCNOENT) {
773 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
774 return;
775 }
776 }
777
778 if (desc->parent_type == WCLASS_WINDOW) {
779 XSync(dpy, 0);
780
744ccb85 Johann Haarhoff2010-03-14 10:49:14 +0200781 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200782 XAllowEvents(dpy, AsyncPointer, CurrentTime);
12d16d92
BJ
Brad Jorsch2010-04-26 13:56:36 -0400783 } else {
784 /* if (wPreferences.focus_mode == WKF_CLICK) { */
785 if (wPreferences.ignore_focus_click) {
786 XAllowEvents(dpy, AsyncPointer, CurrentTime);
787 }
788 XAllowEvents(dpy, ReplayPointer, CurrentTime);
789 /* } */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200790 }
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200791 XSync(dpy, 0);
792 } else if (desc->parent_type == WCLASS_APPICON
793 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
794 if (event->xbutton.state & MOD_MASK) {
795 XSync(dpy, 0);
796 XAllowEvents(dpy, AsyncPointer, CurrentTime);
797 XSync(dpy, 0);
798 }
799 }
800
801 if (desc->handle_mousedown != NULL) {
802 (*desc->handle_mousedown) (desc, event);
803 }
804
805 /* save double-click information */
806 if (scr->flags.next_click_is_not_double) {
807 scr->flags.next_click_is_not_double = 0;
808 } else {
809 scr->last_click_time = event->xbutton.time;
810 scr->last_click_button = event->xbutton.button;
811 scr->last_click_window = event->xbutton.window;
812 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000813}
814
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200815static void handleMapNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000816{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200817 WWindow *wwin;
5178465b Carlos R. Mafra2010-03-17 17:44:14 +0100818
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200819 wwin = wWindowFor(event->xmap.event);
820 if (wwin && wwin->client_win == event->xmap.event) {
821 if (wwin->flags.miniaturized) {
822 wDeiconifyWindow(wwin);
823 } else {
824 XGrabServer(dpy);
825 wWindowMap(wwin);
826 wClientSetState(wwin, NormalState, None);
827 XUngrabServer(dpy);
828 }
829 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000830}
831
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200832static void handleUnmapNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000833{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200834 WWindow *wwin;
835 XEvent ev;
836 Bool withdraw = False;
5178465b Carlos R. Mafra2010-03-17 17:44:14 +0100837
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200838 /* only process windows with StructureNotify selected
839 * (ignore SubstructureNotify) */
840 wwin = wWindowFor(event->xunmap.window);
841 if (!wwin)
842 return;
843
844 /* whether the event is a Withdrawal request */
845 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
846 withdraw = True;
847
848 if (wwin->client_win != event->xunmap.event && !withdraw)
849 return;
850
851 if (!wwin->flags.mapped && !withdraw
852 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
853 && !wwin->flags.miniaturized && !wwin->flags.hidden)
854 return;
855
856 XGrabServer(dpy);
857 XUnmapWindow(dpy, wwin->frame->core->window);
858 wwin->flags.mapped = 0;
859 XSync(dpy, 0);
860 /* check if the window was destroyed */
861 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
862 DispatchEvent(&ev);
863 } else {
864 Bool reparented = False;
865
866 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
867 reparented = True;
868
869 /* withdraw window */
870 wwin->flags.mapped = 0;
871 if (!reparented)
872 wClientSetState(wwin, WithdrawnState, None);
873
874 /* if the window was reparented, do not reparent it back to the
875 * root window */
876 wUnmanageWindow(wwin, !reparented, False);
877 }
878 XUngrabServer(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000879}
880
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200881static void handleConfigureRequest(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000882{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200883 WWindow *wwin;
5178465b Carlos R. Mafra2010-03-17 17:44:14 +0100884
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200885 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
886 /*
887 * Configure request for unmapped window
888 */
889 wClientConfigure(NULL, &(event->xconfigurerequest));
890 } else {
891 wClientConfigure(wwin, &(event->xconfigurerequest));
892 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000893}
894
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200895static void handlePropertyNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000896{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200897 WWindow *wwin;
898 WApplication *wapp;
899 Window jr;
900 int ji;
901 unsigned int ju;
902 WScreen *scr;
6b08fc9e Carlos R. Mafra2009-08-08 23:37:27 +0200903
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200904 wwin = wWindowFor(event->xproperty.window);
905 if (wwin) {
906 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
907 return;
908 }
909 wClientCheckProperty(wwin, &event->xproperty);
910 }
911 wapp = wApplicationOf(event->xproperty.window);
912 if (wapp) {
913 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
914 }
915
916 scr = wScreenForWindow(event->xproperty.window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000917}
918
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200919static void handleClientMessage(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000920{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200921 WWindow *wwin;
922 WObjDescriptor *desc;
5178465b Carlos R. Mafra2010-03-17 17:44:14 +0100923
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200924 /* handle transition from Normal to Iconic state */
925 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
926 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
927
928 wwin = wWindowFor(event->xclient.window);
929 if (!wwin)
930 return;
931 if (!wwin->flags.miniaturized)
932 wIconifyWindow(wwin);
933 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY && event->xclient.format == 32) {
934 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
935
936 if (!scr)
937 return;
938
939 if (event->xclient.data.l[1] == 1) { /* starting */
940 wColormapAllowClientInstallation(scr, True);
941 } else { /* stopping */
942 wColormapAllowClientInstallation(scr, False);
943 }
944 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
945
81aaed2b
TT
Tamas TEVESZ2010-04-01 03:26:30 +0200946 char *command;
947 size_t len;
948
949 len = sizeof(event->xclient.data.b) + 1;
950 command = wmalloc(len);
951 memset(command, 0, len);
952 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
953
954 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
955 wwarning(_("Got Reconfigure command"));
956 wDefaultsCheckDomains(NULL);
957 } else {
958 wwarning(_("Got unknown command %s"), command);
959 }
960
961 wfree(command);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200962
963 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
964 WApplication *wapp;
965 int done = 0;
966 wapp = wApplicationOf(event->xclient.window);
967 if (wapp) {
968 switch (event->xclient.data.l[0]) {
969 case WMFHideOtherApplications:
970 wHideOtherApplications(wapp->main_window_desc);
971 done = 1;
972 break;
973
974 case WMFHideApplication:
975 wHideApplication(wapp);
976 done = 1;
977 break;
978 }
979 }
980 if (!done) {
981 wwin = wWindowFor(event->xclient.window);
982 if (wwin) {
983 switch (event->xclient.data.l[0]) {
984 case WMFHideOtherApplications:
985 wHideOtherApplications(wwin);
986 break;
987
988 case WMFHideApplication:
989 wHideApplication(wApplicationOf(wwin->main_window));
990 break;
991 }
992 }
993 }
994 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
995 wwin = wWindowFor(event->xclient.window);
996 if (!wwin)
997 return;
998 switch (event->xclient.data.l[0]) {
999 case GSWindowLevelAttr:
1000 {
1001 int level = (int)event->xclient.data.l[1];
1002
1003 if (WINDOW_LEVEL(wwin) != level) {
1004 ChangeStackingLevel(wwin->frame->core, level);
1005 }
1006 }
1007 break;
1008 }
1009 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1010 wwin = wWindowFor(event->xclient.window);
1011 if (!wwin)
1012 return;
1013 switch (event->xclient.data.l[0]) {
1014 case WMTitleBarNormal:
1015 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1016 break;
1017 case WMTitleBarMain:
1018 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1019 break;
1020 case WMTitleBarKey:
1021 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1022 break;
1023 }
3f7110b1
DD
Daniel Déchelotte2009-08-19 00:50:49 +02001024 } else if (event->xclient.message_type == _XA_WM_IGNORE_FOCUS_EVENTS) {
1025 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
1026 if (!scr)
1027 return;
1028 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001029 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1030 /* do nothing */
d4025072 id1999-05-17 20:21:43 +00001031#ifdef XDND
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001032 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1033 /* do nothing */
1034#endif /* XDND */
1035 } else {
1036 /*
1037 * Non-standard thing, but needed by OffiX DND.
1038 * For when the icon frame gets a ClientMessage
1039 * that should have gone to the icon_window.
1040 */
1041 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1042 struct WIcon *icon = NULL;
1043
1044 if (desc->parent_type == WCLASS_MINIWINDOW) {
1045 icon = (WIcon *) desc->parent;
1046 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1047 icon = ((WAppIcon *) desc->parent)->icon;
1048 }
1049 if (icon && (wwin = icon->owner)) {
1050 if (wwin->client_win != event->xclient.window) {
1051 event->xclient.window = wwin->client_win;
1052 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1053 }
1054 }
1055 }
1056 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001057}
1058
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001059static void raiseWindow(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001060{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001061 WWindow *wwin;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001062
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001063 scr->autoRaiseTimer = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001064
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001065 wwin = wWindowFor(scr->autoRaiseWindow);
1066 if (!wwin)
1067 return;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001068
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001069 if (!wwin->flags.destroyed && wwin->flags.focused) {
1070 wRaiseFrame(wwin->frame->core);
1071 /* this is needed or a race condition will occur */
1072 XSync(dpy, False);
1073 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001074}
1075
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001076static void handleEnterNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001077{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001078 WWindow *wwin;
1079 WObjDescriptor *desc = NULL;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001080 XEvent ev;
1081 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
8eb0c79c id1999-12-18 11:52:49 +00001082
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001083 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1084 /* already left the window... */
1085 saveTimestamp(&ev);
1086 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1087 return;
1088 }
1089 }
1090
1091 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1092 if (desc->handle_enternotify)
1093 (*desc->handle_enternotify) (desc, event);
1094 }
1095
1096 /* enter to window */
1097 wwin = wWindowFor(event->xcrossing.window);
1098 if (!wwin) {
1099 if (wPreferences.colormap_mode == WCM_POINTER) {
1100 wColormapInstallForWindow(scr, NULL);
1101 }
1102 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1103 WMDeleteTimerHandler(scr->autoRaiseTimer);
1104 scr->autoRaiseTimer = NULL;
1105 }
1106 } else {
1107 /* set auto raise timer even if in focus-follows-mouse mode
1108 * and the event is for the frame window, even if the window
1109 * has focus already. useful if you move the pointer from a focused
1110 * window to the root window and back pretty fast
1111 *
1112 * set focus if in focus-follows-mouse mode and the event
1113 * is for the frame window and window doesn't have focus yet */
1114 if (wPreferences.focus_mode == WKF_SLOPPY
1115 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1116
1117 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1118 wSetFocusTo(scr, wwin);
1119
1120 if (scr->autoRaiseTimer)
1121 WMDeleteTimerHandler(scr->autoRaiseTimer);
1122 scr->autoRaiseTimer = NULL;
1123
1124 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1125 scr->autoRaiseWindow = wwin->frame->core->window;
1126 scr->autoRaiseTimer
1127 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1128 }
1129 }
1130 /* Install colormap for window, if the colormap installation mode
1131 * is colormap_follows_mouse */
1132 if (wPreferences.colormap_mode == WCM_POINTER) {
1133 if (wwin->client_win == event->xcrossing.window)
1134 wColormapInstallForWindow(scr, wwin);
1135 else
1136 wColormapInstallForWindow(scr, NULL);
1137 }
1138 }
1139
1140 /* a little kluge to hide the clip balloon */
1141 if (!wPreferences.flags.noclip && scr->flags.clip_balloon_mapped) {
1142 if (!desc) {
1143 XUnmapWindow(dpy, scr->clip_balloon);
1144 scr->flags.clip_balloon_mapped = 0;
1145 } else {
1146 if (desc->parent_type != WCLASS_DOCK_ICON || scr->clip_icon != desc->parent) {
1147 XUnmapWindow(dpy, scr->clip_balloon);
1148 scr->flags.clip_balloon_mapped = 0;
1149 }
1150 }
1151 }
1152
1153 if (event->xcrossing.window == event->xcrossing.root
1154 && event->xcrossing.detail == NotifyNormal
1155 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1156
1157 wSetFocusTo(scr, scr->focused_window);
1158 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001159#ifdef BALLOON_TEXT
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001160 wBalloonEnteredObject(scr, desc);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001161#endif
1162}
1163
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001164static void handleLeaveNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001165{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001166 WObjDescriptor *desc = NULL;
34cd5125 kojima2000-01-20 11:51:42 +00001167
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001168 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1169 if (desc->handle_leavenotify)
1170 (*desc->handle_leavenotify) (desc, event);
1171 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001172}
1173
9d2e6ef9 scottc1998-09-29 22:36:29 +00001174#ifdef SHAPE
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001175static void handleShapeNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001176{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001177 XShapeEvent *shev = (XShapeEvent *) event;
1178 WWindow *wwin;
bbf36355
BJ
Brad Jorsch2010-04-08 14:43:51 -04001179 union {
1180 XEvent xevent;
1181 XShapeEvent xshape;
1182 } ev;
1183
1184 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1185 if (ev.xshape.kind == ShapeBounding) {
1186 if (ev.xshape.shaped == shev->shaped) {
1187 *shev = ev.xshape;
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001188 } else {
bbf36355 Brad Jorsch2010-04-08 14:43:51 -04001189 XPutBackEvent(dpy, &ev.xevent);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001190 break;
1191 }
1192 }
1193 }
9af1c6c4 dan1998-10-21 14:43:47 +00001194
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001195 wwin = wWindowFor(shev->window);
1196 if (!wwin || shev->kind != ShapeBounding)
1197 return;
9af1c6c4 dan1998-10-21 14:43:47 +00001198
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001199 if (!shev->shaped && wwin->flags.shaped) {
9af1c6c4 dan1998-10-21 14:43:47 +00001200
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001201 wwin->flags.shaped = 0;
1202 wWindowClearShape(wwin);
9af1c6c4 dan1998-10-21 14:43:47 +00001203
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001204 } else if (shev->shaped) {
9af1c6c4 dan1998-10-21 14:43:47 +00001205
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001206 wwin->flags.shaped = 1;
1207 wWindowSetShape(wwin);
1208 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001209}
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001210#endif /* SHAPE */
9d2e6ef9 scottc1998-09-29 22:36:29 +00001211
6830b057 dan2004-10-12 21:28:27 +00001212#ifdef KEEP_XKB_LOCK_STATUS
7f9f8894 kojima1999-03-15 23:41:22 +00001213/* please help ]d if you know what to do */
65aa3ae9 Brad Jorsch2010-04-08 17:01:24 -04001214static void handleXkbIndicatorStateNotify(XEvent *event)
7f9f8894 kojima1999-03-15 23:41:22 +00001215{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001216 WWindow *wwin;
1217 WScreen *scr;
1218 XkbStateRec staterec;
1219 int i;
1220
1221 for (i = 0; i < wScreenCount; i++) {
1222 scr = wScreenWithNumber(i);
1223 wwin = scr->focused_window;
1224 if (wwin && wwin->flags.focused) {
1225 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1226 if (wwin->frame->languagemode != staterec.group) {
1227 wwin->frame->last_languagemode = wwin->frame->languagemode;
1228 wwin->frame->languagemode = staterec.group;
1229 }
c28f3b2c id1999-05-20 23:30:53 +00001230#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001231 if (wwin->frame->titlebar) {
1232 wFrameWindowPaint(wwin->frame);
1233 }
c28f3b2c id1999-05-20 23:30:53 +00001234#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001235 }
1236 }
7f9f8894 kojima1999-03-15 23:41:22 +00001237}
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001238#endif /*KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +00001239
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001240static void handleColormapNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001241{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001242 WWindow *wwin;
1243 WScreen *scr;
1244 Bool reinstall = False;
1245
1246 wwin = wWindowFor(event->xcolormap.window);
1247 if (!wwin)
1248 return;
1249
1250 scr = wwin->screen_ptr;
1251
1252 do {
1253 if (wwin) {
1254 if (event->xcolormap.new) {
1255 XWindowAttributes attr;
1256
1257 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1258
1259 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1260 scr->current_colormap = attr.colormap;
1261
1262 reinstall = True;
1263 } else if (event->xcolormap.state == ColormapUninstalled &&
1264 scr->current_colormap == event->xcolormap.colormap) {
1265
1266 /* some bastard app (like XV) removed our colormap */
1267 /*
1268 * can't enforce or things like xscreensaver wont work
1269 * reinstall = True;
1270 */
1271 } else if (event->xcolormap.state == ColormapInstalled &&
1272 scr->current_colormap == event->xcolormap.colormap) {
1273
1274 /* someone has put our colormap back */
1275 reinstall = False;
1276 }
1277 }
1278 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1279 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1280
1281 if (reinstall && scr->current_colormap != None) {
1282 if (!scr->flags.colormap_stuff_blocked)
1283 XInstallColormap(dpy, scr->current_colormap);
1284 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001285}
1286
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001287static void handleFocusIn(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001288{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001289 WWindow *wwin;
1290
1291 /*
1292 * For applications that like stealing the focus.
1293 */
1294 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1295 saveTimestamp(event);
1296 if (event->xfocus.mode == NotifyUngrab
1297 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1298 return;
1299 }
1300
1301 wwin = wWindowFor(event->xfocus.window);
1302 if (wwin && !wwin->flags.focused) {
1303 if (wwin->flags.mapped)
1304 wSetFocusTo(wwin->screen_ptr, wwin);
1305 else
1306 wSetFocusTo(wwin->screen_ptr, NULL);
1307 } else if (!wwin) {
1308 WScreen *scr = wScreenForWindow(event->xfocus.window);
1309 if (scr)
1310 wSetFocusTo(scr, NULL);
1311 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001312}
1313
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001314static WWindow *windowUnderPointer(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001315{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001316 unsigned int mask;
1317 int foo;
1318 Window bar, win;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001319
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001320 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1321 return wWindowFor(win);
1322 return NULL;
1323}
3c046182 kojima1999-09-16 02:58:56 +00001324
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001325static int CheckFullScreenWindowFocused(WScreen * scr)
30247c58 kojima2004-10-18 01:43:13 +00001326{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001327 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1328 return 1;
1329 else
1330 return 0;
30247c58 kojima2004-10-18 01:43:13 +00001331}
3c046182 kojima1999-09-16 02:58:56 +00001332
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001333static void handleKeyPress(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001334{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001335 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1336 WWindow *wwin = scr->focused_window;
1337 short i, widx;
1338 int modifiers;
1339 int command = -1;
6830b057 dan2004-10-12 21:28:27 +00001340#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001341 XkbStateRec staterec;
1342#endif /*KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +00001343
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001344 /* ignore CapsLock */
1345 modifiers = event->xkey.state & ValidModMask;
6830b057 dan2004-10-12 21:28:27 +00001346
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001347 for (i = 0; i < WKBD_LAST; i++) {
1348 if (wKeyBindings[i].keycode == 0)
1349 continue;
9e615bcf dan2001-12-28 03:29:50 +00001350
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001351 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1352 || */ wKeyBindings[i].modifier ==
1353 modifiers)) {
1354 command = i;
1355 break;
1356 }
1357 }
9e615bcf dan2001-12-28 03:29:50 +00001358
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001359 if (command < 0) {
fe736e84 Carlos R. Mafra2009-08-23 18:25:59 +02001360
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001361 if (!wRootMenuPerformShortcut(event)) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001362 static int dontLoop = 0;
1363
1364 if (dontLoop > 10) {
1365 wwarning("problem with key event processing code");
1366 return;
1367 }
1368 dontLoop++;
1369 /* if the focused window is an internal window, try redispatching
1370 * the event to the managed window, as it can be a WINGs window */
1371 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1372 /* client_leader contains the WINGs toplevel */
1373 event->xany.window = wwin->client_leader;
1374 WMHandleEvent(event);
1375 }
1376 dontLoop--;
1377 }
1378 return;
1379 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001380#define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1381#define ISFOCUSED(w) ((w) && (w)->flags.focused)
1382
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001383 switch (command) {
fe736e84 Carlos R. Mafra2009-08-23 18:25:59 +02001384
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001385 case WKBD_ROOTMENU:
1386 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1387 if (!CheckFullScreenWindowFocused(scr)) {
1388 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1389 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1390 True);
1391 }
1392 break;
1393 case WKBD_WINDOWLIST:
1394 if (!CheckFullScreenWindowFocused(scr)) {
1395 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1396 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1397 True);
1398 }
1399 break;
fe736e84 Carlos R. Mafra2009-08-23 18:25:59 +02001400
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001401 case WKBD_WINDOWMENU:
1402 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1403 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1404 break;
1405 case WKBD_MINIATURIZE:
1406 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1407 && !WFLAGP(wwin, no_miniaturizable)) {
1408 CloseWindowMenu(scr);
1409
1410 if (wwin->protocols.MINIATURIZE_WINDOW)
1411 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, event->xbutton.time);
1412 else {
1413 wIconifyWindow(wwin);
1414 }
1415 }
1416 break;
1417 case WKBD_HIDE:
1418 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1419 WApplication *wapp = wApplicationOf(wwin->main_window);
1420 CloseWindowMenu(scr);
1421
1422 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1423 wHideApplication(wapp);
1424 }
1425 }
1426 break;
1427 case WKBD_HIDE_OTHERS:
1428 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1429 CloseWindowMenu(scr);
1430
1431 wHideOtherApplications(wwin);
1432 }
1433 break;
1434 case WKBD_MAXIMIZE:
1435 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001436 CloseWindowMenu(scr);
1437
c04a2cae Carlos R. Mafra2009-09-01 03:22:57 +02001438 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_HORIZONTAL))
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001439 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001440 else
1441 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001442 }
1443 break;
1444 case WKBD_VMAXIMIZE:
1445 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001446 CloseWindowMenu(scr);
1447
c04a2cae Carlos R. Mafra2009-09-01 03:22:57 +02001448 if (wwin->flags.maximized == MAX_VERTICAL)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001449 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001450 else
1451 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_KEYBOARD);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001452 }
1453 break;
1454 case WKBD_HMAXIMIZE:
1455 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001456 CloseWindowMenu(scr);
1457
c04a2cae Carlos R. Mafra2009-09-01 03:22:57 +02001458 if (wwin->flags.maximized == MAX_HORIZONTAL)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001459 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001460 else
1461 wMaximizeWindow(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001462 }
1463 break;
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001464 case WKBD_LHMAXIMIZE:
1465 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001466 CloseWindowMenu(scr);
1467
4bce3e38 Carlos R. Mafra2009-09-13 18:28:42 +02001468 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_LEFTHALF))
69244548 Johann Haarhoff2009-08-28 21:31:26 +02001469 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001470 else
1471 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001472 }
1473 break;
1474 case WKBD_RHMAXIMIZE:
1475 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001476 CloseWindowMenu(scr);
1477
4bce3e38 Carlos R. Mafra2009-09-13 18:28:42 +02001478 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_RIGHTHALF))
69244548 Johann Haarhoff2009-08-28 21:31:26 +02001479 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001480 else
1481 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001482 }
1483 break;
cf62d159
CM
Carlos R. Mafra2009-09-02 01:37:47 +02001484 case WKBD_MAXIMUS:
1485 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
cf62d159
CM
Carlos R. Mafra2009-09-02 01:37:47 +02001486 CloseWindowMenu(scr);
1487
c04a2cae Carlos R. Mafra2009-09-01 03:22:57 +02001488 if (wwin->flags.maximized == MAX_MAXIMUS)
cf62d159 Carlos R. Mafra2009-09-02 01:37:47 +02001489 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001490 else
1491 wMaximizeWindow(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
cf62d159
CM
Carlos R. Mafra2009-09-02 01:37:47 +02001492 }
1493 break;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001494 case WKBD_RAISE:
1495 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1496 CloseWindowMenu(scr);
1497
1498 wRaiseFrame(wwin->frame->core);
1499 }
1500 break;
1501 case WKBD_LOWER:
1502 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1503 CloseWindowMenu(scr);
1504
1505 wLowerFrame(wwin->frame->core);
1506 }
1507 break;
1508 case WKBD_RAISELOWER:
1509 /* raise or lower the window under the pointer, not the
1510 * focused one
1511 */
1512 wwin = windowUnderPointer(scr);
1513 if (wwin)
1514 wRaiseLowerFrame(wwin->frame->core);
1515 break;
1516 case WKBD_SHADE:
1517 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1518 if (wwin->flags.shaded)
1519 wUnshadeWindow(wwin);
1520 else
1521 wShadeWindow(wwin);
1522 }
1523 break;
1524 case WKBD_MOVERESIZE:
1525 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1526 CloseWindowMenu(scr);
1527
1528 wKeyboardMoveResizeWindow(wwin);
1529 }
1530 break;
1531 case WKBD_CLOSE:
1532 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1533 CloseWindowMenu(scr);
1534 if (wwin->protocols.DELETE_WINDOW)
1535 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, event->xkey.time);
1536 }
1537 break;
1538 case WKBD_SELECT:
1539 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1540 wSelectWindow(wwin, !wwin->flags.selected);
1541 }
1542 break;
1543 case WKBD_FOCUSNEXT:
18408fff Iain Patterson2009-09-14 14:37:15 +01001544 StartWindozeCycle(wwin, event, True, False);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001545 break;
1546
1547 case WKBD_FOCUSPREV:
18408fff
IP
Iain Patterson2009-09-14 14:37:15 +01001548 StartWindozeCycle(wwin, event, False, False);
1549 break;
1550
1551 case WKBD_GROUPNEXT:
1552 StartWindozeCycle(wwin, event, True, True);
1553 break;
1554
1555 case WKBD_GROUPPREV:
1556 StartWindozeCycle(wwin, event, False, True);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001557 break;
1558
874b0fad Carlos R. Mafra2009-08-23 17:28:04 +02001559 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001560 widx = command - WKBD_WORKSPACE1;
1561 i = (scr->current_workspace / 10) * 10 + widx;
1562 if (wPreferences.ws_advance || i < scr->workspace_count)
1563 wWorkspaceChange(scr, i);
1564 break;
1565
1566 case WKBD_NEXTWORKSPACE:
1567 wWorkspaceRelativeChange(scr, 1);
1568 break;
1569 case WKBD_PREVWORKSPACE:
1570 wWorkspaceRelativeChange(scr, -1);
1571 break;
874b0fad Carlos R. Mafra2009-08-23 17:28:04 +02001572
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001573 case WKBD_WINDOW1:
1574 case WKBD_WINDOW2:
1575 case WKBD_WINDOW3:
1576 case WKBD_WINDOW4:
1577 case WKBD_WINDOW5:
1578 case WKBD_WINDOW6:
1579 case WKBD_WINDOW7:
1580 case WKBD_WINDOW8:
1581 case WKBD_WINDOW9:
1582 case WKBD_WINDOW10:
1583
1584 widx = command - WKBD_WINDOW1;
1585
1586 if (scr->shortcutWindows[widx]) {
1587 WMArray *list = scr->shortcutWindows[widx];
1588 int cw;
1589 int count = WMGetArrayItemCount(list);
1590 WWindow *twin;
1591 WMArrayIterator iter;
1592 WWindow *wwin;
1593
1594 wUnselectWindows(scr);
1595 cw = scr->current_workspace;
1596
1597 WM_ETARETI_ARRAY(list, wwin, iter) {
1598 if (count > 1)
1599 wWindowChangeWorkspace(wwin, cw);
1600
1601 wMakeWindowVisible(wwin);
1602
1603 if (count > 1)
1604 wSelectWindow(wwin, True);
1605 }
1606
1607 /* rotate the order of windows, to create a cycling effect */
1608 twin = WMGetFromArray(list, 0);
1609 WMDeleteFromArray(list, 0);
1610 WMAddToArray(list, twin);
1611
1612 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1613 if (scr->shortcutWindows[widx]) {
1614 WMFreeArray(scr->shortcutWindows[widx]);
1615 scr->shortcutWindows[widx] = NULL;
1616 }
1617
1618 if (wwin->flags.selected && scr->selected_windows) {
1619 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1620 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1621 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1622 } else {
1623 scr->shortcutWindows[widx] = WMCreateArray(4);
1624 WMAddToArray(scr->shortcutWindows[widx], wwin);
1625 }
1626
1627 wSelectWindow(wwin, !wwin->flags.selected);
1628 XFlush(dpy);
1629 wusleep(3000);
1630 wSelectWindow(wwin, !wwin->flags.selected);
1631 XFlush(dpy);
1632
1633 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1634
1635 if (wwin->flags.selected && scr->selected_windows) {
1636 if (scr->shortcutWindows[widx]) {
1637 WMFreeArray(scr->shortcutWindows[widx]);
1638 }
1639 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1640 }
1641 }
1642
1643 break;
1644
1645 case WKBD_SWITCH_SCREEN:
1646 if (wScreenCount > 1) {
1647 WScreen *scr2;
1648 int i;
1649
1650 /* find index of this screen */
1651 for (i = 0; i < wScreenCount; i++) {
1652 if (wScreenWithNumber(i) == scr)
1653 break;
1654 }
1655 i++;
1656 if (i >= wScreenCount) {
1657 i = 0;
1658 }
1659 scr2 = wScreenWithNumber(i);
1660
1661 if (scr2) {
1662 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1663 scr2->scr_width / 2, scr2->scr_height / 2);
1664 }
1665 }
1666 break;
1667
1668 case WKBD_NEXTWSLAYER:
1669 case WKBD_PREVWSLAYER:
1670 {
1671 int row, column;
1672
1673 row = scr->current_workspace / 10;
1674 column = scr->current_workspace % 10;
1675
1676 if (command == WKBD_NEXTWSLAYER) {
1677 if ((row + 1) * 10 < scr->workspace_count)
1678 wWorkspaceChange(scr, column + (row + 1) * 10);
1679 } else {
1680 if (row > 0)
1681 wWorkspaceChange(scr, column + (row - 1) * 10);
1682 }
1683 }
1684 break;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001685 case WKBD_CLIPRAISELOWER:
1686 if (!wPreferences.flags.noclip)
1687 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1688 break;
b6689a01
BJ
Brad Jorsch2010-01-06 12:50:36 -05001689 case WKBD_DOCKRAISELOWER:
1690 if (!wPreferences.flags.nodock)
1691 wDockRaiseLower(scr->dock);
1692 break;
59eb9475 dan1998-11-03 12:53:26 +00001693#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001694 case WKBD_TOGGLE:
1695 if (wPreferences.modelock) {
1696 /*toggle */
1697 wwin = scr->focused_window;
1698
1699 if (wwin && wwin->flags.mapped
1700 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1701 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1702 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1703
1704 wwin->frame->languagemode = wwin->frame->last_languagemode;
1705 wwin->frame->last_languagemode = staterec.group;
1706 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1707
1708 }
1709 }
1710 break;
72dfe4aa Carlos R. Mafra2010-01-09 13:12:20 +01001711#endif /* KEEP_XKB_LOCK_STATUS */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001712 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001713}
1714
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001715static void handleMotionNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001716{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001717 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1718
1719 if (wPreferences.scrollable_menus) {
1720 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1721 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1722
1723 if (scr->flags.jump_back_pending ||
1724 p.x <= (rect.pos.x + 1) ||
1725 p.x >= (rect.pos.x + rect.size.width - 2) ||
1726 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1727 WMenu *menu;
5178465b Carlos R. Mafra2010-03-17 17:44:14 +01001728
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001729 menu = wMenuUnderPointer(scr);
1730 if (menu != NULL)
1731 wMenuScroll(menu, event);
1732 }
1733 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001734}
416e3a82 dan1999-01-25 19:06:50 +00001735
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001736static void handleVisibilityNotify(XEvent * event)
e7495baf dan1999-02-17 11:06:40 +00001737{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001738 WWindow *wwin;
e7495baf dan1999-02-17 11:06:40 +00001739
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001740 wwin = wWindowFor(event->xvisibility.window);
1741 if (!wwin)
1742 return;
1743 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);
e7495baf dan1999-02-17 11:06:40 +00001744}