summaryrefslogtreecommitdiff
path: root/mswitch.c
blob: 6fa4e7176e21404bfddb662c87b8fc3ae72596f0 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *
 * Compiz mouse gesture viewport switch plugin
 * mswitch.c
 * Copyright (c) 2007 Robert Carr <racarr@opencompositing.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <compiz-core.h>
#include <math.h>
#include <X11/Xlib.h>
#include "mswitch_options.h"

static int x,y;
static CompScreen *s = NULL;


static void mswitchMove(CompScreen *s, int dx, int dy)
{
	XEvent xev;
	xev.xclient.type = ClientMessage;
	xev.xclient.display = s->display->display;
	xev.xclient.format = 32;
	xev.xclient.message_type = s->display->desktopViewportAtom;
	xev.xclient.window = s->root;
	xev.xclient.data.l[0] = (s->x+dx)*s->width;
	xev.xclient.data.l[1] = (s->y+dy)*s->height;
	xev.xclient.data.l[2] = 0;
	xev.xclient.data.l[3] = 0;
	xev.xclient.data.l[4] = 0;
	
	XSendEvent(s->display->display, s->root, FALSE, 
		   SubstructureRedirectMask | SubstructureNotifyMask, &xev);
}


static Bool mswitchBegin(CompDisplay *d, CompAction * action, 
			 CompActionState state, CompOption *option,
			 int nOption)
{
	Window xid;

	xid = getIntOptionNamed(option, nOption, "root", 0);
	if (xid)
	    s = findScreenAtDisplay(d, xid);

	if (state & CompActionStateInitButton)
		action->state |= CompActionStateTermButton;
	if (state & CompActionStateInitKey)
		action->state |= CompActionStateTermKey;
	
	x = pointerX;
	y = pointerY;
	
	return TRUE;
}

static Bool mswitchTerminate(CompDisplay *d, CompAction * action,
		        CompActionState state, CompOption * option,
			int nOption)
{
	Window xid;

	int mx = MAX(pointerX,x)-MIN(pointerX,x);
	int my = MAX(pointerY,y)-MIN(pointerY,y);
	
	int dx = 0, dy = 0;

	if (mx > my)
	{
		dx = 1;
		if (my/(float)mx > .75 && my/(float)mx < 1.25)
			dy=1;
	}
	else
	{
		dy = 1;
		if (my/(float)mx > .75 && my/(float)mx < 1.25)
			dx=1;
	}
	

	if (pointerX < x)
		dx *= -1;
	if (pointerY < y)
		dy *= -1;
	
	xid = getIntOptionNamed(option, nOption, "root", 0);
	if (xid && s == findScreenAtDisplay(d, xid))
	    mswitchMove(s, dx, dy);
	
	s = NULL;
	
	action->state &= ~(CompActionStateTermKey | CompActionStateTermButton);
	
	return FALSE;
}

static Bool mswitchInitDisplay(CompPlugin *p,
			       CompDisplay *d)
{
	mswitchSetBeginKeyInitiate(d, mswitchBegin);
	mswitchSetBeginKeyTerminate(d, mswitchTerminate);
	mswitchSetBeginButtonInitiate(d, mswitchBegin);
	mswitchSetBeginButtonTerminate(d, mswitchTerminate);
	return TRUE;
}

static CompBool
mswitchInitObject (CompPlugin *p,
		       CompObject *o)
{
	static InitPluginObjectProc dispTab[] = {
		(InitPluginObjectProc) 0, /* InitCore */
		(InitPluginObjectProc) mswitchInitDisplay,
    };

    RETURN_DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), TRUE, (p, o));
}

CompPluginVTable mswitchVTable = {
	"mswitch",
	0,
	0,
	0,
	mswitchInitObject,
	0,
	0,
	0
};

CompPluginVTable * getCompPluginInfo(void)
{
	return &mswitchVTable;
}