summaryrefslogtreecommitdiff
path: root/dodge.c
blob: 049845b5ded46dc8f10a5576774e245bf16d5a74 (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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/**
 *
 * dodge.c
 *
 * Copyright (c) 2007 Douglas Young <rcxdude@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 <compiz-core.h>
#include <compiz-mousepoll.h>
#include <math.h>

#include "dodge_options.h"



static int displayPrivateIndex;

typedef struct _DodgeDisplay
{
    int screenPrivateIndex;

    MousePollFunc *mpFunc;	
    Atom resizeNotifyAtom;
} DodgeDisplay;

#define NORTH 0
#define SOUTH 1
#define EAST  2
#define WEST  3

typedef struct _DodgeWindow
{
    float   vx; /* velocity */
    float   vy;

    int     ox; /* original position */
    int	    oy;
    
    int     pos; /* North South East West */

    Bool    selected;
    Bool    isdodge;
} DodgeWindow;

typedef struct _DodgeScreen
{
    PaintOutputProc        paintOutput;
    PreparePaintScreenProc preparePaintScreen;
    PositionPollingHandle  pollHandle;
    
    int windowPrivateIndex;
    CompWindow *window;
    
    int px;
    int py;

    int ox;
    int oy; /* the previous viewport position */
    
    int model;
    int padding;
    float springk;
    float friction;
    Bool active;
    Bool exiting;

} DodgeScreen;


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

#define DODGE_DISPLAY(d) \
    DodgeDisplay *dd = GET_DODGE_DISPLAY (d)

#define GET_DODGE_SCREEN(s, dd) \
    ((DodgeScreen *) (s)->base.privates[(dd)->screenPrivateIndex].ptr)

#define DODGE_SCREEN(s) \
    DodgeScreen *ds = GET_DODGE_SCREEN (s, \
		      GET_DODGE_DISPLAY (s->display))

#define GET_DODGE_WINDOW(w, ds) \
    ((DodgeWindow *) (w)->base.privates[(ds)->windowPrivateIndex].ptr)

#define DODGE_WINDOW(w) \
    DodgeWindow *dw = GET_DODGE_WINDOW  (w, \
		      GET_DODGE_SCREEN  (w->screen, \
		      GET_DODGE_DISPLAY (w->screen->display)))
#define POSITIVE(x) ((x)>0?(x):0)
	
static void
positionUpdate (CompScreen *s, int x, int y)
{
    DODGE_SCREEN (s);
    ds->px = x;
    ds->py = y;
}
	
#define PI 3.1415926
#define FRICTION (0.005f * ds->friction)
#define SPRING_K (0.00005f * ds->springk)
#define PADDING (ds->padding)

#define WIN_X(w) ((w)->attrib.x - (w)->input.left)
#define WIN_Y(w) ((w)->attrib.y - (w)->input.top)
#define WIN_W(w) ((w)->width + (w)->input.left + (w)->input.right)
#define WIN_H(w) ((w)->height + (w)->input.top + (w)->input.bottom)

#define WIN_CX(w) (WIN_X(w) + WIN_W(w)/2)
#define WIN_CY(w) (WIN_Y(w) + WIN_H(w)/2) 

