summaryrefslogtreecommitdiff
path: root/throw.c
blob: cac8b9af6f8c3b8294099a091dddadfed9d7ee77 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
 *
 * Compiz throw windows plugin
 *
 * throw.c
 *
 * Copyright (c) 2008 Sam Spilsbury <smspillaz@gmail.com>
 *
 * 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.
 *
 **/

#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <compiz-core.h>

#include "throw_options.h"

#define PI 3.1415926

static int displayPrivateIndex;

typedef struct _ThrowDisplay
{
    int screenPrivateIndex;
} ThrowDisplay;

typedef struct _ThrowScreen
{
    WindowGrabNotifyProc   windowGrabNotify;
    WindowUngrabNotifyProc windowUngrabNotify;
    WindowMoveNotifyProc   windowMoveNotify;
    PreparePaintScreenProc preparePaintScreen;
    DonePaintScreenProc	   donePaintScreen;

    int                    windowPrivateIndex;
} ThrowScreen;


typedef struct _ThrowWindow
{
    float		   xVelocity;
    float		   yVelocity;
    int			   time;
    Bool		   moving;
} ThrowWindow;

#define GET_THROW_DISPLAY(d)				    \
    ((ThrowDisplay *) (d)->base.privates[displayPrivateIndex].ptr)

#define THROW_DISPLAY(d)			  \
    ThrowDisplay *td = GET_THROW_DISPLAY (d)

#define GET_THROW_SCREEN(s, td)					\
    ((ThrowScreen *) (s)->base.privates[(td)->screenPrivateIndex].ptr)

#define THROW_SCREEN(s)						       \
    ThrowScreen *ts = GET_THROW_SCREEN (s, GET_THROW_DISPLAY (s->display))

#define GET_THROW_WINDOW(w, ts)                                        \
    ((ThrowWindow *) (w)->base.privates[(ts)->windowPrivateIndex].ptr)

#define THROW_WINDOW(w)                                         \
    ThrowWindow *tw = GET_THROW_WINDOW  (w,                    \
                       GET_THROW_SCREEN  (w->screen,            \
                       GET_THROW_DISPLAY (w->screen->display)))

#define WIN_REAL_X(w) (w->attrib.x - w->input.left)
#define WIN_REAL_Y(w) (w->attrib.y - w->input.top)

#define WIN_REAL_W(w) (w->width + w->input.left + w->input.right)
#define WIN_REAL_H(w) (w->height + w->input.top + w->input.bottom)

/*  Handle the velocity */
static void
throwPreparePaintScreen (CompScreen *s,
			int        ms)
{
    CompWindow *w;
    THROW_SCREEN (s);

    for (w = s->windows; w; w = w->next)
    {
	THROW_WINDOW (w);

	if (tw->moving)
	    tw->time += ms;

	if (!tw->moving && (
	    (tw->xVelocity < 0.0f || tw->xVelocity > 0.0f) ||
	    (tw->yVelocity < 0.0f || tw->yVelocity > 0.0)))
	{

	    tw->xVelocity /= (1.0 + (throwGetFrictionConstant (s) / 100));
	    tw->yVelocity /= (1.0 + (throwGetFrictionConstant (s) / 100));
	    int dx = roundf(tw->xVelocity * (ms / 10) * (throwGetVelocityX (s) / 10));
	    int dy = roundf (tw->yVelocity * (ms / 10) * (throwGetVelocityY (s) / 10));

	    if (throwGetConstrainX (s))
	    {
		if ((WIN_REAL_X (w) + dx) < 0)
		    dx = 0;
		else if ((WIN_REAL_X (w) + WIN_REAL_W (w) + dx) > w->screen->width)
		    dx = 0;
	    }
	    if (throwGetConstrainY (s))
	    {
		if ((WIN_REAL_Y (w) + dy) < 0)
		    dy = 0;
		else if ((WIN_REAL_Y (w) + WIN_REAL_H (w) + dy) > w->screen->height)
		    dy = 0;
	    }

	    moveWindow (w, dx, dy, TRUE, FALSE);
	    syncWindowPosition (w);
	}

    }

    UNWRAP (ts, s, preparePaintScreen);
    (*s->preparePaintScreen) (s, ms);
    WRAP (ts, s, preparePaintScreen, throwPreparePaintScreen);
}

static void
throwDonePaintScreen (CompScreen *s)
{
    THROW_SCREEN (s);
    CompWindow *w;
    for (w = s->windows; w; w = w->next)
    {
	THROW_WINDOW (w);
	if (tw->moving ||
	    (tw->xVelocity < 0.0f || tw->xVelocity > 0.0f) ||
	    (tw->yVelocity < 0.0f || tw->yVelocity > 0.0f))
	{
	    addWindowDamage (w);
	}
    }

    UNWRAP (ts, s, donePaintScreen);
    (*s->donePaintScreen) (s);
    WRAP (ts, s, donePaintScreen, throwDonePaintScreen);
}

static void
throwWindowGrabNotify (CompWindow   *w,
    		      int          x,
		      int          y,
		      unsigned int state,
		      unsigned int mask)
{
    CompScreen * s = w->screen;

    THROW_SCREEN (s);

    if (mask & CompWindowGrabMoveMask)
    {
	THROW_WINDOW (w);

	tw->moving = TRUE;

	tw->time = 0;
	tw->xVelocity = 0.0f;
	tw->yVelocity = 0.0f;
    }
	
    UNWRAP (ts, s, windowGrabNotify);
    (*s->windowGrabNotify) (w, x, y, state, mask);
    WRAP (ts, s, windowGrabNotify, throwWindowGrabNotify);
}

