Mac OS X-style window cycling.
[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
56d85687 Rodney Padgett2008-08-09 11:39:24 +010023#include <sys/inotify.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000024#include "wconfig.h"
25
9d2e6ef9 scottc1998-09-29 22:36:29 +000026#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <string.h>
30
31#include <X11/Xlib.h>
32#include <X11/Xutil.h>
33#ifdef SHAPE
eb87c409 kojima1999-10-20 03:25:06 +000034# include <X11/extensions/shape.h>
9d2e6ef9 scottc1998-09-29 22:36:29 +000035#endif
d4025072 id1999-05-17 20:21:43 +000036#ifdef XDND
37#include "xdnd.h"
9af1c6c4 dan1998-10-21 14:43:47 +000038#endif
39
6830b057 dan2004-10-12 21:28:27 +000040#ifdef KEEP_XKB_LOCK_STATUS
41#include <X11/XKBlib.h>
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020042#endif /* KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +000043
44#include "WindowMaker.h"
45#include "window.h"
46#include "actions.h"
47#include "client.h"
48#include "funcs.h"
49#include "keybind.h"
50#include "application.h"
51#include "stacking.h"
52#include "defaults.h"
53#include "workspace.h"
54#include "dock.h"
55#include "framewin.h"
56#include "properties.h"
57#include "balloon.h"
a10214a5 kojima2002-11-28 22:04:07 +000058#include "xinerama.h"
3c046182 kojima1999-09-16 02:58:56 +000059
9aca0d5f dan2004-10-12 01:34:32 +000060#ifdef NETWM_HINTS
61# include "wmspec.h"
62#endif
9d2e6ef9 scottc1998-09-29 22:36:29 +000063
64/******** Global Variables **********/
65extern XContext wWinContext;
9aca0d5f dan2004-10-12 01:34:32 +000066extern XContext wVEdgeContext;
9d2e6ef9 scottc1998-09-29 22:36:29 +000067
68extern Cursor wCursor[WCUR_LAST];
69
70extern WShortKey wKeyBindings[WKBD_LAST];
71extern int wScreenCount;
72extern Time LastTimestamp;
73extern Time LastFocusChange;
74
75extern WPreferences wPreferences;
76
77#define MOD_MASK wPreferences.modifier_mask
78
59eb9475 dan1998-11-03 12:53:26 +000079extern Atom _XA_WM_COLORMAP_NOTIFY;
80
9d2e6ef9 scottc1998-09-29 22:36:29 +000081extern Atom _XA_WM_CHANGE_STATE;
82extern Atom _XA_WM_DELETE_WINDOW;
0af24483 richard2000-03-02 07:36:02 +000083extern Atom _XA_GNUSTEP_WM_ATTR;
9af1c6c4 dan1998-10-21 14:43:47 +000084extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
0af24483 richard2000-03-02 07:36:02 +000085extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
9d2e6ef9 scottc1998-09-29 22:36:29 +000086extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
9df0e167 kojima1999-04-03 03:51:17 +000087extern Atom _XA_WINDOWMAKER_COMMAND;
3f7110b1 Daniel Déchelotte2009-08-19 00:50:49 +020088extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
9d2e6ef9 scottc1998-09-29 22:36:29 +000089
9d2e6ef9 scottc1998-09-29 22:36:29 +000090#ifdef SHAPE
9af1c6c4 dan1998-10-21 14:43:47 +000091extern Bool wShapeSupported;
92extern int wShapeEventBase;
9d2e6ef9 scottc1998-09-29 22:36:29 +000093#endif
94
6830b057 dan2004-10-12 21:28:27 +000095#ifdef KEEP_XKB_LOCK_STATUS
2bce7940 id1999-05-23 19:33:19 +000096extern int wXkbEventBase;
97#endif
98
9d2e6ef9 scottc1998-09-29 22:36:29 +000099/* special flags */
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100100/*extern char WDelayedActionSet;*/
9d2e6ef9 scottc1998-09-29 22:36:29 +0000101
9d2e6ef9 scottc1998-09-29 22:36:29 +0000102/************ Local stuff ***********/
103
31f16389
CM
Carlos R. Mafra2009-08-23 15:15:29 +0200104static void saveTimestamp(XEvent *event);
105static void handleColormapNotify(XEvent *event);
106static void handleMapNotify(XEvent *event);
107static void handleUnmapNotify(XEvent *event);
108static void handleButtonPress(XEvent *event);
109static void handleExpose(XEvent *event);
110static void handleDestroyNotify(XEvent *event);
111static void handleConfigureRequest(XEvent *event);
112static void handleMapRequest(XEvent *event);
113static void handlePropertyNotify(XEvent *event);
114static void handleEnterNotify(XEvent *event);
115static void handleLeaveNotify(XEvent *event);
116static void handleExtensions(XEvent *event);
117static void handleClientMessage(XEvent *event);
118static void handleKeyPress(XEvent *event);
119static void handleFocusIn(XEvent *event);
120static void handleMotionNotify(XEvent *event);
121static void handleVisibilityNotify(XEvent *event);
59eb9475 dan1998-11-03 12:53:26 +0000122
9d2e6ef9 scottc1998-09-29 22:36:29 +0000123#ifdef SHAPE
31f16389 Carlos R. Mafra2009-08-23 15:15:29 +0200124static void handleShapeNotify(XEvent *event);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000125#endif
126
127/* called from the signal handler */
128void NotifyDeadProcess(pid_t pid, unsigned char status);
129
130/* real dead process handler */
131static void handleDeadProcess(void *foo);
132
9d2e6ef9 scottc1998-09-29 22:36:29 +0000133typedef struct DeadProcesses {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200134 pid_t pid;
135 unsigned char exit_status;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000136} DeadProcesses;
137
138/* stack of dead processes */
139static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200140static int deadProcessPtr = 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000141
142typedef struct DeathHandler {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200143 WDeathHandler *callback;
144 pid_t pid;
145 void *client_data;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000146} DeathHandler;
147
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200148static WMArray *deathHandlers = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000149
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200150WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000151{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200152 DeathHandler *handler;
6830b057 dan2004-10-12 21:28:27 +0000153
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200154 handler = malloc(sizeof(DeathHandler));
155 if (!handler)
156 return 0;
3c046182 kojima1999-09-16 02:58:56 +0000157
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200158 handler->pid = pid;
159 handler->callback = callback;
160 handler->client_data = cdata;
3c046182 kojima1999-09-16 02:58:56 +0000161
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200162 if (!deathHandlers)
163 deathHandlers = WMCreateArrayWithDestructor(8, wfree);
3c046182 kojima1999-09-16 02:58:56 +0000164
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200165 WMAddToArray(deathHandlers, handler);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000166
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200167 return handler;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000168}
169
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200170void wDeleteDeathHandler(WMagicNumber id)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000171{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200172 DeathHandler *handler = (DeathHandler *) id;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000173
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200174 if (!handler || !deathHandlers)
175 return;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000176
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200177 /* array destructor will call wfree(handler) */
178 WMRemoveFromArray(deathHandlers, handler);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000179}
180
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200181void DispatchEvent(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000182{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200183 if (deathHandlers)
184 handleDeadProcess(NULL);
185
186 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
187 WCHANGE_STATE(WSTATE_EXITING);
188 /* received SIGTERM */
189 /*
190 * WMHandleEvent() can't be called from anything
191 * executed inside here, or we can get in a infinite
192 * recursive loop.
193 */
194 Shutdown(WSExitMode);
195
196 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
197 WCHANGE_STATE(WSTATE_RESTARTING);
198
199 Shutdown(WSRestartPreparationMode);
200 /* received SIGHUP */
201 Restart(NULL, True);
202 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
203 WCHANGE_STATE(WSTATE_NORMAL);
204 wDefaultsCheckDomains();
205 }
206
207 /* for the case that all that is wanted to be dispatched is
208 * the stuff above */
209 if (!event)
210 return;
211
212 saveTimestamp(event);
213 switch (event->type) {
214 case MapRequest:
215 handleMapRequest(event);
216 break;
217
218 case KeyPress:
219 handleKeyPress(event);
220 break;
221
222 case MotionNotify:
223 handleMotionNotify(event);
224 break;
225
226 case ConfigureRequest:
227 handleConfigureRequest(event);
228 break;
229
230 case DestroyNotify:
231 handleDestroyNotify(event);
232 break;
233
234 case MapNotify:
235 handleMapNotify(event);
236 break;
237
238 case UnmapNotify:
239 handleUnmapNotify(event);
240 break;
241
242 case ButtonPress:
243 handleButtonPress(event);
244 break;
245
246 case Expose:
247 handleExpose(event);
248 break;
249
250 case PropertyNotify:
251 handlePropertyNotify(event);
252 break;
253
254 case EnterNotify:
255 handleEnterNotify(event);
256 break;
257
258 case LeaveNotify:
259 handleLeaveNotify(event);
260 break;
261
262 case ClientMessage:
263 handleClientMessage(event);
264 break;
265
266 case ColormapNotify:
267 handleColormapNotify(event);
268 break;
269
270 case MappingNotify:
271 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
272 XRefreshKeyboardMapping(&event->xmapping);
273 break;
274
275 case FocusIn:
276 handleFocusIn(event);
277 break;
278
279 case VisibilityNotify:
280 handleVisibilityNotify(event);
281 break;
282 default:
283 handleExtensions(event);
284 break;
285 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000286}
287
56d85687
RP
Rodney Padgett2008-08-09 11:39:24 +0100288/*
289 *----------------------------------------------------------------------
290 * inotifyHandleEvents-
291 * Check for inotify events
292 *
293 * Returns:
294 * After reading events for the given file descriptor (fd) and
295 * watch descriptor (wd)
296 *
297 * Side effects:
298 * Calls wDefaultsCheckDomains if config database is updated
299 *----------------------------------------------------------------------
300 */
8efc4903
CM
Carlos R. Mafra2009-09-04 15:02:00 +0200301/* allow 5 simultaneous events, with path + filenames up to 64 chars */
302#define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200303void inotifyHandleEvents(int fd, int wd)
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100304{
31f16389 Carlos R. Mafra2009-08-23 15:15:29 +0200305 extern void wDefaultsCheckDomains(void);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200306 ssize_t eventQLength, i = 0;
307 char buff[BUFF_SIZE] = { 0 };
308 /* Check config only once per read of the event queue */
309 int oneShotFlag = 0;
310
311 /*
312 * Read off the queued events
313 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
314 * not occur; the block is on Xevents, but a config file change will normally
315 * occur as a result of an Xevent - so the event queue should never have more than
316 * a few entries before a read().
317 */
318 eventQLength = read(fd, buff, BUFF_SIZE);
319
320 /* check what events occured */
321 /* Should really check wd here too, but for now we only have one watch! */
322 while (i < eventQLength) {
323 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
324
325 /*
326 * see inotify.h for event types.
327 */
328 if (pevent->mask & IN_DELETE_SELF) {
329 wwarning(_("the defaults database has been deleted!"
330 " Restart Window Maker to create the database" " with the default settings"));
331 close(fd);
332 }
333 if (pevent->mask & IN_UNMOUNT) {
334 wwarning(_("the unit containing the defaults database has"
335 " been unmounted. Setting --static mode." " Any changes will not be saved."));
336 close(fd);
337 wPreferences.flags.noupdates = 1;
338 }
339 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
340 fprintf(stdout, "wmaker: reading config files in defaults database.\n");
341 wDefaultsCheckDomains();
342 }
343
344 /* move to next event in the buffer */
345 i += sizeof(struct inotify_event) + pevent->len;
346 }
f18567db Carlos R. Mafra2009-08-18 00:30:17 +0200347}
9d2e6ef9 scottc1998-09-29 22:36:29 +0000348
349/*
350 *----------------------------------------------------------------------
351 * EventLoop-
352 * Processes X and internal events indefinitely.
6830b057 dan2004-10-12 21:28:27 +0000353 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000354 * Returns:
355 * Never returns
6830b057 dan2004-10-12 21:28:27 +0000356 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000357 * Side effects:
358 * The LastTimestamp global variable is updated.
56d85687 Rodney Padgett2008-08-09 11:39:24 +0100359 * Calls inotifyGetEvents if defaults database changes.
6830b057 dan2004-10-12 21:28:27 +0000360 *----------------------------------------------------------------------
9d2e6ef9 scottc1998-09-29 22:36:29 +0000361 */
31f16389 Carlos R. Mafra2009-08-23 15:15:29 +0200362void EventLoop(void)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000363{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200364 XEvent event;
365 extern int inotifyFD;
366 extern int inotifyWD;
367 struct timeval time;
368 fd_set rfds;
369 int retVal = 0;
370
371 if (inotifyFD < 0 || inotifyWD < 0)
372 retVal = -1;
373
374 for (;;) {
375
376 WMNextEvent(dpy, &event); /* Blocks here */
377 WMHandleEvent(&event);
378
379 if (retVal != -1) {
380 time.tv_sec = 0;
381 time.tv_usec = 0;
382 FD_ZERO(&rfds);
383 FD_SET(inotifyFD, &rfds);
384
385 /* check for available read data from inotify - don't block! */
386 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
387
388 if (retVal < 0) { /* an error has occured */
389 wwarning(_("select failed. The inotify instance will be closed."
390 " Changes to the defaults database will require"
391 " a restart to take effect."));
392 close(inotifyFD);
393 continue;
394 }
395 if (FD_ISSET(inotifyFD, &rfds))
396 inotifyHandleEvents(inotifyFD, inotifyWD);
397 }
398 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000399}
400
9aca0d5f dan2004-10-12 01:34:32 +0000401/*
402 *----------------------------------------------------------------------
403 * ProcessPendingEvents --
404 * Processes the events that are currently pending (at the time
405 * this function is called) in the display's queue.
406 *
407 * Returns:
408 * After the pending events that were present at the function call
409 * are processed.
410 *
411 * Side effects:
6830b057 dan2004-10-12 21:28:27 +0000412 * Many -- whatever handling events may involve.
9aca0d5f dan2004-10-12 01:34:32 +0000413 *
414 *----------------------------------------------------------------------
415 */
31f16389 Carlos R. Mafra2009-08-23 15:15:29 +0200416void ProcessPendingEvents(void)
9aca0d5f dan2004-10-12 01:34:32 +0000417{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200418 XEvent event;
419 int count;
9aca0d5f dan2004-10-12 01:34:32 +0000420
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200421 XSync(dpy, False);
9aca0d5f dan2004-10-12 01:34:32 +0000422
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200423 /* Take a snapshot of the event count in the queue */
424 count = XPending(dpy);
9aca0d5f dan2004-10-12 01:34:32 +0000425
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200426 while (count > 0 && XPending(dpy)) {
427 WMNextEvent(dpy, &event);
428 WMHandleEvent(&event);
429 count--;
430 }
9aca0d5f dan2004-10-12 01:34:32 +0000431}
432
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200433Bool IsDoubleClick(WScreen * scr, XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000434{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200435 if ((scr->last_click_time > 0) &&
436 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
437 && (event->xbutton.button == scr->last_click_button)
438 && (event->xbutton.window == scr->last_click_window)) {
439
440 scr->flags.next_click_is_not_double = 1;
441 scr->last_click_time = 0;
442 scr->last_click_window = event->xbutton.window;
443
444 return True;
445 }
446 return False;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000447}
448
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200449void NotifyDeadProcess(pid_t pid, unsigned char status)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000450{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200451 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
452 wwarning("stack overflow: too many dead processes");
453 return;
454 }
455 /* stack the process to be handled later,
456 * as this is called from the signal handler */
457 deadProcesses[deadProcessPtr].pid = pid;
458 deadProcesses[deadProcessPtr].exit_status = status;
459 deadProcessPtr++;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000460}
461
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200462static void handleDeadProcess(void *foo)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000463{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200464 DeathHandler *tmp;
465 int i;
466
467 for (i = 0; i < deadProcessPtr; i++) {
468 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
469 }
470
471 if (!deathHandlers) {
472 deadProcessPtr = 0;
473 return;
474 }
475
476 /* get the pids on the queue and call handlers */
477 while (deadProcessPtr > 0) {
478 deadProcessPtr--;
479
480 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
481 tmp = WMGetFromArray(deathHandlers, i);
482 if (!tmp)
483 continue;
484
485 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
486 (*tmp->callback) (tmp->pid,
487 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
488 wDeleteDeathHandler(tmp);
489 }
490 }
491 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000492}
493
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200494static void saveTimestamp(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000495{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200496 /*
497 * Never save CurrentTime as LastTimestamp because CurrentTime
498 * it's not a real timestamp (it's the 0L constant)
499 */
500
501 switch (event->type) {
502 case ButtonRelease:
503 case ButtonPress:
504 LastTimestamp = event->xbutton.time;
505 break;
506 case KeyPress:
507 case KeyRelease:
508 LastTimestamp = event->xkey.time;
509 break;
510 case MotionNotify:
511 LastTimestamp = event->xmotion.time;
512 break;
513 case PropertyNotify:
514 LastTimestamp = event->xproperty.time;
515 break;
516 case EnterNotify:
517 case LeaveNotify:
518 LastTimestamp = event->xcrossing.time;
519 break;
520 case SelectionClear:
521 LastTimestamp = event->xselectionclear.time;
522 break;
523 case SelectionRequest:
524 LastTimestamp = event->xselectionrequest.time;
525 break;
526 case SelectionNotify:
527 LastTimestamp = event->xselection.time;
d4025072 id1999-05-17 20:21:43 +0000528#ifdef XDND
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200529 wXDNDProcessSelection(event);
d4025072 id1999-05-17 20:21:43 +0000530#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200531 break;
532 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000533}
534
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200535static int matchWindow(void *item, void *cdata)
90c77b1a dan2001-12-17 14:46:31 +0000536{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200537 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
90c77b1a dan2001-12-17 14:46:31 +0000538}
539
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200540static void handleExtensions(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000541{
6830b057 dan2004-10-12 21:28:27 +0000542#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200543 XkbEvent *xkbevent;
544 xkbevent = (XkbEvent *) event;
545#endif /*KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000546#ifdef SHAPE
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200547 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
548 handleShapeNotify(event);
549 }
6830b057 dan2004-10-12 21:28:27 +0000550#endif
551#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200552 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
553 handleXkbIndicatorStateNotify(event);
554 }
555#endif /*KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +0000556}
557
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200558static void handleMapRequest(XEvent * ev)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000559{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200560 WWindow *wwin;
561 WScreen *scr = NULL;
562 Window window = ev->xmaprequest.window;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000563
564#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200565 printf("got map request for %x\n", (unsigned)window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000566#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200567 if ((wwin = wWindowFor(window))) {
568 if (wwin->flags.shaded) {
569 wUnshadeWindow(wwin);
570 }
571 /* deiconify window */
572 if (wwin->flags.miniaturized) {
573 wDeiconifyWindow(wwin);
574 } else if (wwin->flags.hidden) {
575 WApplication *wapp = wApplicationOf(wwin->main_window);
576 /* go to the last workspace that the user worked on the app */
577 if (wapp) {
578 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
579 }
580 wUnhideApplication(wapp, False, False);
581 }
582 return;
583 }
584
585 scr = wScreenForRootWindow(ev->xmaprequest.parent);
586
587 wwin = wManageWindow(scr, window);
588
589 /*
590 * This is to let the Dock know that the application it launched
591 * has already been mapped (eg: it has finished launching).
592 * It is not necessary for normally docked apps, but is needed for
593 * apps that were forcedly docked (like with dockit).
594 */
595 if (scr->last_dock) {
596 if (wwin && wwin->main_window != None && wwin->main_window != window)
597 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
598 else
599 wDockTrackWindowLaunch(scr->last_dock, window);
600 }
601
602 if (wwin) {
603 wClientSetState(wwin, NormalState, None);
604 if (wwin->flags.maximized) {
605 wMaximizeWindow(wwin, wwin->flags.maximized);
606 }
607 if (wwin->flags.shaded) {
608 wwin->flags.shaded = 0;
609 wwin->flags.skip_next_animation = 1;
610 wShadeWindow(wwin);
611 }
612 if (wwin->flags.miniaturized) {
613 wwin->flags.miniaturized = 0;
614 wwin->flags.skip_next_animation = 1;
615 wIconifyWindow(wwin);
616 }
617 if (wwin->flags.fullscreen) {
618 wwin->flags.fullscreen = 0;
619 wFullscreenWindow(wwin);
620 }
621 if (wwin->flags.hidden) {
622 WApplication *wapp = wApplicationOf(wwin->main_window);
623
624 wwin->flags.hidden = 0;
625 wwin->flags.skip_next_animation = 1;
626 if (wapp) {
627 wHideApplication(wapp);
628 }
629 }
630 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000631}
632
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200633static void handleDestroyNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000634{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200635 WWindow *wwin;
636 WApplication *app;
637 Window window = event->xdestroywindow.window;
638 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
639 int widx;
0261c326 dan1999-01-06 15:22:33 +0000640
9d2e6ef9 scottc1998-09-29 22:36:29 +0000641#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200642 printf("got destroy notify\n");
eb87c409 kojima1999-10-20 03:25:06 +0000643#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200644 wwin = wWindowFor(window);
645 if (wwin) {
646 wUnmanageWindow(wwin, False, True);
647 }
648
649 if (scr != NULL) {
650 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
651 WFakeGroupLeader *fPtr;
652
653 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
654 if (fPtr->retainCount > 0) {
655 fPtr->retainCount--;
656 if (fPtr->retainCount == 0 && fPtr->leader != None) {
657 XDestroyWindow(dpy, fPtr->leader);
658 fPtr->leader = None;
659 XFlush(dpy);
660 }
661 }
662 fPtr->origLeader = None;
663 }
664 }
665
666 app = wApplicationOf(window);
667 if (app) {
668 if (window == app->main_window) {
669 app->refcount = 0;
670 wwin = app->main_window_desc->screen_ptr->focused_window;
671 while (wwin) {
672 if (wwin->main_window == window) {
673 wwin->main_window = None;
674 }
675 wwin = wwin->prev;
676 }
677 }
678 wApplicationDestroy(app);
679 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000680}
681
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200682static void handleExpose(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000683{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200684 WObjDescriptor *desc;
685 XEvent ev;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000686
eb87c409 kojima1999-10-20 03:25:06 +0000687#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200688 printf("got expose\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000689#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200690 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
e7495baf dan1999-02-17 11:06:40 +0000691
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200692 if (XFindContext(dpy, event->xexpose.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
693 return;
694 }
6830b057 dan2004-10-12 21:28:27 +0000695
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200696 if (desc->handle_expose) {
697 (*desc->handle_expose) (desc, event);
698 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000699}
700
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200701static void executeButtonAction(WScreen * scr, XEvent * event, int action)
3bde6495 dan2001-04-27 23:41:17 +0000702{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200703 switch (action) {
704 case WA_SELECT_WINDOWS:
705 wUnselectWindows(scr);
706 wSelectWindows(scr, event);
707 break;
708 case WA_OPEN_APPMENU:
709 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
710 /* ugly hack */
711 if (scr->root_menu) {
712 if (scr->root_menu->brother->flags.mapped)
713 event->xbutton.window = scr->root_menu->brother->frame->core->window;
714 else
715 event->xbutton.window = scr->root_menu->frame->core->window;
716 }
717 break;
718 case WA_OPEN_WINLISTMENU:
719 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
720 if (scr->switch_menu) {
721 if (scr->switch_menu->brother->flags.mapped)
722 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
723 else
724 event->xbutton.window = scr->switch_menu->frame->core->window;
725 }
726 break;
727 default:
728 break;
729 }
3bde6495 dan2001-04-27 23:41:17 +0000730}
731
9d2e6ef9 scottc1998-09-29 22:36:29 +0000732/* bindable */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200733static void handleButtonPress(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000734{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200735 WObjDescriptor *desc;
736 WScreen *scr;
6830b057 dan2004-10-12 21:28:27 +0000737
eb87c409 kojima1999-10-20 03:25:06 +0000738#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200739 printf("got button press\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000740#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200741 scr = wScreenForRootWindow(event->xbutton.root);
6830b057 dan2004-10-12 21:28:27 +0000742
9d2e6ef9 scottc1998-09-29 22:36:29 +0000743#ifdef BALLOON_TEXT
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200744 wBalloonHide(scr);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000745#endif
746
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200747 if (event->xbutton.window == scr->root_win) {
748 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
749 executeButtonAction(scr, event, wPreferences.mouse_button1);
750 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
751 executeButtonAction(scr, event, wPreferences.mouse_button2);
752 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
753 executeButtonAction(scr, event, wPreferences.mouse_button3);
754 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
755 wWorkspaceRelativeChange(scr, 1);
756 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
757 wWorkspaceRelativeChange(scr, -1);
758 }
759 }
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200760
761 desc = NULL;
762 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext, (XPointer *) & desc) == XCNOENT) {
763 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
764 return;
765 }
766 }
767
768 if (desc->parent_type == WCLASS_WINDOW) {
769 XSync(dpy, 0);
770
771 if (event->xbutton.state & MOD_MASK) {
772 XAllowEvents(dpy, AsyncPointer, CurrentTime);
773 }
774
775 /* if (wPreferences.focus_mode == WKF_CLICK) { */
776 if (wPreferences.ignore_focus_click) {
777 XAllowEvents(dpy, AsyncPointer, CurrentTime);
778 }
779 XAllowEvents(dpy, ReplayPointer, CurrentTime);
780 /* } */
781 XSync(dpy, 0);
782 } else if (desc->parent_type == WCLASS_APPICON
783 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
784 if (event->xbutton.state & MOD_MASK) {
785 XSync(dpy, 0);
786 XAllowEvents(dpy, AsyncPointer, CurrentTime);
787 XSync(dpy, 0);
788 }
789 }
790
791 if (desc->handle_mousedown != NULL) {
792 (*desc->handle_mousedown) (desc, event);
793 }
794
795 /* save double-click information */
796 if (scr->flags.next_click_is_not_double) {
797 scr->flags.next_click_is_not_double = 0;
798 } else {
799 scr->last_click_time = event->xbutton.time;
800 scr->last_click_button = event->xbutton.button;
801 scr->last_click_window = event->xbutton.window;
802 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000803}
804
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200805static void handleMapNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000806{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200807 WWindow *wwin;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000808#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200809 printf("got map\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000810#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200811 wwin = wWindowFor(event->xmap.event);
812 if (wwin && wwin->client_win == event->xmap.event) {
813 if (wwin->flags.miniaturized) {
814 wDeiconifyWindow(wwin);
815 } else {
816 XGrabServer(dpy);
817 wWindowMap(wwin);
818 wClientSetState(wwin, NormalState, None);
819 XUngrabServer(dpy);
820 }
821 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000822}
823
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200824static void handleUnmapNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000825{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200826 WWindow *wwin;
827 XEvent ev;
828 Bool withdraw = False;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000829#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200830 printf("got unmap\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000831#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200832 /* only process windows with StructureNotify selected
833 * (ignore SubstructureNotify) */
834 wwin = wWindowFor(event->xunmap.window);
835 if (!wwin)
836 return;
837
838 /* whether the event is a Withdrawal request */
839 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
840 withdraw = True;
841
842 if (wwin->client_win != event->xunmap.event && !withdraw)
843 return;
844
845 if (!wwin->flags.mapped && !withdraw
846 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
847 && !wwin->flags.miniaturized && !wwin->flags.hidden)
848 return;
849
850 XGrabServer(dpy);
851 XUnmapWindow(dpy, wwin->frame->core->window);
852 wwin->flags.mapped = 0;
853 XSync(dpy, 0);
854 /* check if the window was destroyed */
855 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
856 DispatchEvent(&ev);
857 } else {
858 Bool reparented = False;
859
860 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
861 reparented = True;
862
863 /* withdraw window */
864 wwin->flags.mapped = 0;
865 if (!reparented)
866 wClientSetState(wwin, WithdrawnState, None);
867
868 /* if the window was reparented, do not reparent it back to the
869 * root window */
870 wUnmanageWindow(wwin, !reparented, False);
871 }
872 XUngrabServer(dpy);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000873}
874
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200875static void handleConfigureRequest(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000876{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200877 WWindow *wwin;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000878#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200879 printf("got configure request\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000880#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200881 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
882 /*
883 * Configure request for unmapped window
884 */
885 wClientConfigure(NULL, &(event->xconfigurerequest));
886 } else {
887 wClientConfigure(wwin, &(event->xconfigurerequest));
888 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000889}
890
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200891static void handlePropertyNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000892{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200893 WWindow *wwin;
894 WApplication *wapp;
895 Window jr;
896 int ji;
897 unsigned int ju;
898 WScreen *scr;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000899#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200900 printf("got property notify\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000901#endif
6b08fc9e Carlos R. Mafra2009-08-08 23:37:27 +0200902
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200903 wwin = wWindowFor(event->xproperty.window);
904 if (wwin) {
905 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
906 return;
907 }
908 wClientCheckProperty(wwin, &event->xproperty);
909 }
910 wapp = wApplicationOf(event->xproperty.window);
911 if (wapp) {
912 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
913 }
914
915 scr = wScreenForWindow(event->xproperty.window);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000916}
917
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200918static void handleClientMessage(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000919{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200920 WWindow *wwin;
921 WObjDescriptor *desc;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000922#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200923 printf("got client message\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +0000924#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200925 /* handle transition from Normal to Iconic state */
926 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
927 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
928
929 wwin = wWindowFor(event->xclient.window);
930 if (!wwin)
931 return;
932 if (!wwin->flags.miniaturized)
933 wIconifyWindow(wwin);
934 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY && event->xclient.format == 32) {
935 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
936
937 if (!scr)
938 return;
939
940 if (event->xclient.data.l[1] == 1) { /* starting */
941 wColormapAllowClientInstallation(scr, True);
942 } else { /* stopping */
943 wColormapAllowClientInstallation(scr, False);
944 }
945 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
946
947 wDefaultsCheckDomains();
948
949 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
950 WApplication *wapp;
951 int done = 0;
952 wapp = wApplicationOf(event->xclient.window);
953 if (wapp) {
954 switch (event->xclient.data.l[0]) {
955 case WMFHideOtherApplications:
956 wHideOtherApplications(wapp->main_window_desc);
957 done = 1;
958 break;
959
960 case WMFHideApplication:
961 wHideApplication(wapp);
962 done = 1;
963 break;
964 }
965 }
966 if (!done) {
967 wwin = wWindowFor(event->xclient.window);
968 if (wwin) {
969 switch (event->xclient.data.l[0]) {
970 case WMFHideOtherApplications:
971 wHideOtherApplications(wwin);
972 break;
973
974 case WMFHideApplication:
975 wHideApplication(wApplicationOf(wwin->main_window));
976 break;
977 }
978 }
979 }
980 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
981 wwin = wWindowFor(event->xclient.window);
982 if (!wwin)
983 return;
984 switch (event->xclient.data.l[0]) {
985 case GSWindowLevelAttr:
986 {
987 int level = (int)event->xclient.data.l[1];
988
989 if (WINDOW_LEVEL(wwin) != level) {
990 ChangeStackingLevel(wwin->frame->core, level);
991 }
992 }
993 break;
994 }
995 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
996 wwin = wWindowFor(event->xclient.window);
997 if (!wwin)
998 return;
999 switch (event->xclient.data.l[0]) {
1000 case WMTitleBarNormal:
1001 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1002 break;
1003 case WMTitleBarMain:
1004 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1005 break;
1006 case WMTitleBarKey:
1007 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1008 break;
1009 }
3f7110b1
DD
Daniel Déchelotte2009-08-19 00:50:49 +02001010 } else if (event->xclient.message_type == _XA_WM_IGNORE_FOCUS_EVENTS) {
1011 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
1012 if (!scr)
1013 return;
1014 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
9aca0d5f dan2004-10-12 01:34:32 +00001015#ifdef NETWM_HINTS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001016 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1017 /* do nothing */
9aca0d5f dan2004-10-12 01:34:32 +00001018#endif
d4025072 id1999-05-17 20:21:43 +00001019#ifdef XDND
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001020 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1021 /* do nothing */
1022#endif /* XDND */
1023 } else {
1024 /*
1025 * Non-standard thing, but needed by OffiX DND.
1026 * For when the icon frame gets a ClientMessage
1027 * that should have gone to the icon_window.
1028 */
1029 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1030 struct WIcon *icon = NULL;
1031
1032 if (desc->parent_type == WCLASS_MINIWINDOW) {
1033 icon = (WIcon *) desc->parent;
1034 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1035 icon = ((WAppIcon *) desc->parent)->icon;
1036 }
1037 if (icon && (wwin = icon->owner)) {
1038 if (wwin->client_win != event->xclient.window) {
1039 event->xclient.window = wwin->client_win;
1040 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1041 }
1042 }
1043 }
1044 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001045}
1046
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001047static void raiseWindow(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001048{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001049 WWindow *wwin;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001050
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001051 scr->autoRaiseTimer = NULL;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001052
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001053 wwin = wWindowFor(scr->autoRaiseWindow);
1054 if (!wwin)
1055 return;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001056
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001057 if (!wwin->flags.destroyed && wwin->flags.focused) {
1058 wRaiseFrame(wwin->frame->core);
1059 /* this is needed or a race condition will occur */
1060 XSync(dpy, False);
1061 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001062}
1063
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001064static void handleEnterNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001065{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001066 WWindow *wwin;
1067 WObjDescriptor *desc = NULL;
9aca0d5f dan2004-10-12 01:34:32 +00001068#ifdef VIRTUAL_DESKTOP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001069 void (*vdHandler) (XEvent * event);
9aca0d5f dan2004-10-12 01:34:32 +00001070#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001071 XEvent ev;
1072 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
9d2e6ef9 scottc1998-09-29 22:36:29 +00001073#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001074 printf("got enter notify\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +00001075#endif
8eb0c79c id1999-12-18 11:52:49 +00001076
1077#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001078 if (XFindContext(dpy, event->xcrossing.window, wVEdgeContext, (XPointer *) & vdHandler) != XCNOENT) {
1079 (*vdHandler) (event);
1080 }
8eb0c79c id1999-12-18 11:52:49 +00001081#endif
1082
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;
1179 XEvent ev;
652caa1f dan1999-10-21 00:54:37 +00001180#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001181 printf("got shape notify\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +00001182#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001183 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev)) {
1184 XShapeEvent *sev = (XShapeEvent *) & ev;
6830b057 dan2004-10-12 21:28:27 +00001185
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001186 if (sev->kind == ShapeBounding) {
1187 if (sev->shaped == shev->shaped) {
1188 *shev = *sev;
1189 } else {
1190 XPutBackEvent(dpy, &ev);
1191 break;
1192 }
1193 }
1194 }
9af1c6c4 dan1998-10-21 14:43:47 +00001195
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001196 wwin = wWindowFor(shev->window);
1197 if (!wwin || shev->kind != ShapeBounding)
1198 return;
9af1c6c4 dan1998-10-21 14:43:47 +00001199
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001200 if (!shev->shaped && wwin->flags.shaped) {
9af1c6c4 dan1998-10-21 14:43:47 +00001201
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001202 wwin->flags.shaped = 0;
1203 wWindowClearShape(wwin);
9af1c6c4 dan1998-10-21 14:43:47 +00001204
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001205 } else if (shev->shaped) {
9af1c6c4 dan1998-10-21 14:43:47 +00001206
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001207 wwin->flags.shaped = 1;
1208 wWindowSetShape(wwin);
1209 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001210}
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001211#endif /* SHAPE */
9d2e6ef9 scottc1998-09-29 22:36:29 +00001212
6830b057 dan2004-10-12 21:28:27 +00001213#ifdef KEEP_XKB_LOCK_STATUS
7f9f8894 kojima1999-03-15 23:41:22 +00001214/* please help ]d if you know what to do */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001215handleXkbIndicatorStateNotify(XEvent * event)
7f9f8894 kojima1999-03-15 23:41:22 +00001216{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001217 WWindow *wwin;
1218 WScreen *scr;
1219 XkbStateRec staterec;
1220 int i;
1221
1222 for (i = 0; i < wScreenCount; i++) {
1223 scr = wScreenWithNumber(i);
1224 wwin = scr->focused_window;
1225 if (wwin && wwin->flags.focused) {
1226 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1227 if (wwin->frame->languagemode != staterec.group) {
1228 wwin->frame->last_languagemode = wwin->frame->languagemode;
1229 wwin->frame->languagemode = staterec.group;
1230 }
c28f3b2c id1999-05-20 23:30:53 +00001231#ifdef XKB_BUTTON_HINT
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001232 if (wwin->frame->titlebar) {
1233 wFrameWindowPaint(wwin->frame);
1234 }
c28f3b2c id1999-05-20 23:30:53 +00001235#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001236 }
1237 }
7f9f8894 kojima1999-03-15 23:41:22 +00001238}
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001239#endif /*KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +00001240
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001241static void handleColormapNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001242{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001243 WWindow *wwin;
1244 WScreen *scr;
1245 Bool reinstall = False;
1246
1247 wwin = wWindowFor(event->xcolormap.window);
1248 if (!wwin)
1249 return;
1250
1251 scr = wwin->screen_ptr;
1252
1253 do {
1254 if (wwin) {
1255 if (event->xcolormap.new) {
1256 XWindowAttributes attr;
1257
1258 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1259
1260 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1261 scr->current_colormap = attr.colormap;
1262
1263 reinstall = True;
1264 } else if (event->xcolormap.state == ColormapUninstalled &&
1265 scr->current_colormap == event->xcolormap.colormap) {
1266
1267 /* some bastard app (like XV) removed our colormap */
1268 /*
1269 * can't enforce or things like xscreensaver wont work
1270 * reinstall = True;
1271 */
1272 } else if (event->xcolormap.state == ColormapInstalled &&
1273 scr->current_colormap == event->xcolormap.colormap) {
1274
1275 /* someone has put our colormap back */
1276 reinstall = False;
1277 }
1278 }
1279 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1280 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1281
1282 if (reinstall && scr->current_colormap != None) {
1283 if (!scr->flags.colormap_stuff_blocked)
1284 XInstallColormap(dpy, scr->current_colormap);
1285 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001286}
1287
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001288static void handleFocusIn(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001289{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001290 WWindow *wwin;
1291
1292 /*
1293 * For applications that like stealing the focus.
1294 */
1295 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1296 saveTimestamp(event);
1297 if (event->xfocus.mode == NotifyUngrab
1298 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1299 return;
1300 }
1301
1302 wwin = wWindowFor(event->xfocus.window);
1303 if (wwin && !wwin->flags.focused) {
1304 if (wwin->flags.mapped)
1305 wSetFocusTo(wwin->screen_ptr, wwin);
1306 else
1307 wSetFocusTo(wwin->screen_ptr, NULL);
1308 } else if (!wwin) {
1309 WScreen *scr = wScreenForWindow(event->xfocus.window);
1310 if (scr)
1311 wSetFocusTo(scr, NULL);
1312 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001313}
1314
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001315static WWindow *windowUnderPointer(WScreen * scr)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001316{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001317 unsigned int mask;
1318 int foo;
1319 Window bar, win;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001320
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001321 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1322 return wWindowFor(win);
1323 return NULL;
1324}
3c046182 kojima1999-09-16 02:58:56 +00001325
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001326static int CheckFullScreenWindowFocused(WScreen * scr)
30247c58 kojima2004-10-18 01:43:13 +00001327{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001328 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1329 return 1;
1330 else
1331 return 0;
30247c58 kojima2004-10-18 01:43:13 +00001332}
3c046182 kojima1999-09-16 02:58:56 +00001333
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001334static void handleKeyPress(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001335{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001336 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1337 WWindow *wwin = scr->focused_window;
1338 short i, widx;
1339 int modifiers;
1340 int command = -1;
6830b057 dan2004-10-12 21:28:27 +00001341#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001342 XkbStateRec staterec;
1343#endif /*KEEP_XKB_LOCK_STATUS */
9d2e6ef9 scottc1998-09-29 22:36:29 +00001344
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001345 /* ignore CapsLock */
1346 modifiers = event->xkey.state & ValidModMask;
6830b057 dan2004-10-12 21:28:27 +00001347
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001348 for (i = 0; i < WKBD_LAST; i++) {
1349 if (wKeyBindings[i].keycode == 0)
1350 continue;
9e615bcf dan2001-12-28 03:29:50 +00001351
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001352 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1353 || */ wKeyBindings[i].modifier ==
1354 modifiers)) {
1355 command = i;
1356 break;
1357 }
1358 }
9e615bcf dan2001-12-28 03:29:50 +00001359
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001360 if (command < 0) {
fe736e84 Carlos R. Mafra2009-08-23 18:25:59 +02001361
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001362 if (!wRootMenuPerformShortcut(event)) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001363 static int dontLoop = 0;
1364
1365 if (dontLoop > 10) {
1366 wwarning("problem with key event processing code");
1367 return;
1368 }
1369 dontLoop++;
1370 /* if the focused window is an internal window, try redispatching
1371 * the event to the managed window, as it can be a WINGs window */
1372 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1373 /* client_leader contains the WINGs toplevel */
1374 event->xany.window = wwin->client_leader;
1375 WMHandleEvent(event);
1376 }
1377 dontLoop--;
1378 }
1379 return;
1380 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001381#define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1382#define ISFOCUSED(w) ((w) && (w)->flags.focused)
1383
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001384 switch (command) {
fe736e84 Carlos R. Mafra2009-08-23 18:25:59 +02001385
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001386 case WKBD_ROOTMENU:
1387 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1388 if (!CheckFullScreenWindowFocused(scr)) {
1389 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1390 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1391 True);
1392 }
1393 break;
1394 case WKBD_WINDOWLIST:
1395 if (!CheckFullScreenWindowFocused(scr)) {
1396 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1397 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1398 True);
1399 }
1400 break;
fe736e84 Carlos R. Mafra2009-08-23 18:25:59 +02001401
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001402 case WKBD_WINDOWMENU:
1403 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1404 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1405 break;
1406 case WKBD_MINIATURIZE:
1407 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1408 && !WFLAGP(wwin, no_miniaturizable)) {
1409 CloseWindowMenu(scr);
1410
1411 if (wwin->protocols.MINIATURIZE_WINDOW)
1412 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, event->xbutton.time);
1413 else {
1414 wIconifyWindow(wwin);
1415 }
1416 }
1417 break;
1418 case WKBD_HIDE:
1419 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1420 WApplication *wapp = wApplicationOf(wwin->main_window);
1421 CloseWindowMenu(scr);
1422
1423 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1424 wHideApplication(wapp);
1425 }
1426 }
1427 break;
1428 case WKBD_HIDE_OTHERS:
1429 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1430 CloseWindowMenu(scr);
1431
1432 wHideOtherApplications(wwin);
1433 }
1434 break;
1435 case WKBD_MAXIMIZE:
1436 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001437 CloseWindowMenu(scr);
1438
c04a2cae Carlos R. Mafra2009-09-01 03:22:57 +02001439 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_HORIZONTAL))
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001440 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001441 else
1442 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001443 }
1444 break;
1445 case WKBD_VMAXIMIZE:
1446 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001447 CloseWindowMenu(scr);
1448
c04a2cae Carlos R. Mafra2009-09-01 03:22:57 +02001449 if (wwin->flags.maximized == MAX_VERTICAL)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001450 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001451 else
1452 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_KEYBOARD);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001453 }
1454 break;
1455 case WKBD_HMAXIMIZE:
1456 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001457 CloseWindowMenu(scr);
1458
c04a2cae Carlos R. Mafra2009-09-01 03:22:57 +02001459 if (wwin->flags.maximized == MAX_HORIZONTAL)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001460 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001461 else
1462 wMaximizeWindow(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001463 }
1464 break;
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001465 case WKBD_LHMAXIMIZE:
1466 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001467 CloseWindowMenu(scr);
1468
4bce3e38 Carlos R. Mafra2009-09-13 18:28:42 +02001469 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_LEFTHALF))
69244548 Johann Haarhoff2009-08-28 21:31:26 +02001470 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001471 else
1472 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001473 }
1474 break;
1475 case WKBD_RHMAXIMIZE:
1476 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001477 CloseWindowMenu(scr);
1478
4bce3e38 Carlos R. Mafra2009-09-13 18:28:42 +02001479 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_RIGHTHALF))
69244548 Johann Haarhoff2009-08-28 21:31:26 +02001480 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001481 else
1482 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
69244548
JH
Johann Haarhoff2009-08-28 21:31:26 +02001483 }
1484 break;
cf62d159
CM
Carlos R. Mafra2009-09-02 01:37:47 +02001485 case WKBD_MAXIMUS:
1486 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
cf62d159
CM
Carlos R. Mafra2009-09-02 01:37:47 +02001487 CloseWindowMenu(scr);
1488
c04a2cae Carlos R. Mafra2009-09-01 03:22:57 +02001489 if (wwin->flags.maximized == MAX_MAXIMUS)
cf62d159 Carlos R. Mafra2009-09-02 01:37:47 +02001490 wUnmaximizeWindow(wwin);
c04a2cae
CM
Carlos R. Mafra2009-09-01 03:22:57 +02001491 else
1492 wMaximizeWindow(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
cf62d159
CM
Carlos R. Mafra2009-09-02 01:37:47 +02001493 }
1494 break;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001495 case WKBD_RAISE:
1496 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1497 CloseWindowMenu(scr);
1498
1499 wRaiseFrame(wwin->frame->core);
1500 }
1501 break;
1502 case WKBD_LOWER:
1503 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1504 CloseWindowMenu(scr);
1505
1506 wLowerFrame(wwin->frame->core);
1507 }
1508 break;
1509 case WKBD_RAISELOWER:
1510 /* raise or lower the window under the pointer, not the
1511 * focused one
1512 */
1513 wwin = windowUnderPointer(scr);
1514 if (wwin)
1515 wRaiseLowerFrame(wwin->frame->core);
1516 break;
1517 case WKBD_SHADE:
1518 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1519 if (wwin->flags.shaded)
1520 wUnshadeWindow(wwin);
1521 else
1522 wShadeWindow(wwin);
1523 }
1524 break;
1525 case WKBD_MOVERESIZE:
1526 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1527 CloseWindowMenu(scr);
1528
1529 wKeyboardMoveResizeWindow(wwin);
1530 }
1531 break;
1532 case WKBD_CLOSE:
1533 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1534 CloseWindowMenu(scr);
1535 if (wwin->protocols.DELETE_WINDOW)
1536 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, event->xkey.time);
1537 }
1538 break;
1539 case WKBD_SELECT:
1540 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1541 wSelectWindow(wwin, !wwin->flags.selected);
1542 }
1543 break;
1544 case WKBD_FOCUSNEXT:
18408fff Iain Patterson2009-09-14 14:37:15 +01001545 StartWindozeCycle(wwin, event, True, False);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001546 break;
1547
1548 case WKBD_FOCUSPREV:
18408fff
IP
Iain Patterson2009-09-14 14:37:15 +01001549 StartWindozeCycle(wwin, event, False, False);
1550 break;
1551
1552 case WKBD_GROUPNEXT:
1553 StartWindozeCycle(wwin, event, True, True);
1554 break;
1555
1556 case WKBD_GROUPPREV:
1557 StartWindozeCycle(wwin, event, False, True);
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001558 break;
1559
874b0fad Carlos R. Mafra2009-08-23 17:28:04 +02001560 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001561 widx = command - WKBD_WORKSPACE1;
1562 i = (scr->current_workspace / 10) * 10 + widx;
1563 if (wPreferences.ws_advance || i < scr->workspace_count)
1564 wWorkspaceChange(scr, i);
1565 break;
1566
1567 case WKBD_NEXTWORKSPACE:
1568 wWorkspaceRelativeChange(scr, 1);
1569 break;
1570 case WKBD_PREVWORKSPACE:
1571 wWorkspaceRelativeChange(scr, -1);
1572 break;
874b0fad Carlos R. Mafra2009-08-23 17:28:04 +02001573
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001574 case WKBD_WINDOW1:
1575 case WKBD_WINDOW2:
1576 case WKBD_WINDOW3:
1577 case WKBD_WINDOW4:
1578 case WKBD_WINDOW5:
1579 case WKBD_WINDOW6:
1580 case WKBD_WINDOW7:
1581 case WKBD_WINDOW8:
1582 case WKBD_WINDOW9:
1583 case WKBD_WINDOW10:
1584
1585 widx = command - WKBD_WINDOW1;
1586
1587 if (scr->shortcutWindows[widx]) {
1588 WMArray *list = scr->shortcutWindows[widx];
1589 int cw;
1590 int count = WMGetArrayItemCount(list);
1591 WWindow *twin;
1592 WMArrayIterator iter;
1593 WWindow *wwin;
1594
1595 wUnselectWindows(scr);
1596 cw = scr->current_workspace;
1597
1598 WM_ETARETI_ARRAY(list, wwin, iter) {
1599 if (count > 1)
1600 wWindowChangeWorkspace(wwin, cw);
1601
1602 wMakeWindowVisible(wwin);
1603
1604 if (count > 1)
1605 wSelectWindow(wwin, True);
1606 }
1607
1608 /* rotate the order of windows, to create a cycling effect */
1609 twin = WMGetFromArray(list, 0);
1610 WMDeleteFromArray(list, 0);
1611 WMAddToArray(list, twin);
1612
1613 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1614 if (scr->shortcutWindows[widx]) {
1615 WMFreeArray(scr->shortcutWindows[widx]);
1616 scr->shortcutWindows[widx] = NULL;
1617 }
1618
1619 if (wwin->flags.selected && scr->selected_windows) {
1620 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1621 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1622 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1623 } else {
1624 scr->shortcutWindows[widx] = WMCreateArray(4);
1625 WMAddToArray(scr->shortcutWindows[widx], wwin);
1626 }
1627
1628 wSelectWindow(wwin, !wwin->flags.selected);
1629 XFlush(dpy);
1630 wusleep(3000);
1631 wSelectWindow(wwin, !wwin->flags.selected);
1632 XFlush(dpy);
1633
1634 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1635
1636 if (wwin->flags.selected && scr->selected_windows) {
1637 if (scr->shortcutWindows[widx]) {
1638 WMFreeArray(scr->shortcutWindows[widx]);
1639 }
1640 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1641 }
1642 }
1643
1644 break;
1645
1646 case WKBD_SWITCH_SCREEN:
1647 if (wScreenCount > 1) {
1648 WScreen *scr2;
1649 int i;
1650
1651 /* find index of this screen */
1652 for (i = 0; i < wScreenCount; i++) {
1653 if (wScreenWithNumber(i) == scr)
1654 break;
1655 }
1656 i++;
1657 if (i >= wScreenCount) {
1658 i = 0;
1659 }
1660 scr2 = wScreenWithNumber(i);
1661
1662 if (scr2) {
1663 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1664 scr2->scr_width / 2, scr2->scr_height / 2);
1665 }
1666 }
1667 break;
1668
1669 case WKBD_NEXTWSLAYER:
1670 case WKBD_PREVWSLAYER:
1671 {
1672 int row, column;
1673
1674 row = scr->current_workspace / 10;
1675 column = scr->current_workspace % 10;
1676
1677 if (command == WKBD_NEXTWSLAYER) {
1678 if ((row + 1) * 10 < scr->workspace_count)
1679 wWorkspaceChange(scr, column + (row + 1) * 10);
1680 } else {
1681 if (row > 0)
1682 wWorkspaceChange(scr, column + (row - 1) * 10);
1683 }
1684 }
1685 break;
1686 case WKBD_CLIPLOWER:
1687 if (!wPreferences.flags.noclip)
1688 wDockLower(scr->workspaces[scr->current_workspace]->clip);
1689 break;
1690 case WKBD_CLIPRAISE:
1691 if (!wPreferences.flags.noclip)
1692 wDockRaise(scr->workspaces[scr->current_workspace]->clip);
1693 break;
1694 case WKBD_CLIPRAISELOWER:
1695 if (!wPreferences.flags.noclip)
1696 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1697 break;
59eb9475 dan1998-11-03 12:53:26 +00001698#ifdef KEEP_XKB_LOCK_STATUS
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001699 case WKBD_TOGGLE:
1700 if (wPreferences.modelock) {
1701 /*toggle */
1702 wwin = scr->focused_window;
1703
1704 if (wwin && wwin->flags.mapped
1705 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1706 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1707 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1708
1709 wwin->frame->languagemode = wwin->frame->last_languagemode;
1710 wwin->frame->last_languagemode = staterec.group;
1711 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1712
1713 }
1714 }
1715 break;
1716#endif /* KEEP_XKB_LOCK_STATUS */
9aca0d5f dan2004-10-12 01:34:32 +00001717#ifdef VIRTUAL_DESKTOP
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001718 case WKBD_VDESK_LEFT:
1719 wWorkspaceKeyboardMoveDesktop(scr, VEC_LEFT);
1720 break;
9aca0d5f dan2004-10-12 01:34:32 +00001721
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001722 case WKBD_VDESK_RIGHT:
1723 wWorkspaceKeyboardMoveDesktop(scr, VEC_RIGHT);
1724 break;
9aca0d5f dan2004-10-12 01:34:32 +00001725
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001726 case WKBD_VDESK_UP:
1727 wWorkspaceKeyboardMoveDesktop(scr, VEC_UP);
1728 break;
9aca0d5f dan2004-10-12 01:34:32 +00001729
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001730 case WKBD_VDESK_DOWN:
1731 wWorkspaceKeyboardMoveDesktop(scr, VEC_DOWN);
1732 break;
9aca0d5f dan2004-10-12 01:34:32 +00001733#endif
59eb9475 dan1998-11-03 12:53:26 +00001734
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001735 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001736}
1737
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001738static void handleMotionNotify(XEvent * event)
9d2e6ef9 scottc1998-09-29 22:36:29 +00001739{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001740 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1741
1742 if (wPreferences.scrollable_menus) {
1743 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1744 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1745
1746 if (scr->flags.jump_back_pending ||
1747 p.x <= (rect.pos.x + 1) ||
1748 p.x >= (rect.pos.x + rect.size.width - 2) ||
1749 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1750 WMenu *menu;
9d2e6ef9 scottc1998-09-29 22:36:29 +00001751#ifdef DEBUG
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001752 printf("pointer at screen edge\n");
9d2e6ef9 scottc1998-09-29 22:36:29 +00001753#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001754 menu = wMenuUnderPointer(scr);
1755 if (menu != NULL)
1756 wMenuScroll(menu, event);
1757 }
1758 }
9d2e6ef9 scottc1998-09-29 22:36:29 +00001759}
416e3a82 dan1999-01-25 19:06:50 +00001760
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001761static void handleVisibilityNotify(XEvent * event)
e7495baf dan1999-02-17 11:06:40 +00001762{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +02001763 WWindow *wwin;
e7495baf dan1999-02-17 11:06:40 +00001764
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +02001765 wwin = wWindowFor(event->xvisibility.window);
1766 if (!wwin)
1767 return;
1768 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);
e7495baf dan1999-02-17 11:06:40 +00001769}