static int
dodgestep (CompWindow *w, int ms, int away)
{
    DODGE_WINDOW (w);
    DODGE_SCREEN (w->screen);
    int dx; /*  x distance from pointer */
    int dy; /*  y distance from pointer */
    float dp; /*  actual distance from pointer */
    switch (ds->model)
    {
	case ModelSimpleAvoid:
	    /* increase velocity away from the pointer */
	    dx = WIN_CX(w) - ds->px;
	    dy = WIN_CY(w) - ds->py; 
	    
	    dp = sqrt(dx*dx + dy*dy);
	    if(WIN_W(w)/2 + PADDING - abs(dx) > 
	       WIN_H(w)/2 + PADDING - abs(dy))
	    {
		dw->vx += POSITIVE(WIN_H(w)/2 + PADDING - abs(dy)) 
		    * SPRING_K * dx/(abs(dx) + abs(dy)) * ms;
		dw->vy += POSITIVE(WIN_H(w)/2 + PADDING - abs(dy)) 
		    * SPRING_K * dy/(abs(dx) + abs(dy)) * ms;
	    } else {
		dw->vx += POSITIVE(WIN_W(w)/2 + PADDING - abs(dx)) 
		    * SPRING_K * dx/(abs(dx) + abs(dy)) * ms;
		dw->vy += POSITIVE(WIN_W(w)/2 + PADDING - abs(dx)) 
		    * SPRING_K * dy/(abs(dx) + abs(dy)) * ms;
	    }
	    break;
	case ModelReturnToPosition:
	    if ((ds->px > dw->ox - WIN_W(w)/2 - PADDING &&
		ds->py > dw->oy - WIN_H(w)/2 - PADDING &&
		ds->px < dw->ox + WIN_W(w)/2 + PADDING &&
		ds->py < dw->oy + WIN_H(w)/2 + PADDING) &&
		!ds->exiting && away)
	    {
		/* increase velocity away from / toward the pointer */
		dx = WIN_CX(w) - ds->px;
		dy = WIN_CY(w) - ds->py; 
		
		dp = sqrt(dx*dx + dy*dy);
		if(WIN_W(w)/2 + PADDING - abs(dx) > 
		   WIN_H(w)/2 + PADDING - abs(dy))
		{
		    dw->vx += (WIN_H(w)/2 + PADDING - abs(dy)) 
			      * SPRING_K * dx/(abs(dx) + abs(dy)) * ms;
		    dw->vy += (WIN_H(w)/2 + PADDING - abs(dy)) 
			      * SPRING_K * dy/(abs(dx) + abs(dy)) * ms;
		} else {
		    dw->vx += (WIN_W(w)/2 + PADDING - abs(dx)) 
			      * SPRING_K * dx/(abs(dx) + abs(dy)) * ms;
		    dw->vy += (WIN_W(w)/2 + PADDING - abs(dx)) 
			      * SPRING_K * dy/(abs(dx) + abs(dy)) * ms;
		}
	    } else {
		/* increase velocity towards the original position */
		dx = WIN_CX(w) - dw->ox;
		dy = WIN_CY(w) - dw->oy; 
		
		dp = sqrt(dx*dx + dy*dy);
		if (dx || dy) 
		{
		    dw->vx -= dp * SPRING_K * dx/(abs(dx) + abs(dy)) * ms;
		    dw->vy -= dp * SPRING_K * dy/(abs(dx) + abs(dy)) * ms;
		}
	    }
	    break;
	case ModelOffScreen:
	    if ((ds->px > dw->ox - WIN_W(w)/2 - PADDING &&
		ds->py > dw->oy - WIN_H(w)/2 - PADDING &&
		ds->px < dw->ox + WIN_W(w)/2 + PADDING &&
		ds->py < dw->oy + WIN_H(w)/2 + PADDING) &&
		!ds->exiting && away)
	    {
		switch (dw->pos)
		{
		    case NORTH:
			dx = 0;
			dy = WIN_Y(w) + WIN_H(w);
			break;
		    case SOUTH:
			dx = 0;
			dy = WIN_Y(w) - w->screen->height;
			break;
		    case EAST:
			dx = WIN_X(w) + WIN_W(w);
			dy = 0;
			break;
		    case WEST:
			dx = WIN_X(w) - w->screen->width;
			dy = 0;
			break;
		}
		
		dw->vx -= dx * SPRING_K * ms;
		dw->vy -= dy * SPRING_K * ms;
	    } else {
		dx = WIN_CX(w) - dw->ox;
		dy = WIN_CY(w) - dw->oy; 
		
		dp = sqrt(dx*dx + dy*dy);
		if (dx || dy) 
		{
		    dw->vx -= dp * SPRING_K * dx/(abs(dx) + abs(dy)) * ms * 0.5;
		    dw->vy -= dp * SPRING_K * dy/(abs(dx) + abs(dy)) * ms * 0.5;
		}
	    }
	    break;

    }
    /* apply friction */
    dw->vx -= FRICTION * dw->vx * ms;
    dw->vy -= FRICTION * dw->vy * ms;
    
    /* stop the window going offscreen with the simple model */
    if (((WIN_X(w) + WIN_W(w) > w->screen->width  && dw->vx > 0) || 
	(WIN_X(w) < 0                             && dw->vx < 0)) &&
	ds->model == ModelSimpleAvoid)
	dw->vx = 0;
    if (((WIN_Y(w) + WIN_H(w) > w->screen->height && dw->vy > 0) ||
	(WIN_Y(w) < 0                             && dw->vy < 0)) &&
	ds->model == ModelSimpleAvoid)
	dw->vy = 0;
    /* stop excessive velocity  */
    if (dw->vx > w->screen->width  || dw->vx < 0 - w->screen->width ||
	dw->vy > w->screen->height || dw->vx < 0 - w->screen->height)
	    dw->vx = dw->vy = 0;
	
    /* if there's enough velocity, move the window */
    if (fabs(dw->vx)*ms >= 1 || fabs(dw->vy)*ms >= 1)
    {
	moveWindow (w,dw->vx*ms,dw->vy*ms,TRUE,TRUE);
	syncWindowPosition (w);
	return 1;
    }
    /*if(abs(WIN_CX(w) - dw->ox) < 1 && 
       abs(WIN_CY(w) - dw->oy) < 1)
    {
	moveWindowToViewportPosition(w,(dw->ox - WIN_W(w)/2),
				       (dw->oy - WIN_H(w)/2),1);
	printf("returning 0\n");
	return 0;
    }*/
    return 0;

}

