util: fix image leak in wmsebg's parseTexture (Coverity #50166)
[wmaker-crm.git] / src / moveres.c
blob6754be2c87ce451b9f8a380485ef294273f2e2c2
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/keysym.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "WindowMaker.h"
32 #include "framewin.h"
33 #include "window.h"
34 #include "client.h"
35 #include "icon.h"
36 #include "dock.h"
37 #include "actions.h"
38 #include "workspace.h"
39 #include "placement.h"
41 #include "geomview.h"
42 #include "screen.h"
43 #include "xinerama.h"
45 #include <WINGs/WINGsP.h>
47 /* How many different types of geometry/position
48 display thingies are there? */
49 #define NUM_DISPLAYS 5
51 #define LEFT 1
52 #define RIGHT 2
53 #define HORIZONTAL (LEFT|RIGHT)
54 #define UP 4
55 #define DOWN 8
56 #define VERTICAL (UP|DOWN)
58 /* True if window currently has a border. This also includes borderless
59 * windows which are currently selected
61 #define HAS_BORDER_WITH_SELECT(w) ((w)->flags.selected || HAS_BORDER(w))
65 *----------------------------------------------------------------------
66 * checkMouseSamplingRate-
67 * For lowering the mouse motion sampling rate for machines where
68 * it's too high (SGIs). If it returns False then the event should be
69 * ignored.
70 *----------------------------------------------------------------------
72 static Bool checkMouseSamplingRate(XEvent * ev)
74 static Time previousMotion = 0;
76 if (ev->type == MotionNotify) {
77 if (ev->xmotion.time - previousMotion < DELAY_BETWEEN_MOUSE_SAMPLING) {
78 return False;
79 } else {
80 previousMotion = ev->xmotion.time;
83 return True;
87 *----------------------------------------------------------------------
88 * moveGeometryDisplayCentered
90 * routine that moves the geometry/position window on scr so it is
91 * centered over the given coordinates (x,y). Also the window position
92 * is clamped so it stays on the screen at all times.
93 *----------------------------------------------------------------------
95 static void moveGeometryDisplayCentered(WScreen * scr, int x, int y)
97 unsigned int w = WMWidgetWidth(scr->gview);
98 unsigned int h = WMWidgetHeight(scr->gview);
99 int x1 = 0, y1 = 0, x2 = scr->scr_width, y2 = scr->scr_height;
101 x -= w / 2;
102 y -= h / 2;
104 /* dead area check */
105 if (scr->xine_info.count) {
106 WMRect rect;
107 int head, flags;
109 rect.pos.x = x;
110 rect.pos.y = y;
111 rect.size.width = w;
112 rect.size.height = h;
114 head = wGetRectPlacementInfo(scr, rect, &flags);
116 if (flags & (XFLAG_DEAD | XFLAG_PARTIAL)) {
117 rect = wGetRectForHead(scr, head);
118 x1 = rect.pos.x;
119 y1 = rect.pos.y;
120 x2 = x1 + rect.size.width;
121 y2 = y1 + rect.size.height;
125 if (x < x1 + 1)
126 x = x1 + 1;
127 else if (x > (x2 - w))
128 x = x2 - w;
130 if (y < y1 + 1)
131 y = y1 + 1;
132 else if (y > (y2 - h))
133 y = y2 - h;
135 WMMoveWidget(scr->gview, x, y);
138 static void showPosition(WWindow * wwin, int x, int y)
140 WScreen *scr = wwin->screen_ptr;
142 if (wPreferences.move_display == WDIS_NEW) {
143 #if 0
144 int width = wwin->frame->core->width;
145 int height = wwin->frame->core->height;
147 GC lgc = scr->line_gc;
148 XSetForeground(dpy, lgc, scr->line_pixel);
149 sprintf(num, "%i", x);
151 XDrawLine(dpy, scr->root_win, lgc, 0, y - 1, scr->scr_width, y - 1);
152 XDrawLine(dpy, scr->root_win, lgc, 0, y + height + 2, scr->scr_width, y + height + 2);
153 XDrawLine(dpy, scr->root_win, lgc, x - 1, 0, x - 1, scr->scr_height);
154 XDrawLine(dpy, scr->root_win, lgc, x + width + 2, 0, x + width + 2, scr->scr_height);
155 #endif
156 } else {
157 WSetGeometryViewShownPosition(scr->gview, x, y);
161 static void cyclePositionDisplay(WWindow * wwin, int x, int y, int w, int h)
163 WScreen *scr = wwin->screen_ptr;
164 WMRect rect;
166 wPreferences.move_display++;
167 wPreferences.move_display %= NUM_DISPLAYS;
169 if (wPreferences.move_display == WDIS_NEW) {
170 wPreferences.move_display++;
171 wPreferences.move_display %= NUM_DISPLAYS;
174 if (wPreferences.move_display == WDIS_NONE) {
175 WMUnmapWidget(scr->gview);
176 } else {
177 if (wPreferences.move_display == WDIS_CENTER) {
178 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
179 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
180 rect.pos.y + rect.size.height / 2);
181 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
182 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
183 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
184 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
185 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
187 WMMapWidget(scr->gview);
191 static void mapPositionDisplay(WWindow * wwin, int x, int y, int w, int h)
193 WScreen *scr = wwin->screen_ptr;
194 WMRect rect;
196 if (wPreferences.move_display == WDIS_NEW || wPreferences.move_display == WDIS_NONE) {
197 return;
198 } else if (wPreferences.move_display == WDIS_CENTER) {
199 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
200 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
201 rect.pos.y + rect.size.height / 2);
202 } else if (wPreferences.move_display == WDIS_TOPLEFT) {
203 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
204 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
205 } else if (wPreferences.move_display == WDIS_FRAME_CENTER) {
206 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
208 WMMapWidget(scr->gview);
209 WSetGeometryViewShownPosition(scr->gview, x, y);
212 static void showGeometry(WWindow * wwin, int x1, int y1, int x2, int y2, int direction)
214 WScreen *scr = wwin->screen_ptr;
215 Window root = scr->root_win;
216 GC gc = scr->line_gc;
217 int ty, by, my, x, y, mx, s;
218 char num[16];
219 XSegment segment[4];
220 int fw, fh;
222 /* This seems necessary for some odd reason (too lazy to write x1-1 and
223 * x2-1 everywhere below in the code). But why only for x? */
224 x1--;
225 x2--;
227 if (HAS_BORDER_WITH_SELECT(wwin)) {
228 x1 += scr->frame_border_width;
229 x2 += scr->frame_border_width;
230 y1 += scr->frame_border_width;
231 y2 += scr->frame_border_width;
234 ty = y1 + wwin->frame->top_width;
235 by = y2 - wwin->frame->bottom_width;
237 if (wPreferences.size_display == WDIS_NEW) {
238 fw = XTextWidth(scr->tech_draw_font, "8888", 4);
239 fh = scr->tech_draw_font->ascent + scr->tech_draw_font->descent;
241 XSetForeground(dpy, gc, scr->line_pixel);
243 /* vertical geometry */
244 if (((direction & LEFT) && (x2 < scr->scr_width - fw)) || (x1 < fw)) {
245 x = x2;
246 s = -15;
247 } else {
248 x = x1;
249 s = 15;
251 my = (ty + by) / 2;
253 /* top arrow & end bar */
254 segment[0].x1 = x - (s + 6);
255 segment[0].y1 = ty;
256 segment[0].x2 = x - (s - 10);
257 segment[0].y2 = ty;
259 /* arrowhead */
260 segment[1].x1 = x - (s - 2);
261 segment[1].y1 = ty + 1;
262 segment[1].x2 = x - (s - 5);
263 segment[1].y2 = ty + 7;
265 segment[2].x1 = x - (s - 2);
266 segment[2].y1 = ty + 1;
267 segment[2].x2 = x - (s + 1);
268 segment[2].y2 = ty + 7;
270 /* line */
271 segment[3].x1 = x - (s - 2);
272 segment[3].y1 = ty + 1;
273 segment[3].x2 = x - (s - 2);
274 segment[3].y2 = my - fh / 2 - 1;
276 XDrawSegments(dpy, root, gc, segment, 4);
278 /* bottom arrow & end bar */
279 segment[0].y1 = by;
280 segment[0].y2 = by;
282 /* arrowhead */
283 segment[1].y1 = by - 1;
284 segment[1].y2 = by - 7;
286 segment[2].y1 = by - 1;
287 segment[2].y2 = by - 7;
289 /* line */
290 segment[3].y1 = my + fh / 2 + 2;
291 segment[3].y2 = by - 1;
293 XDrawSegments(dpy, root, gc, segment, 4);
295 snprintf(num, sizeof(num), "%i", (by - ty - wwin->normal_hints->base_height) /
296 wwin->normal_hints->height_inc);
297 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
299 /* Display the height. */
300 XSetFont(dpy, gc, scr->tech_draw_font->fid);
301 XDrawString(dpy, root, gc, x - s + 3 - fw / 2, my + scr->tech_draw_font->ascent - fh / 2 + 1, num,
302 strlen(num));
304 /* horizontal geometry */
305 if (y1 < 15) {
306 y = y2;
307 s = -15;
308 } else {
309 y = y1;
310 s = 15;
312 mx = x1 + (x2 - x1) / 2;
313 snprintf(num, sizeof(num), "%i", (x2 - x1 - wwin->normal_hints->base_width) /
314 wwin->normal_hints->width_inc);
315 fw = XTextWidth(scr->tech_draw_font, num, strlen(num));
317 /* left arrow & end bar */
318 segment[0].x1 = x1;
319 segment[0].y1 = y - (s + 6);
320 segment[0].x2 = x1;
321 segment[0].y2 = y - (s - 10);
323 /* arrowhead */
324 segment[1].x1 = x1 + 7;
325 segment[1].y1 = y - (s + 1);
326 segment[1].x2 = x1 + 1;
327 segment[1].y2 = y - (s - 2);
329 segment[2].x1 = x1 + 1;
330 segment[2].y1 = y - (s - 2);
331 segment[2].x2 = x1 + 7;
332 segment[2].y2 = y - (s - 5);
334 /* line */
335 segment[3].x1 = x1 + 1;
336 segment[3].y1 = y - (s - 2);
337 segment[3].x2 = mx - fw / 2 - 2;
338 segment[3].y2 = y - (s - 2);
340 XDrawSegments(dpy, root, gc, segment, 4);
342 /* right arrow & end bar */
343 segment[0].x1 = x2 + 1;
344 segment[0].x2 = x2 + 1;
346 /* arrowhead */
347 segment[1].x1 = x2 - 6;
348 segment[1].x2 = x2;
350 segment[2].x1 = x2;
351 segment[2].x2 = x2 - 6;
353 /* line */
354 segment[3].x1 = mx + fw / 2 + 2;
355 segment[3].x2 = x2;
357 XDrawSegments(dpy, root, gc, segment, 4);
359 /* Display the width. */
360 XDrawString(dpy, root, gc, mx - fw / 2 + 1, y - s + scr->tech_draw_font->ascent - fh / 2 + 1, num,
361 strlen(num));
362 } else {
363 WSetGeometryViewShownSize(scr->gview, (x2 - x1 - wwin->normal_hints->base_width)
364 / wwin->normal_hints->width_inc,
365 (by - ty - wwin->normal_hints->base_height)
366 / wwin->normal_hints->height_inc);
370 static void cycleGeometryDisplay(WWindow * wwin, int x, int y, int w, int h, int dir)
372 WScreen *scr = wwin->screen_ptr;
373 WMRect rect;
375 wPreferences.size_display++;
376 wPreferences.size_display %= NUM_DISPLAYS;
378 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE) {
379 WMUnmapWidget(scr->gview);
380 } else {
381 if (wPreferences.size_display == WDIS_CENTER) {
382 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
383 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
384 rect.pos.y + rect.size.height / 2);
385 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
386 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
387 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
388 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
389 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
391 WMMapWidget(scr->gview);
392 showGeometry(wwin, x, y, x + w, y + h, dir);
396 static void mapGeometryDisplay(WWindow * wwin, int x, int y, int w, int h)
398 WScreen *scr = wwin->screen_ptr;
399 WMRect rect;
401 if (wPreferences.size_display == WDIS_NEW || wPreferences.size_display == WDIS_NONE)
402 return;
404 if (wPreferences.size_display == WDIS_CENTER) {
405 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
406 moveGeometryDisplayCentered(scr, rect.pos.x + rect.size.width / 2,
407 rect.pos.y + rect.size.height / 2);
408 } else if (wPreferences.size_display == WDIS_TOPLEFT) {
409 rect = wGetRectForHead(scr, wGetHeadForWindow(wwin));
410 moveGeometryDisplayCentered(scr, rect.pos.x + 1, rect.pos.y + 1);
411 } else if (wPreferences.size_display == WDIS_FRAME_CENTER) {
412 moveGeometryDisplayCentered(scr, x + w / 2, y + h / 2);
414 WMMapWidget(scr->gview);
415 showGeometry(wwin, x, y, x + w, y + h, 0);
418 static void doWindowMove(WWindow * wwin, WMArray * array, int dx, int dy)
420 WWindow *tmpw;
421 WScreen *scr = wwin->screen_ptr;
422 int x, y;
424 if (!array || !WMGetArrayItemCount(array)) {
425 wWindowMove(wwin, wwin->frame_x + dx, wwin->frame_y + dy);
426 } else {
427 WMArrayIterator iter;
429 WM_ITERATE_ARRAY(array, tmpw, iter) {
430 x = tmpw->frame_x + dx;
431 y = tmpw->frame_y + dy;
433 #if 1 /* XXX: with xinerama patch was #if 0, check this */
434 /* don't let windows become unreachable */
436 if (x + (int)tmpw->frame->core->width < 20)
437 x = 20 - (int)tmpw->frame->core->width;
438 else if (x + 20 > scr->scr_width)
439 x = scr->scr_width - 20;
441 if (y + (int)tmpw->frame->core->height < 20)
442 y = 20 - (int)tmpw->frame->core->height;
443 else if (y + 20 > scr->scr_height)
444 y = scr->scr_height - 20;
445 #else
446 wScreenBringInside(scr, &x, &y,
447 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
448 #endif
450 wWindowMove(tmpw, x, y);
455 static void drawTransparentFrame(WWindow * wwin, int x, int y, int width, int height)
457 Window root = wwin->screen_ptr->root_win;
458 GC gc = wwin->screen_ptr->frame_gc;
459 int h = 0;
460 int bottom = 0;
462 if (HAS_BORDER_WITH_SELECT(wwin)) {
463 x += wwin->screen_ptr->frame_border_width;
464 y += wwin->screen_ptr->frame_border_width;
467 if (HAS_TITLEBAR(wwin) && !wwin->flags.shaded) {
468 h = WMFontHeight(wwin->screen_ptr->title_font) + (wPreferences.window_title_clearance +
469 TITLEBAR_EXTEND_SPACE) * 2;
471 if (h > wPreferences.window_title_max_height)
472 h = wPreferences.window_title_max_height;
474 if (h < wPreferences.window_title_min_height)
475 h = wPreferences.window_title_min_height;
477 if (HAS_RESIZEBAR(wwin) && !wwin->flags.shaded) {
478 /* Can't use wwin-frame->bottom_width because, in some cases
479 (e.g. interactive placement), frame does not point to anything. */
480 bottom = RESIZEBAR_HEIGHT;
482 XDrawRectangle(dpy, root, gc, x - 1, y - 1, width + 1, height + 1);
484 if (h > 0) {
485 XDrawLine(dpy, root, gc, x, y + h - 1, x + width, y + h - 1);
487 if (bottom > 0) {
488 XDrawLine(dpy, root, gc, x, y + height - bottom, x + width, y + height - bottom);
492 static void drawFrames(WWindow * wwin, WMArray * array, int dx, int dy)
494 WWindow *tmpw;
495 int scr_width = wwin->screen_ptr->scr_width;
496 int scr_height = wwin->screen_ptr->scr_height;
497 int x, y;
499 if (!array) {
501 x = wwin->frame_x + dx;
502 y = wwin->frame_y + dy;
504 drawTransparentFrame(wwin, x, y, wwin->frame->core->width, wwin->frame->core->height);
506 } else {
507 WMArrayIterator iter;
509 WM_ITERATE_ARRAY(array, tmpw, iter) {
510 x = tmpw->frame_x + dx;
511 y = tmpw->frame_y + dy;
513 /* don't let windows become unreachable */
514 #if 1 /* XXX: was 0 in XINERAMA patch, check */
515 if (x + (int)tmpw->frame->core->width < 20)
516 x = 20 - (int)tmpw->frame->core->width;
517 else if (x + 20 > scr_width)
518 x = scr_width - 20;
520 if (y + (int)tmpw->frame->core->height < 20)
521 y = 20 - (int)tmpw->frame->core->height;
522 else if (y + 20 > scr_height)
523 y = scr_height - 20;
525 #else
526 wScreenBringInside(wwin->screen_ptr, &x, &y,
527 (int)tmpw->frame->core->width, (int)tmpw->frame->core->height);
528 #endif
530 drawTransparentFrame(tmpw, x, y, tmpw->frame->core->width, tmpw->frame->core->height);
535 static void flushMotion(void)
537 XEvent ev;
539 XSync(dpy, False);
540 while (XCheckMaskEvent(dpy, ButtonMotionMask, &ev)) ;
543 static void crossWorkspace(WScreen * scr, WWindow * wwin, int opaque_move, int new_workspace, int rewind)
545 /* do not let window be unmapped */
546 if (opaque_move) {
547 wwin->flags.changing_workspace = 1;
548 wWindowChangeWorkspace(wwin, new_workspace);
550 /* go to new workspace */
551 wWorkspaceChange(scr, new_workspace);
553 wwin->flags.changing_workspace = 0;
555 if (rewind)
556 XWarpPointer(dpy, None, None, 0, 0, 0, 0, scr->scr_width - 20, 0);
557 else
558 XWarpPointer(dpy, None, None, 0, 0, 0, 0, -(scr->scr_width - 20), 0);
560 flushMotion();
562 if (!opaque_move) {
563 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
564 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
565 GrabModeAsync, None, wPreferences.cursor[WCUR_MOVE], CurrentTime);
569 typedef struct {
570 /* arrays of WWindows sorted by the respective border position */
571 WWindow **topList; /* top border */
572 WWindow **leftList; /* left border */
573 WWindow **rightList; /* right border */
574 WWindow **bottomList; /* bottom border */
575 int count;
577 /* index of window in the above lists indicating the relative position
578 * of the window with the others */
579 int topIndex;
580 int leftIndex;
581 int rightIndex;
582 int bottomIndex;
584 int rubCount; /* for workspace switching */
586 int winWidth, winHeight; /* width/height of the window */
587 int realX, realY; /* actual position of the window */
588 int calcX, calcY; /* calculated position of window */
589 int omouseX, omouseY; /* old mouse position */
590 int mouseX, mouseY; /* last known position of the pointer */
591 } MoveData;
593 #define WTOP(w) (w)->frame_y
594 #define WLEFT(w) (w)->frame_x
595 #define WRIGHT(w) ((w)->frame_x + (int)(w)->frame->core->width - 1 + \
596 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
597 #define WBOTTOM(w) ((w)->frame_y + (int)(w)->frame->core->height - 1 + \
598 (HAS_BORDER_WITH_SELECT(w) ? 2*(w)->screen_ptr->frame_border_width : 0))
600 static int compareWTop(const void *a, const void *b)
602 WWindow *wwin1 = *(WWindow **) a;
603 WWindow *wwin2 = *(WWindow **) b;
605 if (WTOP(wwin1) > WTOP(wwin2))
606 return -1;
607 else if (WTOP(wwin1) < WTOP(wwin2))
608 return 1;
609 else
610 return 0;
613 static int compareWLeft(const void *a, const void *b)
615 WWindow *wwin1 = *(WWindow **) a;
616 WWindow *wwin2 = *(WWindow **) b;
618 if (WLEFT(wwin1) > WLEFT(wwin2))
619 return -1;
620 else if (WLEFT(wwin1) < WLEFT(wwin2))
621 return 1;
622 else
623 return 0;
626 static int compareWRight(const void *a, const void *b)
628 WWindow *wwin1 = *(WWindow **) a;
629 WWindow *wwin2 = *(WWindow **) b;
631 if (WRIGHT(wwin1) < WRIGHT(wwin2))
632 return -1;
633 else if (WRIGHT(wwin1) > WRIGHT(wwin2))
634 return 1;
635 else
636 return 0;
639 static int compareWBottom(const void *a, const void *b)
641 WWindow *wwin1 = *(WWindow **) a;
642 WWindow *wwin2 = *(WWindow **) b;
644 if (WBOTTOM(wwin1) < WBOTTOM(wwin2))
645 return -1;
646 else if (WBOTTOM(wwin1) > WBOTTOM(wwin2))
647 return 1;
648 else
649 return 0;
652 static void updateResistance(MoveData *data, int newX, int newY)
654 int i;
655 int newX2 = newX + data->winWidth;
656 int newY2 = newY + data->winHeight;
657 Bool ok = False;
659 if (newX < data->realX) {
660 if (data->rightIndex > 0 && newX < WRIGHT(data->rightList[data->rightIndex - 1])) {
661 ok = True;
662 } else if (data->leftIndex <= data->count - 1 && newX2 <= WLEFT(data->leftList[data->leftIndex])) {
663 ok = True;
665 } else if (newX > data->realX) {
666 if (data->leftIndex > 0 && newX2 > WLEFT(data->leftList[data->leftIndex - 1])) {
667 ok = True;
668 } else if (data->rightIndex <= data->count - 1
669 && newX >= WRIGHT(data->rightList[data->rightIndex])) {
670 ok = True;
674 if (!ok) {
675 if (newY < data->realY) {
676 if (data->bottomIndex > 0 && newY < WBOTTOM(data->bottomList[data->bottomIndex - 1])) {
677 ok = True;
678 } else if (data->topIndex <= data->count - 1
679 && newY2 <= WTOP(data->topList[data->topIndex])) {
680 ok = True;
682 } else if (newY > data->realY) {
683 if (data->topIndex > 0 && newY2 > WTOP(data->topList[data->topIndex - 1])) {
684 ok = True;
685 } else if (data->bottomIndex <= data->count - 1
686 && newY >= WBOTTOM(data->bottomList[data->bottomIndex])) {
687 ok = True;
692 if (!ok)
693 return;
695 /* TODO: optimize this */
696 if (data->realY < WBOTTOM(data->bottomList[0])) {
697 data->bottomIndex = 0;
699 if (data->realX < WRIGHT(data->rightList[0])) {
700 data->rightIndex = 0;
702 if ((data->realX + data->winWidth) > WLEFT(data->leftList[0])) {
703 data->leftIndex = 0;
705 if ((data->realY + data->winHeight) > WTOP(data->topList[0])) {
706 data->topIndex = 0;
708 for (i = 0; i < data->count; i++) {
709 if (data->realY > WBOTTOM(data->bottomList[i])) {
710 data->bottomIndex = i + 1;
712 if (data->realX > WRIGHT(data->rightList[i])) {
713 data->rightIndex = i + 1;
715 if ((data->realX + data->winWidth) < WLEFT(data->leftList[i])) {
716 data->leftIndex = i + 1;
718 if ((data->realY + data->winHeight) < WTOP(data->topList[i])) {
719 data->topIndex = i + 1;
724 static void freeMoveData(MoveData * data)
726 if (data->topList)
727 wfree(data->topList);
728 if (data->leftList)
729 wfree(data->leftList);
730 if (data->rightList)
731 wfree(data->rightList);
732 if (data->bottomList)
733 wfree(data->bottomList);
736 static void updateMoveData(WWindow * wwin, MoveData * data)
738 WScreen *scr = wwin->screen_ptr;
739 WWindow *tmp;
740 int i;
742 data->count = 0;
743 tmp = scr->focused_window;
744 while (tmp) {
745 if (tmp != wwin && w_global.workspace.current == tmp->frame->workspace
746 && !tmp->flags.miniaturized
747 && !tmp->flags.hidden && !tmp->flags.obscured && !WFLAGP(tmp, sunken)) {
748 data->topList[data->count] = tmp;
749 data->leftList[data->count] = tmp;
750 data->rightList[data->count] = tmp;
751 data->bottomList[data->count] = tmp;
752 data->count++;
754 tmp = tmp->prev;
757 if (data->count == 0) {
758 data->topIndex = 0;
759 data->leftIndex = 0;
760 data->rightIndex = 0;
761 data->bottomIndex = 0;
762 return;
765 /* order from closest to the border of the screen to farthest */
767 qsort(data->topList, data->count, sizeof(WWindow **), compareWTop);
768 qsort(data->leftList, data->count, sizeof(WWindow **), compareWLeft);
769 qsort(data->rightList, data->count, sizeof(WWindow **), compareWRight);
770 qsort(data->bottomList, data->count, sizeof(WWindow **), compareWBottom);
772 /* figure the position of the window relative to the others */
774 data->topIndex = -1;
775 data->leftIndex = -1;
776 data->rightIndex = -1;
777 data->bottomIndex = -1;
779 if (WTOP(wwin) < WBOTTOM(data->bottomList[0])) {
780 data->bottomIndex = 0;
782 if (WLEFT(wwin) < WRIGHT(data->rightList[0])) {
783 data->rightIndex = 0;
785 if (WRIGHT(wwin) > WLEFT(data->leftList[0])) {
786 data->leftIndex = 0;
788 if (WBOTTOM(wwin) > WTOP(data->topList[0])) {
789 data->topIndex = 0;
791 for (i = 0; i < data->count; i++) {
792 if (WTOP(wwin) >= WBOTTOM(data->bottomList[i])) {
793 data->bottomIndex = i + 1;
795 if (WLEFT(wwin) >= WRIGHT(data->rightList[i])) {
796 data->rightIndex = i + 1;
798 if (WRIGHT(wwin) <= WLEFT(data->leftList[i])) {
799 data->leftIndex = i + 1;
801 if (WBOTTOM(wwin) <= WTOP(data->topList[i])) {
802 data->topIndex = i + 1;
807 static void initMoveData(WWindow * wwin, MoveData * data)
809 int i;
810 WWindow *tmp;
812 memset(data, 0, sizeof(MoveData));
814 for (i = 0, tmp = wwin->screen_ptr->focused_window; tmp != NULL; tmp = tmp->prev, i++) ;
816 if (i > 1) {
817 data->topList = wmalloc(sizeof(WWindow *) * i);
818 data->leftList = wmalloc(sizeof(WWindow *) * i);
819 data->rightList = wmalloc(sizeof(WWindow *) * i);
820 data->bottomList = wmalloc(sizeof(WWindow *) * i);
822 updateMoveData(wwin, data);
825 data->realX = wwin->frame_x;
826 data->realY = wwin->frame_y;
827 data->calcX = wwin->frame_x;
828 data->calcY = wwin->frame_y;
830 data->winWidth = wwin->frame->core->width + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
831 data->winHeight = wwin->frame->core->height + (HAS_BORDER_WITH_SELECT(wwin) ? 2 * wwin->screen_ptr->frame_border_width : 0);
834 static Bool checkWorkspaceChange(WWindow * wwin, MoveData * data, Bool opaqueMove)
836 WScreen *scr = wwin->screen_ptr;
837 Bool changed = False;
839 if (data->mouseX <= 1) {
840 if (w_global.workspace.current > 0) {
841 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current - 1, True);
842 changed = True;
843 data->rubCount = 0;
844 } else if (w_global.workspace.current == 0 && wPreferences.ws_cycle) {
845 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.count - 1, True);
846 changed = True;
847 data->rubCount = 0;
849 } else if (data->mouseX >= scr->scr_width - 2) {
850 if (w_global.workspace.current == w_global.workspace.count - 1) {
851 if (wPreferences.ws_cycle || w_global.workspace.count == MAX_WORKSPACES) {
852 crossWorkspace(scr, wwin, opaqueMove, 0, False);
853 changed = True;
854 data->rubCount = 0;
856 /* if user insists on trying to go to next workspace even when
857 * it's already the last, create a new one */
858 else if (data->omouseX == data->mouseX && wPreferences.ws_advance) {
860 /* detect user "rubbing" the window against the edge */
861 if (data->rubCount > 0 && data->omouseY - data->mouseY > MOVE_THRESHOLD) {
863 data->rubCount = -(data->rubCount + 1);
865 } else if (data->rubCount <= 0 && data->mouseY - data->omouseY > MOVE_THRESHOLD) {
867 data->rubCount = -data->rubCount + 1;
870 /* create a new workspace */
871 if (abs(data->rubCount) > 2) {
872 /* go to next workspace */
873 wWorkspaceNew(scr);
875 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current + 1, False);
876 changed = True;
877 data->rubCount = 0;
879 } else if (w_global.workspace.current < w_global.workspace.count) {
880 /* go to next workspace */
881 crossWorkspace(scr, wwin, opaqueMove, w_global.workspace.current + 1, False);
882 changed = True;
883 data->rubCount = 0;
885 } else {
886 data->rubCount = 0;
889 return changed;
892 static void
893 updateWindowPosition(WWindow * wwin, MoveData * data, Bool doResistance,
894 Bool opaqueMove, int newMouseX, int newMouseY)
896 WScreen *scr = wwin->screen_ptr;
897 int dx, dy; /* how much mouse moved */
898 int winL, winR, winT, winB; /* requested new window position */
899 int newX, newY; /* actual new window position */
900 Bool hresist, vresist;
901 Bool attract;
903 hresist = False;
904 vresist = False;
906 /* check the direction of the movement */
907 dx = newMouseX - data->mouseX;
908 dy = newMouseY - data->mouseY;
910 data->omouseX = data->mouseX;
911 data->omouseY = data->mouseY;
912 data->mouseX = newMouseX;
913 data->mouseY = newMouseY;
915 winL = data->calcX + dx;
916 winR = data->calcX + data->winWidth + dx;
917 winT = data->calcY + dy;
918 winB = data->calcY + data->winHeight + dy;
920 newX = data->realX;
921 newY = data->realY;
923 if (doResistance) {
924 int l_edge, r_edge;
925 int edge_l, edge_r;
926 int t_edge, b_edge;
927 int edge_t, edge_b;
928 int resist;
930 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
931 attract = wPreferences.attract;
932 /* horizontal movement: check horizontal edge resistances */
933 if (dx || dy) {
934 WMRect rect;
935 int i, head;
936 /* window is the leftmost window: check against screen edge */
938 /* Add inter head resistance 1/2 (if needed) */
939 head = wGetHeadForPointerLocation(scr);
940 rect = wGetRectForHead(scr, head);
942 l_edge = WMAX(scr->totalUsableArea[head].x1, rect.pos.x);
943 edge_l = l_edge - resist;
944 edge_r = WMIN(scr->totalUsableArea[head].x2, rect.pos.x + rect.size.width);
945 r_edge = edge_r + resist;
947 /* 1 */
948 if ((data->rightIndex >= 0) && (data->rightIndex <= data->count)) {
949 WWindow *looprw;
951 for (i = data->rightIndex - 1; i >= 0; i--) {
952 looprw = data->rightList[i];
953 if (!(data->realY > WBOTTOM(looprw)
954 || (data->realY + data->winHeight) < WTOP(looprw))) {
955 if (attract || ((data->realX < (WRIGHT(looprw) + 2)) && dx < 0)) {
956 l_edge = WRIGHT(looprw) + 1;
957 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
959 break;
963 if (attract) {
964 for (i = data->rightIndex; i < data->count; i++) {
965 looprw = data->rightList[i];
966 if (!(data->realY > WBOTTOM(looprw)
967 || (data->realY + data->winHeight) < WTOP(looprw))) {
968 r_edge = WRIGHT(looprw) + 1;
969 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
970 break;
976 if ((data->leftIndex >= 0) && (data->leftIndex <= data->count)) {
977 WWindow *looprw;
979 for (i = data->leftIndex - 1; i >= 0; i--) {
980 looprw = data->leftList[i];
981 if (!(data->realY > WBOTTOM(looprw)
982 || (data->realY + data->winHeight) < WTOP(looprw))) {
983 if (attract
984 || (((data->realX + data->winWidth) > (WLEFT(looprw) - 1))
985 && dx > 0)) {
986 edge_r = WLEFT(looprw);
987 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
989 break;
993 if (attract)
994 for (i = data->leftIndex; i < data->count; i++) {
995 looprw = data->leftList[i];
996 if (!(data->realY > WBOTTOM(looprw)
997 || (data->realY + data->winHeight) < WTOP(looprw))) {
998 edge_l = WLEFT(looprw);
999 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1000 break;
1006 printf("%d %d\n",winL,winR);
1007 printf("l_ %d r_ %d _l %d _r %d\n",l_edge,r_edge,edge_l,edge_r);
1010 if ((winL - l_edge) < (r_edge - winL)) {
1011 if (resist > 0) {
1012 if ((attract && winL <= l_edge + resist && winL >= l_edge - resist)
1013 || (dx < 0 && winL <= l_edge && winL >= l_edge - resist)) {
1014 newX = l_edge;
1015 hresist = True;
1018 } else {
1019 if (resist > 0 && attract && winL >= r_edge - resist && winL <= r_edge + resist) {
1020 newX = r_edge;
1021 hresist = True;
1025 if ((winR - edge_l) < (edge_r - winR)) {
1026 if (resist > 0 && attract && winR <= edge_l + resist && winR >= edge_l - resist) {
1027 newX = edge_l - data->winWidth;
1028 hresist = True;
1030 } else {
1031 if (resist > 0) {
1032 if ((attract && winR >= edge_r - resist && winR <= edge_r + resist)
1033 || (dx > 0 && winR >= edge_r && winR <= edge_r + resist)) {
1034 newX = edge_r - data->winWidth;
1035 hresist = True;
1040 /* VeRT */
1041 /* Add inter head resistance 2/2 (if needed) */
1042 t_edge = WMAX(scr->totalUsableArea[head].y1, rect.pos.y);
1043 edge_t = t_edge - resist;
1044 edge_b = WMIN(scr->totalUsableArea[head].y2, rect.pos.y + rect.size.height);
1045 b_edge = edge_b + resist;
1047 if ((data->bottomIndex >= 0) && (data->bottomIndex <= data->count)) {
1048 WWindow *looprw;
1050 for (i = data->bottomIndex - 1; i >= 0; i--) {
1051 looprw = data->bottomList[i];
1052 if (!(data->realX > WRIGHT(looprw)
1053 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1054 if (attract || ((data->realY < (WBOTTOM(looprw) + 2)) && dy < 0)) {
1055 t_edge = WBOTTOM(looprw) + 1;
1056 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1058 break;
1062 if (attract) {
1063 for (i = data->bottomIndex; i < data->count; i++) {
1064 looprw = data->bottomList[i];
1065 if (!(data->realX > WRIGHT(looprw)
1066 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1067 b_edge = WBOTTOM(looprw) + 1;
1068 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1069 break;
1075 if ((data->topIndex >= 0) && (data->topIndex <= data->count)) {
1076 WWindow *looprw;
1078 for (i = data->topIndex - 1; i >= 0; i--) {
1079 looprw = data->topList[i];
1080 if (!(data->realX > WRIGHT(looprw)
1081 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1082 if (attract
1083 || (((data->realY + data->winHeight) > (WTOP(looprw) - 1))
1084 && dy > 0)) {
1085 edge_b = WTOP(looprw);
1086 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1088 break;
1092 if (attract)
1093 for (i = data->topIndex; i < data->count; i++) {
1094 looprw = data->topList[i];
1095 if (!(data->realX > WRIGHT(looprw)
1096 || (data->realX + data->winWidth) < WLEFT(looprw))) {
1097 edge_t = WTOP(looprw);
1098 resist = WIN_RESISTANCE(wPreferences.edge_resistance);
1099 break;
1104 if ((winT - t_edge) < (b_edge - winT)) {
1105 if (resist > 0) {
1106 if ((attract && winT <= t_edge + resist && winT >= t_edge - resist)
1107 || (dy < 0 && winT <= t_edge && winT >= t_edge - resist)) {
1108 newY = t_edge;
1109 vresist = True;
1112 } else {
1113 if (resist > 0 && attract && winT >= b_edge - resist && winT <= b_edge + resist) {
1114 newY = b_edge;
1115 vresist = True;
1119 if ((winB - edge_t) < (edge_b - winB)) {
1120 if (resist > 0 && attract && winB <= edge_t + resist && winB >= edge_t - resist) {
1121 newY = edge_t - data->winHeight;
1122 vresist = True;
1124 } else {
1125 if (resist > 0) {
1126 if ((attract && winB >= edge_b - resist && winB <= edge_b + resist)
1127 || (dy > 0 && winB >= edge_b && winB <= edge_b + resist)) {
1128 newY = edge_b - data->winHeight;
1129 vresist = True;
1134 /* END VeRT */
1138 /* update window position */
1139 data->calcX += dx;
1140 data->calcY += dy;
1142 if (((dx > 0 && data->calcX - data->realX > 0)
1143 || (dx < 0 && data->calcX - data->realX < 0)) && !hresist)
1144 newX = data->calcX;
1146 if (((dy > 0 && data->calcY - data->realY > 0)
1147 || (dy < 0 && data->calcY - data->realY < 0)) && !vresist)
1148 newY = data->calcY;
1150 if (data->realX != newX || data->realY != newY) {
1152 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1153 showPosition(wwin, data->realX, data->realY);
1155 if (opaqueMove) {
1156 doWindowMove(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1157 } else {
1158 /* erase frames */
1159 drawFrames(wwin, scr->selected_windows,
1160 data->realX - wwin->frame_x, data->realY - wwin->frame_y);
1163 if (!scr->selected_windows && wPreferences.move_display == WDIS_FRAME_CENTER) {
1165 moveGeometryDisplayCentered(scr, newX + data->winWidth / 2, newY + data->winHeight / 2);
1168 if (!opaqueMove) {
1169 /* draw frames */
1170 drawFrames(wwin, scr->selected_windows, newX - wwin->frame_x, newY - wwin->frame_y);
1173 if (!scr->selected_windows) {
1174 showPosition(wwin, newX, newY);
1178 /* recalc relative window position */
1179 if (doResistance && (data->realX != newX || data->realY != newY)) {
1180 updateResistance(data, newX, newY);
1183 data->realX = newX;
1184 data->realY = newY;
1187 #define _KS KEY_CONTROL_WINDOW_WEIGHT
1189 #define MOVABLE_BIT 0x01
1190 #define RESIZABLE_BIT 0x02
1192 int wKeyboardMoveResizeWindow(WWindow * wwin)
1194 WScreen *scr = wwin->screen_ptr;
1195 Window root = scr->root_win;
1196 XEvent event;
1197 int w = wwin->frame->core->width;
1198 int h = wwin->frame->core->height;
1199 int scr_width = wwin->screen_ptr->scr_width;
1200 int scr_height = wwin->screen_ptr->scr_height;
1201 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1202 int src_x = wwin->frame_x;
1203 int src_y = wwin->frame_y;
1204 int original_w = w;
1205 int original_h = h;
1206 int done, off_x, off_y, ww, wh;
1207 int kspeed = _KS;
1208 int opaqueMoveResize = wPreferences.opaque_move_resize_keyboard;
1209 Time lastTime = 0;
1210 KeyCode shiftl, shiftr, ctrlmode;
1211 KeySym keysym = NoSymbol;
1212 int moment = 0;
1213 int modes = ((IS_MOVABLE(wwin) ? MOVABLE_BIT : 0) | (IS_RESIZABLE(wwin) ? RESIZABLE_BIT : 0));
1214 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1215 ? wGetHeadForWindow(wwin)
1216 : scr->xine_info.primary_head);
1218 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1219 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1220 ctrlmode = done = off_x = off_y = 0;
1222 if (modes == RESIZABLE_BIT) {
1223 ctrlmode = 1;
1226 XSync(dpy, False);
1227 wusleep(10000);
1228 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1230 if (!wwin->flags.selected) {
1231 wUnselectWindows(scr);
1233 XGrabServer(dpy);
1234 XGrabPointer(dpy, scr->root_win, True, PointerMotionMask
1235 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
1236 GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime);
1240 if (!opaqueMoveResize) {
1241 if (wwin->flags.shaded || scr->selected_windows) {
1242 if (scr->selected_windows)
1243 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1244 else
1245 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1246 } else {
1247 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1250 if ((wwin->flags.shaded || scr->selected_windows) && (!scr->selected_windows)) {
1251 mapPositionDisplay(wwin, src_x, src_y, w, h);
1254 ww = w;
1255 wh = h;
1256 while (1) {
1258 looper.ox=off_x;
1259 looper.oy=off_y;
1261 do {
1262 WMMaskEvent(dpy, KeyPressMask | ButtonReleaseMask
1263 | ButtonPressMask | ExposureMask, &event);
1264 if (event.type == Expose) {
1265 WMHandleEvent(&event);
1267 } while (event.type == Expose);
1269 if (!opaqueMoveResize) {
1270 if (wwin->flags.shaded || scr->selected_windows) {
1271 if (scr->selected_windows)
1272 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1273 else
1274 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1275 /*** I HATE EDGE RESISTANCE - ]d ***/
1276 } else {
1277 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1281 if (ctrlmode)
1282 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1285 XUngrabServer(dpy);
1286 XSync(dpy, False);
1288 switch (event.type) {
1289 case KeyPress:
1290 /* accelerate */
1291 if (event.xkey.time - lastTime > 50) {
1292 kspeed /= (1 + (event.xkey.time - lastTime) / 100);
1293 } else {
1294 if (kspeed < 20) {
1295 kspeed++;
1298 if (kspeed < _KS)
1299 kspeed = _KS;
1300 lastTime = event.xkey.time;
1301 if (modes == (MOVABLE_BIT | RESIZABLE_BIT)) {
1302 if ((event.xkey.state & ControlMask) && !wwin->flags.shaded) {
1303 ctrlmode = 1;
1304 wUnselectWindows(scr);
1305 } else {
1306 ctrlmode = 0;
1309 if (event.xkey.keycode == shiftl || event.xkey.keycode == shiftr) {
1310 if (ctrlmode)
1311 cycleGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh, 0);
1312 else
1313 cyclePositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1314 } else {
1316 keysym = XLookupKeysym(&event.xkey, 0);
1317 switch (keysym) {
1318 case XK_Return:
1319 done = 2;
1320 break;
1321 case XK_Escape:
1322 done = 1;
1323 break;
1324 case XK_Up:
1325 #ifdef XK_KP_Up
1326 case XK_KP_Up:
1327 #endif
1328 case XK_k:
1329 if (ctrlmode) {
1330 if (moment != UP)
1331 h = wh;
1332 h -= kspeed;
1333 moment = UP;
1334 if (h < 1)
1335 h = 1;
1336 } else
1337 off_y -= kspeed;
1338 break;
1339 case XK_Down:
1340 #ifdef XK_KP_Down
1341 case XK_KP_Down:
1342 #endif
1343 case XK_j:
1344 if (ctrlmode) {
1345 if (moment != DOWN)
1346 h = wh;
1347 h += kspeed;
1348 moment = DOWN;
1349 } else
1350 off_y += kspeed;
1351 break;
1352 case XK_Left:
1353 #ifdef XK_KP_Left
1354 case XK_KP_Left:
1355 #endif
1356 case XK_h:
1357 if (ctrlmode) {
1358 if (moment != LEFT)
1359 w = ww;
1360 w -= kspeed;
1361 if (w < 1)
1362 w = 1;
1363 moment = LEFT;
1364 } else
1365 off_x -= kspeed;
1366 break;
1367 case XK_Right:
1368 #ifdef XK_KP_Right
1369 case XK_KP_Right:
1370 #endif
1371 case XK_l:
1372 if (ctrlmode) {
1373 if (moment != RIGHT)
1374 w = ww;
1375 w += kspeed;
1376 moment = RIGHT;
1377 } else
1378 off_x += kspeed;
1379 break;
1382 ww = w;
1383 wh = h;
1384 wh -= vert_border;
1385 wWindowConstrainSize(wwin, (unsigned int *)&ww, (unsigned int *)&wh);
1386 wh += vert_border;
1388 if (wPreferences.ws_cycle) {
1389 if (src_x + off_x + ww < 20) {
1390 if (!w_global.workspace.current)
1391 wWorkspaceChange(scr, w_global.workspace.count - 1);
1392 else
1393 wWorkspaceChange(scr, w_global.workspace.current - 1);
1395 off_x += scr_width;
1396 } else if (src_x + off_x + 20 > scr_width) {
1397 if (w_global.workspace.current == w_global.workspace.count - 1)
1398 wWorkspaceChange(scr, 0);
1399 else
1400 wWorkspaceChange(scr, w_global.workspace.current + 1);
1402 off_x -= scr_width;
1404 } else {
1405 if (src_x + off_x + ww < 20)
1406 off_x = 20 - ww - src_x;
1407 else if (src_x + off_x + 20 > scr_width)
1408 off_x = scr_width - 20 - src_x;
1411 if (src_y + off_y + wh < 20) {
1412 off_y = 20 - wh - src_y;
1413 } else if (src_y + off_y + 20 > scr_height) {
1414 off_y = scr_height - 20 - src_y;
1417 break;
1418 case ButtonPress:
1419 case ButtonRelease:
1420 done = 1;
1421 break;
1422 case Expose:
1423 WMHandleEvent(&event);
1424 while (XCheckTypedEvent(dpy, Expose, &event)) {
1425 WMHandleEvent(&event);
1427 break;
1429 default:
1430 WMHandleEvent(&event);
1431 break;
1434 XGrabServer(dpy);
1435 /*xxx */
1437 if (wwin->flags.shaded && !scr->selected_windows) {
1438 moveGeometryDisplayCentered(scr, src_x + off_x + w / 2, src_y + off_y + h / 2);
1439 } else {
1440 if (ctrlmode) {
1441 WMUnmapWidget(scr->gview);
1442 mapGeometryDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1443 } else if (!scr->selected_windows) {
1444 WMUnmapWidget(scr->gview);
1445 mapPositionDisplay(wwin, src_x + off_x, src_y + off_y, ww, wh);
1449 if (!opaqueMoveResize) {
1450 if (wwin->flags.shaded || scr->selected_windows) {
1451 if (scr->selected_windows)
1452 drawFrames(wwin, scr->selected_windows, off_x, off_y);
1453 else
1454 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, w, h);
1455 } else {
1456 drawTransparentFrame(wwin, src_x + off_x, src_y + off_y, ww, wh);
1460 if (ctrlmode) {
1461 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww, src_y + off_y + wh,
1463 } else if (!scr->selected_windows)
1464 showPosition(wwin, src_x + off_x, src_y + off_y);
1466 if (opaqueMoveResize) {
1467 XUngrabServer(dpy);
1468 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1471 if (done) {
1472 if (!opaqueMoveResize) { /* ctrlmode => resize */
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
1477 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);
1483 if (ctrlmode) {
1484 showGeometry(wwin, src_x + off_x, src_y + off_y, src_x + off_x + ww,
1485 src_y + off_y + wh, 0);
1486 WMUnmapWidget(scr->gview);
1487 } else
1488 WMUnmapWidget(scr->gview);
1490 XUngrabKeyboard(dpy, CurrentTime);
1491 XUngrabPointer(dpy, CurrentTime);
1492 XUngrabServer(dpy);
1494 if (done == 2) {
1495 if (wwin->flags.shaded || scr->selected_windows) {
1496 if (!scr->selected_windows) {
1497 wWindowMove(wwin, src_x + off_x, src_y + off_y);
1498 wWindowSynthConfigureNotify(wwin);
1499 } else {
1500 WMArrayIterator iter;
1501 WWindow *foo;
1503 doWindowMove(wwin, scr->selected_windows, off_x, off_y);
1505 WM_ITERATE_ARRAY(scr->selected_windows, foo, iter) {
1506 wWindowSynthConfigureNotify(foo);
1509 } else {
1510 if (ww != original_w)
1511 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
1513 if (wh != original_h)
1514 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
1516 wWindowConfigure(wwin, src_x + off_x, src_y + off_y, ww, wh - vert_border);
1517 wWindowSynthConfigureNotify(wwin);
1519 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1520 wSetFocusTo(scr, wwin);
1523 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1524 head != wGetHeadForWindow(wwin)) {
1525 wArrangeIcons(scr, True);
1528 update_saved_geometry(wwin);
1530 return 1;
1536 *----------------------------------------------------------------------
1537 * wMouseMoveWindow--
1538 * Move the named window and the other selected ones (if any),
1539 * interactively. Also shows the position of the window, if only one
1540 * window is being moved.
1541 * If the window is not on the selected window list, the selected
1542 * windows are deselected.
1543 * If shift is pressed during the operation, the position display
1544 * is changed to another type.
1546 * Returns:
1547 * True if the window was moved, False otherwise.
1549 * Side effects:
1550 * The window(s) position is changed, and the client(s) are
1551 * notified about that.
1552 * The position display configuration may be changed.
1553 *----------------------------------------------------------------------
1555 int wMouseMoveWindow(WWindow * wwin, XEvent * ev)
1557 WScreen *scr = wwin->screen_ptr;
1558 XEvent event;
1559 Window root = scr->root_win;
1560 KeyCode shiftl, shiftr;
1561 Bool done = False;
1562 int started = 0;
1563 int warped = 0;
1564 /* This needs not to change while moving, else bad things can happen */
1565 int opaqueMove = wPreferences.opaque_move;
1566 MoveData moveData;
1567 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1568 ? wGetHeadForWindow(wwin)
1569 : scr->xine_info.primary_head);
1571 if (!IS_MOVABLE(wwin))
1572 return False;
1574 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1575 XSetWindowAttributes attr;
1577 attr.save_under = True;
1578 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1581 initMoveData(wwin, &moveData);
1583 moveData.mouseX = ev->xmotion.x_root;
1584 moveData.mouseY = ev->xmotion.y_root;
1586 if (!wwin->flags.selected) {
1587 /* this window is not selected, unselect others and move only wwin */
1588 wUnselectWindows(scr);
1590 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1591 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1592 while (!done) {
1593 if (warped) {
1594 int junk;
1595 Window junkw;
1597 /* XWarpPointer() doesn't seem to generate Motion events, so
1598 * we've got to simulate them */
1599 XQueryPointer(dpy, root, &junkw, &junkw, &event.xmotion.x_root,
1600 &event.xmotion.y_root, &junk, &junk, (unsigned *)&junk);
1601 } else {
1602 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1603 | PointerMotionHintMask
1604 | ButtonReleaseMask | ButtonPressMask | ExposureMask, &event);
1606 if (event.type == MotionNotify) {
1607 /* compress MotionNotify events */
1608 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1609 if (!checkMouseSamplingRate(&event))
1610 continue;
1613 switch (event.type) {
1614 case KeyPress:
1615 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1616 && started && !scr->selected_windows) {
1618 if (!opaqueMove) {
1619 drawFrames(wwin, scr->selected_windows,
1620 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1623 if (wPreferences.move_display == WDIS_NEW && !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 && !scr->selected_windows) {
1631 XGrabServer(dpy);
1632 showPosition(wwin, moveData.realX, moveData.realY);
1635 if (!opaqueMove) {
1636 drawFrames(wwin, scr->selected_windows,
1637 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1639 /*} else {
1640 WMHandleEvent(&event); this causes problems needs fixing */
1642 break;
1644 case MotionNotify:
1645 if (started) {
1646 updateWindowPosition(wwin, &moveData,
1647 scr->selected_windows == NULL
1648 && wPreferences.edge_resistance > 0,
1649 opaqueMove, event.xmotion.x_root, event.xmotion.y_root);
1651 if (!warped && !wPreferences.no_autowrap) {
1652 int oldWorkspace = w_global.workspace.current;
1654 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1655 showPosition(wwin, moveData.realX, moveData.realY);
1656 XUngrabServer(dpy);
1658 if (!opaqueMove) {
1659 drawFrames(wwin, scr->selected_windows,
1660 moveData.realX - wwin->frame_x,
1661 moveData.realY - wwin->frame_y);
1663 if (checkWorkspaceChange(wwin, &moveData, opaqueMove)) {
1664 if (w_global.workspace.current != oldWorkspace
1665 && wPreferences.edge_resistance > 0
1666 && scr->selected_windows == NULL)
1667 updateMoveData(wwin, &moveData);
1668 warped = 1;
1670 if (!opaqueMove) {
1671 drawFrames(wwin, scr->selected_windows,
1672 moveData.realX - wwin->frame_x,
1673 moveData.realY - wwin->frame_y);
1675 if (wPreferences.move_display == WDIS_NEW && !scr->selected_windows) {
1676 XSync(dpy, False);
1677 showPosition(wwin, moveData.realX, moveData.realY);
1678 XGrabServer(dpy);
1680 } else {
1681 warped = 0;
1683 } else if (abs(ev->xmotion.x_root - event.xmotion.x_root) >= MOVE_THRESHOLD
1684 || abs(ev->xmotion.y_root - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1686 XChangeActivePointerGrab(dpy, ButtonMotionMask
1687 | ButtonReleaseMask | ButtonPressMask,
1688 wPreferences.cursor[WCUR_MOVE], CurrentTime);
1689 started = 1;
1690 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
1692 if (!scr->selected_windows)
1693 mapPositionDisplay(wwin, moveData.realX, moveData.realY,
1694 moveData.winWidth, moveData.winHeight);
1696 if (started && !opaqueMove)
1697 drawFrames(wwin, scr->selected_windows, 0, 0);
1699 if (!opaqueMove || (wPreferences.move_display == WDIS_NEW
1700 && !scr->selected_windows)) {
1701 XGrabServer(dpy);
1702 if (wPreferences.move_display == WDIS_NEW)
1703 showPosition(wwin, moveData.realX, moveData.realY);
1706 break;
1708 case ButtonPress:
1709 break;
1711 case ButtonRelease:
1712 if (event.xbutton.button != ev->xbutton.button)
1713 break;
1715 if (started) {
1716 XEvent e;
1717 if (!opaqueMove) {
1718 drawFrames(wwin, scr->selected_windows,
1719 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1720 XSync(dpy, 0);
1721 doWindowMove(wwin, scr->selected_windows,
1722 moveData.realX - wwin->frame_x,
1723 moveData.realY - wwin->frame_y);
1725 #ifndef CONFIGURE_WINDOW_WHILE_MOVING
1726 wWindowSynthConfigureNotify(wwin);
1727 #endif
1728 XUngrabKeyboard(dpy, CurrentTime);
1729 XUngrabServer(dpy);
1730 if (!opaqueMove) {
1731 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1732 wSetFocusTo(scr, wwin);
1734 if (wPreferences.move_display == WDIS_NEW)
1735 showPosition(wwin, moveData.realX, moveData.realY);
1737 /* discard all enter/leave events that happened until
1738 * the time the button was released */
1739 while (XCheckTypedEvent(dpy, EnterNotify, &e)) {
1740 if (e.xcrossing.time > event.xbutton.time) {
1741 XPutBackEvent(dpy, &e);
1742 break;
1745 while (XCheckTypedEvent(dpy, LeaveNotify, &e)) {
1746 if (e.xcrossing.time > event.xbutton.time) {
1747 XPutBackEvent(dpy, &e);
1748 break;
1752 if (!scr->selected_windows) {
1753 /* get rid of the geometry window */
1754 WMUnmapWidget(scr->gview);
1757 done = True;
1758 break;
1760 default:
1761 if (started && !opaqueMove) {
1762 drawFrames(wwin, scr->selected_windows,
1763 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1764 XUngrabServer(dpy);
1765 WMHandleEvent(&event);
1766 XSync(dpy, False);
1767 XGrabServer(dpy);
1768 drawFrames(wwin, scr->selected_windows,
1769 moveData.realX - wwin->frame_x, moveData.realY - wwin->frame_y);
1770 } else {
1771 WMHandleEvent(&event);
1773 break;
1777 if (wPreferences.opaque_move && !wPreferences.use_saveunders) {
1778 XSetWindowAttributes attr;
1780 attr.save_under = False;
1781 XChangeWindowAttributes(dpy, wwin->frame->core->window, CWSaveUnder, &attr);
1785 freeMoveData(&moveData);
1787 if (started && wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 &&
1788 head != wGetHeadForWindow(wwin)) {
1789 wArrangeIcons(scr, True);
1792 if (started)
1793 update_saved_geometry(wwin);
1795 return started;
1798 #define RESIZEBAR 1
1799 #define HCONSTRAIN 2
1801 static int getResizeDirection(WWindow * wwin, int x, int y, int dx, int dy, int flags)
1803 int w = wwin->frame->core->width - 1;
1804 int cw = wwin->frame->resizebar_corner_width;
1805 int dir;
1807 /* if not resizing through the resizebar */
1808 if (!(flags & RESIZEBAR)) {
1809 int xdir = (abs(x) < (wwin->client.width / 2)) ? LEFT : RIGHT;
1810 int ydir = (abs(y) < (wwin->client.height / 2)) ? UP : DOWN;
1811 if (abs(dx) < 2 || abs(dy) < 2) {
1812 if (abs(dy) > abs(dx))
1813 xdir = 0;
1814 else
1815 ydir = 0;
1817 return (xdir | ydir);
1820 /* window is too narrow. allow diagonal resize */
1821 if (cw * 2 >= w) {
1822 int ydir;
1824 if (flags & HCONSTRAIN)
1825 ydir = 0;
1826 else
1827 ydir = DOWN;
1828 if (x < cw)
1829 return (LEFT | ydir);
1830 else
1831 return (RIGHT | ydir);
1833 /* vertical resize */
1834 if ((x > cw) && (x < w - cw))
1835 return DOWN;
1837 if (x < cw)
1838 dir = LEFT;
1839 else
1840 dir = RIGHT;
1842 if ((abs(dy) > 0) && !(flags & HCONSTRAIN))
1843 dir |= DOWN;
1845 return dir;
1848 void wMouseResizeWindow(WWindow * wwin, XEvent * ev)
1850 XEvent event;
1851 WScreen *scr = wwin->screen_ptr;
1852 Window root = scr->root_win;
1853 int vert_border = wwin->frame->top_width + wwin->frame->bottom_width;
1854 int fw = wwin->frame->core->width;
1855 int fh = wwin->frame->core->height;
1856 int fx = wwin->frame_x;
1857 int fy = wwin->frame_y;
1858 int is_resizebar = (wwin->frame->resizebar && ev->xany.window == wwin->frame->resizebar->window);
1859 int orig_x, orig_y;
1860 int started;
1861 int dw, dh;
1862 int rw = fw, rh = fh;
1863 int rx1, ry1, rx2, ry2;
1864 int res = 0;
1865 KeyCode shiftl, shiftr;
1866 int orig_fx = fx;
1867 int orig_fy = fy;
1868 int orig_fw = fw;
1869 int orig_fh = fh;
1870 int original_fw = fw;
1871 int original_fh = fh;
1872 int head = ((wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1)
1873 ? wGetHeadForWindow(wwin)
1874 : scr->xine_info.primary_head);
1875 int opaqueResize = wPreferences.opaque_resize;
1877 if (!IS_RESIZABLE(wwin))
1878 return;
1880 if (wwin->flags.shaded) {
1881 wwarning("internal error: tryein");
1882 return;
1884 orig_x = ev->xbutton.x_root;
1885 orig_y = ev->xbutton.y_root;
1887 started = 0;
1888 wUnselectWindows(scr);
1889 rx1 = fx;
1890 rx2 = fx + fw - 1;
1891 ry1 = fy;
1892 ry2 = fy + fh - 1;
1893 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
1894 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
1896 while (1) {
1897 WMMaskEvent(dpy, KeyPressMask | ButtonMotionMask
1898 | ButtonReleaseMask | PointerMotionHintMask | ButtonPressMask | ExposureMask, &event);
1899 if (!checkMouseSamplingRate(&event))
1900 continue;
1902 switch (event.type) {
1903 case KeyPress:
1904 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1905 if (!opaqueResize) {
1906 if ((event.xkey.keycode == shiftl || event.xkey.keycode == shiftr)
1907 && started) {
1908 drawTransparentFrame(wwin, fx, fy, fw, fh);
1909 cycleGeometryDisplay(wwin, fx, fy, fw, fh, res);
1910 drawTransparentFrame(wwin, fx, fy, fw, fh);
1913 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
1914 break;
1916 case MotionNotify:
1917 if (started) {
1918 while (XCheckMaskEvent(dpy, ButtonMotionMask, &event)) ;
1920 dw = 0;
1921 dh = 0;
1923 orig_fx = fx;
1924 orig_fy = fy;
1925 orig_fw = fw;
1926 orig_fh = fh;
1928 if (res & LEFT)
1929 dw = orig_x - event.xmotion.x_root;
1930 else if (res & RIGHT)
1931 dw = event.xmotion.x_root - orig_x;
1932 if (res & UP)
1933 dh = orig_y - event.xmotion.y_root;
1934 else if (res & DOWN)
1935 dh = event.xmotion.y_root - orig_y;
1937 orig_x = event.xmotion.x_root;
1938 orig_y = event.xmotion.y_root;
1940 rw += dw;
1941 rh += dh;
1942 fw = rw;
1943 fh = rh - vert_border;
1944 wWindowConstrainSize(wwin, (unsigned int *)&fw, (unsigned int *)&fh);
1945 fh += vert_border;
1946 if (res & LEFT)
1947 fx = rx2 - fw + 1;
1948 else if (res & RIGHT)
1949 fx = rx1;
1950 if (res & UP)
1951 fy = ry2 - fh + 1;
1952 else if (res & DOWN)
1953 fy = ry1;
1954 } else if (abs(orig_x - event.xmotion.x_root) >= MOVE_THRESHOLD
1955 || abs(orig_y - event.xmotion.y_root) >= MOVE_THRESHOLD) {
1956 int tx, ty;
1957 Window junkw;
1958 int flags;
1960 XTranslateCoordinates(dpy, root, wwin->frame->core->window,
1961 orig_x, orig_y, &tx, &ty, &junkw);
1963 /* check if resizing through resizebar */
1964 if (is_resizebar)
1965 flags = RESIZEBAR;
1966 else
1967 flags = 0;
1969 if (is_resizebar && ((ev->xbutton.state & ShiftMask)
1970 || abs(orig_y - event.xmotion.y_root) < HRESIZE_THRESHOLD))
1971 flags |= HCONSTRAIN;
1973 res = getResizeDirection(wwin, tx, ty,
1974 orig_x - event.xmotion.x_root,
1975 orig_y - event.xmotion.y_root, flags);
1977 if (res == (UP | LEFT))
1978 XChangeActivePointerGrab(dpy, ButtonMotionMask
1979 | ButtonReleaseMask | ButtonPressMask,
1980 wPreferences.cursor[WCUR_TOPLEFTRESIZE], CurrentTime);
1981 else if (res == (UP | RIGHT))
1982 XChangeActivePointerGrab(dpy, ButtonMotionMask
1983 | ButtonReleaseMask | ButtonPressMask,
1984 wPreferences.cursor[WCUR_TOPRIGHTRESIZE], CurrentTime);
1985 else if (res == (DOWN | LEFT))
1986 XChangeActivePointerGrab(dpy, ButtonMotionMask
1987 | ButtonReleaseMask | ButtonPressMask,
1988 wPreferences.cursor[WCUR_BOTTOMLEFTRESIZE], CurrentTime);
1989 else if (res == (DOWN | RIGHT))
1990 XChangeActivePointerGrab(dpy, ButtonMotionMask
1991 | ButtonReleaseMask | ButtonPressMask,
1992 wPreferences.cursor[WCUR_BOTTOMRIGHTRESIZE], CurrentTime);
1993 else if (res == DOWN || res == UP)
1994 XChangeActivePointerGrab(dpy, ButtonMotionMask
1995 | ButtonReleaseMask | ButtonPressMask,
1996 wPreferences.cursor[WCUR_VERTICALRESIZE], CurrentTime);
1997 else if (res & (DOWN | UP))
1998 XChangeActivePointerGrab(dpy, ButtonMotionMask
1999 | ButtonReleaseMask | ButtonPressMask,
2000 wPreferences.cursor[WCUR_VERTICALRESIZE], CurrentTime);
2001 else if (res & (LEFT | RIGHT))
2002 XChangeActivePointerGrab(dpy, ButtonMotionMask
2003 | ButtonReleaseMask | ButtonPressMask,
2004 wPreferences.cursor[WCUR_HORIZONRESIZE], CurrentTime);
2006 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2008 XGrabServer(dpy);
2010 /* Draw the resize frame for the first time. */
2011 mapGeometryDisplay(wwin, fx, fy, fw, fh);
2013 if (!opaqueResize)
2014 drawTransparentFrame(wwin, fx, fy, fw, fh);
2016 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2018 started = 1;
2020 if (started) {
2021 if (!opaqueResize)
2022 drawTransparentFrame(wwin, orig_fx, orig_fy, orig_fw, orig_fh);
2024 if (wPreferences.size_display == WDIS_FRAME_CENTER)
2025 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2027 if (!opaqueResize)
2028 drawTransparentFrame(wwin, fx, fy, fw, fh);
2030 if (fh != orig_fh || fw != orig_fw) {
2031 if (wPreferences.size_display == WDIS_NEW)
2032 showGeometry(wwin, orig_fx, orig_fy, orig_fx + orig_fw,
2033 orig_fy + orig_fh, res);
2035 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2038 if (opaqueResize) {
2039 /* Fist clean the geometry line */
2040 showGeometry(wwin, fx, fy, fx + fw, fy + fh, res);
2041 /* Now, continue drawing */
2042 XUngrabServer(dpy);
2043 moveGeometryDisplayCentered(scr, fx + fw / 2, fy + fh / 2);
2044 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
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 if (!opaqueResize)
2061 drawTransparentFrame(wwin, fx, fy, fw, fh);
2063 XUngrabKeyboard(dpy, CurrentTime);
2064 WMUnmapWidget(scr->gview);
2065 XUngrabServer(dpy);
2067 if (fw != original_fw)
2068 wwin->flags.maximized &= ~(MAX_HORIZONTAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
2070 if (fh != original_fh)
2071 wwin->flags.maximized &= ~(MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
2073 wWindowConfigure(wwin, fx, fy, fw, fh - vert_border);
2074 wWindowSynthConfigureNotify(wwin);
2076 return;
2078 default:
2079 WMHandleEvent(&event);
2083 if (wPreferences.auto_arrange_icons && wXineramaHeads(scr) > 1 && head != wGetHeadForWindow(wwin))
2084 wArrangeIcons(scr, True);
2087 #undef LEFT
2088 #undef RIGHT
2089 #undef HORIZONTAL
2090 #undef UP
2091 #undef DOWN
2092 #undef VERTICAL
2093 #undef HCONSTRAIN
2094 #undef RESIZEBAR
2096 void wUnselectWindows(WScreen * scr)
2098 WWindow *wwin;
2100 if (!scr->selected_windows)
2101 return;
2103 while (WMGetArrayItemCount(scr->selected_windows)) {
2104 wwin = WMGetFromArray(scr->selected_windows, 0);
2105 if (wwin->flags.miniaturized && wwin->icon && wwin->icon->selected)
2106 wIconSelect(wwin->icon);
2108 wSelectWindow(wwin, False);
2110 WMFreeArray(scr->selected_windows);
2111 scr->selected_windows = NULL;
2114 static void selectWindowsInside(WScreen * scr, int x1, int y1, int x2, int y2)
2116 WWindow *tmpw;
2118 /* select the windows and put them in the selected window list */
2119 tmpw = scr->focused_window;
2120 while (tmpw != NULL) {
2121 if (!(tmpw->flags.miniaturized || tmpw->flags.hidden)) {
2122 if ((tmpw->frame->workspace == w_global.workspace.current || IS_OMNIPRESENT(tmpw))
2123 && (tmpw->frame_x >= x1) && (tmpw->frame_y >= y1)
2124 && (tmpw->frame->core->width + tmpw->frame_x <= x2)
2125 && (tmpw->frame->core->height + tmpw->frame_y <= y2)) {
2126 wSelectWindow(tmpw, True);
2129 tmpw = tmpw->prev;
2133 void wSelectWindows(WScreen * scr, XEvent * ev)
2135 XEvent event;
2136 Window root = scr->root_win;
2137 GC gc = scr->frame_gc;
2138 int xp = ev->xbutton.x_root;
2139 int yp = ev->xbutton.y_root;
2140 int w = 0, h = 0;
2141 int x = xp, y = yp;
2143 if (XGrabPointer(dpy, scr->root_win, False, ButtonMotionMask
2144 | ButtonReleaseMask | ButtonPressMask, GrabModeAsync,
2145 GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime) != Success) {
2146 return;
2148 XGrabServer(dpy);
2150 wUnselectWindows(scr);
2152 XDrawRectangle(dpy, root, gc, xp, yp, w, h);
2153 while (1) {
2154 WMMaskEvent(dpy, ButtonReleaseMask | PointerMotionMask | ButtonPressMask, &event);
2156 switch (event.type) {
2157 case MotionNotify:
2158 XDrawRectangle(dpy, root, gc, x, y, w, h);
2159 x = event.xmotion.x_root;
2160 if (x < xp) {
2161 w = xp - x;
2162 } else {
2163 w = x - xp;
2164 x = xp;
2166 y = event.xmotion.y_root;
2167 if (y < yp) {
2168 h = yp - y;
2169 } else {
2170 h = y - yp;
2171 y = yp;
2173 XDrawRectangle(dpy, root, gc, x, y, w, h);
2174 break;
2176 case ButtonPress:
2177 break;
2179 case ButtonRelease:
2180 if (event.xbutton.button != ev->xbutton.button)
2181 break;
2183 XDrawRectangle(dpy, root, gc, x, y, w, h);
2184 XUngrabServer(dpy);
2185 XUngrabPointer(dpy, CurrentTime);
2186 selectWindowsInside(scr, x, y, x + w, y + h);
2187 return;
2189 default:
2190 WMHandleEvent(&event);
2191 break;
2196 void InteractivePlaceWindow(WWindow * wwin, int *x_ret, int *y_ret, unsigned width, unsigned height)
2198 WScreen *scr = wwin->screen_ptr;
2199 Window root = scr->root_win;
2200 int x, y, h = 0;
2201 XEvent event;
2202 KeyCode shiftl, shiftr;
2203 Window junkw;
2204 int junk;
2206 if (XGrabPointer(dpy, root, True, PointerMotionMask | ButtonPressMask,
2207 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_NORMAL], CurrentTime) != Success) {
2208 *x_ret = 0;
2209 *y_ret = 0;
2210 return;
2212 if (HAS_TITLEBAR(wwin)) {
2213 h = WMFontHeight(scr->title_font) + (wPreferences.window_title_clearance +
2214 TITLEBAR_EXTEND_SPACE) * 2;
2216 if (h > wPreferences.window_title_max_height)
2217 h = wPreferences.window_title_max_height;
2219 if (h < wPreferences.window_title_min_height)
2220 h = wPreferences.window_title_min_height;
2222 height += h;
2224 if (HAS_RESIZEBAR(wwin)) {
2225 height += RESIZEBAR_HEIGHT;
2227 XGrabKeyboard(dpy, root, False, GrabModeAsync, GrabModeAsync, CurrentTime);
2228 XQueryPointer(dpy, root, &junkw, &junkw, &x, &y, &junk, &junk, (unsigned *)&junk);
2229 mapPositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2231 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2233 shiftl = XKeysymToKeycode(dpy, XK_Shift_L);
2234 shiftr = XKeysymToKeycode(dpy, XK_Shift_R);
2235 while (1) {
2236 WMMaskEvent(dpy, PointerMotionMask | ButtonPressMask | ExposureMask | KeyPressMask, &event);
2238 if (!checkMouseSamplingRate(&event))
2239 continue;
2241 switch (event.type) {
2242 case KeyPress:
2243 if ((event.xkey.keycode == shiftl)
2244 || (event.xkey.keycode == shiftr)) {
2245 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2246 cyclePositionDisplay(wwin, x - width / 2, y - h / 2, width, height);
2247 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2249 break;
2251 case MotionNotify:
2252 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2254 x = event.xmotion.x_root;
2255 y = event.xmotion.y_root;
2257 if (wPreferences.move_display == WDIS_FRAME_CENTER)
2258 moveGeometryDisplayCentered(scr, x, y + (height - h) / 2);
2260 showPosition(wwin, x - width / 2, y - h / 2);
2262 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2264 break;
2266 case ButtonPress:
2267 drawTransparentFrame(wwin, x - width / 2, y - h / 2, width, height);
2268 XSync(dpy, 0);
2269 *x_ret = x - width / 2;
2270 *y_ret = y - h / 2;
2271 XUngrabPointer(dpy, CurrentTime);
2272 XUngrabKeyboard(dpy, CurrentTime);
2273 /* get rid of the geometry window */
2274 WMUnmapWidget(scr->gview);
2275 return;
2277 default:
2278 WMHandleEvent(&event);
2279 break;