summaryrefslogtreecommitdiff
path: root/mousegestures.c
blob: 400b8f576127d8440cb847eca749887948f7df0b (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
/*
 * Copyright © 2006 Mike Dransfield
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Author: Mike Dransfield <mike@blueroot.co.uk>
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <compiz-core.h>

#define MOUSEGESTURES_DISPLAY_OPTION_INITIATE_BUTTON 0
#define MOUSEGESTURES_DISPLAY_OPTION_DETECT_DIAGONAL 1
#define MOUSEGESTURES_DISPLAY_OPTION_GESTURE         2
#define MOUSEGESTURES_DISPLAY_OPTION_PLUGIN          3
#define MOUSEGESTURES_DISPLAY_OPTION_ACTION          4
#define MOUSEGESTURES_DISPLAY_OPTION_NUM             5

#define MOUSEGESTURE_MIN_DELTA                   80
#define MOUSEGESTURE_MAX_VARIANCE                (MOUSEGESTURE_MIN_DELTA / 2)
#define MOUSEGESTURES_MAX_STROKE		 32
#define MOUSEGESTURES_MAX_GESTURES		 128

#define M_UP    'u'
#define M_RIGHT 'r'
#define M_DOWN  'd'
#define M_LEFT  'l'
#define M_9     '9'
#define M_3     '3'
#define M_1     '1'
#define M_7     '7'

#define GET_MOUSEGESTURES_DISPLAY(d) \
    ((MousegesturesDisplay *) (d)->base.privates[displayPrivateIndex].ptr)
#define MOUSEGESTURES_DISPLAY(d) \
    MousegesturesDisplay *md = GET_MOUSEGESTURES_DISPLAY (d)
#define GET_MOUSEGESTURES_SCREEN(s, md) \
    ((MousegesturesScreen *) (s)->base.privates[(md)->screenPrivateIndex].ptr)
#define MOUSEGESTURES_SCREEN(s) \
    MousegesturesScreen *ms = GET_MOUSEGESTURES_SCREEN (s, \
			      GET_MOUSEGESTURES_DISPLAY (s->display))

#define NUM_OPTIONS(s) (sizeof ((s)->opt) / sizeof (CompOption))

static int displayPrivateIndex;

static CompMetadata mousegesturesMetadata;

typedef struct _Mousegesture {
    char stroke[MOUSEGESTURES_MAX_STROKE];
    int  nStrokes;
    char *pluginName;
    char *actionName;
} Mousegesture;


typedef struct _MousegesturesDisplay {
    int		    screenPrivateIndex;
    CompOption      opt[MOUSEGESTURES_DISPLAY_OPTION_NUM];
    HandleEventProc handleEvent;

    int grabIndex;
    int originalX;
    int originalY;
    int deltaX;
    int deltaY;

    char	 currentStroke;
    char	 lastStroke;
    char	 currentGesture[MOUSEGESTURES_MAX_STROKE];
    int		 nCurrentGesture;
    Mousegesture gesture[MOUSEGESTURES_MAX_GESTURES];
    int 	 nGesture;
} MousegesturesDisplay;

static void
mousegesturesDisplayActionInitiate (CompDisplay *d,
				    char        *pluginName,
				    char        *actionName)
{
    CompPlugin *p;
    CompOption *option = NULL;

    if (!strlen (pluginName) || !strlen (actionName))
	return;

    p = findActivePlugin (pluginName);
    if (p && p->vTable->getObjectOptions)
    {
	CompObject *object;
	object = compObjectFind (&core.base, COMP_OBJECT_TYPE_DISPLAY, NULL);
	if (object)
	{
	    CompOption *options;
	    int        nOptions;

	    options = (*p->vTable->getObjectOptions) (p, object, &nOptions);
	    option = compFindOption (options, nOptions, actionName, 0);
	}
    }
    else if (strcmp (pluginName, "exec") == 0)
    {
	return;
    }
    else
    {
	compLogMessage (d, "mousegestures", CompLogLevelWarn,
			"Could not find plugin called '%s', check that you " \
			"have it loaded and that you spelt it correctly in " \
			"the option.", pluginName);
	return;
    }

    if (option && option->value.action.initiate)
    {
        CompOption args[4];

	args[0].name    = "root";
	args[0].type    = CompOptionTypeInt;
	args[0].value.i = currentRoot;

	args[1].name    = "window";
	args[1].type    = CompOptionTypeInt;
	args[1].value.i = d->activeWindow;

	args[2].name    = "x";
	args[2].type    = CompOptionTypeInt;
	args[2].value.i = pointerX;

	args[3].name    = "y";
	args[3].type    = CompOptionTypeInt;
	args[3].value.i = pointerY;

	(*option->value.action.initiate)  (d, &option->value.action,
					   0, args, 4);
    }
    else
    {
	compLogMessage (d, "mousegestures", CompLogLevelWarn,
			"I found the plugin called '%s' but I could not" \
			" find the action called '%s', check that you spelt " \
			"it correctly in the option.", pluginName, actionName);
    }
}

static Bool
mousegesturesInitiate (CompDisplay     *d,
		       CompAction      *action,
		       CompActionState state,
		       CompOption      *option,
		       int             nOption)
{
    CompScreen *s;
    int        xid;

    MOUSEGESTURES_DISPLAY (d);

    xid = getIntOptionNamed (option, nOption, "root", 0);

    s = findScreenAtDisplay (d, xid);
    if (s)
    {
	md->originalX = pointerX;
	md->originalY = pointerY;
	md->deltaX    = 0;
	md->deltaY    = 0;

	md->currentStroke   = '\0';
	md->lastStroke      = '\0';
	memset (&md->currentGesture, 0, sizeof (md->currentGesture));
	md->nCurrentGesture = 0;

	md->grabIndex = pushScreenGrab (s, s->invisibleCursor, "mousegestures");

	if (state & CompActionStateInitButton)
	    action->state |= CompActionStateTermButton;

	return TRUE;
    }

    return FALSE;
}

static Bool
mousegesturesTerminate (CompDisplay     *d,
			CompAction      *action,
			CompActionState state,
			CompOption      *option,
			int             nOption)
{
    CompScreen   *s;
    int          ng, xid;
    Mousegesture *mg;

    MOUSEGESTURES_DISPLAY (d);

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

    if (!s)
	return FALSE;

    ng = md->nGesture;
    while (ng--)
    {
        mg = &md->gesture[ng];

	if (mg->nStrokes != md->nCurrentGesture)
	    continue;

	if (strncmp (md->currentGesture, mg->stroke, mg->nStrokes) != 0)
	    continue;

	if (!mg->pluginName || !mg->actionName)
	    continue;

	mousegesturesDisplayActionInitiate (d, mg->pluginName, mg->actionName);
    }

    removeScreenGrab (s, md->grabIndex, NULL);
    md->grabIndex = 0;

    action->state &= ~CompActionStateTermButton;

    return TRUE;
}

static void
mousegesturesHandleEvent (CompDisplay *d,
			  XEvent      *event)
{
    MOUSEGESTURES_DISPLAY (d);

    switch (event->type) {
    case MotionNotify:
	if (md->grabIndex)
	{
	    Bool detectDiagonal;
	    char currentStroke = '\0';
	    int  deltaX = 0, deltaY = 0, opt;

	    md->deltaX += (pointerX - lastPointerX);
	    md->deltaY += (pointerY - lastPointerY);

	    opt = MOUSEGESTURES_DISPLAY_OPTION_DETECT_DIAGONAL;
	    detectDiagonal = md->opt[opt].value.b;

	    if (abs (md->deltaX) > MOUSEGESTURE_MIN_DELTA)
	    {
		deltaX = md->deltaX;
		if (detectDiagonal &&
		    abs (md->deltaY) > MOUSEGESTURE_MAX_VARIANCE)
		{
		    deltaY = md->deltaY;
		}
	    }
	    else if (abs (md->deltaY) > MOUSEGESTURE_MIN_DELTA)
	    {
		deltaY = md->deltaY;
		if (detectDiagonal &&
		    abs (md->deltaX) > MOUSEGESTURE_MAX_VARIANCE)
		{
		    deltaX = md->deltaX;
		}
	    }

	    /* handle horizontal and vertical cases first */
	    if (!deltaY && deltaX < 0)
		currentStroke = M_LEFT;
	    else if (!deltaY && deltaX > 0)
		currentStroke = M_RIGHT;
	    else if (!deltaX && deltaY < 0)
		currentStroke = M_UP;
	    else if (!deltaX && deltaY > 0)
		currentStroke = M_DOWN;
	    else if (deltaX && deltaY)
	    {
		/* deltaX and deltaY != 0, thus diagonal */
		if (deltaX < 0)
		{
		    if (deltaY < 0)
			currentStroke = M_7;
		    else
			currentStroke = M_1;
		}
		else
		{
		    if (deltaY < 0)
			currentStroke = M_9;
		    else
			currentStroke = M_3;
		}
	    }

	    if (currentStroke && md->currentStroke != currentStroke)
	    {
		md->currentStroke = currentStroke;
		if (md->currentStroke != md->lastStroke)
		{
		    md->lastStroke = md->currentStroke;
		    if (md->nCurrentGesture < MOUSEGESTURES_MAX_STROKE)
		    {
			md->currentGesture[md->nCurrentGesture] =
			    md->currentStroke;
			md->nCurrentGesture++;
		    }
		}
		md->deltaX = 0;
		md->deltaY = 0;
	    }
	    else if (md->currentStroke && md->currentStroke == currentStroke)
	    {
		md->deltaX = 0;
		md->deltaY = 0;
	    }
	}
	break;
    }

    UNWRAP (md, d, handleEvent);
    (*d->handleEvent) (d, event);
    WRAP (md, d, handleEvent, mousegesturesHandleEvent);
}