static void
dodgeMarkDodgeWindows (CompScreen *s)
{
    DODGE_SCREEN (s);
    CompWindow *w;
    for (w = s->windows; w; w = w->next)
    {
	DODGE_WINDOW (w);
	if((matchEval(dodgeGetWindowTypes (s->display),w) || dw->selected) &&
	   !(w->invisible || w->destroyed || w->hidden || w->minimized))
	{
	    if (dw)
	    {
		dw->isdodge = TRUE;
		dw->vx = 0;
		dw->vy = 0;
		dw->ox = WIN_CX(w);
		dw->oy = WIN_CY(w);
		if (ds->model == ModelOffScreen)
		{
		    if (WIN_CX(w) > WIN_CY(w) &&
			WIN_CX(w) * w->screen->height + 
			WIN_CY(w) * w->screen->width  >
			(w->screen->width * w->screen->height))
			    dw->pos = WEST;
		    if (WIN_CX(w) < WIN_CY(w) &&
			WIN_CX(w) * w->screen->height + 
			WIN_CY(w) * w->screen->width  >
			(w->screen->width * w->screen->height))
			    dw->pos = SOUTH;
		    if (WIN_CX(w) > WIN_CY(w) &&
			WIN_CX(w) * w->screen->height + 
			WIN_CY(w) * w->screen->width  <
			(w->screen->width * w->screen->height))
			    dw->pos = NORTH;
		    if (WIN_CX(w) < WIN_CY(w) &&
			WIN_CX(w) * w->screen->height + 
			WIN_CY(w) * w->screen->width  <
			(w->screen->width * w->screen->height))
			    dw->pos = EAST;
		}
	    }
	}
	else
	{
	    if (dw->isdodge)
	    {
		dw->isdodge = FALSE;
		dw->ox = 0;
		dw->oy = 0;
	    }
	}
    }
}
static void
dodgePreparePaintScreen (CompScreen *s,
			int	ms)
{
    DODGE_SCREEN (s);
    CompWindow *w;
    int count = 0; /* number of windows moved */
    /*  if another plugin has grabbed the screen we don't want to interfere
     *  except for expo or rotate cube, in which case windows return to their original
     *  positions */
    if (otherScreenGrabExist(s,"dodge","expo","rotate",0))
    {
	UNWRAP (ds, s, preparePaintScreen);
	(*s->preparePaintScreen) (s, ms);
	WRAP (ds, s, preparePaintScreen, dodgePreparePaintScreen);
	return;
    }

    /* update original positions to prevent windows springing to new viewport */
    if ((ds->ox != s->x || ds->oy != s->y) &&
	ds->active)
    {
	for (w = s->windows; w; w = w->next)
	{
	    DODGE_WINDOW (w);
	    if (dw->isdodge && !(w->state & CompWindowStateStickyMask))
	    {
		dw->ox -= (s->x - ds->ox) * s->width;
		dw->oy -= (s->y - ds->oy) * s->height;
	    }
	}
	ds->ox = s->x;
	ds->oy = s->y;
    }
	
    if (ds->active)
    {
	for (w = s->windows; w; w = w->next)
	{
	    DODGE_WINDOW (w);
	    if (dw->isdodge)
		count += dodgestep(w,ms,!otherScreenGrabExist(s,"dodge",0));
	}
    }
    if (ds->exiting && !count)
    {
	ds->active = FALSE;
	ds->exiting = FALSE;
    }

    UNWRAP (ds, s, preparePaintScreen);
    (*s->preparePaintScreen) (s, ms);
    WRAP (ds, s, preparePaintScreen, dodgePreparePaintScreen);
}

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

    xid = getIntOptionNamed (option, nOption, "window", 0);
    w   = findWindowAtDisplay (d, xid);
    DODGE_WINDOW(w);
    dw->selected = !dw->selected;
    return TRUE;
}
static Bool
dodgeToggle (CompDisplay    *d,
	    CompAction     *action,
	    CompActionState state,
	    CompOption     *option,
	    int           nOption)
{
    CompScreen *s;
    Window    xid;
    xid = getIntOptionNamed (option, nOption, "root", 0);
    s = findScreenAtDisplay (d, xid);
    if (s)
    {
	DODGE_SCREEN(s);
	if (ds->active)
	    ds->exiting = TRUE;
	else
	{
	    ds->model    = dodgeGetModel    (d);
	    ds->springk  = dodgeGetSpringK  (d);
	    ds->friction = dodgeGetFriction (d);
	    ds->padding  = dodgeGetPadding  (d);
	    ds->ox       = s->x;
	    ds->oy       = s->y;
	    dodgeMarkDodgeWindows (s);
	    ds->active = TRUE;
	}
	return TRUE;
    }
    return FALSE;
}


