- s/sprintf/snprintf
[wmaker-crm.git] / src / moveres.c
blobe6db658895bbf023eaaa247a421e21c419c4c0b2
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 5
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 || wPreferences.move_display == WDIS_NONE) {
172 WMUnmapWidget(scr->gview);
173 } else {
174 if (wPreferences.move_display == WDIS_CENTER) {
175 moveGeometryDisplayCentered(scr,
176 scr->scr_width/2, scr->scr_height/2);
177 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
178 moveGeometryDisplayCentered(scr, 1, 1);
179 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
180 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
182 WMMapWidget(scr->gview);
187 static void
188 mapPositionDisplay(WWindow *wwin, int x, int y, int w, int h)
190 WScreen *scr = wwin->screen_ptr;
192 if (wPreferences.move_display == WDIS_NEW
193 || wPreferences.move_display == WDIS_NONE) {
194 return;
195 } else if (wPreferences.move_display == WDIS_CENTER) {
196 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
197 scr->scr_height / 2);
198 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
199 moveGeometryDisplayCentered(scr, 1, 1);
200 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
201 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
203 WMMapWidget(scr->gview);
204 WSetGeometryViewShownPosition(scr->gview, x, y);
208 static void
209 showGeometry(WWindow *wwin, int x1, int y1, int x2, int y2, int direction)
211 WScreen *scr = wwin->screen_ptr;
212 Window root = scr->root_win;
213 GC gc = scr->line_gc;
214 int ty, by, my, x, y, mx, s;
215 char num[16];
216 XSegment segment[4];
217 int fw, fh;
219 ty = y1 + wwin->frame->top_width;
220 by = y2 - wwin->frame->bottom_width;
221 fw = WMWidthOfString(scr->info_text_font, "8888", 4);
222 fh = WMFontHeight(scr->info_text_font);
224 if (wPreferences.size_display == WDIS_NEW) {
225 XSetForeground(dpy, gc, scr->line_pixel);
227 /* vertical geometry */
228 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
229 x = x2;
230 s = -15;
231 } else {
232 x = x1;
233 s = 15;
235 my = (ty + by) / 2;
237 /* top arrow */
238 /* end bar */
239 segment[0].x1 = x - (s + 6); segment[0].y1 = ty;
240 segment[0].x2 = x - (s - 10); segment[0].y2 = ty;
242 /* arrowhead */
243 segment[1].x1 = x - (s - 2); segment[1].y1 = ty + 1;
244 segment[1].x2 = x - (s - 5); segment[1].y2 = ty + 7;
246 segment[2].x1 = x - (s - 2); segment[2].y1 = ty + 1;
247 segment[2].x2 = x - (s + 1); segment[2].y2 = ty + 7;
249 /* line */
250 segment[3].x1 = x - (s - 2); segment[3].y1 = ty + 1;
251 segment[3].x2 = x - (s - 2); segment[3].y2 = my - fh/2 - 1;
253 XDrawSegments(dpy, root, gc, segment, 4);
255 /* bottom arrow */
256 /* end bar */
257 segment[0].y1 = by;
258 segment[0].y2 = by;
260 /* arrowhead */
261 segment[1].y1 = by - 1;
262 segment[1].y2 = by - 7;
264 segment[2].y1 = by - 1;
265 segment[2].y2 = by - 7;
267 /* line */
268 segment[3].y1 = my + fh/2 + 2;
269 segment[3].y2 = by - 1;
271 XDrawSegments(dpy, root, gc, segment, 4);
273 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
274 wwin->normal_hints->height_inc);
275 fw = WMWidthOfString(scr->info_text_font, num, strlen(num));
277 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
279 /* Display the height. */
280 WMDrawString(scr->wmscreen, root, gc, scr->info_text_font,
281 x - s + 3 - fw/2, my - fh/2 + 1, num, strlen(num));
282 XSetForeground(dpy, gc, scr->line_pixel);
283 /* horizontal geometry */
284 if (y1 < 15) {
285 y = y2;
286 s = -15;
287 } else {
288 y = y1;
289 s = 15;
291 mx = x1 + (x2 - x1)/2;
292 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
293 wwin->normal_hints->width_inc);
294 fw = WMWidthOfString(scr->info_text_font, num, strlen(num));
296 /* left arrow */
297 /* end bar */
298 segment[0].x1 = x1; segment[0].y1 = y - (s + 6);
299 segment[0].x2 = x1; segment[0].y2 = y - (s - 10);
301 /* arrowhead */
302 segment[1].x1 = x1 + 7; segment[1].y1 = y - (s + 1);
303 segment[1].x2 = x1 + 1; segment[1].y2 = y - (s - 2);
305 segment[2].x1 = x1 + 1; segment[2].y1 = y - (s - 2);
306 segment[2].x2 = x1 + 7; segment[2].y2 = y - (s - 5);
308 /* line */
309 segment[3].x1 = x1 + 1; segment[3].y1 = y - (s - 2);
310 segment[3].x2 = mx - fw/2 - 2; segment[3].y2 = y - (s - 2);
312 XDrawSegments(dpy, root, gc, segment, 4);
314 /* right arrow */
315 /* end bar */
316 segment[0].x1 = x2 + 1;
317 segment[0].x2 = x2 + 1;
319 /* arrowhead */
320 segment[1].x1 = x2 - 6;
321 segment[1].x2 = x2;
323 segment[2].x1 = x2;
324 segment[2].x2 = x2 - 6;
326 /* line */
327 segment[3].x1 = mx + fw/2 + 2;
328 segment[3].x2 = x2;
330 XDrawSegments(dpy, root, gc, segment, 4);
332 /* XSetForeground(dpy, gc, scr->window_title_pixel[WS_UNFOCUSED]); */
334 /* Display the width. */
335 WMDrawString(scr->wmscreen, root, gc, scr->info_text_font,
336 mx - fw/2 + 1, y - s - fh/2 + 1, num, strlen(num));
337 } else {
338 WSetGeometryViewShownSize(scr->gview,
339 (x2 - x1 - wwin->normal_hints->base_width)
340 / wwin->normal_hints->width_inc,
341 (by - ty - wwin->normal_hints->base_height)
342 / wwin->normal_hints->height_inc);
347 static void
348 cycleGeometryDisplay(WWindow *wwin, int x, int y, int w, int h, int dir)
350 WScreen *scr = wwin->screen_ptr;
352 wPreferences.size_display++;
353 wPreferences.size_display %= NUM_DISPLAYS;
355 if (wPreferences.size_display == WDIS_NEW
356 || wPreferences.size_display == WDIS_NONE) {
357 WMUnmapWidget(scr->gview);
358 } else {
359 if (wPreferences.size_display == WDIS_CENTER) {
360 moveGeometryDisplayCentered(scr,
361 scr->scr_width / 2, scr->scr_height / 2);
362 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
363 moveGeometryDisplayCentered(scr, 1, 1);
364 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
365 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
367 WMMapWidget(scr->gview);
368 showGeometry(wwin, x, y, x + w, y + h, dir);
373 static void
374 mapGeometryDisplay(WWindow *wwin, int x, int y, int w, int h)
376 WScreen *scr = wwin->screen_ptr;
378 if (wPreferences.size_display == WDIS_NEW
379 || wPreferences.size_display == WDIS_NONE)
380 return;
382 if (wPreferences.size_display == WDIS_CENTER) {
383 moveGeometryDisplayCentered(scr, scr->scr_width / 2,
384 scr->scr_height / 2);
385 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
386 moveGeometryDisplayCentered(scr, 1, 1);
387 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
388 moveGeometryDisplayCentered(scr, x + w/2, y + h/2);
390 WMMapWidget(scr->gview);
391 showGeometry(wwin, x, y, x + w, y + h, 0);
396 static void
397 doWindowMove(WWindow *wwin, WMArray *array, int dx, int dy)
399 WWindow *tmpw;
400 int x, y;
401 int scr_width = wwin->screen_ptr->scr_width;
402 int scr_height = wwin->screen_ptr->scr_height;
404 if (!array || !WMGetArrayItemCount(array)) {
405 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
406 } else {
407 WMArrayIterator iter;
409 WM_ITERATE_ARRAY(array, tmpw, iter) {
410 x = tmpw->frame_x + dx;
411 y = tmpw->frame_y + dy;
413 /* don't let windows become unreachable */
415 if (x + (int)tmpw->frame->core->width < 20)
416 x = 20 - (int)tmpw->frame->core->width;
417 else if (x + 20 > scr_width)
418 x = scr_width - 20;
420 if (y + (int)tmpw->frame->core->height < 20)
421 y = 20 - (int)tmpw->frame->core->height;
422 else if (y + 20 > scr_height)
423 y = scr_height - 20;
425 wWindowMove(tmpw, x, y);
431 static void
432 drawTransparentFrame(WWindow *wwin, int x, int y, int width, int height)
434 Window root = wwin->screen_ptr->root_win;
435 GC gc = wwin->screen_ptr->frame_gc;
436 int h = 0;
437 int bottom = 0;
439 if (!WFLAGP(wwin, no_titlebar) && !wwin->flags.shaded) {
440 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
442 if (!WFLAGP(wwin, no_resizebar) && !wwin->flags.shaded) {
443 /* Can't use wwin-frame->bottom_width because, in some cases
444 (e.g. interactive placement), frame does not point to anything. */
445 bottom = RESIZEBAR_HEIGHT - 1;
447 XDrawRectangle(dpy, root, gc, x, y, width + 1, height + 1);
449 if (h > 0) {
450 XDrawLine(dpy, root, gc, x + 1, y + h, x + width + 1, y + h);
452 if (bottom > 0) {
453 XDrawLine(dpy, root, gc, x + 1,
454 y + height - bottom,
455 x + width + 1,
456 y + height - bottom);
461 static void
462 drawFrames(WWindow *wwin, WMArray *array, int dx, int dy)
464 WWindow *tmpw;
465 int scr_width = wwin->screen_ptr->scr_width;
466 int scr_height = wwin->screen_ptr->scr_height;
467 int x, y;
469 if (!array) {
471 x = wwin->frame_x + dx;
472 y = wwin->frame_y + dy;
474 drawTransparentFrame(wwin, x, y,
475 wwin->frame->core->width,
476 wwin->frame->core->height);
478 } else {
479 WMArrayIterator iter;
481 WM_ITERATE_ARRAY(array, tmpw, iter) {
482 x = tmpw->frame_x + dx;
483 y = tmpw->frame_y + dy;
485 /* don't let windows become unreachable */
487 if (x + (int)tmpw->frame->core->width < 20)
488 x = 20 - (int)tmpw->frame->core->width;
489 else if (x + 20 > scr_width)
490 x = scr_width - 20;
492 if (y + (int)tmpw->frame->core->height < 20)
493 y = 20 - (int)tmpw->frame->core->height;
494 else if (y + 20 > scr_height)
495 y = scr_height - 20;
497 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width,
498 tmpw->frame->core->height);
505 static void
506 flushMotion()
508 XEvent ev;
510 XSync(dpy, False);
511 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
515 static void
516 crossWorkspace(WScreen *scr, WWindow *wwin, int opaque_move,
517 int new_workspace, int rewind)
519 /* do not let window be unmapped */
520 if (opaque_move) {
521 wwin->flags.changing_workspace = 1;
522 wWindowChangeWorkspace(wwin, new_workspace);
524 /* go to new workspace */
525 wWorkspaceChange(scr, new_workspace);
527 wwin->flags.changing_workspace = 0;
529 if (rewind)
530 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
531 else
532 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
534 flushMotion();
536 if (!opaque_move) {
537 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
538 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
539 GrabModeAsync, None, wCursor[WCUR_MOVE], CurrentTime);
546 typedef struct {
547 /* arrays of WWindows sorted by the respective border position */
548 WWindow **topList; /* top border */
549 WWindow **leftList; /* left border */
550 WWindow **rightList; /* right border */
551 WWindow **bottomList; /* bottom border */
552 int count;
554 /* index of window in the above lists indicating the relative position
555 * of the window with the others */
556 int topIndex;
557 int leftIndex;
558 int rightIndex;
559 int bottomIndex;
561 int rubCount; /* for workspace switching */
563 int winWidth, winHeight; /* width/height of the window */
564 int realX, realY; /* actual position of the window */
565 int calcX, calcY; /* calculated position of window */
566 int omouseX, omouseY; /* old mouse position */
567 int mouseX, mouseY; /* last known position of the pointer */
568 } MoveData;
570 #define WTOP(w) (w)->frame_y
571 #define WLEFT(w) (w)->frame_x
572 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width + FRAME_BORDER_WIDTH)
573 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height + FRAME_BORDER_WIDTH)
575 static int
576 compareWTop(const void *a, const void *b)
578 WWindow *wwin1 = *(WWindow**)a;
579 WWindow *wwin2 = *(WWindow**)b;
581 if (WTOP(wwin1) > WTOP(wwin2))
582 return -1;
583 else if (WTOP(wwin1) < WTOP(wwin2))
584 return 1;
585 else
586 return 0;
590 static int
591 compareWLeft(const void *a, const void *b)
593 WWindow *wwin1 = *(WWindow**)a;
594 WWindow *wwin2 = *(WWindow**)b;
596 if (WLEFT(wwin1) > WLEFT(wwin2))
597 return -1;
598 else if (WLEFT(wwin1) < WLEFT(wwin2))
599 return 1;
600 else
601 return 0;
605 static int
606 compareWRight(const void *a, const void *b)
608 WWindow *wwin1 = *(WWindow**)a;
609 WWindow *wwin2 = *(WWindow**)b;
611 if (WRIGHT(wwin1) < WRIGHT(wwin2))
612 return -1;
613 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
614 return 1;
615 else
616 return 0;
621 static int
622 compareWBottom(const void *a, const void *b)
624 WWindow *wwin1 = *(WWindow**)a;
625 WWindow *wwin2 = *(WWindow**)b;
627 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
628 return -1;
629 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
630 return 1;
631 else
632 return 0;
636 static void
637 updateResistance(WWindow *wwin, MoveData *data, int newX, int newY)
639 int i;
640 int newX2 = newX + data->winWidth;
641 int newY2 = newY + data->winHeight;
642 Bool ok = False;
644 if (newX < data->realX) {
645 if (data->rightIndex > 0
646 && newX < WRIGHT(data->rightList[data->rightIndex-1])) {
647 ok = True;
648 } else if (data->leftIndex <= data->count-1
649 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
650 ok = True;
652 } else if (newX > data->realX) {
653 if (data->leftIndex > 0
654 && newX2 > WLEFT(data->leftList[data->leftIndex-1])) {
655 ok = True;
656 } else if (data->rightIndex <= data->count-1
657 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
658 ok = True;
662 if (!ok) {
663 if (newY < data->realY) {
664 if (data->bottomIndex > 0
665 && newY < WBOTTOM(data->bottomList[data->bottomIndex-1])) {
666 ok = True;
667 } else if (data->topIndex <= data->count-1
668 && newY2 <= WTOP(data->topList[data->topIndex])) {
669 ok = True;
671 } else if (newY > data->realY) {
672 if (data->topIndex > 0
673 && newY2 > WTOP(data->topList[data->topIndex-1])) {
674 ok = True;
675 } else if (data->bottomIndex <= data->count-1
676 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
677 ok = True;
682 if (!ok)
683 return;
685 /* TODO: optimize this */
686 if (data->realY < WBOTTOM(data->bottomList[0])) {
687 data->bottomIndex = 0;
689 if (data->realX < WRIGHT(data->rightList[0])) {
690 data->rightIndex = 0;
692 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
693 data->leftIndex = 0;
695 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
696 data->topIndex = 0;
698 for (i = 0; i < data->count; i++) {
699 if (data->realY > WBOTTOM(data->bottomList[i])) {
700 data->bottomIndex = i + 1;
702 if (data->realX > WRIGHT(data->rightList[i])) {
703 data->rightIndex = i + 1;
705 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
706 data->leftIndex = i + 1;
708 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
709 data->topIndex = i + 1;
715 static void
716 freeMoveData(MoveData *data)
718 if (data->topList)
719 wfree(data->topList);
720 if (data->leftList)
721 wfree(data->leftList);
722 if (data->rightList)
723 wfree(data->rightList);
724 if (data->bottomList)
725 wfree(data->bottomList);
729 static void
730 updateMoveData(WWindow *wwin, MoveData *data)
732 WScreen *scr = wwin->screen_ptr;
733 WWindow *tmp;
734 int i;
736 data->count = 0;
737 tmp = scr->focused_window;
738 while (tmp) {
739 if (tmp != wwin && scr->current_workspace == tmp->frame->workspace
740 && !tmp->flags.miniaturized
741 && !tmp->flags.hidden
742 && !tmp->flags.obscured
743 && !WFLAGP(tmp, sunken)) {
744 data->topList[data->count] = tmp;
745 data->leftList[data->count] = tmp;
746 data->rightList[data->count] = tmp;
747 data->bottomList[data->count] = tmp;
748 data->count++;
750 tmp = tmp->prev;
753 if (data->count == 0) {
754 data->topIndex = 0;
755 data->leftIndex = 0;
756 data->rightIndex = 0;
757 data->bottomIndex = 0;
758 return;
762 * order from closest to the border of the screen to farthest
764 qsort(data->topList, data->count, sizeof(WWindow**), compareWTop);
765 qsort(data->leftList, data->count, sizeof(WWindow**), compareWLeft);
766 qsort(data->rightList, data->count, sizeof(WWindow**), compareWRight);
767 qsort(data->bottomList, data->count, sizeof(WWindow**), compareWBottom);
769 /* figure the position of the window relative to the others */
771 data->topIndex = -1;
772 data->leftIndex = -1;
773 data->rightIndex = -1;
774 data->bottomIndex = -1;
776 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
777 data->bottomIndex = 0;
779 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
780 data->rightIndex = 0;
782 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
783 data->leftIndex = 0;
785 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
786 data->topIndex = 0;
788 for (i = 0; i < data->count; i++) {
789 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
790 data->bottomIndex = i + 1;
792 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
793 data->rightIndex = i + 1;
795 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
796 data->leftIndex = i + 1;
798 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
799 data->topIndex = i + 1;
805 static void
806 initMoveData(WWindow *wwin, MoveData *data)
808 int i;
809 WWindow *tmp;
811 memset(data, 0, sizeof(MoveData));
813 for (i = 0, tmp = wwin->screen_ptr->focused_window;
814 tmp != NULL;
815 tmp = tmp->prev, i++);
817 if (i > 1) {
818 data->topList = wmalloc(sizeof(WWindow*) * i);
819 data->leftList = wmalloc(sizeof(WWindow*) * i);
820 data->rightList = wmalloc(sizeof(WWindow*) * i);
821 data->bottomList = wmalloc(sizeof(WWindow*) * i);
823 updateMoveData(wwin, data);
826 data->realX = wwin->frame_x;
827 data->realY = wwin->frame_y;
828 data->calcX = wwin->frame_x;
829 data->calcY = wwin->frame_y;
831 data->winWidth = wwin->frame->core->width + 2;
832 data->winHeight = wwin->frame->core->height + 2;
836 static Bool
837 checkWorkspaceChange(WWindow *wwin, MoveData *data, Bool opaqueMove)
839 WScreen *scr = wwin->screen_ptr;
840 Bool changed = False;
842 if (data->mouseX <= 1) {
843 if (scr->current_workspace > 0) {
845 crossWorkspace(scr, wwin, opaqueMove, scr->current_workspace - 1,
846 True);
847 changed = True;
848 data->rubCount = 0;
850 } else if (scr->current_workspace == 0 && wPreferences.ws_cycle) {
852 crossWorkspace(scr, wwin, opaqueMove, scr->workspace_count - 1,
853 True);
854 changed = True;
855 data->rubCount = 0;
857 } else if (data->mouseX >= scr->scr_width - 2) {
859 if (scr->current_workspace == scr->workspace_count - 1) {
861 if (wPreferences.ws_cycle
862 || scr->workspace_count == MAX_WORKSPACES) {
864 crossWorkspace(scr, wwin, opaqueMove, 0, False);
865 changed = True;
866 data->rubCount = 0;
868 /* if user insists on trying to go to next workspace even when
869 * it's already the last, create a new one */
870 else if (data->omouseX == data->mouseX
871 && wPreferences.ws_advance) {
873 /* detect user "rubbing" the window against the edge */
874 if (data->rubCount > 0
875 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
877 data->rubCount = -(data->rubCount + 1);
879 } else if (data->rubCount <= 0
880 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
882 data->rubCount = -data->rubCount + 1;
885 /* create a new workspace */
886 if (abs(data->rubCount) > 2) {
887 /* go to next workspace */
888 wWorkspaceNew(scr);
890 crossWorkspace(scr, wwin, opaqueMove,
891 scr->current_workspace+1, False);
892 changed = True;
893 data->rubCount = 0;
895 } else if (scr->current_workspace < scr->workspace_count) {
897 /* go to next workspace */
898 crossWorkspace(scr, wwin, opaqueMove,
899 scr->current_workspace+1, False);
900 changed = True;
901 data->rubCount = 0;
903 } else {
904 data->rubCount = 0;
907 return changed;
911 static void
912 updateWindowPosition(WWindow *wwin, MoveData *data, Bool doResistance,
913 Bool opaqueMove, int newMouseX, int newMouseY)
915 WScreen *scr = wwin->screen_ptr;
916 int dx, dy; /* how much mouse moved */
917 int winL, winR, winT, winB; /* requested new window position */
918 int newX, newY; /* actual new window position */
919 Bool hresist, vresist;
920 Bool updateIndex;
921 Bool attract;
923 hresist = False;
924 vresist = False;
926 updateIndex = False;
928 /* check the direction of the movement */
929 dx = newMouseX - data->mouseX;
930 dy = newMouseY - data->mouseY;
932 data->omouseX = data->mouseX;
933 data->omouseY = data->mouseY;
934 data->mouseX = newMouseX;
935 data->mouseY = newMouseY;
937 winL = data->calcX + dx;
938 winR = data->calcX + data->winWidth + dx;
939 winT = data->calcY + dy;
940 winB = data->calcY + data->winHeight + dy;
942 newX = data->realX;
943 newY = data->realY;
945 if (doResistance) {
946 int l_edge, r_edge;
947 int edge_l, edge_r;
948 int t_edge, b_edge;
949 int edge_t, edge_b;
950 int resist;
952 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
953 attract = wPreferences.attract;
954 /* horizontal movement: check horizontal edge resistances */
955 if (dx || dy) {
956 int i;
957 /* window is the leftmost window: check against screen edge */
958 l_edge = scr->totalUsableArea.x1;
959 r_edge = scr->totalUsableArea.x2 + resist;
960 edge_l = scr->totalUsableArea.x1 - resist;
961 edge_r = scr->totalUsableArea.x2;
963 /* 1 */
964 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
965 WWindow *looprw;
967 for (i = data->rightIndex - 1; i >= 0; i--) {
968 looprw = data->rightList[i];
969 if (!(data->realY > WBOTTOM(looprw)
970 || (data->realY + data->winHeight) < WTOP(looprw))) {
971 if (attract
972 || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
973 l_edge = WRIGHT(looprw) + 1;
974 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
976 break;
980 if (attract) {
981 for (i = data->rightIndex; i < data->count; i++) {
982 looprw = data->rightList[i];
983 if(!(data->realY > WBOTTOM(looprw)
984 || (data->realY + data->winHeight) < WTOP(looprw))) {
985 r_edge = WRIGHT(looprw) + 1;
986 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
987 break;
993 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
994 WWindow *looprw;
996 for (i = data->leftIndex - 1; i >= 0; i--) {
997 looprw = data->leftList[i];
998 if (!(data->realY > WBOTTOM(looprw)
999 || (data->realY + data->winHeight) < WTOP(looprw))) {
1000 if (attract
1001 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1)) && dx > 0)) {
1002 edge_r = WLEFT(looprw);
1003 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1005 break;
1009 if (attract)
1010 for (i = data->leftIndex; i < data->count; i++) {
1011 looprw = data->leftList[i];
1012 if(!(data->realY > WBOTTOM(looprw)
1013 || (data->realY + data->winHeight) < WTOP(looprw))) {
1014 edge_l = WLEFT(looprw);
1015 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1016 break;
1022 printf("%d %d\n",winL,winR);
1023 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1026 if ((winL - l_edge) < (r_edge - winL)) {
1027 if (resist > 0) {
1028 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1029 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1030 newX = l_edge;
1031 hresist = True;
1034 } else {
1035 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1036 newX = r_edge;
1037 hresist = True;
1041 if ((winR - edge_l) < (edge_r - winR)) {
1042 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1043 newX = edge_l - data->winWidth;
1044 hresist = True;
1046 } else {
1047 if (resist > 0) {
1048 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1049 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1050 newX = edge_r - data->winWidth;
1051 hresist = True;
1056 /* VeRT */
1057 t_edge = scr->totalUsableArea.y1;
1058 b_edge = scr->totalUsableArea.y2 + resist;
1059 edge_t = scr->totalUsableArea.y1 - resist;
1060 edge_b = scr->totalUsableArea.y2;
1062 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1063 WWindow *looprw;
1065 for (i = data->bottomIndex - 1; i >= 0; i--) {
1066 looprw = data->bottomList[i];
1067 if (!(data->realX > WRIGHT(looprw)
1068 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1069 if (attract
1070 || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1071 t_edge = WBOTTOM(looprw) + 1;
1072 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1074 break;
1078 if (attract) {
1079 for (i = data->bottomIndex; i < data->count; i++) {
1080 looprw = data->bottomList[i];
1081 if(!(data->realX > WRIGHT(looprw)
1082 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1083 b_edge = WBOTTOM(looprw) + 1;
1084 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1085 break;
1091 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1092 WWindow *looprw;
1094 for (i = data->topIndex - 1; i >= 0; i--) {
1095 looprw = data->topList[i];
1096 if (!(data->realX > WRIGHT(looprw)
1097 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1098 if (attract
1099 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1)) && dy > 0)) {
1100 edge_b = WTOP(looprw);
1101 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1103 break;
1107 if (attract)
1108 for (i = data->topIndex; i < data->count; i++) {
1109 looprw = data->topList[i];
1110 if(!(data->realX > WRIGHT(looprw)
1111 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1112 edge_t = WTOP(looprw);
1113 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1114 break;
1119 if ((winT - t_edge) < (b_edge - winT)) {
1120 if (resist > 0) {
1121 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1122 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1123 newY = t_edge;
1124 vresist = True;
1128 else {
1129 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1130 newY = b_edge;
1131 vresist = True;
1135 if ((winB - edge_t) < (edge_b - winB)) {
1136 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1137 newY = edge_t - data->winHeight;
1138 vresist = True;
1141 else {
1142 if (resist > 0) {
1143 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1144 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1145 newY = edge_b - data->winHeight;
1146 vresist = True;
1151 /* END VeRT */
1155 /* update window position */
1156 data->calcX += dx;
1157 data->calcY += dy;
1159 if (((dx > 0 && data->calcX - data->realX > 0)
1160 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1161 newX = data->calcX;
1163 if (((dy > 0 && data->calcY - data->realY > 0)
1164 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1165 newY = data->calcY;
1167 if (data->realX != newX || data->realY != newY) {
1169 if (wPreferences.move_display == WDIS_NEW
1170 && !scr->selected_windows) {
1171 showPosition(wwin, data->realX, data->realY);
1173 if (opaqueMove) {
1174 doWindowMove(wwin, scr->selected_windows,
1175 newX - wwin->frame_x,
1176 newY - wwin->frame_y);
1177 } else {
1178 /* erase frames */
1179 drawFrames(wwin, scr->selected_windows,
1180 data->realX - wwin->frame_x,
1181 data->realY - wwin->frame_y);
1184 if (!scr->selected_windows
1185 && wPreferences.move_display == WDIS_FRAME_CENTER) {
1187 moveGeometryDisplayCentered(scr, newX + data->winWidth/2,
1188 newY + data->winHeight/2);
1191 if (!opaqueMove) {
1192 /* draw frames */
1193 drawFrames(wwin, scr->selected_windows,
1194 newX - wwin->frame_x,
1195 newY - wwin->frame_y);
1198 if (!scr->selected_windows) {
1199 showPosition(wwin, newX, newY);
1204 /* recalc relative window position */
1205 if (doResistance && (data->realX != newX || data->realY != newY)) {
1206 updateResistance(wwin, data, newX, newY);
1209 data->realX = newX;
1210 data->realY = newY;
1214 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1217 wKeyboardMoveResizeWindow(WWindow *wwin)
1219 WScreen *scr = wwin->screen_ptr;
1220 Window root = scr->root_win;
1221 XEvent event;
1222 int w = wwin->frame->core->width;
1223 int h = wwin->frame->core->height;
1224 int scr_width = wwin->screen_ptr->scr_width;
1225 int scr_height = wwin->screen_ptr->scr_height;
1226 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1227 int src_x = wwin->frame_x;
1228 int src_y = wwin->frame_y;
1229 int done,off_x,off_y,ww,wh;
1230 int kspeed = _KS;
1231 Time lastTime = 0;
1232 KeySym keysym=NoSymbol;
1233 int moment=0;
1234 KeyCode shiftl,shiftr,ctrll,ctrlmode;
1236 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1237 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1238 ctrll = XKeysymToKeycode(dpy, XK_Control_L);
1239 ctrlmode=done=off_x=off_y=0;
1241 XSync(dpy, False);
1242 wusleep(10000);
1243 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1245 if (!wwin->flags.selected) {
1246 wUnselectWindows(scr);
1248 XGrabServer(dpy);
1249 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1250 |ButtonReleaseMask|ButtonPressMask, GrabModeAsync,
1251 GrabModeAsync, None, wCursor[WCUR_DEFAULT], CurrentTime);
1253 if (wwin->flags.shaded || scr->selected_windows) {
1254 if(scr->selected_windows)
1255 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1256 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1257 if(!scr->selected_windows)
1258 mapPositionDisplay(wwin, src_x, src_y, w, h);
1259 } else {
1260 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1262 ww=w;
1263 wh=h;
1264 while(1) {
1266 looper.ox=off_x;
1267 looper.oy=off_y;
1269 do {
1270 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1271 | ButtonPressMask | ExposureMask, &event);
1272 if (event.type == Expose) {
1273 WMHandleEvent(&event);
1275 } while (event.type == Expose);
1278 if (wwin->flags.shaded || scr->selected_windows) {
1279 if(scr->selected_windows)
1280 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1281 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1282 /*** I HATE EDGE RESISTANCE - ]d ***/
1284 else {
1285 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1288 if(ctrlmode)
1289 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1291 XUngrabServer(dpy);
1292 XSync(dpy, False);
1294 switch (event.type) {
1295 case KeyPress:
1296 /* accelerate */
1297 if (event.xkey.time - lastTime > 50) {
1298 kspeed/=(1 + (event.xkey.time - lastTime)/100);
1299 } else {
1300 if (kspeed < 20) {
1301 kspeed++;
1304 if (kspeed < _KS) kspeed = _KS;
1305 lastTime = event.xkey.time;
1307 if (event.xkey.state & ControlMask && !wwin->flags.shaded) {
1308 ctrlmode=1;
1309 wUnselectWindows(scr);
1311 else {
1312 ctrlmode=0;
1314 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1315 if (ctrlmode)
1316 cycleGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh, 0);
1317 else
1318 cyclePositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1320 else {
1322 keysym = XLookupKeysym(&event.xkey, 0);
1323 switch (keysym) {
1324 case XK_Return:
1325 done=2;
1326 break;
1327 case XK_Escape:
1328 done=1;
1329 break;
1330 case XK_Up:
1331 #ifdef XK_KP_Up
1332 case XK_KP_Up:
1333 #endif
1334 case XK_k:
1335 if (ctrlmode){
1336 if (moment != UP)
1337 h = wh;
1338 h-=kspeed;
1339 moment = UP;
1340 if (h < 1) h = 1;
1342 else off_y-=kspeed;
1343 break;
1344 case XK_Down:
1345 #ifdef XK_KP_Down
1346 case XK_KP_Down:
1347 #endif
1348 case XK_j:
1349 if (ctrlmode){
1350 if (moment != DOWN)
1351 h = wh;
1352 h+=kspeed;
1353 moment = DOWN;
1355 else off_y+=kspeed;
1356 break;
1357 case XK_Left:
1358 #ifdef XK_KP_Left
1359 case XK_KP_Left:
1360 #endif
1361 case XK_h:
1362 if (ctrlmode) {
1363 if (moment != LEFT)
1364 w = ww;
1365 w-=kspeed;
1366 if (w < 1) w = 1;
1367 moment = LEFT;
1369 else off_x-=kspeed;
1370 break;
1371 case XK_Right:
1372 #ifdef XK_KP_Right
1373 case XK_KP_Right:
1374 #endif
1375 case XK_l:
1376 if (ctrlmode) {
1377 if (moment != RIGHT)
1378 w = ww;
1379 w+=kspeed;
1380 moment = RIGHT;
1382 else off_x+=kspeed;
1383 break;
1386 ww=w;wh=h;
1387 wh-=vert_border;
1388 wWindowConstrainSize(wwin, &ww, &wh);
1389 wh+=vert_border;
1391 if (wPreferences.ws_cycle){
1392 if (src_x + off_x + ww < 20){
1393 if(!scr->current_workspace) {
1394 wWorkspaceChange(scr, scr->workspace_count-1);
1396 else wWorkspaceChange(scr, scr->current_workspace-1);
1397 off_x += scr_width;
1399 else if (src_x + off_x + 20 > scr_width){
1400 if(scr->current_workspace == scr->workspace_count-1) {
1401 wWorkspaceChange(scr, 0);
1403 else wWorkspaceChange(scr, scr->current_workspace+1);
1404 off_x -= scr_width;
1407 else {
1408 if (src_x + off_x + ww < 20)
1409 off_x = 20 - ww - src_x;
1410 else if (src_x + off_x + 20 > scr_width)
1411 off_x = scr_width - 20 - src_x;
1414 if (src_y + off_y + wh < 20) {
1415 off_y = 20 - wh - src_y;
1417 else if (src_y + off_y + 20 > scr_height) {
1418 off_y = scr_height - 20 - src_y;
1421 break;
1422 case ButtonPress:
1423 case ButtonRelease:
1424 done=1;
1425 break;
1426 case Expose:
1427 WMHandleEvent(&event);
1428 while (XCheckTypedEvent(dpy, Expose, &event)) {
1429 WMHandleEvent(&event);
1431 break;
1433 default:
1434 WMHandleEvent(&event);
1435 break;
1438 XGrabServer(dpy);
1439 /*xxx*/
1441 if (wwin->flags.shaded && !scr->selected_windows){
1442 moveGeometryDisplayCentered(scr, src_x+off_x + w/2, src_y+off_y + h/2);
1443 } else {
1444 if (ctrlmode) {
1445 WMUnmapWidget(scr->gview);
1446 mapGeometryDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1447 } else if(!scr->selected_windows) {
1448 WMUnmapWidget(scr->gview);
1449 mapPositionDisplay(wwin, src_x+off_x, src_y+off_y, ww, wh);
1453 if (wwin->flags.shaded || scr->selected_windows) {
1454 if (scr->selected_windows)
1455 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1456 else
1457 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1458 } else {
1459 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1463 if (ctrlmode) {
1464 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1465 } else if(!scr->selected_windows)
1466 showPosition(wwin, src_x+off_x, src_y+off_y);
1467 /**/
1469 if (done) {
1470 scr->keymove_tick=0;
1472 WMDeleteTimerWithClientData(&looper);
1474 if (wwin->flags.shaded || scr->selected_windows) {
1475 if(scr->selected_windows)
1476 drawFrames(wwin,scr->selected_windows,off_x,off_y);
1477 else drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, w, h);
1479 else {
1480 drawTransparentFrame(wwin, src_x+off_x, src_y+off_y, ww, wh);
1483 if (ctrlmode) {
1484 showGeometry(wwin, src_x+off_x, src_y+off_y, src_x+off_x+ww, src_y+off_y+wh,0);
1485 WMUnmapWidget(scr->gview);
1486 } else
1487 WMUnmapWidget(scr->gview);
1489 XUngrabKeyboard(dpy, CurrentTime);
1490 XUngrabPointer(dpy, CurrentTime);
1491 XUngrabServer(dpy);
1493 if(done==2) {
1494 if (wwin->flags.shaded || scr->selected_windows) {
1495 if (!scr->selected_windows) {
1496 wWindowMove(wwin, src_x+off_x, src_y+off_y);
1497 wWindowSynthConfigureNotify(wwin);
1498 } else {
1499 WMArrayIterator iter;
1500 WWindow *foo;
1502 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1504 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1505 wWindowSynthConfigureNotify(foo);
1508 } else {
1509 if (wwin->client.width != ww)
1510 wwin->flags.user_changed_width = 1;
1512 if (wwin->client.height != wh - vert_border)
1513 wwin->flags.user_changed_height = 1;
1515 wWindowConfigure(wwin, src_x+off_x, src_y+off_y,
1516 ww, wh - vert_border);
1517 wWindowSynthConfigureNotify(wwin);
1519 wWindowChangeWorkspace(wwin, scr->current_workspace);
1520 wSetFocusTo(scr, wwin);
1522 return 1;
1529 *----------------------------------------------------------------------
1530 * wMouseMoveWindow--
1531 * Move the named window and the other selected ones (if any),
1532 * interactively. Also shows the position of the window, if only one
1533 * window is being moved.
1534 * If the window is not on the selected window list, the selected
1535 * windows are deselected.
1536 * If shift is pressed during the operation, the position display
1537 * is changed to another type.
1539 * Returns:
1540 * True if the window was moved, False otherwise.
1542 * Side effects:
1543 * The window(s) position is changed, and the client(s) are
1544 * notified about that.
1545 * The position display configuration may be changed.
1546 *----------------------------------------------------------------------
1549 wMouseMoveWindow(WWindow *wwin, XEvent *ev)
1551 WScreen *scr = wwin->screen_ptr;
1552 XEvent event;
1553 Window root = scr->root_win;
1554 KeyCode shiftl, shiftr;
1555 Bool done = False;
1556 int started = 0;
1557 int warped = 0;
1558 /* This needs not to change while moving, else bad things can happen */
1559 int opaqueMove = wPreferences.opaque_move;
1560 MoveData moveData;
1561 #ifdef GHOST_WINDOW_MOVE
1562 RImage *rimg;
1564 rimg = InitGhostWindowMove(scr);
1565 #endif
1568 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1569 XSetWindowAttributes attr;
1571 attr.save_under = True;
1572 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1573 CWSaveUnder, &attr);
1577 initMoveData(wwin, &moveData);
1579 moveData.mouseX = ev->xmotion.x_root;
1580 moveData.mouseY = ev->xmotion.y_root;
1582 if (!wwin->flags.selected) {
1583 /* this window is not selected, unselect others and move only wwin */
1584 wUnselectWindows(scr);
1586 #ifdef DEBUG
1587 puts("Moving window");
1588 #endif
1589 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1590 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1591 while (!done) {
1592 if (warped) {
1593 int junk;
1594 Window junkw;
1596 /* XWarpPointer() doesn't seem to generate Motion events, so
1597 we've got to simulate them */
1598 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1599 &event.xmotion.y_root, &junk, &junk,
1600 (unsigned *) &junk);
1601 } else {
1602 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1603 | PointerMotionHintMask
1604 | ButtonReleaseMask | ButtonPressMask | ExposureMask,
1605 &event);
1607 if (event.type == MotionNotify) {
1608 /* compress MotionNotify events */
1609 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1610 if (!checkMouseSamplingRate(&event))
1611 continue;
1614 switch (event.type) {
1615 case KeyPress:
1616 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1617 && started && !scr->selected_windows) {
1619 if (!opaqueMove) {
1620 drawFrames(wwin, scr->selected_windows,
1621 moveData.realX - wwin->frame_x,
1622 moveData.realY - wwin->frame_y);
1625 if (wPreferences.move_display == WDIS_NEW
1626 && !scr->selected_windows) {
1627 showPosition(wwin, moveData.realX, moveData.realY);
1628 XUngrabServer(dpy);
1630 cyclePositionDisplay(wwin, moveData.realX, moveData.realY,
1631 moveData.winWidth, moveData.winHeight);
1633 if (wPreferences.move_display == WDIS_NEW
1634 && !scr->selected_windows) {
1635 XGrabServer(dpy);
1636 showPosition(wwin, moveData.realX, moveData.realY);
1639 if (!opaqueMove) {
1640 drawFrames(wwin, scr->selected_windows,
1641 moveData.realX - wwin->frame_x,
1642 moveData.realY - wwin->frame_y);
1645 break;
1647 case MotionNotify:
1648 if (started) {
1649 updateWindowPosition(wwin, &moveData,
1650 scr->selected_windows == NULL
1651 && wPreferences.edge_resistance > 0,
1652 opaqueMove,
1653 event.xmotion.x_root,
1654 event.xmotion.y_root);
1656 if (!warped && !wPreferences.no_autowrap) {
1657 int oldWorkspace = scr->current_workspace;
1659 if (wPreferences.move_display == WDIS_NEW
1660 && !scr->selected_windows) {
1661 showPosition(wwin, moveData.realX, moveData.realY);
1662 XUngrabServer(dpy);
1664 if (!opaqueMove) {
1665 drawFrames(wwin, scr->selected_windows,
1666 moveData.realX - wwin->frame_x,
1667 moveData.realY - wwin->frame_y);
1669 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1670 if (scr->current_workspace != oldWorkspace
1671 && wPreferences.edge_resistance > 0
1672 && scr->selected_windows == NULL)
1673 updateMoveData(wwin, &moveData);
1674 warped = 1;
1676 if (!opaqueMove) {
1677 drawFrames(wwin, scr->selected_windows,
1678 moveData.realX - wwin->frame_x,
1679 moveData.realY - wwin->frame_y);
1681 if (wPreferences.move_display == WDIS_NEW
1682 && !scr->selected_windows) {
1683 XSync(dpy, False);
1684 showPosition(wwin, moveData.realX, moveData.realY);
1685 XGrabServer(dpy);
1687 } else {
1688 warped = 0;
1690 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1691 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1693 XChangeActivePointerGrab(dpy, ButtonMotionMask
1694 | ButtonReleaseMask | ButtonPressMask,
1695 wCursor[WCUR_MOVE], CurrentTime);
1696 started = 1;
1697 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
1698 CurrentTime);
1700 if (!scr->selected_windows)
1701 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1702 moveData.winWidth, moveData.winHeight);
1704 if (started && !opaqueMove)
1705 drawFrames(wwin, scr->selected_windows, 0, 0);
1707 if (!opaqueMove || (wPreferences.move_display==WDIS_NEW
1708 && !scr->selected_windows)) {
1709 XGrabServer(dpy);
1710 if (wPreferences.move_display==WDIS_NEW)
1711 showPosition(wwin, moveData.realX, moveData.realY);
1714 break;
1716 case ButtonPress:
1717 break;
1719 case ButtonRelease:
1720 if (event.xbutton.button != ev->xbutton.button)
1721 break;
1723 if (started) {
1724 XEvent e;
1725 if (!opaqueMove) {
1726 drawFrames(wwin, scr->selected_windows,
1727 moveData.realX - wwin->frame_x,
1728 moveData.realY - wwin->frame_y);
1729 XSync(dpy, 0);
1730 doWindowMove(wwin, scr->selected_windows,
1731 moveData.realX - wwin->frame_x,
1732 moveData.realY - wwin->frame_y);
1734 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1735 wWindowSynthConfigureNotify(wwin);
1736 #endif
1737 XUngrabKeyboard(dpy, CurrentTime);
1738 XUngrabServer(dpy);
1739 if (!opaqueMove) {
1740 wWindowChangeWorkspace(wwin, scr->current_workspace);
1741 wSetFocusTo(scr, wwin);
1743 if (wPreferences.move_display == WDIS_NEW)
1744 showPosition(wwin, moveData.realX, moveData.realY);
1746 /* discard all enter/leave events that happened until
1747 * the time the button was released */
1748 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1749 if (e.xcrossing.time > event.xbutton.time) {
1750 XPutBackEvent(dpy, &e);
1751 break;
1754 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1755 if (e.xcrossing.time > event.xbutton.time) {
1756 XPutBackEvent(dpy, &e);
1757 break;
1761 if (!scr->selected_windows) {
1762 /* get rid of the geometry window */
1763 WMUnmapWidget(scr->gview);
1766 #ifdef DEBUG
1767 puts("End move window");
1768 #endif
1769 done = True;
1770 break;
1772 default:
1773 if (started && !opaqueMove) {
1774 drawFrames(wwin, scr->selected_windows,
1775 moveData.realX - wwin->frame_x,
1776 moveData.realY - wwin->frame_y);
1777 XUngrabServer(dpy);
1778 WMHandleEvent(&event);
1779 XSync(dpy, False);
1780 XGrabServer(dpy);
1781 drawFrames(wwin, scr->selected_windows,
1782 moveData.realX - wwin->frame_x,
1783 moveData.realY - wwin->frame_y);
1784 } else {
1785 WMHandleEvent(&event);
1787 break;
1791 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1792 XSetWindowAttributes attr;
1795 attr.save_under = False;
1796 XChangeWindowAttributes(dpy, wwin->frame->core->window,
1797 CWSaveUnder, &attr);
1801 freeMoveData(&moveData);
1803 return started;
1807 #define RESIZEBAR 1
1808 #define HCONSTRAIN 2
1810 static int
1811 getResizeDirection(WWindow *wwin, int x, int y, int dx, int dy,
1812 int flags)
1814 int w = wwin->frame->core->width - 1;
1815 int cw = wwin->frame->resizebar_corner_width;
1816 int dir;
1818 /* if not resizing through the resizebar */
1819 if (!(flags & RESIZEBAR)) {
1820 int xdir = (abs(x) < (wwin->client.width/2)) ? LEFT : RIGHT;
1821 int ydir = (abs(y) < (wwin->client.height/2)) ? UP : DOWN;
1822 if (abs(dx) < 2 || abs(dy) < 2) {
1823 if (abs(dy) > abs(dx))
1824 xdir = 0;
1825 else
1826 ydir = 0;
1828 return (xdir | ydir);
1831 /* window is too narrow. allow diagonal resize */
1832 if (cw * 2 >= w) {
1833 int ydir;
1835 if (flags & HCONSTRAIN)
1836 ydir = 0;
1837 else
1838 ydir = DOWN;
1839 if (x < cw)
1840 return (LEFT | ydir);
1841 else
1842 return (RIGHT | ydir);
1844 /* vertical resize */
1845 if ((x > cw) && (x < w - cw))
1846 return DOWN;
1848 if (x < cw)
1849 dir = LEFT;
1850 else
1851 dir = RIGHT;
1853 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1854 dir |= DOWN;
1856 return dir;
1860 void
1861 wMouseResizeWindow(WWindow *wwin, XEvent *ev)
1863 XEvent event;
1864 WScreen *scr = wwin->screen_ptr;
1865 Window root = scr->root_win;
1866 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1867 int fw = wwin->frame->core->width;
1868 int fh = wwin->frame->core->height;
1869 int fx = wwin->frame_x;
1870 int fy = wwin->frame_y;
1871 int is_resizebar = (wwin->frame->resizebar
1872 && ev->xany.window==wwin->frame->resizebar->window);
1873 int orig_x, orig_y;
1874 int started;
1875 int dw, dh;
1876 int rw = fw, rh = fh;
1877 int rx1, ry1, rx2, ry2;
1878 int res = 0;
1879 KeyCode shiftl, shiftr;
1880 int h = 0;
1881 int orig_fx = fx;
1882 int orig_fy = fy;
1883 int orig_fw = fw;
1884 int orig_fh = fh;
1886 if (wwin->flags.shaded) {
1887 wwarning("internal error: tryein");
1888 return;
1890 orig_x = ev->xbutton.x_root;
1891 orig_y = ev->xbutton.y_root;
1893 started = 0;
1894 #ifdef DEBUG
1895 puts("Resizing window");
1896 #endif
1898 wUnselectWindows(scr);
1899 rx1 = fx;
1900 rx2 = fx + fw - 1;
1901 ry1 = fy;
1902 ry2 = fy + fh - 1;
1903 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1904 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1905 if (!WFLAGP(wwin, no_titlebar))
1906 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
1907 else
1908 h = 0;
1909 while (1) {
1910 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1911 | ButtonReleaseMask | PointerMotionHintMask
1912 | ButtonPressMask | ExposureMask, &event);
1913 if (!checkMouseSamplingRate(&event))
1914 continue;
1916 switch (event.type) {
1917 case KeyPress:
1918 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1919 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1920 && started) {
1921 drawTransparentFrame(wwin, fx, fy, fw, fh);
1922 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1923 drawTransparentFrame(wwin, fx, fy, fw, fh);
1925 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1926 break;
1928 case MotionNotify:
1929 if (started) {
1930 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1932 dw = 0;
1933 dh = 0;
1935 orig_fx = fx;
1936 orig_fy = fy;
1937 orig_fw = fw;
1938 orig_fh = fh;
1940 if (res & LEFT)
1941 dw = orig_x - event.xmotion.x_root;
1942 else if (res & RIGHT)
1943 dw = event.xmotion.x_root - orig_x;
1944 if (res & UP)
1945 dh = orig_y - event.xmotion.y_root;
1946 else if (res & DOWN)
1947 dh = event.xmotion.y_root - orig_y;
1949 orig_x = event.xmotion.x_root;
1950 orig_y = event.xmotion.y_root;
1952 rw += dw;
1953 rh += dh;
1954 fw = rw;
1955 fh = rh - vert_border;
1956 wWindowConstrainSize(wwin, &fw, &fh);
1957 fh += vert_border;
1958 if (res & LEFT)
1959 fx = rx2 - fw + 1;
1960 else if (res & RIGHT)
1961 fx = rx1;
1962 if (res & UP)
1963 fy = ry2 - fh + 1;
1964 else if (res & DOWN)
1965 fy = ry1;
1966 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1967 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1968 int tx, ty;
1969 Window junkw;
1970 int flags;
1972 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1973 orig_x, orig_y, &tx, &ty, &junkw);
1975 /* check if resizing through resizebar */
1976 if (is_resizebar)
1977 flags = RESIZEBAR;
1978 else
1979 flags = 0;
1981 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1982 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1983 flags |= HCONSTRAIN;
1985 res = getResizeDirection(wwin, tx, ty,
1986 orig_x - event.xmotion.x_root,
1987 orig_y - event.xmotion.y_root, flags);
1989 if (res == (UP|LEFT))
1990 XChangeActivePointerGrab(dpy, ButtonMotionMask
1991 | ButtonReleaseMask | ButtonPressMask,
1992 wCursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1993 else if (res == (UP|RIGHT))
1994 XChangeActivePointerGrab(dpy, ButtonMotionMask
1995 | ButtonReleaseMask | ButtonPressMask,
1996 wCursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1997 else if (res == (DOWN|LEFT))
1998 XChangeActivePointerGrab(dpy, ButtonMotionMask
1999 | ButtonReleaseMask | ButtonPressMask,
2000 wCursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
2001 else if (res == (DOWN|RIGHT))
2002 XChangeActivePointerGrab(dpy, ButtonMotionMask
2003 | ButtonReleaseMask | ButtonPressMask,
2004 wCursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
2005 else if (res == DOWN || res == UP)
2006 XChangeActivePointerGrab(dpy, ButtonMotionMask
2007 | ButtonReleaseMask | ButtonPressMask,
2008 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2009 else if (res & (DOWN|UP))
2010 XChangeActivePointerGrab(dpy, ButtonMotionMask
2011 | ButtonReleaseMask | ButtonPressMask,
2012 wCursor[WCUR_VERTICALRESIZE], CurrentTime);
2013 else if (res & (LEFT|RIGHT))
2014 XChangeActivePointerGrab(dpy, ButtonMotionMask
2015 | ButtonReleaseMask | ButtonPressMask,
2016 wCursor[WCUR_HORIZONRESIZE], CurrentTime);
2018 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync,
2019 CurrentTime);
2021 XGrabServer(dpy);
2023 /* Draw the resize frame for the first time. */
2024 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2026 drawTransparentFrame(wwin, fx, fy, fw, fh);
2028 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2030 started = 1;
2032 if (started) {
2033 if (wPreferences.size_display == WDIS_FRAME_CENTER) {
2034 drawTransparentFrame(wwin, orig_fx, orig_fy,
2035 orig_fw, orig_fh);
2036 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2037 drawTransparentFrame(wwin, fx, fy, fw, fh);
2038 } else {
2039 drawTransparentFrame(wwin, orig_fx, orig_fy,
2040 orig_fw, orig_fh);
2041 drawTransparentFrame(wwin, fx, fy, fw, fh);
2043 if (fh != orig_fh || fw != orig_fw) {
2044 if (wPreferences.size_display == WDIS_NEW) {
2045 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2046 orig_fy + orig_fh, res);
2048 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2051 break;
2053 case ButtonPress:
2054 break;
2056 case ButtonRelease:
2057 if (event.xbutton.button != ev->xbutton.button)
2058 break;
2060 if (started) {
2061 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2063 drawTransparentFrame(wwin, fx, fy, fw, fh);
2065 XUngrabKeyboard(dpy, CurrentTime);
2066 WMUnmapWidget(scr->gview);
2067 XUngrabServer(dpy);
2069 if (wwin->client.width != fw)
2070 wwin->flags.user_changed_width = 1;
2072 if (wwin->client.height != fh - vert_border)
2073 wwin->flags.user_changed_height = 1;
2075 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2077 #ifdef DEBUG
2078 puts("End resize window");
2079 #endif
2080 return;
2082 default:
2083 WMHandleEvent(&event);
2088 #undef LEFT
2089 #undef RIGHT
2090 #undef HORIZONTAL
2091 #undef UP
2092 #undef DOWN
2093 #undef VERTICAL
2094 #undef HCONSTRAIN
2095 #undef RESIZEBAR
2097 void
2098 wUnselectWindows(WScreen *scr)
2100 WWindow *wwin;
2102 if (!scr->selected_windows)
2103 return;
2105 while (WMGetArrayItemCount(scr->selected_windows)) {
2106 wwin = WMGetFromArray(scr->selected_windows, 0);
2107 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2108 wIconSelect(wwin->icon);
2110 wSelectWindow(wwin, False);
2112 WMFreeArray(scr->selected_windows);
2113 scr->selected_windows = NULL;
2116 #ifndef LITE
2117 static void
2118 selectWindowsInside(WScreen *scr, int x1, int y1, int x2, int y2)
2120 WWindow *tmpw;
2122 /* select the windows and put them in the selected window list */
2123 tmpw = scr->focused_window;
2124 while (tmpw != NULL) {
2125 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2126 if ((tmpw->frame->workspace == scr->current_workspace
2127 || IS_OMNIPRESENT(tmpw))
2128 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2129 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2130 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2131 wSelectWindow(tmpw, True);
2134 tmpw = tmpw->prev;
2139 void
2140 wSelectWindows(WScreen *scr, XEvent *ev)
2142 XEvent event;
2143 Window root = scr->root_win;
2144 GC gc = scr->frame_gc;
2145 int xp = ev->xbutton.x_root;
2146 int yp = ev->xbutton.y_root;
2147 int w = 0, h = 0;
2148 int x = xp, y = yp;
2150 #ifdef DEBUG
2151 puts("Selecting windows");
2152 #endif
2153 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2154 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2155 GrabModeAsync, None, wCursor[WCUR_DEFAULT],
2156 CurrentTime) != Success) {
2157 return;
2159 XGrabServer(dpy);
2161 wUnselectWindows(scr);
2163 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2164 while (1) {
2165 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask
2166 | ButtonPressMask, &event);
2168 switch (event.type) {
2169 case MotionNotify:
2170 XDrawRectangle(dpy, root, gc, x, y, w, h);
2171 x = event.xmotion.x_root;
2172 if (x < xp) {
2173 w = xp - x;
2174 } else {
2175 w = x - xp;
2176 x = xp;
2178 y = event.xmotion.y_root;
2179 if (y < yp) {
2180 h = yp - y;
2181 } else {
2182 h = y - yp;
2183 y = yp;
2185 XDrawRectangle(dpy, root, gc, x, y, w, h);
2186 break;
2188 case ButtonPress:
2189 break;
2191 case ButtonRelease:
2192 if (event.xbutton.button != ev->xbutton.button)
2193 break;
2195 XDrawRectangle(dpy, root, gc, x, y, w, h);
2196 XUngrabServer(dpy);
2197 XUngrabPointer(dpy, CurrentTime);
2198 selectWindowsInside(scr, x, y, x + w, y + h);
2200 #ifdef KWM_HINTS
2201 wKWMSelectRootRegion(scr, xp, yp, w, h,
2202 event.xbutton.state & ControlMask);
2203 #endif /* KWM_HINTS */
2205 #ifdef DEBUG
2206 puts("End window selection");
2207 #endif
2208 return;
2210 default:
2211 WMHandleEvent(&event);
2212 break;
2216 #endif /* !LITE */
2218 void
2219 InteractivePlaceWindow(WWindow *wwin, int *x_ret, int *y_ret,
2220 unsigned width, unsigned height)
2222 WScreen *scr = wwin->screen_ptr;
2223 Window root = scr->root_win;
2224 int x, y, h = 0;
2225 XEvent event;
2226 KeyCode shiftl, shiftr;
2227 Window junkw;
2228 int junk;
2230 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2231 GrabModeAsync, GrabModeAsync, None,
2232 wCursor[WCUR_DEFAULT], CurrentTime) != Success) {
2233 *x_ret = 0;
2234 *y_ret = 0;
2235 return;
2237 if (!WFLAGP(wwin, no_titlebar)) {
2238 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance + TITLEBAR_EXTEND_SPACE) * 2;
2239 height += h;
2241 if (!WFLAGP(wwin, no_resizebar)) {
2242 height += RESIZEBAR_HEIGHT;
2244 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2245 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk,
2246 (unsigned *) &junk);
2247 mapPositionDisplay(wwin, x - width/2, y - h/2, width, height);
2249 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2251 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2252 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2253 while (1) {
2254 WMMaskEvent(dpy, PointerMotionMask|ButtonPressMask|ExposureMask|KeyPressMask,
2255 &event);
2257 if (!checkMouseSamplingRate(&event))
2258 continue;
2260 switch (event.type) {
2261 case KeyPress:
2262 if ((event.xkey.keycode == shiftl)
2263 || (event.xkey.keycode == shiftr)) {
2264 drawTransparentFrame(wwin,
2265 x - width/2, y - h/2, width, height);
2266 cyclePositionDisplay(wwin,
2267 x - width/2, y - h/2, width, height);
2268 drawTransparentFrame(wwin,
2269 x - width/2, y - h/2, width, height);
2271 break;
2273 case MotionNotify:
2274 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2276 x = event.xmotion.x_root;
2277 y = event.xmotion.y_root;
2279 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2280 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2282 showPosition(wwin, x - width/2, y - h/2);
2284 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2286 break;
2288 case ButtonPress:
2289 drawTransparentFrame(wwin, x - width/2, y - h/2, width, height);
2290 XSync(dpy, 0);
2291 *x_ret = x - width/2;
2292 *y_ret = y - h/2;
2293 XUngrabPointer(dpy, CurrentTime);
2294 XUngrabKeyboard(dpy, CurrentTime);
2295 /* get rid of the geometry window */
2296 WMUnmapWidget(scr->gview);
2297 return;
2299 default:
2300 WMHandleEvent(&event);
2301 break;