static void
loadMouseGestures (CompDisplay *d)
{
    int             nGestures, nPlugins, nActions;
    CompOptionValue *gestureVal, *pluginVal, *actionVal;
    Mousegesture    *gesture;
    int             gNum = 0;

    MOUSEGESTURES_DISPLAY (d);

    nGestures = md->opt[MOUSEGESTURES_DISPLAY_OPTION_GESTURE].value.list.nValue;
    gestureVal = md->opt[MOUSEGESTURES_DISPLAY_OPTION_GESTURE].value.list.value;
    nPlugins = md->opt[MOUSEGESTURES_DISPLAY_OPTION_PLUGIN].value.list.nValue;
    pluginVal = md->opt[MOUSEGESTURES_DISPLAY_OPTION_PLUGIN].value.list.value;
    nActions = md->opt[MOUSEGESTURES_DISPLAY_OPTION_ACTION].value.list.nValue;
    actionVal = md->opt[MOUSEGESTURES_DISPLAY_OPTION_ACTION].value.list.value;

    if ((nGestures != nPlugins) || (nGestures != nActions))
	return;

    while (nGestures-- && (gNum < MOUSEGESTURES_MAX_GESTURES))
    {
	gesture = &md->gesture[gNum];
	strncpy (gesture->stroke, gestureVal->s, MOUSEGESTURES_MAX_STROKE - 1);
	gesture->nStrokes = strlen (gestureVal->s);

	if (gesture->pluginName)
	    free (gesture->pluginName);
	gesture->pluginName = strdup (pluginVal->s);

	if (gesture->actionName)
	    free (gesture->actionName);
	gesture->actionName = strdup (actionVal->s);

	gNum++;

	gestureVal++;
	pluginVal++;
	actionVal++;
    }

    md->nGesture = gNum;
}

