~ctwm/ctwm/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Functions related to captive-mode ctwm.
 */

#include "ctwm.h"

#include <stdlib.h>

#include "captive.h"
#include "cursor.h"
#include "events.h"
#include "functions_internal.h"
#include "screen.h"


/*
 * Pulling a window into the ctwm it was invoked from.
 */
DFHANDLER(adoptwindow)
{
	AdoptWindow();
}


/*
 * Interactively moving a window between ctwm's.
 */
DFHANDLER(hypermove)
{
	bool    cont = true;
	Window  root = RootWindow(dpy, Scr->screen);
	Cursor  cursor;
	Window captive_root;

	if(tmp_win->iswinbox || tmp_win->iswspmgr) {
		XBell(dpy, 0);
		return;
	}

	{
		CaptiveCTWM cctwm = GetCaptiveCTWMUnderPointer();
		cursor = MakeStringCursor(cctwm.name);
		free(cctwm.name);
		captive_root = cctwm.root;
	}

	XGrabPointer(dpy, root, True,
	             ButtonPressMask | ButtonMotionMask | ButtonReleaseMask,
	             GrabModeAsync, GrabModeAsync, root, cursor, CurrentTime);
	while(cont) {
		XMaskEvent(dpy, ButtonPressMask | ButtonMotionMask |
		           ButtonReleaseMask, &Event);
		switch(Event.xany.type) {
			case ButtonPress:
				cont = false;
				break;

			case ButtonRelease: {
				CaptiveCTWM cctwm = GetCaptiveCTWMUnderPointer();
				cont = false;
				free(cctwm.name);
				if(cctwm.root == Scr->Root) {
					break;
				}
				if(cctwm.root == Scr->XineramaRoot) {
					break;
				}
				SetNoRedirect(tmp_win->w);
				XUngrabButton(dpy, AnyButton, AnyModifier, tmp_win->w);
				XReparentWindow(dpy, tmp_win->w, cctwm.root, 0, 0);
				XMapWindow(dpy, tmp_win->w);
				break;
			}

			case MotionNotify: {
				CaptiveCTWM cctwm = GetCaptiveCTWMUnderPointer();
				if(cctwm.root != captive_root) {
					unsigned int chmask;

					XFreeCursor(dpy, cursor);
					cursor = MakeStringCursor(cctwm.name);
					captive_root = cctwm.root;

					chmask = (ButtonPressMask | ButtonMotionMask
					          | ButtonReleaseMask);
					XChangeActivePointerGrab(dpy, chmask,
					                         cursor, CurrentTime);
				}
				free(cctwm.name);
				break;
			}
		}
	}

	ButtonPressed = -1;
	XUngrabPointer(dpy, CurrentTime);
	XFreeCursor(dpy, cursor);

	return;
}