static Bool
dodgePaintOutput (CompScreen            *s,
		 const ScreenPaintAttrib *sAttrib,
		 const CompTransform    *transform,
		 Region               region,
		 CompOutput            *output,
		 unsigned int          mask)
{
    Bool status;

    DODGE_SCREEN (s);
    if (ds->active)
    {
	/* damage a small area around the mouse to prevent jerkiness */
	REGION r;
	
	r.rects = &r.extents;
	r.numRects = r.size = 1;
	r.extents.x1 = ds->px - PADDING;
	r.extents.x2 = ds->px + PADDING;
	r.extents.y1 = ds->py - PADDING;
	r.extents.y2 = ds->py + PADDING;

	damageScreenRegion(s,&r);
    }


    UNWRAP (ds, s, paintOutput);
    status = (*s->paintOutput) (s, sAttrib, transform, region, output, mask);
    WRAP (ds, s, paintOutput, dodgePaintOutput);

    return status;
}


static Bool
dodgeInitDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    DodgeDisplay *dd;
    int mousepollindex;
    if (!checkPluginABI ("core", CORE_ABIVERSION))
	return FALSE;

    dd = malloc (sizeof (DodgeDisplay));
    if (!dd)
	return FALSE;

    if (!getPluginDisplayIndex (d, "mousepoll", &mousepollindex))
	return FALSE;

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

    d->base.privates[displayPrivateIndex].ptr = dd;
    dodgeSetDodgeToggleKeyInitiate    (d, dodgeToggle);
    dodgeSetDodgeToggleButtonInitiate (d, dodgeToggle);
    dodgeSetDodgeToggleEdgeInitiate   (d, dodgeToggle);
    dodgeSetDodgeSelectKeyInitiate    (d, dodgeMark);

    dd->mpFunc = d->base.privates[mousepollindex].ptr;
    return TRUE;
}

static void
dodgeFiniDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    DODGE_DISPLAY (d);
    freeScreenPrivateIndex (d, dd->screenPrivateIndex);
    free (dd);
}

static Bool
dodgeInitScreen (CompPlugin *p,
		CompScreen *s)
{
    DodgeScreen * ds;

    DODGE_DISPLAY (s->display);

    ds = malloc (sizeof (DodgeScreen));
    if (!ds)
	return FALSE;

    ds->active = FALSE;
    ds->exiting = FALSE;

    WRAP (ds, s, preparePaintScreen, dodgePreparePaintScreen);
    WRAP (ds, s, paintOutput, dodgePaintOutput);
    ds->windowPrivateIndex = allocateWindowPrivateIndex (s);
    s->base.privates[dd->screenPrivateIndex].ptr = ds;
    
    (*dd->mpFunc->getCurrentPosition) (s, &ds->px, &ds->py);
    ds->pollHandle = (*dd->mpFunc->addPositionPolling) (s, positionUpdate);
    return TRUE;
}

static void
dodgeFiniScreen (CompPlugin *p,
		CompScreen *s)
{
    DODGE_SCREEN (s);
    DODGE_DISPLAY (s->display);	
    UNWRAP (ds, s, preparePaintScreen);
    UNWRAP (ds, s, paintOutput);
    (*dd->mpFunc->removePositionPolling) (s, ds->pollHandle);
    free (ds);
}

static Bool
dodgeInitWindow (CompPlugin *p,
		 CompWindow *w)
{
    DodgeWindow *dw;

    DODGE_SCREEN (w->screen);

    dw = malloc (sizeof (DodgeWindow));
    if (!dw)
	return FALSE;
    dw->vx = 0;
    dw->vy = 0;
    dw->isdodge = FALSE;
    dw->selected = FALSE;
    w->base.privates[ds->windowPrivateIndex].ptr = dw;
    
    return TRUE;
}

static void
dodgeFiniWindow (CompPlugin *p,
		 CompWindow *w)
{
    DODGE_WINDOW (w);
    free (dw);
}

static CompBool
dodgeInitObject (CompPlugin *p,
		CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) 0, /* InitCore */
	(InitPluginObjectProc) dodgeInitDisplay,
	(InitPluginObjectProc) dodgeInitScreen,
	(InitPluginObjectProc) dodgeInitWindow
    };

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

static void
dodgeFiniObject (CompPlugin *p,
		CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) 0, /* FiniCore */
	(FiniPluginObjectProc) dodgeFiniDisplay,
	(FiniPluginObjectProc) dodgeFiniScreen,
	(FiniPluginObjectProc) dodgeFiniWindow
    };

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

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

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

static CompPluginVTable dodgeVTable = {
    "dodge",
    0,
    dodgeInit,
    dodgeFini,
    dodgeInitObject,
    dodgeFiniObject,
    0,
    0
};

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