static CompOption *
mousegesturesGetDisplayOptions (CompPlugin  *p,
				CompDisplay *d,
				int         *count)
{
    MOUSEGESTURES_DISPLAY (d);

    *count = NUM_OPTIONS (md);
    return md->opt;
}

static Bool
mousegesturesSetDisplayOption (CompPlugin      *p,
			       CompDisplay     *d,
			       const char      *name,
			       CompOptionValue *value)
{
    CompOption *o;
    int         index;

    MOUSEGESTURES_DISPLAY (d);

    o = compFindOption (md->opt, NUM_OPTIONS (md), name, &index);
    if (!o)
        return FALSE;

    switch (index) 
    {
        case MOUSEGESTURES_DISPLAY_OPTION_INITIATE_BUTTON:
            if (setDisplayAction (d, o, value))
	        return TRUE;
            break;
        default:
            if (compSetOption (o, value))
	    {
		loadMouseGestures (d);
	        return TRUE;
	    }
            break;
    }

    return FALSE;
}

static const CompMetadataOptionInfo mousegesturesDisplayOptionInfo[] = {
    { "initiate_button", "button", 0,
      mousegesturesInitiate, mousegesturesTerminate },
    { "detect_diagonal", "bool", 0, 0, 0 },
    { "gestures", "list", "<type>string</type>", 0, 0 },
    { "plugins", "list", "<type>string</type>", 0, 0 },
    { "actions", "list", "<type>string</type>", 0, 0 }
};