static void
throwWindowUngrabNotify (CompWindow *w)
{
    CompScreen *s = w->screen;

    THROW_SCREEN (s);
    THROW_WINDOW (w);

    tw->moving = FALSE;
	
    UNWRAP (ts, s, windowUngrabNotify);
    (*s->windowUngrabNotify) (w);
    WRAP (ts, s, windowUngrabNotify, throwWindowUngrabNotify);
}

static void
throwWindowMoveNotify (CompWindow *w,
		       int        dx,
		       int        dy,
		       Bool	  immediate)
{
    THROW_WINDOW (w);
    THROW_SCREEN (w->screen);

    if (tw->moving)
    {

	if (tw->time < 1)
	    tw->time = 1;

	tw->xVelocity = dx / tw->time;
	tw->yVelocity = dy / tw->time;
	tw->time = 1;
    }

    UNWRAP (ts, w->screen, windowMoveNotify);
    (*w->screen->windowMoveNotify) (w, dx, dy, immediate);
    WRAP (ts, w->screen, windowMoveNotify, throwWindowMoveNotify);
}

static Bool
throwInitDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    ThrowDisplay *td;

    if (!checkPluginABI ("core", CORE_ABIVERSION))
	return FALSE;

    td = malloc (sizeof (ThrowDisplay));
    if (!td)
	return FALSE;

    td->screenPrivateIndex = allocateScreenPrivateIndex (d);
    if (td->screenPrivateIndex < 0)
    {
	free (td);
	return FALSE;
    }

    d->base.privates[displayPrivateIndex].ptr = td;

    return TRUE;
}

static void
throwFiniDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    THROW_DISPLAY (d);

    freeScreenPrivateIndex (d, td->screenPrivateIndex);
	
    free (td);
}

static Bool
throwInitScreen (CompPlugin *p,
		CompScreen *s)
{
    ThrowScreen * ts;

    THROW_DISPLAY (s->display);

    ts = malloc (sizeof (ThrowScreen));
    if (!ts)
	return FALSE;

    ts->windowPrivateIndex = allocateWindowPrivateIndex (s);
    if (ts->windowPrivateIndex < 0)
    {
	free (ts);
	return FALSE;
    }

    WRAP (ts, s, windowGrabNotify, throwWindowGrabNotify);
    WRAP (ts, s, windowUngrabNotify, throwWindowUngrabNotify);
    WRAP (ts, s, preparePaintScreen, throwPreparePaintScreen);
    WRAP (ts, s, windowMoveNotify, throwWindowMoveNotify);
    WRAP (ts, s, donePaintScreen, throwDonePaintScreen);

    s->base.privates[td->screenPrivateIndex].ptr = ts;

    return TRUE;
}

static Bool
throwInitWindow (CompPlugin *p,
		 CompWindow *w)
{
    ThrowWindow *tw;

    THROW_SCREEN (w->screen);

    tw = calloc (1, sizeof (ThrowWindow));
    if (!tw)
	return FALSE;

    tw->xVelocity = 0.0f;
    tw->yVelocity = 0.0f;
    tw->moving = FALSE;
    tw->time = 0;

    w->base.privates[ts->windowPrivateIndex].ptr = tw;

    return TRUE;
}

static void
throwFiniWindow (CompPlugin *p,
		 CompWindow *w)
{
    THROW_WINDOW (w);
    free (tw);
}

static void
throwFiniScreen (CompPlugin *p,
		CompScreen *s)
{
    THROW_SCREEN (s);

    freeWindowPrivateIndex (s, ts->windowPrivateIndex);

    UNWRAP (ts, s, windowGrabNotify);
    UNWRAP (ts, s, windowUngrabNotify);
    UNWRAP (ts, s, preparePaintScreen);
    UNWRAP (ts, s, windowMoveNotify);
    UNWRAP (ts, s, donePaintScreen);

    free (ts);
}

static CompBool
throwInitObject (CompPlugin *p,
		CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) 0, /* InitCore */
	(InitPluginObjectProc) throwInitDisplay,
	(InitPluginObjectProc) throwInitScreen,
	(InitPluginObjectProc) throwInitWindow,
    };

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

static void
throwFiniObject (CompPlugin *p,
		CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) 0, /* FiniCore */
	(FiniPluginObjectProc) throwFiniDisplay,
	(FiniPluginObjectProc) throwFiniScreen,
	(FiniPluginObjectProc) throwFiniWindow,
    };

    DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), (p, o));
}

static Bool
throwInit (CompPlugin *p)
{
    displayPrivateIndex = allocateDisplayPrivateIndex ();
    if (displayPrivateIndex < 0)
	return FALSE;
	
    return TRUE;
}

static void
throwFini (CompPlugin *p)
{
    freeDisplayPrivateIndex(displayPrivateIndex);
}

static CompPluginVTable throwVTable = {
    "throw",
    0,
    throwInit,
    throwFini,
    throwInitObject,
    throwFiniObject,
    0,
    0
};

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