Add delay to send configure notify in virtual edge and
[wmaker-crm.git] / src / moveres.c
blob34d20054db2235de824da9e2c6f1219bb32575f0
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998, 1999 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/keysym.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 #include "WindowMaker.h"
33 #include "wcore.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "dock.h"
39 #include "funcs.h"
40 #include "actions.h"
41 #include "workspace.h"
43 #include "geomview.h"
46 #ifdef KWM_HINTS
47 #include "kwm.h"
48 #endif
50 /* How many different types of geometry/position
51 display thingies are there? */
52 #define NUM_DISPLAYS 4
54 #define LEFT 1
55 #define RIGHT 2
56 #define HORIZONTAL (LEFT|RIGHT)
57 #define UP 4
58 #define DOWN 8
59 #define VERTICAL (UP|DOWN)
61 /****** Global Variables ******/
62 extern Time LastTimestamp;
64 extern Cursor wCursor[WCUR_LAST];
66 extern WPreferences wPreferences;
68 extern Atom _XA_WM_PROTOCOLS;
73 *----------------------------------------------------------------------
74 * checkMouseSamplingRate-
75 * For lowering the mouse motion sampling rate for machines where
76 * it's too high (SGIs). If it returns False then the event should be
77 * ignored.
78 *----------------------------------------------------------------------
80 static Bool
81 checkMouseSamplingRate(XEvent *ev)
83 static Time previousMotion = 0;
85 if (ev->type == MotionNotify) {
86 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
87 return False;
88 } else {
89 previousMotion = ev->xmotion.time;
92 return True;
98 *----------------------------------------------------------------------
99 * moveGeometryDisplayCentered
101 * routine that moves the geometry/position window on scr so it is
102 * centered over the given coordinates (x,y). Also the window position
103 * is clamped so it stays on the screen at all times.
104 *----------------------------------------------------------------------
106 static void
107 moveGeometryDisplayCentered(WScreen *scr, int x, int y)
109 unsigned int w = WMWidgetWidth(scr->gview);
110 unsigned int h = WMWidgetHeight(scr->gview);
112 x -= w / 2;
113 y -= h / 2;
115 if (x < 1)
116 x = 1;
117 else if (x > (scr->scr_width - w - 3))
118 x = scr->scr_width - w - 3;
120 if (y < 1)
121 y = 1;
122 else if (y > (scr->scr_height - h - 3))
123 y = scr->scr_height - h - 3;
125 WMMoveWidget(scr->gview, x, y);
129 static void
130 showPosition(WWindow *wwin, int x, int y)
132 WScreen *scr = wwin->screen_ptr;
134 if (wPreferences.move_display == WDIS_NEW) {
135 #if 0
136 int width = wwin->frame->core->width;
137 int height = wwin->frame->core->height;
139 GC lgc = scr->line_gc;
140 XSetForeground(dpy, lgc, scr->line_pixel);
141 sprintf(num, "%i", x);
143 XDrawLine(dpy, scr->root_win, lgc, 0, y-1, scr->scr_width, y-1);
144 XDrawLine(dpy, scr->root_win, lgc, 0, y+height+2, scr->scr_width,
145 y+height+2);
146 XDrawLine(dpy, scr->root_win, lgc, x-1, 0, x-1, scr->scr_height);
147 XDrawLine(dpy, scr->root_win, lgc, x+width+2, 0, x+width+2,
148 scr->scr_height);
149 #endif
150 } else {
151 #ifdef VIRTUAL_DESKTOP
152 WSetGeometryViewShownPosition(scr->gview,
153 x + scr->workspaces[scr->current_workspace]->view_x,
154 y + scr->workspaces[scr->current_workspace]->view_y);
155 #else
156 WSetGeometryViewShownPosition(scr->gview, x, y);
157 #endif
162 static void
163 cyclePositionDisplay(WWindow *wwin, int x, int y, int w, int h)
165 WScreen *scr = wwin->screen_ptr;
167 wPreferences.move_display++;
168 wPreferences.move_display %= NUM_DISPLAYS;
170 if (wPreferences.move_display == WDIS_NEW) {
171 WMUnmapWidget(scr->gview);
172 } else {
173 if (wPreferences.move_display == WDIS_CENTER) {
174 moveGeometryDisplayCentered(scr,
175 scr->scr_width/2, scr->scr_height/2);
176 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
177 moveGeometryDisplayCentered(scr, 1, 1);
178 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
179 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
181 WMMapWidget(scr->gview);
186 static void
187 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
189 WScreen *scr = wwin->screen_ptr;
191 if (wPreferences.move_display == WDIS_NEW) {
192 return;
193 } else if (wPreferences.move_display == WDIS_CENTER) {
194 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
195 scr->scr_height / 2);
196 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
197 moveGeometryDisplayCentered(scr, 1, 1);
198 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
199 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
201 WMMapWidget(scr->gview);
202 WSetGeometryViewShownPosition(scr->gview, x, y);
206 static void
207 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
209 WScreen *scr = wwin->screen_ptr;
210 Window root = scr->root_win;
211 GC gc = scr->line_gc;
212 int ty, by, my, x, y, mx, s;
213 char num[16];
214 XSegment segment[4];
215 int fw, fh;
217 ty = y1 + wwin->frame->top_width;
218 by = y2 - wwin->frame->bottom_width;
219 fw = WMWidthOfString(scr->info_text_font, "8888", 4);
220 fh = WMFontHeight(scr->info_text_font);
222 if (wPreferences.size_display == WDIS_NEW) {
223 XSetForeground(dpy, gc, scr->line_pixel);
225 /* vertical geometry */
226 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
227 x = x2;
228 s = -15;
229 } else {
230 x = x1;
231 s = 15;
233 my = (ty + by) / 2;
235 /* top arrow */
236 /* end bar */
237 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
238 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
240 /* arrowhead */
241 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
242 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
244 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
245 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
247 /* line */
248 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
249 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
251 XDrawSegments(dpy, root, gc, segment, 4);
253 /* bottom arrow */
254 /* end bar */
255 segment[0].y1 = by;
256 segment[0].y2 = by;
258 /* arrowhead */
259 segment[1].y1 = by - 1;
260 segment[1].y2 = by - 7;
262 segment[2].y1 = by - 1;
263 segment[2].y2 = by - 7;
265 /* line */
266 segment[3].y1 = my + fh/2 + 2;
267 segment[3].y2 = by - 1;
269 XDrawSegments(dpy, root, gc, segment, 4);
271 sprintf(num, "%i", (by - ty - wwin->normal_hints->base_height) /
272 wwin->normal_hints->height_inc);
273 fw = WMWidthOfString(scr->info_text_font, num, strlen(num));
275 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
277 /* Display the height. */
278 WMDrawString(scr->wmscreen, root, gc, scr->info_text_font,
279 x - s + 3 - fw/2, my - fh/2 + 1, num, strlen(num));
280 XSetForeground(dpy, gc, scr->line_pixel);
281 /* horizontal geometry */
282 if (y1 < 15) {
283 y = y2;
284 s = -15;
285 } else {
286 y = y1;
287 s = 15;
289 mx = x1 + (x2 - x1)/2;
290 sprintf(num, "%i", (x2 - x1 - wwin->normal_hints->base_width) /
291 wwin->normal_hints->width_inc);
292 fw = WMWidthOfString(scr->info_text_font, num, strlen(num));
294 /* left arrow */
295 /* end bar */
296 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
297 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
299 /* arrowhead */
300 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
301 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
303 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
304 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
306 /* line */
307 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
308 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
310 XDrawSegments(dpy, root, gc, segment, 4);
312 /* right arrow */
313 /* end bar */
314 segment[0].x1 = x2 + 1;
315 segment[0].x2 = x2 + 1;
317 /* arrowhead */
318 segment[1].x1 = x2 - 6;
319 segment[1].x2 = x2;
321 segment[2].x1 = x2;
322 segment[2].x2 = x2 - 6;
324 /* line */
325 segment[3].x1 = mx + fw/2 + 2;
326 segment[3].x2 = x2;
328 XDrawSegments(dpy, root, gc, segment, 4);
330 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
332 /* Display the width. */
333 WMDrawString(scr->wmscreen, root, gc, scr->info_text_font,
334 mx - fw/2 + 1, y - s - fh/2 + 1, num, strlen(num));
335 } else {
336 WSetGeometryViewShownSize(scr->gview,
337 (x2 - x1 - wwin->normal_hints->base_width)
338 / wwin->normal_hints->width_inc,
339 (by - ty - wwin->normal_hints->base_height)
340 / wwin->normal_hints->height_inc);
345 static void
346 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
348 WScreen *scr = wwin->screen_ptr;
350 wPreferences.size_display++;
351 wPreferences.size_display %= NUM_DISPLAYS;
353 if (wPreferences.size_display == WDIS_NEW) {
354 WMUnmapWidget(scr->gview);
355 } else {
356 if (wPreferences.size_display == WDIS_CENTER) {
357 moveGeometryDisplayCentered(scr,
358 scr->scr_width / 2, scr->scr_height / 2);
359 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
360 moveGeometryDisplayCentered(scr, 1, 1);
361 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
362 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
364 WMMapWidget(scr->gview);
365 showGeometry(wwin, x, y, x + w, y + h, dir);
370 static void
371 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
373 WScreen *scr = wwin->screen_ptr;
375 if (wPreferences.size_display == WDIS_NEW)
376 return;
378 if (wPreferences.size_display == WDIS_CENTER) {
379 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
380 scr->scr_height / 2);
381 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
382 moveGeometryDisplayCentered(scr, 1, 1);
383 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
384 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
386 WMMapWidget(scr->gview);
387 showGeometry(wwin, x, y, x + w, y + h, 0);
392 static void
393 doWindowMove(WWindow *wwin, WMBag *bag, int dx, int dy)
395 WWindow *tmpw;
396 int x, y;
397 int scr_width = wwin->screen_ptr->scr_width;
398 int scr_height = wwin->screen_ptr->scr_height;
400 if (!bag || !WMGetBagItemCount(bag)) {
401 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
402 } else {
403 int i;
404 for (i = 0; i < WMGetBagItemCount(bag); i++) {
405 tmpw = WMGetFromBag(bag, i);
406 x = tmpw->frame_x + dx;
407 y = tmpw->frame_y + dy;
409 /* don't let windows become unreachable */
411 if (x + (int)tmpw->frame->core->width < 20)
412 x = 20 - (int)tmpw->frame->core->width;
413 else if (x + 20 > scr_width)
414 x = scr_width - 20;
416 if (y + (int)tmpw->frame->core->height < 20)
417 y = 20 - (int)tmpw->frame->core->height;
418 else if (y + 20 > scr_height)
419 y = scr_height - 20;
421 wWindowMove(tmpw, x, y);
427 static void
428 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
430 Window root = wwin->screen_ptr->root_win;
431 GC gc = wwin->screen_ptr->frame_gc;
432 int h = 0;
433 int bottom = 0;
435 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
436 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
438 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
439 /* Can't use wwin-frame->bottom_width because, in some cases
440 (e.g. interactive placement), frame does not point to anything. */
441 bottom = RESIZEBAR_HEIGHT - 1;
443 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
445 if (h > 0) {
446 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
448 if (bottom > 0) {
449 XDrawLine(dpy, root, gc, x + 1,
450 y + height - bottom,
451 x + width + 1,
452 y + height - bottom);
457 static void
458 drawFrames(WWindow *wwin, WMBag *bag, int dx, int dy)
460 WWindow *tmpw;
461 int scr_width = wwin->screen_ptr->scr_width;
462 int scr_height = wwin->screen_ptr->scr_height;
463 int x, y;
465 if (!bag) {
467 x = wwin->frame_x + dx;
468 y = wwin->frame_y + dy;
470 drawTransparentFrame(wwin, x, y,
471 wwin->frame->core->width,
472 wwin->frame->core->height);
474 } else {
475 int i;
476 for (i = 0; i < WMGetBagItemCount(bag); i++) {
477 tmpw = WMGetFromBag(bag, i);
478 x = tmpw->frame_x + dx;
479 y = tmpw->frame_y + dy;
481 /* don't let windows become unreachable */
483 if (x + (int)tmpw->frame->core->width < 20)
484 x = 20 - (int)tmpw->frame->core->width;
485 else if (x + 20 > scr_width)
486 x = scr_width - 20;
488 if (y + (int)tmpw->frame->core->height < 20)
489 y = 20 - (int)tmpw->frame->core->height;
490 else if (y + 20 > scr_height)
491 y = scr_height - 20;
493 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
494 tmpw->frame->core->height);
501 static void
502 flushMotion()
504 XEvent ev;
506 XSync(dpy, False);
507 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
511 static void
512 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
513 int new_workspace, int rewind)
515 /* do not let window be unmapped */
516 if (opaque_move) {
517 wwin->flags.changing_workspace = 1;
518 wWindowChangeWorkspace(wwin, new_workspace);
520 /* go to new workspace */
521 wWorkspaceChange(scr, new_workspace);
523 wwin->flags.changing_workspace = 0;
525 if (rewind)
526 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
527 else
528 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
530 flushMotion();
532 if (!opaque_move) {
533 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
534 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
535 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
542 typedef struct {
543 /* arrays of WWindows sorted by the respective border position */
544 WWindow **topList; /* top border */
545 WWindow **leftList; /* left border */
546 WWindow **rightList; /* right border */
547 WWindow **bottomList; /* bottom border */
548 int count;
550 /* index of window in the above lists indicating the relative position
551 * of the window with the others */
552 int topIndex;
553 int leftIndex;
554 int rightIndex;
555 int bottomIndex;
557 int rubCount; /* for workspace switching */
559 int winWidth, winHeight; /* width/height of the window */
560 int realX, realY; /* actual position of the window */
561 int calcX, calcY; /* calculated position of window */
562 int omouseX, omouseY; /* old mouse position */
563 int mouseX, mouseY; /* last known position of the pointer */
564 } MoveData;
566 #define WTOP(w) (w)->frame_y
567 #define WLEFT(w) (w)->frame_x
568 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
569 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
571 static int
572 compareWTop(const void *a, const void *b)
574 WWindow *wwin1 = *(WWindow**)a;
575 WWindow *wwin2 = *(WWindow**)b;
577 if (WTOP(wwin1) > WTOP(wwin2))
578 return -1;
579 else if (WTOP(wwin1) < WTOP(wwin2))
580 return 1;
581 else
582 return 0;
586 static int
587 compareWLeft(const void *a, const void *b)
589 WWindow *wwin1 = *(WWindow**)a;
590 WWindow *wwin2 = *(WWindow**)b;
592 if (WLEFT(wwin1) > WLEFT(wwin2))
593 return -1;
594 else if (WLEFT(wwin1) < WLEFT(wwin2))
595 return 1;
596 else
597 return 0;
601 static int
602 compareWRight(const void *a, const void *b)
604 WWindow *wwin1 = *(WWindow**)a;
605 WWindow *wwin2 = *(WWindow**)b;
607 if (WRIGHT(wwin1) < WRIGHT(wwin2))
608 return -1;
609 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
610 return 1;
611 else
612 return 0;
617 static int
618 compareWBottom(const void *a, const void *b)
620 WWindow *wwin1 = *(WWindow**)a;
621 WWindow *wwin2 = *(WWindow**)b;
623 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
624 return -1;
625 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
626 return 1;
627 else
628 return 0;
632 static void
633 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
635 int i;
636 int newX2 = newX + data->winWidth;
637 int newY2 = newY + data->winHeight;
638 Bool ok = False;
640 if (newX < data->realX) {
641 if (data->rightIndex > 0
642 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
643 ok = True;
644 } else if (data->leftIndex <= data->count-1
645 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
646 ok = True;
648 } else if (newX > data->realX) {
649 if (data->leftIndex > 0
650 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
651 ok = True;
652 } else if (data->rightIndex <= data->count-1
653 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
654 ok = True;
658 if (!ok) {
659 if (newY < data->realY) {
660 if (data->bottomIndex > 0
661 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
662 ok = True;
663 } else if (data->topIndex <= data->count-1
664 && newY2 <= WTOP(data->topList[data->topIndex])) {
665 ok = True;
667 } else if (newY > data->realY) {
668 if (data->topIndex > 0
669 && newY2 > WTOP(data->topList[data->topIndex-1])) {
670 ok = True;
671 } else if (data->bottomIndex <= data->count-1
672 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
673 ok = True;
678 if (!ok)
679 return;
681 /* TODO: optimize this */
682 if (data->realY < WBOTTOM(data->bottomList[0])) {
683 data->bottomIndex = 0;
685 if (data->realX < WRIGHT(data->rightList[0])) {
686 data->rightIndex = 0;
688 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
689 data->leftIndex = 0;
691 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
692 data->topIndex = 0;
694 for (i = 0; i < data->count; i++) {
695 if (data->realY > WBOTTOM(data->bottomList[i])) {
696 data->bottomIndex = i + 1;
698 if (data->realX > WRIGHT(data->rightList[i])) {
699 data->rightIndex = i + 1;
701 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
702 data->leftIndex = i + 1;
704 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
705 data->topIndex = i + 1;
711 static void
712 freeMoveData(MoveData *data)
714 if (data->topList)
715 wfree(data->topList);
716 if (data->leftList)
717 wfree(data->leftList);
718 if (data->rightList)
719 wfree(data->rightList);
720 if (data->bottomList)
721 wfree(data->bottomList);
725 static void
726 updateMoveData(WWindow *wwin, MoveData *data)
728 WScreen *scr = wwin->screen_ptr;
729 WWindow *tmp;
730 int i;
732 data->count = 0;
733 tmp = scr->focused_window;
734 while (tmp) {
735 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
736 && !tmp->flags.miniaturized
737 && !tmp->flags.hidden
738 && !tmp->flags.obscured
739 && !WFLAGP(tmp, sunken)) {
740 data->topList[data->count] = tmp;
741 data->leftList[data->count] = tmp;
742 data->rightList[data->count] = tmp;
743 data->bottomList[data->count] = tmp;
744 data->count++;
746 tmp = tmp->prev;
749 if (data->count == 0) {
750 data->topIndex = 0;
751 data->leftIndex = 0;
752 data->rightIndex = 0;
753 data->bottomIndex = 0;
754 return;
758 * order from closest to the border of the screen to farthest
760 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
761 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
762 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
763 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
765 /* figure the position of the window relative to the others */
767 data->topIndex = -1;
768 data->leftIndex = -1;
769 data->rightIndex = -1;
770 data->bottomIndex = -1;
772 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
773 data->bottomIndex = 0;
775 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
776 data->rightIndex = 0;
778 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
779 data->leftIndex = 0;
781 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
782 data->topIndex = 0;
784 for (i = 0; i < data->count; i++) {
785 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
786 data->bottomIndex = i + 1;
788 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
789 data->rightIndex = i + 1;
791 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
792 data->leftIndex = i + 1;
794 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
795 data->topIndex = i + 1;
801 static void
802 initMoveData(WWindow *wwin, MoveData *data)
804 int i;
805 WWindow *tmp;
807 memset(data, 0, sizeof(MoveData));
809 for (i = 0, tmp = wwin->screen_ptr->focused_window;
810 tmp != NULL;
811 tmp = tmp->prev, i++);
813 if (i > 1) {
814 data->topList = wmalloc(sizeof(WWindow*) * i);
815 data->leftList = wmalloc(sizeof(WWindow*) * i);
816 data->rightList = wmalloc(sizeof(WWindow*) * i);
817 data->bottomList = wmalloc(sizeof(WWindow*) * i);
819 updateMoveData(wwin, data);
822 data->realX = wwin->frame_x;
823 data->realY = wwin->frame_y;
824 data->calcX = wwin->frame_x;
825 data->calcY = wwin->frame_y;
827 data->winWidth = wwin->frame->core->width + 2;
828 data->winHeight = wwin->frame->core->height + 2;
832 static Bool
833 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
835 WScreen *scr = wwin->screen_ptr;
836 Bool changed = False;
838 if (data->mouseX <= 1) {
839 if (scr->current_workspace > 0) {
841 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
842 True);
843 changed = True;
844 data->rubCount = 0;
846 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
848 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
849 True);
850 changed = True;
851 data->rubCount = 0;
853 } else if (data->mouseX >= scr->scr_width - 2) {
855 if (scr->current_workspace == scr->workspace_count - 1) {
857 if (wPreferences.ws_cycle
858 || scr->workspace_count == MAX_WORKSPACES) {
860 crossWorkspace(scr, wwin, opaqueMove, 0, False);
861 changed = True;
862 data->rubCount = 0;
864 /* if user insists on trying to go to next workspace even when
865 * it's already the last, create a new one */
866 else if (data->omouseX == data->mouseX
867 && wPreferences.ws_advance) {
869 /* detect user "rubbing" the window against the edge */
870 if (data->rubCount > 0
871 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
873 data->rubCount = -(data->rubCount + 1);
875 } else if (data->rubCount <= 0
876 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
878 data->rubCount = -data->rubCount + 1;
881 /* create a new workspace */
882 if (abs(data->rubCount) > 2) {
883 /* go to next workspace */
884 wWorkspaceNew(scr);
886 crossWorkspace(scr, wwin, opaqueMove,
887 scr->current_workspace+1, False);
888 changed = True;
889 data->rubCount = 0;
891 } else if (scr->current_workspace < scr->workspace_count) {
893 /* go to next workspace */
894 crossWorkspace(scr, wwin, opaqueMove,
895 scr->current_workspace+1, False);
896 changed = True;
897 data->rubCount = 0;
899 } else {
900 data->rubCount = 0;
903 return changed;
907 static void
908 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
909 Bool opaqueMove, int newMouseX, int newMouseY)
911 WScreen *scr = wwin->screen_ptr;
912 int dx, dy; /* how much mouse moved */
913 int winL, winR, winT, winB; /* requested new window position */
914 int newX, newY; /* actual new window position */
915 Bool hresist, vresist;
916 Bool updateIndex;
917 Bool attract;
919 hresist = False;
920 vresist = False;
922 updateIndex = False;
924 /* check the direction of the movement */
925 dx = newMouseX - data->mouseX;
926 dy = newMouseY - data->mouseY;
928 data->omouseX = data->mouseX;
929 data->omouseY = data->mouseY;
930 data->mouseX = newMouseX;
931 data->mouseY = newMouseY;
933 winL = data->calcX + dx;
934 winR = data->calcX + data->winWidth + dx;
935 winT = data->calcY + dy;
936 winB = data->calcY + data->winHeight + dy;
938 newX = data->realX;
939 newY = data->realY;
941 if (doResistance) {
942 int l_edge, r_edge;
943 int edge_l, edge_r;
944 int t_edge, b_edge;
945 int edge_t, edge_b;
946 int resist;
948 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
949 attract = wPreferences.attract;
950 /* horizontal movement: check horizontal edge resistances */
951 if (dx || dy) {
952 int i;
953 /* window is the leftmost window: check against screen edge */
954 l_edge = scr->totalUsableArea.x1;
955 r_edge = scr->totalUsableArea.x2 + resist;
956 edge_l = scr->totalUsableArea.x1 - resist;
957 edge_r = scr->totalUsableArea.x2;
959 /* 1 */
960 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
961 WWindow *looprw;
963 for (i = data->rightIndex - 1; i >= 0; i--) {
964 looprw = data->rightList[i];
965 if (!(data->realY > WBOTTOM(looprw)
966 || (data->realY + data->winHeight) < WTOP(looprw))) {
967 if (attract
968 || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
969 l_edge = WRIGHT(looprw) + 1;
970 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
972 break;
976 if (attract) {
977 for (i = data->rightIndex; i < data->count; i++) {
978 looprw = data->rightList[i];
979 if(!(data->realY > WBOTTOM(looprw)
980 || (data->realY + data->winHeight) < WTOP(looprw))) {
981 r_edge = WRIGHT(looprw) + 1;
982 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
983 break;
989 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
990 WWindow *looprw;
992 for (i = data->leftIndex - 1; i >= 0; i--) {
993 looprw = data->leftList[i];
994 if (!(data->realY > WBOTTOM(looprw)
995 || (data->realY + data->winHeight) < WTOP(looprw))) {
996 if (attract
997 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1)) && dx > 0)) {
998 edge_r = WLEFT(looprw);
999 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1001 break;
1005 if (attract)
1006 for (i = data->leftIndex; i < data->count; i++) {
1007 looprw = data->leftList[i];
1008 if(!(data->realY > WBOTTOM(looprw)
1009 || (data->realY + data->winHeight) < WTOP(looprw))) {
1010 edge_l = WLEFT(looprw);
1011 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1012 break;
1018 printf("%d %d\n",winL,winR);
1019 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1022 if ((winL - l_edge) < (r_edge - winL)) {
1023 if (resist > 0) {
1024 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1025 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1026 newX = l_edge;
1027 hresist = True;
1030 } else {
1031 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1032 newX = r_edge;
1033 hresist = True;
1037 if ((winR - edge_l) < (edge_r - winR)) {
1038 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1039 newX = edge_l - data->winWidth;
1040 hresist = True;
1042 } else {
1043 if (resist > 0) {
1044 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1045 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1046 newX = edge_r - data->winWidth;
1047 hresist = True;
1052 /* VeRT */
1053 t_edge = scr->totalUsableArea.y1;
1054 b_edge = scr->totalUsableArea.y2 + resist;
1055 edge_t = scr->totalUsableArea.y1 - resist;
1056 edge_b = scr->totalUsableArea.y2;
1058 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1059 WWindow *looprw;
1061 for (i = data->bottomIndex - 1; i >= 0; i--) {
1062 looprw = data->bottomList[i];
1063 if (!(data->realX > WRIGHT(looprw)
1064 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1065 if (attract
1066 || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1067 t_edge = WBOTTOM(looprw) + 1;
1068 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1070 break;
1074 if (attract) {
1075 for (i = data->bottomIndex; i < data->count; i++) {
1076 looprw = data->bottomList[i];
1077 if(!(data->realX > WRIGHT(looprw)
1078 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1079 b_edge = WBOTTOM(looprw) + 1;
1080 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1081 break;
1087 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1088 WWindow *looprw;
1090 for (i = data->topIndex - 1; i >= 0; i--) {
1091 looprw = data->topList[i];
1092 if (!(data->realX > WRIGHT(looprw)
1093 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1094 if (attract
1095 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1)) && dy > 0)) {
1096 edge_b = WTOP(looprw);
1097 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1099 break;
1103 if (attract)
1104 for (i = data->topIndex; i < data->count; i++) {
1105 looprw = data->topList[i];
1106 if(!(data->realX > WRIGHT(looprw)
1107 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1108 edge_t = WTOP(looprw);
1109 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1110 break;
1115 if ((winT - t_edge) < (b_edge - winT)) {
1116 if (resist > 0) {
1117 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1118 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1119 newY = t_edge;
1120 vresist = True;
1124 else {
1125 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1126 newY = b_edge;
1127 vresist = True;
1131 if ((winB - edge_t) < (edge_b - winB)) {
1132 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1133 newY = edge_t - data->winHeight;
1134 vresist = True;
1137 else {
1138 if (resist > 0) {
1139 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1140 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1141 newY = edge_b - data->winHeight;
1142 vresist = True;
1147 /* END VeRT */
1151 /* update window position */
1152 data->calcX += dx;
1153 data->calcY += dy;
1155 if (((dx > 0 && data->calcX - data->realX > 0)
1156 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1157 newX = data->calcX;
1159 if (((dy > 0 && data->calcY - data->realY > 0)
1160 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1161 newY = data->calcY;
1163 if (data->realX != newX || data->realY != newY) {
1165 if (wPreferences.move_display == WDIS_NEW
1166 && !scr->selected_windows) {
1167 showPosition(wwin, data->realX, data->realY);
1169 if (opaqueMove) {
1170 doWindowMove(wwin, scr->selected_windows,
1171 newX - wwin->frame_x,
1172 newY - wwin->frame_y);
1173 } else {
1174 /* erase frames */
1175 drawFrames(wwin, scr->selected_windows,
1176 data->realX - wwin->frame_x,
1177 data->realY - wwin->frame_y);
1180 if (!scr->selected_windows
1181 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1183 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1184 newY + data->winHeight/2);
1187 if (!opaqueMove) {
1188 /* draw frames */
1189 drawFrames(wwin, scr->selected_windows,
1190 newX - wwin->frame_x,
1191 newY - wwin->frame_y);
1194 if (!scr->selected_windows) {
1195 showPosition(wwin, newX, newY);
1200 /* recalc relative window position */
1201 if (doResistance && (data->realX != newX || data->realY != newY)) {
1202 updateResistance(wwin, data, newX, newY);
1205 data->realX = newX;
1206 data->realY = newY;
1210 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1213 wKeyboardMoveResizeWindow(WWindow *wwin)
1215 WScreen *scr = wwin->screen_ptr;
1216 Window root = scr->root_win;
1217 XEvent event;
1218 int w = wwin->frame->core->width;
1219 int h = wwin->frame->core->height;
1220 int scr_width = wwin->screen_ptr->scr_width;
1221 int scr_height = wwin->screen_ptr->scr_height;
1222 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1223 int src_x = wwin->frame_x;
1224 int src_y = wwin->frame_y;
1225 int done,off_x,off_y,ww,wh;
1226 int kspeed = _KS;
1227 Time lastTime = 0;
1228 KeySym keysym=NoSymbol;
1229 int moment=0;
1230 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1232 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1233 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1234 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1235 ctrlmode=done=off_x=off_y=0;
1237 XSync(dpy, False);
1238 wusleep(10000);
1239 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1241 if (!wwin->flags.selected) {
1242 wUnselectWindows(scr);
1244 XGrabServer(dpy);
1245 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1246 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1247 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1249 if (wwin->flags.shaded || scr->selected_windows) {
1250 if(scr->selected_windows)
1251 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1252 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1253 if(!scr->selected_windows)
1254 mapPositionDisplay(wwin, src_x, src_y, w, h);
1255 } else {
1256 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1258 ww=w;
1259 wh=h;
1260 while(1) {
1262 looper.ox=off_x;
1263 looper.oy=off_y;
1265 do {
1266 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1267 | ButtonPressMask | ExposureMask, &event);
1268 if (event.type == Expose) {
1269 WMHandleEvent(&event);
1271 } while (event.type == Expose);
1274 while (XCheckTypedEvent(dpy, Expose, &event)) {
1275 WMHandleEvent(&event);
1277 if (wwin->flags.shaded || scr->selected_windows) {
1278 if(scr->selected_windows)
1279 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1280 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1281 /*** I HATE EDGE RESISTANCE - ]d ***/
1283 else {
1284 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1287 if(ctrlmode)
1288 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1290 XUngrabServer(dpy);
1291 XSync(dpy, False);
1293 switch (event.type) {
1294 case KeyPress:
1295 /* accelerate */
1296 if (event.xkey.time - lastTime > 50) {
1297 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1298 } else {
1299 if (kspeed < 20) {
1300 kspeed++;
1303 if (kspeed < _KS) kspeed = _KS;
1304 lastTime = event.xkey.time;
1306 if (event.xkey.state & ControlMask && !wwin->flags.shaded) {
1307 ctrlmode=1;
1308 wUnselectWindows(scr);
1310 else {
1311 ctrlmode=0;
1313 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1314 if (ctrlmode)
1315 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1316 else
1317 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1319 else {
1321 keysym = XLookupKeysym(&event.xkey, 0);
1322 switch (keysym) {
1323 case XK_Return:
1324 done=2;
1325 break;
1326 case XK_Escape:
1327 done=1;
1328 break;
1329 case XK_Up:
1330 #ifdef XK_KP_Up
1331 case XK_KP_Up:
1332 #endif
1333 case XK_k:
1334 if (ctrlmode){
1335 if (moment != UP)
1336 h = wh;
1337 h-=kspeed;
1338 moment = UP;
1339 if (h < 1) h = 1;
1341 else off_y-=kspeed;
1342 break;
1343 case XK_Down:
1344 #ifdef XK_KP_Down
1345 case XK_KP_Down:
1346 #endif
1347 case XK_j:
1348 if (ctrlmode){
1349 if (moment != DOWN)
1350 h = wh;
1351 h+=kspeed;
1352 moment = DOWN;
1354 else off_y+=kspeed;
1355 break;
1356 case XK_Left:
1357 #ifdef XK_KP_Left
1358 case XK_KP_Left:
1359 #endif
1360 case XK_h:
1361 if (ctrlmode) {
1362 if (moment != LEFT)
1363 w = ww;
1364 w-=kspeed;
1365 if (w < 1) w = 1;
1366 moment = LEFT;
1368 else off_x-=kspeed;
1369 break;
1370 case XK_Right:
1371 #ifdef XK_KP_Right
1372 case XK_KP_Right:
1373 #endif
1374 case XK_l:
1375 if (ctrlmode) {
1376 if (moment != RIGHT)
1377 w = ww;
1378 w+=kspeed;
1379 moment = RIGHT;
1381 else off_x+=kspeed;
1382 break;
1385 ww=w;wh=h;
1386 wh-=vert_border;
1387 wWindowConstrainSize(wwin, &ww, &wh);
1388 wh+=vert_border;
1390 if (wPreferences.ws_cycle){
1391 if (src_x + off_x + ww < 20){
1392 if(!scr->current_workspace) {
1393 wWorkspaceChange(scr, scr->workspace_count-1);
1395 else wWorkspaceChange(scr, scr->current_workspace-1);
1396 off_x += scr_width;
1398 else if (src_x + off_x + 20 > scr_width){
1399 if(scr->current_workspace == scr->workspace_count-1) {
1400 wWorkspaceChange(scr, 0);
1402 else wWorkspaceChange(scr, scr->current_workspace+1);
1403 off_x -= scr_width;
1406 else {
1407 if (src_x + off_x + ww < 20)
1408 off_x = 20 - ww - src_x;
1409 else if (src_x + off_x + 20 > scr_width)
1410 off_x = scr_width - 20 - src_x;
1413 if (src_y + off_y + wh < 20) {
1414 off_y = 20 - wh - src_y;
1416 else if (src_y + off_y + 20 > scr_height) {
1417 off_y = scr_height - 20 - src_y;
1420 break;
1421 case ButtonPress:
1422 case ButtonRelease:
1423 done=1;
1424 break;
1425 case Expose:
1426 WMHandleEvent(&event);
1427 while (XCheckTypedEvent(dpy, Expose, &event)) {
1428 WMHandleEvent(&event);
1430 break;
1432 default:
1433 WMHandleEvent(&event);
1434 break;
1437 XGrabServer(dpy);
1438 /*xxx*/
1440 if (wwin->flags.shaded && !scr->selected_windows){
1441 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1442 } else {
1443 if (ctrlmode) {
1444 WMUnmapWidget(scr->gview);
1445 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1446 } else if(!scr->selected_windows) {
1447 WMUnmapWidget(scr->gview);
1448 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1452 if (wwin->flags.shaded || scr->selected_windows) {
1453 if (scr->selected_windows)
1454 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1455 else
1456 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1457 } else {
1458 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1462 if (ctrlmode) {
1463 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1464 } else if(!scr->selected_windows)
1465 showPosition(wwin, src_x+off_x, src_y+off_y);
1466 /**/
1468 if (done) {
1469 scr->keymove_tick=0;
1471 WMDeleteTimerWithClientData(&looper);
1473 if (wwin->flags.shaded || scr->selected_windows) {
1474 if(scr->selected_windows)
1475 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1476 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1478 else {
1479 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1482 if (ctrlmode) {
1483 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1484 WMUnmapWidget(scr->gview);
1485 } else
1486 WMUnmapWidget(scr->gview);
1488 XUngrabKeyboard(dpy, CurrentTime);
1489 XUngrabPointer(dpy, CurrentTime);
1490 XUngrabServer(dpy);
1492 if(done==2) {
1493 if (wwin->flags.shaded || scr->selected_windows) {
1494 if (!scr->selected_windows) {
1495 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1496 wWindowSynthConfigureNotify(wwin);
1497 } else {
1498 int i;
1499 WMBag *bag = scr->selected_windows;
1500 doWindowMove(wwin,scr->selected_windows,off_x,off_y);
1501 for (i = 0; i < WMGetBagItemCount(bag); i++) {
1502 wWindowSynthConfigureNotify(WMGetFromBag(bag, i));
1505 } else {
1506 if (wwin->client.width != ww)
1507 wwin->flags.user_changed_width = 1;
1509 if (wwin->client.height != wh - vert_border)
1510 wwin->flags.user_changed_height = 1;
1512 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1513 ww, wh - vert_border);
1514 wWindowSynthConfigureNotify(wwin);
1516 wWindowChangeWorkspace(wwin, scr->current_workspace);
1517 wSetFocusTo(scr, wwin);
1519 return 1;
1526 *----------------------------------------------------------------------
1527 * wMouseMoveWindow--
1528 * Move the named window and the other selected ones (if any),
1529 * interactively. Also shows the position of the window, if only one
1530 * window is being moved.
1531 * If the window is not on the selected window list, the selected
1532 * windows are deselected.
1533 * If shift is pressed during the operation, the position display
1534 * is changed to another type.
1536 * Returns:
1537 * True if the window was moved, False otherwise.
1539 * Side effects:
1540 * The window(s) position is changed, and the client(s) are
1541 * notified about that.
1542 * The position display configuration may be changed.
1543 *----------------------------------------------------------------------
1546 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1548 WScreen *scr = wwin->screen_ptr;
1549 XEvent event;
1550 Window root = scr->root_win;
1551 KeyCode shiftl, shiftr;
1552 Bool done = False;
1553 int started = 0;
1554 int warped = 0;
1555 /* This needs not to change while moving, else bad things can happen */
1556 int opaqueMove = wPreferences.opaque_move;
1557 MoveData moveData;
1558 #ifdef GHOST_WINDOW_MOVE
1559 RImage *rimg;
1561 rimg = InitGhostWindowMove(scr);
1562 #endif
1565 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1566 XSetWindowAttributes attr;
1568 attr.save_under = True;
1569 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1570 CWSaveUnder, &attr);
1574 initMoveData(wwin, &moveData);
1576 moveData.mouseX = ev->xmotion.x_root;
1577 moveData.mouseY = ev->xmotion.y_root;
1579 if (!wwin->flags.selected) {
1580 /* this window is not selected, unselect others and move only wwin */
1581 wUnselectWindows(scr);
1583 #ifdef DEBUG
1584 puts("Moving window");
1585 #endif
1586 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1587 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1588 while (!done) {
1589 if (warped) {
1590 int junk;
1591 Window junkw;
1593 /* XWarpPointer() doesn't seem to generate Motion events, so
1594 we've got to simulate them */
1595 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1596 &event.xmotion.y_root, &junk, &junk,
1597 (unsigned *) &junk);
1598 } else {
1599 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1600 | PointerMotionHintMask
1601 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1602 &event);
1604 if (event.type == MotionNotify) {
1605 /* compress MotionNotify events */
1606 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1607 if (!checkMouseSamplingRate(&event))
1608 continue;
1611 switch (event.type) {
1612 case KeyPress:
1613 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1614 && started && !scr->selected_windows) {
1616 if (!opaqueMove) {
1617 drawFrames(wwin, scr->selected_windows,
1618 moveData.realX - wwin->frame_x,
1619 moveData.realY - wwin->frame_y);
1622 if (wPreferences.move_display == WDIS_NEW
1623 && !scr->selected_windows) {
1624 showPosition(wwin, moveData.realX, moveData.realY);
1625 XUngrabServer(dpy);
1627 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1628 moveData.winWidth, moveData.winHeight);
1630 if (wPreferences.move_display == WDIS_NEW
1631 && !scr->selected_windows) {
1632 XGrabServer(dpy);
1633 showPosition(wwin, moveData.realX, moveData.realY);
1636 if (!opaqueMove) {
1637 drawFrames(wwin, scr->selected_windows,
1638 moveData.realX - wwin->frame_x,
1639 moveData.realY - wwin->frame_y);
1642 break;
1644 case MotionNotify:
1645 if (started) {
1646 updateWindowPosition(wwin, &moveData,
1647 scr->selected_windows == NULL
1648 && wPreferences.edge_resistance > 0,
1649 opaqueMove,
1650 event.xmotion.x_root,
1651 event.xmotion.y_root);
1653 if (!warped && !wPreferences.no_autowrap) {
1654 int oldWorkspace = scr->current_workspace;
1656 if (wPreferences.move_display == WDIS_NEW
1657 && !scr->selected_windows) {
1658 showPosition(wwin, moveData.realX, moveData.realY);
1659 XUngrabServer(dpy);
1661 if (!opaqueMove) {
1662 drawFrames(wwin, scr->selected_windows,
1663 moveData.realX - wwin->frame_x,
1664 moveData.realY - wwin->frame_y);
1666 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1667 if (scr->current_workspace != oldWorkspace
1668 && wPreferences.edge_resistance > 0
1669 && scr->selected_windows == NULL)
1670 updateMoveData(wwin, &moveData);
1671 warped = 1;
1673 if (!opaqueMove) {
1674 drawFrames(wwin, scr->selected_windows,
1675 moveData.realX - wwin->frame_x,
1676 moveData.realY - wwin->frame_y);
1678 if (wPreferences.move_display == WDIS_NEW
1679 && !scr->selected_windows) {
1680 XSync(dpy, False);
1681 showPosition(wwin, moveData.realX, moveData.realY);
1682 XGrabServer(dpy);
1684 } else {
1685 warped = 0;
1687 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1688 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1690 XChangeActivePointerGrab(dpy, ButtonMotionMask
1691 | ButtonReleaseMask | ButtonPressMask,
1692 wCursor[WCUR_MOVE], CurrentTime);
1693 started = 1;
1694 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1695 CurrentTime);
1697 if (!scr->selected_windows)
1698 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1699 moveData.winWidth, moveData.winHeight);
1701 if (started && !opaqueMove)
1702 drawFrames(wwin, scr->selected_windows, 0, 0);
1704 if (!opaqueMove || (wPreferences.move_display==WDIS_NEW
1705 && !scr->selected_windows)) {
1706 XGrabServer(dpy);
1707 if (wPreferences.move_display==WDIS_NEW)
1708 showPosition(wwin, moveData.realX, moveData.realY);
1711 break;
1713 case ButtonPress:
1714 break;
1716 case ButtonRelease:
1717 if (event.xbutton.button != ev->xbutton.button)
1718 break;
1720 if (started) {
1721 XEvent e;
1722 if (!opaqueMove) {
1723 drawFrames(wwin, scr->selected_windows,
1724 moveData.realX - wwin->frame_x,
1725 moveData.realY - wwin->frame_y);
1726 XSync(dpy, 0);
1727 doWindowMove(wwin, scr->selected_windows,
1728 moveData.realX - wwin->frame_x,
1729 moveData.realY - wwin->frame_y);
1731 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1732 wWindowSynthConfigureNotify(wwin);
1733 #endif
1734 XUngrabKeyboard(dpy, CurrentTime);
1735 XUngrabServer(dpy);
1736 if (!opaqueMove) {
1737 wWindowChangeWorkspace(wwin, scr->current_workspace);
1738 wSetFocusTo(scr, wwin);
1740 if (wPreferences.move_display == WDIS_NEW)
1741 showPosition(wwin, moveData.realX, moveData.realY);
1743 /* discard all enter/leave events that happened until
1744 * the time the button was released */
1745 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1746 if (e.xcrossing.time > event.xbutton.time) {
1747 XPutBackEvent(dpy, &e);
1748 break;
1751 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1752 if (e.xcrossing.time > event.xbutton.time) {
1753 XPutBackEvent(dpy, &e);
1754 break;
1758 if (!scr->selected_windows) {
1759 /* get rid of the geometry window */
1760 WMUnmapWidget(scr->gview);
1763 #ifdef DEBUG
1764 puts("End move window");
1765 #endif
1766 done = True;
1767 break;
1769 default:
1770 if (started && !opaqueMove) {
1771 drawFrames(wwin, scr->selected_windows,
1772 moveData.realX - wwin->frame_x,
1773 moveData.realY - wwin->frame_y);
1774 XUngrabServer(dpy);
1775 WMHandleEvent(&event);
1776 XSync(dpy, False);
1777 XGrabServer(dpy);
1778 drawFrames(wwin, scr->selected_windows,
1779 moveData.realX - wwin->frame_x,
1780 moveData.realY - wwin->frame_y);
1781 } else {
1782 WMHandleEvent(&event);
1784 break;
1788 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1789 XSetWindowAttributes attr;
1792 attr.save_under = False;
1793 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1794 CWSaveUnder, &attr);
1798 freeMoveData(&moveData);
1800 return started;
1804 #define RESIZEBAR 1
1805 #define HCONSTRAIN 2
1807 static int
1808 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1809 int flags)
1811 int w = wwin->frame->core->width - 1;
1812 int cw = wwin->frame->resizebar_corner_width;
1813 int dir;
1815 /* if not resizing through the resizebar */
1816 if (!(flags & RESIZEBAR)) {
1817 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1818 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1819 if (abs(dx) < 2 || abs(dy) < 2) {
1820 if (abs(dy) > abs(dx))
1821 xdir = 0;
1822 else
1823 ydir = 0;
1825 return (xdir | ydir);
1828 /* window is too narrow. allow diagonal resize */
1829 if (cw * 2 >= w) {
1830 int ydir;
1832 if (flags & HCONSTRAIN)
1833 ydir = 0;
1834 else
1835 ydir = DOWN;
1836 if (x < cw)
1837 return (LEFT | ydir);
1838 else
1839 return (RIGHT | ydir);
1841 /* vertical resize */
1842 if ((x > cw) && (x < w - cw))
1843 return DOWN;
1845 if (x < cw)
1846 dir = LEFT;
1847 else
1848 dir = RIGHT;
1850 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1851 dir |= DOWN;
1853 return dir;
1857 void
1858 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1860 XEvent event;
1861 WScreen *scr = wwin->screen_ptr;
1862 Window root = scr->root_win;
1863 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1864 int fw = wwin->frame->core->width;
1865 int fh = wwin->frame->core->height;
1866 int fx = wwin->frame_x;
1867 int fy = wwin->frame_y;
1868 int is_resizebar = (wwin->frame->resizebar
1869 && ev->xany.window==wwin->frame->resizebar->window);
1870 int orig_x, orig_y;
1871 int started;
1872 int dw, dh;
1873 int rw = fw, rh = fh;
1874 int rx1, ry1, rx2, ry2;
1875 int res = 0;
1876 KeyCode shiftl, shiftr;
1877 int h = 0;
1878 int orig_fx = fx;
1879 int orig_fy = fy;
1880 int orig_fw = fw;
1881 int orig_fh = fh;
1883 if (wwin->flags.shaded) {
1884 wwarning("internal error: tryein");
1885 return;
1887 orig_x = ev->xbutton.x_root;
1888 orig_y = ev->xbutton.y_root;
1890 started = 0;
1891 #ifdef DEBUG
1892 puts("Resizing window");
1893 #endif
1895 wUnselectWindows(scr);
1896 rx1 = fx;
1897 rx2 = fx + fw - 1;
1898 ry1 = fy;
1899 ry2 = fy + fh - 1;
1900 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1901 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1902 if (!WFLAGP(wwin, no_titlebar))
1903 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
1904 else
1905 h = 0;
1906 while (1) {
1907 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1908 | ButtonReleaseMask | PointerMotionHintMask
1909 | ButtonPressMask | ExposureMask, &event);
1910 if (!checkMouseSamplingRate(&event))
1911 continue;
1913 switch (event.type) {
1914 case KeyPress:
1915 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1916 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1917 && started) {
1918 drawTransparentFrame(wwin, fx, fy, fw, fh);
1919 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1920 drawTransparentFrame(wwin, fx, fy, fw, fh);
1922 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1923 break;
1925 case MotionNotify:
1926 if (started) {
1927 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1929 dw = 0;
1930 dh = 0;
1932 orig_fx = fx;
1933 orig_fy = fy;
1934 orig_fw = fw;
1935 orig_fh = fh;
1937 if (res & LEFT)
1938 dw = orig_x - event.xmotion.x_root;
1939 else if (res & RIGHT)
1940 dw = event.xmotion.x_root - orig_x;
1941 if (res & UP)
1942 dh = orig_y - event.xmotion.y_root;
1943 else if (res & DOWN)
1944 dh = event.xmotion.y_root - orig_y;
1946 orig_x = event.xmotion.x_root;
1947 orig_y = event.xmotion.y_root;
1949 rw += dw;
1950 rh += dh;
1951 fw = rw;
1952 fh = rh - vert_border;
1953 wWindowConstrainSize(wwin, &fw, &fh);
1954 fh += vert_border;
1955 if (res & LEFT)
1956 fx = rx2 - fw + 1;
1957 else if (res & RIGHT)
1958 fx = rx1;
1959 if (res & UP)
1960 fy = ry2 - fh + 1;
1961 else if (res & DOWN)
1962 fy = ry1;
1963 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1964 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1965 int tx, ty;
1966 Window junkw;
1967 int flags;
1969 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1970 orig_x, orig_y, &tx, &ty, &junkw);
1972 /* check if resizing through resizebar */
1973 if (is_resizebar)
1974 flags = RESIZEBAR;
1975 else
1976 flags = 0;
1978 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1979 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1980 flags |= HCONSTRAIN;
1982 res = getResizeDirection(wwin, tx, ty,
1983 orig_x - event.xmotion.x_root,
1984 orig_y - event.xmotion.y_root, flags);
1986 if (res == (UP|LEFT))
1987 XChangeActivePointerGrab(dpy, ButtonMotionMask
1988 | ButtonReleaseMask | ButtonPressMask,
1989 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1990 else if (res == (UP|RIGHT))
1991 XChangeActivePointerGrab(dpy, ButtonMotionMask
1992 | ButtonReleaseMask | ButtonPressMask,
1993 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1994 else if (res == (DOWN|LEFT))
1995 XChangeActivePointerGrab(dpy, ButtonMotionMask
1996 | ButtonReleaseMask | ButtonPressMask,
1997 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
1998 else if (res == (DOWN|RIGHT))
1999 XChangeActivePointerGrab(dpy, ButtonMotionMask
2000 | ButtonReleaseMask | ButtonPressMask,
2001 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2002 else if (res == DOWN || res == UP)
2003 XChangeActivePointerGrab(dpy, ButtonMotionMask
2004 | ButtonReleaseMask | ButtonPressMask,
2005 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2006 else if (res & (DOWN|UP))
2007 XChangeActivePointerGrab(dpy, ButtonMotionMask
2008 | ButtonReleaseMask | ButtonPressMask,
2009 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2010 else if (res & (LEFT|RIGHT))
2011 XChangeActivePointerGrab(dpy, ButtonMotionMask
2012 | ButtonReleaseMask | ButtonPressMask,
2013 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2015 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
2016 CurrentTime);
2018 XGrabServer(dpy);
2020 /* Draw the resize frame for the first time. */
2021 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2023 drawTransparentFrame(wwin, fx, fy, fw, fh);
2025 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2027 started = 1;
2029 if (started) {
2030 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2031 drawTransparentFrame(wwin, orig_fx, orig_fy,
2032 orig_fw, orig_fh);
2033 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2034 drawTransparentFrame(wwin, fx, fy, fw, fh);
2035 } else {
2036 drawTransparentFrame(wwin, orig_fx, orig_fy,
2037 orig_fw, orig_fh);
2038 drawTransparentFrame(wwin, fx, fy, fw, fh);
2040 if (fh != orig_fh || fw != orig_fw) {
2041 if (wPreferences.size_display == WDIS_NEW) {
2042 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2043 orig_fy + orig_fh, res);
2045 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2048 break;
2050 case ButtonPress:
2051 break;
2053 case ButtonRelease:
2054 if (event.xbutton.button != ev->xbutton.button)
2055 break;
2057 if (started) {
2058 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2060 drawTransparentFrame(wwin, fx, fy, fw, fh);
2062 XUngrabKeyboard(dpy, CurrentTime);
2063 WMUnmapWidget(scr->gview);
2064 XUngrabServer(dpy);
2066 if (wwin->client.width != fw)
2067 wwin->flags.user_changed_width = 1;
2069 if (wwin->client.height != fh - vert_border)
2070 wwin->flags.user_changed_height = 1;
2072 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2074 #ifdef DEBUG
2075 puts("End resize window");
2076 #endif
2077 return;
2079 default:
2080 WMHandleEvent(&event);
2085 #undef LEFT
2086 #undef RIGHT
2087 #undef HORIZONTAL
2088 #undef UP
2089 #undef DOWN
2090 #undef VERTICAL
2091 #undef HCONSTRAIN
2092 #undef RESIZEBAR
2094 void
2095 wUnselectWindows(WScreen *scr)
2097 WWindow *wwin;
2099 if (!scr->selected_windows)
2100 return;
2102 while (WMGetBagItemCount(scr->selected_windows)) {
2103 WMBagIterator dummy;
2105 wwin = WMBagFirst(scr->selected_windows, &dummy);
2106 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2107 wIconSelect(wwin->icon);
2109 wSelectWindow(wwin, False);
2111 WMFreeBag(scr->selected_windows);
2112 scr->selected_windows = NULL;
2115 #ifndef LITE
2116 static void
2117 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
2119 WWindow *tmpw;
2121 /* select the windows and put them in the selected window list */
2122 tmpw = scr->focused_window;
2123 while (tmpw != NULL) {
2124 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2125 if ((tmpw->frame->workspace == scr->current_workspace
2126 || IS_OMNIPRESENT(tmpw))
2127 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2128 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2129 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2130 wSelectWindow(tmpw, True);
2133 tmpw = tmpw->prev;
2138 void
2139 wSelectWindows(WScreen *scr, XEvent *ev)
2141 XEvent event;
2142 Window root = scr->root_win;
2143 GC gc = scr->frame_gc;
2144 int xp = ev->xbutton.x_root;
2145 int yp = ev->xbutton.y_root;
2146 int w = 0, h = 0;
2147 int x = xp, y = yp;
2149 #ifdef DEBUG
2150 puts("Selecting windows");
2151 #endif
2152 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2153 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2154 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
2155 CurrentTime) != Success) {
2156 return;
2158 XGrabServer(dpy);
2160 wUnselectWindows(scr);
2162 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2163 while (1) {
2164 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
2165 | ButtonPressMask, &event);
2167 switch (event.type) {
2168 case MotionNotify:
2169 XDrawRectangle(dpy, root, gc, x, y, w, h);
2170 x = event.xmotion.x_root;
2171 if (x < xp) {
2172 w = xp - x;
2173 } else {
2174 w = x - xp;
2175 x = xp;
2177 y = event.xmotion.y_root;
2178 if (y < yp) {
2179 h = yp - y;
2180 } else {
2181 h = y - yp;
2182 y = yp;
2184 XDrawRectangle(dpy, root, gc, x, y, w, h);
2185 break;
2187 case ButtonPress:
2188 break;
2190 case ButtonRelease:
2191 if (event.xbutton.button != ev->xbutton.button)
2192 break;
2194 XDrawRectangle(dpy, root, gc, x, y, w, h);
2195 XUngrabServer(dpy);
2196 XUngrabPointer(dpy, CurrentTime);
2197 selectWindowsInside(scr, x, y, x + w, y + h);
2199 #ifdef KWM_HINTS
2200 wKWMSelectRootRegion(scr, xp, yp, w, h,
2201 event.xbutton.state & ControlMask);
2202 #endif /* KWM_HINTS */
2204 #ifdef DEBUG
2205 puts("End window selection");
2206 #endif
2207 return;
2209 default:
2210 WMHandleEvent(&event);
2211 break;
2215 #endif /* !LITE */
2217 void
2218 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2219 unsigned width, unsigned height)
2221 WScreen *scr = wwin->screen_ptr;
2222 Window root = scr->root_win;
2223 int x, y, h = 0;
2224 XEvent event;
2225 KeyCode shiftl, shiftr;
2226 Window junkw;
2227 int junk;
2229 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2230 GrabModeAsync, GrabModeAsync, None,
2231 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2232 *x_ret = 0;
2233 *y_ret = 0;
2234 return;
2236 if (!WFLAGP(wwin, no_titlebar)) {
2237 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
2238 height += h;
2240 if (!WFLAGP(wwin, no_resizebar)) {
2241 height += RESIZEBAR_HEIGHT;
2243 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2244 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2245 (unsigned *) &junk);
2246 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2248 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2250 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2251 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2252 while (1) {
2253 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2254 &event);
2256 if (!checkMouseSamplingRate(&event))
2257 continue;
2259 switch (event.type) {
2260 case KeyPress:
2261 if ((event.xkey.keycode == shiftl)
2262 || (event.xkey.keycode == shiftr)) {
2263 drawTransparentFrame(wwin,
2264 x - width/2, y - h/2, width, height);
2265 cyclePositionDisplay(wwin,
2266 x - width/2, y - h/2, width, height);
2267 drawTransparentFrame(wwin,
2268 x - width/2, y - h/2, width, height);
2270 break;
2272 case MotionNotify:
2273 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2275 x = event.xmotion.x_root;
2276 y = event.xmotion.y_root;
2278 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2279 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2281 showPosition(wwin, x - width/2, y - h/2);
2283 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2285 break;
2287 case ButtonPress:
2288 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2289 XSync(dpy, 0);
2290 *x_ret = x - width/2;
2291 *y_ret = y - h/2;
2292 XUngrabPointer(dpy, CurrentTime);
2293 XUngrabKeyboard(dpy, CurrentTime);
2294 /* get rid of the geometry window */
2295 WMUnmapWidget(scr->gview);
2296 return;
2298 default:
2299 WMHandleEvent(&event);
2300 break;