static Bool
mousegesturesInitDisplay (CompPlugin  *p,
			  CompDisplay *d)
{
    MousegesturesDisplay *md;

    md = malloc (sizeof (MousegesturesDisplay));
    if (!md)
        return FALSE;

    if (!compInitDisplayOptionsFromMetadata (d,
					     &mousegesturesMetadata,
					     mousegesturesDisplayOptionInfo,
					     md->opt,
					     MOUSEGESTURES_DISPLAY_OPTION_NUM))
    {
	free (md);
	return FALSE;
    }

    md->screenPrivateIndex = allocateScreenPrivateIndex (d);
    if (md->screenPrivateIndex < 0)
    {
	compFiniDisplayOptions (d, md->opt, MOUSEGESTURES_DISPLAY_OPTION_NUM);
        free (md);
        return FALSE;
    }

    md->grabIndex = 0;

    md->deltaX = 0;
    md->deltaY = 0;

    md->currentStroke = '\0';
    md->lastStroke = '\0';

    memset (&md->gesture, 0, sizeof (md->gesture));
    md->nGesture = 0;

    WRAP (md, d, handleEvent, mousegesturesHandleEvent);

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

    return TRUE;
}

static void
mousegesturesFiniDisplay (CompPlugin *p, CompDisplay *d)
{
    int i;

    MOUSEGESTURES_DISPLAY (d);

    freeScreenPrivateIndex (d, md->screenPrivateIndex);

    UNWRAP (md, d, handleEvent);

    for (i = 0; i < MOUSEGESTURES_MAX_GESTURES; i++)
    {
	Mousegesture *gesture = &md->gesture[i];

	if (gesture->pluginName)
	    free (gesture->pluginName);

	if (gesture->actionName)
	    free (gesture->actionName);
    }

    compFiniDisplayOptions (d, md->opt, MOUSEGESTURES_DISPLAY_OPTION_NUM);

    free (md);
}

static Bool
mousegesturesInit (CompPlugin *p)
{
    if (!compInitPluginMetadataFromInfo (&mousegesturesMetadata,
					 p->vTable->name,
					 mousegesturesDisplayOptionInfo,
					 MOUSEGESTURES_DISPLAY_OPTION_NUM,
					 0, 0))
	return FALSE;

    displayPrivateIndex = allocateDisplayPrivateIndex ();
    if (displayPrivateIndex < 0)
    {
	compFiniMetadata (&mousegesturesMetadata);
	return FALSE;
    }

    compAddMetadataFromFile (&mousegesturesMetadata, p->vTable->name);

    return TRUE;
}
static CompBool
mousegesturesInitObject (CompPlugin *p,
			 CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) 0, /* InitCore */
	(InitPluginObjectProc) mousegesturesInitDisplay
    };

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

static void
mousegesturesFiniObject (CompPlugin *p,
			 CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) 0, /* FiniCore */
	(FiniPluginObjectProc) mousegesturesFiniDisplay
    };

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

static CompOption *
mousegesturesGetObjectOptions (CompPlugin *plugin,
			       CompObject *object,
			       int	  *count)
{
    static GetPluginObjectOptionsProc dispTab[] = {
	(GetPluginObjectOptionsProc) 0, /* GetCoreOptions */
	(GetPluginObjectOptionsProc) mousegesturesGetDisplayOptions
    };

    RETURN_DISPATCH (object, dispTab, ARRAY_SIZE (dispTab),
		     (void *) (*count = 0), (plugin, object, count));
}

static CompBool
mousegesturesSetObjectOption (CompPlugin      *plugin,
			      CompObject      *object,
			      const char      *name,
			      CompOptionValue *value)
{
    static SetPluginObjectOptionProc dispTab[] = {
	(SetPluginObjectOptionProc) 0, /* SetCoreOption */
	(SetPluginObjectOptionProc) mousegesturesSetDisplayOption
    };

    RETURN_DISPATCH (object, dispTab, ARRAY_SIZE (dispTab), FALSE,
		     (plugin, object, name, value));
}

static void
mousegesturesFini (CompPlugin *p)
{
    freeDisplayPrivateIndex (displayPrivateIndex);

    compFiniMetadata (&mousegesturesMetadata);
}

static CompMetadata *
mousegesturesGetMetadata (CompPlugin *plugin)
{
    return &mousegesturesMetadata;
}

CompPluginVTable mousegesturesVTable = {
    "mousegestures",
    mousegesturesGetMetadata,
    mousegesturesInit,
    mousegesturesFini,
    mousegesturesInitObject,
    mousegesturesFiniObject,
    mousegesturesGetObjectOptions,
    mousegesturesSetObjectOption
};

CompPluginVTable *
getCompPluginInfo20070830 (void)
{
    return &mousegesturesVTable;
}