1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 moveresize.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
21 #include "framerender.h"
29 #include "moveresize.h"
33 #include "extensions.h"
34 #include "render/render.h"
35 #include "render/theme.h"
40 /* how far windows move and resize with the keyboard arrows */
43 gboolean moveresize_in_progress
= FALSE
;
44 ObClient
*moveresize_client
= NULL
;
46 XSyncAlarm moveresize_alarm
= None
;
49 static gboolean moving
= FALSE
; /* TRUE - moving, FALSE - resizing */
51 static gint start_x
, start_y
, start_cx
, start_cy
, start_cw
, start_ch
;
52 static gint cur_x
, cur_y
;
54 static guint32 corner
;
55 static ObCorner lockcorner
;
57 static gboolean waiting_for_sync
;
60 static ObPopup
*popup
= NULL
;
62 static void client_dest(ObClient
*client
, gpointer data
)
64 if (moveresize_client
== client
)
68 void moveresize_startup(gboolean reconfig
)
70 popup
= popup_new(FALSE
);
71 popup_set_text_align(popup
, RR_JUSTIFY_CENTER
);
74 client_add_destroy_notify(client_dest
, NULL
);
77 void moveresize_shutdown(gboolean reconfig
)
80 if (moveresize_in_progress
)
81 moveresize_end(FALSE
);
82 client_remove_destroy_notify(client_dest
);
89 static void get_resize_position(gint
*x
, gint
*y
, gboolean cancel
)
94 *x
= moveresize_client
->frame
->area
.x
;
95 *y
= moveresize_client
->frame
->area
.y
;
105 /* see how much it is actually going to resize */
107 gint cx
= *x
, cy
= *y
;
108 frame_frame_gravity(moveresize_client
->frame
, &cx
, &cy
, w
, h
);
109 client_try_configure(moveresize_client
, &cx
, &cy
, &w
, &h
,
112 dw
= w
- moveresize_client
->area
.width
;
113 dh
= h
- moveresize_client
->area
.height
;
115 switch (lockcorner
) {
116 case OB_CORNER_TOPLEFT
:
118 case OB_CORNER_TOPRIGHT
:
121 case OB_CORNER_BOTTOMLEFT
:
124 case OB_CORNER_BOTTOMRIGHT
:
130 frame_frame_gravity(moveresize_client
->frame
, x
, y
, w
, h
);
133 static void popup_coords(ObClient
*c
, const gchar
*format
, gint a
, gint b
)
137 text
= g_strdup_printf(format
, a
, b
);
138 if (config_resize_popup_pos
== 1) /* == "Top" */
139 popup_position(popup
, SouthGravity
,
141 + c
->frame
->area
.width
/2,
142 c
->frame
->area
.y
- ob_rr_theme
->fbwidth
);
143 else /* == "Center" */
144 popup_position(popup
, CenterGravity
,
145 c
->frame
->area
.x
+ c
->frame
->size
.left
+
147 c
->frame
->area
.y
+ c
->frame
->size
.top
+
149 popup_show(popup
, text
);
153 void moveresize_start(ObClient
*c
, gint x
, gint y
, guint b
, guint32 cnr
)
156 gboolean mv
= (cnr
== prop_atoms
.net_wm_moveresize_move
||
157 cnr
== prop_atoms
.net_wm_moveresize_move_keyboard
);
159 if (moveresize_in_progress
|| !c
->frame
->visible
||
161 (c
->functions
& OB_CLIENT_FUNC_MOVE
) :
162 (c
->functions
& OB_CLIENT_FUNC_RESIZE
)))
165 if (cnr
== prop_atoms
.net_wm_moveresize_size_topleft
)
166 cur
= OB_CURSOR_NORTHWEST
;
167 else if (cnr
== prop_atoms
.net_wm_moveresize_size_top
)
168 cur
= OB_CURSOR_NORTH
;
169 else if (cnr
== prop_atoms
.net_wm_moveresize_size_topright
)
170 cur
= OB_CURSOR_NORTHEAST
;
171 else if (cnr
== prop_atoms
.net_wm_moveresize_size_right
)
172 cur
= OB_CURSOR_EAST
;
173 else if (cnr
== prop_atoms
.net_wm_moveresize_size_bottomright
)
174 cur
= OB_CURSOR_SOUTHEAST
;
175 else if (cnr
== prop_atoms
.net_wm_moveresize_size_bottom
)
176 cur
= OB_CURSOR_SOUTH
;
177 else if (cnr
== prop_atoms
.net_wm_moveresize_size_bottomleft
)
178 cur
= OB_CURSOR_SOUTHWEST
;
179 else if (cnr
== prop_atoms
.net_wm_moveresize_size_left
)
180 cur
= OB_CURSOR_WEST
;
181 else if (cnr
== prop_atoms
.net_wm_moveresize_size_keyboard
)
182 cur
= OB_CURSOR_SOUTHEAST
;
183 else if (cnr
== prop_atoms
.net_wm_moveresize_move
)
184 cur
= OB_CURSOR_MOVE
;
185 else if (cnr
== prop_atoms
.net_wm_moveresize_move_keyboard
)
186 cur
= OB_CURSOR_MOVE
;
188 g_assert_not_reached();
190 /* keep the pointer bounded to the screen for move/resize */
191 if (!grab_pointer(FALSE
, TRUE
, cur
))
193 if (!grab_keyboard()) {
198 frame_end_iconify_animation(c
->frame
);
201 moveresize_client
= c
;
202 start_cx
= c
->area
.x
;
203 start_cy
= c
->area
.y
;
204 /* these adjustments for the size_inc make resizing a terminal more
205 friendly. you essentially start the resize in the middle of the
206 increment instead of at 0, so you have to move half an increment
207 either way instead of a full increment one and 1 px the other. and this
208 is one large mother fucking comment. */
209 start_cw
= c
->area
.width
+ c
->size_inc
.width
/ 2;
210 start_ch
= c
->area
.height
+ c
->size_inc
.height
/ 2;
217 have to change start_cx and start_cy if going to do this..
218 if (corner == prop_atoms.net_wm_moveresize_move_keyboard ||
219 corner == prop_atoms.net_wm_moveresize_size_keyboard)
220 XWarpPointer(ob_display, None, c->window, 0, 0, 0, 0,
221 c->area.width / 2, c->area.height / 2);
232 moveresize_in_progress
= TRUE
;
235 if (config_resize_redraw
&& !moving
&& extensions_shape
&&
236 moveresize_client
->sync_request
&& moveresize_client
->sync_counter
)
238 /* Initialize values for the resize syncing, and create an alarm for
239 the client's xsync counter */
242 XSyncAlarmAttributes aa
;
244 /* set the counter to an initial value */
245 XSyncIntToValue(&val
, 0);
246 XSyncSetCounter(ob_display
, moveresize_client
->sync_counter
, val
);
248 /* this will be incremented when we tell the client what we're
250 moveresize_client
->sync_counter_value
= 0;
252 /* the next sequence we're waiting for with the alarm */
253 XSyncIntToValue(&val
, 1);
255 /* set an alarm on the counter */
256 aa
.trigger
.counter
= moveresize_client
->sync_counter
;
257 aa
.trigger
.wait_value
= val
;
258 aa
.trigger
.value_type
= XSyncAbsolute
;
259 aa
.trigger
.test_type
= XSyncPositiveTransition
;
261 XSyncIntToValue(&aa
.delta
, 1);
262 moveresize_alarm
= XSyncCreateAlarm(ob_display
,
271 waiting_for_sync
= FALSE
;
276 void moveresize_end(gboolean cancel
)
286 client_move(moveresize_client
,
287 (cancel
? start_cx
: cur_x
),
288 (cancel
? start_cy
: cur_y
));
291 /* turn off the alarm */
292 if (moveresize_alarm
!= None
) {
293 XSyncDestroyAlarm(ob_display
, moveresize_alarm
);
294 moveresize_alarm
= None
;
298 get_resize_position(&x
, &y
, cancel
);
299 client_configure(moveresize_client
, x
, y
,
300 (cancel
? start_cw
: cur_x
),
301 (cancel
? start_ch
: cur_y
),
305 moveresize_in_progress
= FALSE
;
306 moveresize_client
= NULL
;
309 static void do_move(gboolean keyboard
)
313 if (keyboard
) resist
= KEY_DIST
- 1; /* resist for one key press */
314 else resist
= config_resist_win
;
315 resist_move_windows(moveresize_client
, resist
, &cur_x
, &cur_y
);
316 if (!keyboard
) resist
= config_resist_edge
;
317 resist_move_monitors(moveresize_client
, resist
, &cur_x
, &cur_y
);
319 client_configure(moveresize_client
, cur_x
, cur_y
,
320 moveresize_client
->area
.width
,
321 moveresize_client
->area
.height
,
323 if (config_resize_popup_show
== 2) /* == "Always" */
324 popup_coords(moveresize_client
, "%d x %d",
325 moveresize_client
->frame
->area
.x
,
326 moveresize_client
->frame
->area
.y
);
329 static void do_resize()
331 gint x
, y
, w
, h
, lw
, lh
;
333 /* see if it is actually going to resize */
338 client_try_configure(moveresize_client
, &x
, &y
, &w
, &h
,
340 if (w
== moveresize_client
->area
.width
&&
341 h
== moveresize_client
->area
.height
)
347 if (config_resize_redraw
&& extensions_sync
&&
348 moveresize_client
->sync_request
&& moveresize_client
->sync_counter
)
353 /* are we already waiting for the sync counter to catch up? */
354 if (waiting_for_sync
)
357 /* increment the value we're waiting for */
358 ++moveresize_client
->sync_counter_value
;
359 XSyncIntToValue(&val
, moveresize_client
->sync_counter_value
);
361 /* tell the client what we're waiting for */
362 ce
.xclient
.type
= ClientMessage
;
363 ce
.xclient
.message_type
= prop_atoms
.wm_protocols
;
364 ce
.xclient
.display
= ob_display
;
365 ce
.xclient
.window
= moveresize_client
->window
;
366 ce
.xclient
.format
= 32;
367 ce
.xclient
.data
.l
[0] = prop_atoms
.net_wm_sync_request
;
368 ce
.xclient
.data
.l
[1] = event_curtime
;
369 ce
.xclient
.data
.l
[2] = XSyncValueLow32(val
);
370 ce
.xclient
.data
.l
[3] = XSyncValueHigh32(val
);
371 ce
.xclient
.data
.l
[4] = 0l;
372 XSendEvent(ob_display
, moveresize_client
->window
, FALSE
,
375 waiting_for_sync
= TRUE
;
379 get_resize_position(&x
, &y
, FALSE
);
380 client_configure(moveresize_client
, x
, y
, cur_x
, cur_y
, TRUE
, FALSE
);
382 /* this would be better with a fixed width font ... XXX can do it better
383 if there are 2 text boxes */
384 if (config_resize_popup_show
== 2 || /* == "Always" */
385 (config_resize_popup_show
== 1 && /* == "Nonpixel" */
386 moveresize_client
->size_inc
.width
> 1 &&
387 moveresize_client
->size_inc
.height
> 1))
388 popup_coords(moveresize_client
, "%d x %d",
389 moveresize_client
->logical_size
.width
,
390 moveresize_client
->logical_size
.height
);
393 static void calc_resize(gboolean keyboard
)
397 /* resist_size_* needs the frame size */
398 cur_x
+= moveresize_client
->frame
->size
.left
+
399 moveresize_client
->frame
->size
.right
;
400 cur_y
+= moveresize_client
->frame
->size
.top
+
401 moveresize_client
->frame
->size
.bottom
;
403 if (keyboard
) resist
= KEY_DIST
- 1; /* resist for one key press */
404 else resist
= config_resist_win
;
405 resist_size_windows(moveresize_client
, resist
, &cur_x
, &cur_y
, lockcorner
);
406 if (!keyboard
) resist
= config_resist_edge
;
407 resist_size_monitors(moveresize_client
, resist
, &cur_x
, &cur_y
,lockcorner
);
409 cur_x
-= moveresize_client
->frame
->size
.left
+
410 moveresize_client
->frame
->size
.right
;
411 cur_y
-= moveresize_client
->frame
->size
.top
+
412 moveresize_client
->frame
->size
.bottom
;
415 gboolean
moveresize_event(XEvent
*e
)
417 gboolean used
= FALSE
;
419 g_assert(moveresize_in_progress
);
421 if (e
->type
== ButtonPress
) {
423 start_x
= e
->xbutton
.x_root
;
424 start_y
= e
->xbutton
.y_root
;
425 button
= e
->xbutton
.button
; /* this will end it now */
427 used
= e
->xbutton
.button
== button
;
428 } else if (e
->type
== ButtonRelease
) {
429 if (!button
|| e
->xbutton
.button
== button
) {
430 moveresize_end(FALSE
);
433 } else if (e
->type
== MotionNotify
) {
435 cur_x
= start_cx
+ e
->xmotion
.x_root
- start_x
;
436 cur_y
= start_cy
+ e
->xmotion
.y_root
- start_y
;
439 if (corner
== prop_atoms
.net_wm_moveresize_size_topleft
) {
440 cur_x
= start_cw
- (e
->xmotion
.x_root
- start_x
);
441 cur_y
= start_ch
- (e
->xmotion
.y_root
- start_y
);
442 lockcorner
= OB_CORNER_BOTTOMRIGHT
;
443 } else if (corner
== prop_atoms
.net_wm_moveresize_size_top
) {
445 cur_y
= start_ch
- (e
->xmotion
.y_root
- start_y
);
446 lockcorner
= OB_CORNER_BOTTOMRIGHT
;
447 } else if (corner
== prop_atoms
.net_wm_moveresize_size_topright
) {
448 cur_x
= start_cw
+ (e
->xmotion
.x_root
- start_x
);
449 cur_y
= start_ch
- (e
->xmotion
.y_root
- start_y
);
450 lockcorner
= OB_CORNER_BOTTOMLEFT
;
451 } else if (corner
== prop_atoms
.net_wm_moveresize_size_right
) {
452 cur_x
= start_cw
+ (e
->xmotion
.x_root
- start_x
);
454 lockcorner
= OB_CORNER_BOTTOMLEFT
;
456 prop_atoms
.net_wm_moveresize_size_bottomright
) {
457 cur_x
= start_cw
+ (e
->xmotion
.x_root
- start_x
);
458 cur_y
= start_ch
+ (e
->xmotion
.y_root
- start_y
);
459 lockcorner
= OB_CORNER_TOPLEFT
;
460 } else if (corner
== prop_atoms
.net_wm_moveresize_size_bottom
) {
462 cur_y
= start_ch
+ (e
->xmotion
.y_root
- start_y
);
463 lockcorner
= OB_CORNER_TOPLEFT
;
465 prop_atoms
.net_wm_moveresize_size_bottomleft
) {
466 cur_x
= start_cw
- (e
->xmotion
.x_root
- start_x
);
467 cur_y
= start_ch
+ (e
->xmotion
.y_root
- start_y
);
468 lockcorner
= OB_CORNER_TOPRIGHT
;
469 } else if (corner
== prop_atoms
.net_wm_moveresize_size_left
) {
470 cur_x
= start_cw
- (e
->xmotion
.x_root
- start_x
);
472 lockcorner
= OB_CORNER_TOPRIGHT
;
473 } else if (corner
== prop_atoms
.net_wm_moveresize_size_keyboard
) {
474 cur_x
= start_cw
+ (e
->xmotion
.x_root
- start_x
);
475 cur_y
= start_ch
+ (e
->xmotion
.y_root
- start_y
);
476 lockcorner
= OB_CORNER_TOPLEFT
;
478 g_assert_not_reached();
484 } else if (e
->type
== KeyPress
) {
485 if (e
->xkey
.keycode
== ob_keycode(OB_KEY_ESCAPE
)) {
486 moveresize_end(TRUE
);
488 } else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_RETURN
)) {
489 moveresize_end(FALSE
);
491 } else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_RIGHT
) ||
492 e
->xkey
.keycode
== ob_keycode(OB_KEY_LEFT
) ||
493 e
->xkey
.keycode
== ob_keycode(OB_KEY_DOWN
) ||
494 e
->xkey
.keycode
== ob_keycode(OB_KEY_UP
))
496 if (corner
== prop_atoms
.net_wm_moveresize_size_keyboard
) {
497 gint dx
= 0, dy
= 0, ox
= cur_x
, oy
= cur_y
;
499 if (e
->xkey
.keycode
== ob_keycode(OB_KEY_RIGHT
))
500 dx
= MAX(KEY_DIST
, moveresize_client
->size_inc
.width
);
501 else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_LEFT
))
502 dx
= -MAX(KEY_DIST
, moveresize_client
->size_inc
.width
);
503 else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_DOWN
))
504 dy
= MAX(KEY_DIST
, moveresize_client
->size_inc
.height
);
505 else /* if (e->xkey.keycode == ob_keycode(OB_KEY_UP)) */
506 dy
= -MAX(KEY_DIST
, moveresize_client
->size_inc
.height
);
510 XWarpPointer(ob_display
, None
, None
, 0, 0, 0, 0, dx
, dy
);
511 /* steal the motion events this causes */
512 XSync(ob_display
, FALSE
);
515 while (XCheckTypedEvent(ob_display
, MotionNotify
, &ce
));
521 /* because the cursor moves even though the window does
522 not nessesarily (resistance), this adjusts where the curor
523 thinks it started so that it keeps up with where the window
525 start_x
+= dx
- (cur_x
- ox
);
526 start_y
+= dy
- (cur_y
- oy
);
529 } else if (corner
== prop_atoms
.net_wm_moveresize_move_keyboard
) {
530 gint dx
= 0, dy
= 0, ox
= cur_x
, oy
= cur_y
;
531 gint opx
, px
, opy
, py
;
533 if (e
->xkey
.keycode
== ob_keycode(OB_KEY_RIGHT
))
535 else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_LEFT
))
537 else if (e
->xkey
.keycode
== ob_keycode(OB_KEY_DOWN
))
539 else /* if (e->xkey.keycode == ob_keycode(OB_KEY_UP)) */
544 screen_pointer_pos(&opx
, &opy
);
545 XWarpPointer(ob_display
, None
, None
, 0, 0, 0, 0, dx
, dy
);
546 /* steal the motion events this causes */
547 XSync(ob_display
, FALSE
);
550 while (XCheckTypedEvent(ob_display
, MotionNotify
, &ce
));
552 screen_pointer_pos(&px
, &py
);
556 /* because the cursor moves even though the window does
557 not nessesarily (resistance), this adjusts where the curor
558 thinks it started so that it keeps up with where the window
560 start_x
+= (px
- opx
) - (cur_x
- ox
);
561 start_y
+= (py
- opy
) - (cur_y
- oy
);
568 else if (e
->type
== extensions_sync_event_basep
+ XSyncAlarmNotify
)
570 waiting_for_sync
= FALSE
; /* we got our sync... */
571 do_resize(); /* ...so try resize if there is more change pending */