summaryrefslogtreecommitdiff
path: root/vigo.c
blob: 6ae1ad720086d508720c08c70279f70cf6856396 (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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
/* Compiz vigo plugin
 *
 * Allows you to navigate between windows by direction;
 * up, down left and right (in/out exists as well, although i dont
 * find them very useful). Some extra features are thrown
 * in as well; bookmarking of windows (inspired by
 * the mX and 'X vi commands) and "raise or lower".
 *
 * copyright (c) 2008 magnus janlert <janlert@acc.umu.se>
 *
 * I used the tile plugin as a template at the start, so there
 * is probably a few lines or code left from there, the
 * tile plugin is:
 * Copyright (c) 2006 Atie H. <atie.at.matrix@gmail.com>
 * Copyright (c) 2006 Michal Fojtik <pichalsi(at)gmail.com>
 * Copyright (c) 2007 Danny Baumann <maniac@beryl-project.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 <X11/keysymdef.h>
#include <compiz-core.h>
#include <string.h>
#include "vigo_options.h"
#include <math.h>

static int displayPrivateIndex;

typedef struct _VigoDisplay
{
    HandleEventProc handleEvent;
    int screenPrivateIndex;
} VigoDisplay;

typedef struct _VigoScreen
{
    int windowPrivateIndex;
    int grab;
    int grabIndex;
    CompWindow *grabWin;
    CompWindow *nextFocus;
    ActivateWindowProc activateWindow;
} VigoScreen;

typedef struct _VigoROLData
{
    CompWindow *w;
    CompScreen *s;
    Region v;
} VigoROLData;

#define VIGO_KSLEN 25
typedef struct _VigoWindow
{
    char mark[VIGO_KSLEN];
} VigoWindow;

typedef struct _VigoPoint
{
    int x, y;
} VigoPoint;

typedef struct _VigoWinExtra VigoWinExtra;
struct _VigoWinExtra
{
    CompWindow *w;
    Region v;
    XRectangle bbox;
    VigoWinExtra *next;
};

#define VIGO_GRAB_NONE 0
#define VIGO_GRAB_MARK 1
#define VIGO_GRAB_GOTO 2

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

#define VIGO_DISPLAY(d)	\
    VigoDisplay *vd = GET_VIGO_DISPLAY (d)

#define GET_VIGO_WINDOW(w, vs) \
    ((VigoWindow *) (w)->base.privates[(vs)->windowPrivateIndex].ptr)

#define VIGO_WINDOW(w) \
    VigoWindow *vw = GET_VIGO_WINDOW  (w, \
		GET_VIGO_SCREEN (w->screen, \
		GET_VIGO_DISPLAY (w->screen->display)))

#define GET_VIGO_SCREEN(s, vd) \
    ((VigoScreen *) (s)->base.privates[(vd)->screenPrivateIndex].ptr)

#define VIGO_SCREEN(s) \
    VigoScreen *vs = GET_VIGO_SCREEN (s, GET_VIGO_DISPLAY (s->display))


#define GET_DATA \
	CompScreen *s;\
	CompWindow *w;\
	Window xid; \
	xid = getIntOptionNamed (option, nOption, "root", 0); \
	s = findScreenAtDisplay (d, xid); \
	if (!s) \
	return FALSE; \
	xid = getIntOptionNamed (option, nOption, "window", 0); \
	w = findWindowAtDisplay (d, xid); \
	if (!w) return FALSE;

#define VIGO_NORTH 1
#define VIGO_SOUTH 2
#define VIGO_WEST 3
#define VIGO_EAST 4


static void
vigoWinRect (CompWindow *w, XRectangle *r)
{
    if (!(w && r))
	return;
    r->x = w->attrib.x - w->input.left;
    r->y = w->attrib.y - w->input.top;
    r->width = w->width + w->input.left + w->input.right;
    r->height = w->height + w->input.top + w->input.bottom;
    return;
}

static void
vigoWinRegion (CompWindow *w, Region r)
{
    if (!(w && r))
	return;
    XRectangle rc;
    vigoWinRect (w, &rc);
    XUnionRectWithRegion (&rc, r, r);
}

static void
vigoGrabRelease (CompScreen *s,
		 VigoScreen *vs)
{
    if (vs->grabIndex)
    {
	removeScreenGrab (s, vs->grabIndex, NULL);
	vs->grabIndex = 0;
    }
}

static Bool
vigoGrabScreen (CompScreen *s,
		int        grabMode,
		CompWindow *w)
{
    VIGO_SCREEN (s);
    if (!vs->grabIndex)
    {
	if (otherScreenGrabExist (s, "vigo", 0))
	    return FALSE;
	vs->grab = grabMode;
	vs->grabWin = w;
	vs->grabIndex = pushScreenGrab (s, s->invisibleCursor, "vigo");
    }
    return TRUE;
}

static Bool
vigoIsCandidate (CompWindow *w)
{
    if (!w->mapNum || w->attrib.map_state != IsViewable)
	return FALSE;

    if (w->invisible || w->hidden || w->minimized)
	return FALSE;

    if (!(w->inputHint || (w->protocols & CompWindowProtocolTakeFocusMask)))
	return FALSE;

    if (w->attrib.override_redirect)
	return FALSE;

    if (w->state & CompWindowStateSkipTaskbarMask)
	return FALSE;

    if (w->wmType & (CompWindowTypeDockMask | CompWindowTypeDesktopMask))
	return FALSE;

    if (!(*w->screen->focusWindow) (w))
	return FALSE;

    if (!matchEval (vigoGetMoveMatch (w->screen->display), w))
	return FALSE;

    return TRUE;
}

// If this window can hide other windows
static Bool
vigoHidesWin (CompWindow *w) {
    if (!w->mapNum)
	return FALSE;
    if (w->invisible || w->hidden || w->minimized)
	return FALSE;
    if (!matchEval (vigoGetMoveMatch (w->screen->display), w))
	return FALSE;
    return TRUE;
}

// Calculate visible region of window w
static Region
vigoVisible (CompWindow *w) {
    Region v = XCreateRegion ();
    Region h = XCreateRegion ();
    if (!(v && h && w))
	return NULL;
    vigoWinRegion (w, v);
    for (w = w->next; w; w = w->next)
    {
	if (vigoHidesWin (w))
	{
	    vigoWinRegion (w, h);
	}
    }
    XSubtractRegion (v, h, v);
    XDestroyRegion (h);
    return v;
}

static CompWindow *
vigoFindOverlapping (CompWindow *w,
		     Bool       up)
{
    CompWindow *i = w;
    Region wr = XCreateRegion ();
    Region ol = XCreateRegion ();
    if (!(w && wr && ol))
	return NULL;
    vigoWinRegion (w, wr);
    while (1)
    {
	i = up ? i->next : i->prev;
	if (!i)
	    break;
	if (!vigoIsCandidate (i))
	    continue;
	vigoWinRegion (i, ol);
	XIntersectRegion (wr, ol, ol);
	// If they overlap
	if (!XEmptyRegion (ol))
	{
	    // Avoid hidden windows
	    Region v = vigoVisible (i);
	    if (!v)
		return NULL;
	    Bool hidden = XEmptyRegion (v);
	    XDestroyRegion (v);
	    if (!hidden)
		break;
	}
    }
    XDestroyRegion (ol);
    XDestroyRegion (wr);
    return i;
}

static VigoPoint
vigoCenterOfMass (Region r)
{
    VigoPoint c;
    float m = 0.0;
    int j;
    float x = 0.0;
    float y = 0.0;
    /* i should probably NOT be poking around in the Region-struct
     * but.. i will anyway */
    for (j = 0; j < r->numRects; j++)
    {
	BOX *b = &(r->rects[j]);
	int w = (MAX (b->x1, b->x2) - MIN (b->x1, b->x2));
	int h = (MAX (b->y1, b->y2) - MIN (b->y1, b->y2));
	float bm = w * h;
	x += bm * (b->x1 + b->x2) / 2;
	y += bm * (b->y1 + b->y2) / 2;
	m += bm;
    }
    c.x = x / m;
    c.y = y / m;
    return c;
}

static Bool
vigoInsideRegion (int          x,
		  int          y,
		  VigoWinExtra *e)
{
    return XPointInRegion (e->v, x, y);
}

static Bool
vigoInsideRect (int          x,
		int          y,
		VigoWinExtra *e)
{
    return (x >= e->bbox.x && x <= e->bbox.x + e->bbox.width &&
	    y >= e->bbox.y && y <= e->bbox.y + e->bbox.height);
}

static float
vigoClosestBBox (int          x,
		 int          y,
		 VigoWinExtra *e)
{
    float dmin = -1.0;
    for (; e; e = e->next)
    {
	int xm = e->bbox.x + e->bbox.width / 2;	
	int ym = e->bbox.y + e->bbox.height / 2;	
	float dx = fabs (xm - x);
	float dy = fabs (ym - y);
	float w = e->bbox.width/2;
	float h = e->bbox.height/2;
	float d = sqrt (dx*dx + dy*dy) - sqrt (w*w + h*h);
	if (d <= 0.0)
	    return 0.0;
	    
	if (d > 0.0 && (dmin < 0.0 || d < dmin))
	    dmin = d;
    }
    return dmin < 1.0 ? 0.0 : dmin;
}

static VigoWinExtra *
vigoRaySlow (int          x1,
	     int          y1,
	     float        alpha,
	     VigoWinExtra *extra,
	     float        *d)
{
    VigoWinExtra *e;
    // how long since we last saw a bbox
    float lastbb = 0.0;
    float dd = 10.0;
    while (lastbb < 41.0)
    {
	int x2 = x1 + sin (alpha) * (*d);
	int y2 = y1 + cos (alpha) * (*d);
	for (e = extra; e; e = e->next)
	{
	    if (vigoInsideRect (x2, y2, e))
	    {
		lastbb = 0.0;
		if (vigoInsideRegion (x2, y2, e))
		{
		    // Avoid very thin regions
		    int x3 = x1 + sin (alpha) * (*d + 40);
		    int y3 = y1 + cos (alpha) * (*d + 40);
		    if (vigoInsideRegion (x3, y3, e))
			return e;
		}
	    }
	}
	lastbb += dd;
	*d += dd;
    }
    return NULL;
}

static VigoWinExtra *
vigoRay (int          x1,
	 int          y1,
	 float        alpha,
	 VigoWinExtra *extra,
	 float        *d)
{
    VigoWinExtra *e;
    while (*d < 2000)
    {
	int x2 = x1 + sin (alpha) * (*d);
	int y2 = y1 + cos (alpha) * (*d);
	// Check if we in a region right now
	for (e = extra; e; e = e->next)
	{
	    if (vigoInsideRect (x2, y2, e) && vigoInsideRegion (x2, y2, e))
		return e;
	}
	// So we're currently not inside any bbox
	float dd = vigoClosestBBox (x2, y2, extra);
	if (dd < 10) {
	    // yes, x1/y1, not x2/y2, we always use the start point as ref.
	    e = vigoRaySlow (x1, y1, alpha, extra, d);
	    if (e)
		return e;
	}
	else
	{
	    *d += dd;
	}
    }
    return NULL;
}

static float
vigoScoreFuncX (float alpha,
		float d)
{
    // should probably be adjusted a bit
    float v = pow (fabs (cos (alpha)), 4);
    int x = 5000 - d ;
    return v * x;
}

static float
vigoGetTheta (int dir)
{
    switch (dir)
    {
	case VIGO_SOUTH: return 0;
	case VIGO_NORTH: return M_PI;
	case VIGO_WEST:  return M_PI + M_PI_2;
	case VIGO_EAST:  return M_PI_2;
    }
    return 0;
}

static CompWindow *
vigoScoreWindows (VigoPoint    s,
		  CompWindow   *cw,
		  int          dir,
		  VigoWinExtra *extra)
{
    float theta = vigoGetTheta (dir);;
    int rays = 4;
    float dt = M_PI_4 / (2 * rays);
    float alpha;
    float bestScore = -1.0;
    VigoWinExtra *bestWin = NULL;
    int i;
    for (i = -rays; i <= rays; i ++)
    {
	float d = 0.0;
	alpha = theta + i * dt;
	VigoWinExtra *hit = vigoRay (s.x, s.y, theta + i * dt, extra, &d);
	if (hit)
	{
	    float s = vigoScoreFuncX (fabs (i * dt), d);
	    if (bestScore < 0 || s > bestScore) {
		bestScore = s;
		bestWin = hit;
	    }
	}
    }
    if (bestWin)
	return bestWin->w;
    return NULL;
}

static VigoWinExtra *
vigoBuildWinExtra (CompWindow *w,
		   VigoWinExtra *next) {
    VigoWinExtra *tmp = malloc (sizeof (VigoWinExtra));
    if (!tmp)
	return NULL;
    tmp->next = next;
    tmp->w = w;
    tmp->v = vigoVisible (w);
    if (!tmp->v)
    {
	free (tmp);
	return NULL;
    }
    XClipBox (tmp->v, &(tmp->bbox));
    return tmp;
}

static VigoWinExtra *
vigoFreeWinExtra (VigoWinExtra *extra)
{
    VigoWinExtra *next = extra->next;
    XDestroyRegion (extra->v);
    free (extra);
    return next;
}

static void
vigoGetScaleFactor (int dir,
		    int *x,
		    int *y)
{
    switch (dir)
    {
	case VIGO_SOUTH: *x = 0; *y = 1; return;
	case VIGO_NORTH: *x = 0; *y = -1; return;
	case VIGO_WEST: *x = -1; *y = 0; return;
	case VIGO_EAST: *x = 1; *y = 0; return;
    }
}

static VigoPoint vigoFindStart (VigoWinExtra *cextra,
				int dir, 
				VigoWinExtra *extra)
{
    // our first guess is the center of mass
    // works well when the window is mostly visible
    VigoPoint com = vigoCenterOfMass (cextra->v);
    if (vigoInsideRegion (com.x , com.y, cextra))
    {
	// if the com is in the visible region, its probably
	// a good guess
	return com;
	// TODO maybe try and shift toward the direction we're moving
	//      could help with high/thin or wide/short windows
    }
    else
    {
	// So that didnt work, try and find if there anything
	// in our visible area along our axis of movement
	// this should take care of the "L-case"
	// xxxx      x = current window, a = other window
	// xaaaa
	// xaaaa
	//  aaaa
	//
	// trying to avoid looking inside Region, so do a stupid for-loop 
	VigoPoint start;
	int sx, sy, xstart, ystart;
	vigoGetScaleFactor (dir, &sx, &sy);
	int len;
	if (dir == VIGO_NORTH || dir == VIGO_SOUTH)
	{
	    xstart = com.x;
	    ystart = cextra->bbox.y;
	    len = cextra->bbox.height;
	    if (dir == VIGO_SOUTH)
		ystart += cextra->bbox.height;

	}
	if (dir == VIGO_EAST || dir == VIGO_WEST)
	{
	    xstart = cextra->bbox.x;
	    ystart = com.y;
	    len = cextra->bbox.width;
	    if (dir == VIGO_EAST)
		xstart += cextra->bbox.width;

	}
	int i;
	int delta = 25;
	for (i = delta / 2; i <= len; i += delta)
	{
	    start.x = xstart - sx * i;
	    start.y = ystart - sy * i;
	    if (vigoInsideRegion (start.x, start.y, cextra))
		return start;
	}

	// Ok, this is nasty one, probably some kind or cross-shape
	//   aa      x = current window, a/b/c/d = other window
	// xxaaxx
	//dddaacccc   just subtract x (all, not just visible) from
	//dddbbcccc   all regions, to clear som space
	// xxbbxx
	//   bb
	//
	Region wr = XCreateRegion ();
	vigoWinRegion (cextra->w, wr);
	VigoWinExtra *j;
	for (j = extra; j; j = j->next)
	{
	    XSubtractRegion (j->v, wr, j->v);
	    XClipBox (j->v, &(j->bbox));
	}
	XDestroyRegion (wr);
	return com;
    }
}

static CompWindow *
vigoGetWindow (CompWindow *cw,
	       int        dir)
{
    CompWindow *h;
    CompWindow *bestWin = NULL;
    VigoWinExtra *extra = NULL;

    VigoWinExtra *cextra = vigoBuildWinExtra (cw, NULL);
    if (!cextra)
	return NULL;

    // Build the extra-struct
    for (h = cw->screen->windows; h; h = h->next)
    {
	if (h == cw || !vigoIsCandidate (h))
	    continue;
	VigoWinExtra *e2 = vigoBuildWinExtra (h, extra);
	if (!e2)
	{
	    vigoFreeWinExtra (extra);
	    vigoFreeWinExtra (cextra);
	    return NULL;
	}
	extra = e2;
    }

    // Find a starting point and get the best scoring window
    VigoPoint origin = vigoFindStart (cextra, dir, extra);
    bestWin = vigoScoreWindows (origin, cw, dir, extra);

    // free extra-struct
    while (extra)
    {
	extra = vigoFreeWinExtra (extra);
    }
    return bestWin;
}

static void
vigoRefocus (CompWindow *w)
{
    CompScreen *s = w->screen;
    VIGO_SCREEN (s);
    UNWRAP (vs, s, activateWindow);
    (*s->activateWindow) (w);
    CompWindow *foc = vs->nextFocus;
    if (foc)
    {
	// The warpPointer should not be needed, but there is a bug
	// in the wall plugin apparently, fixed with
	// http://gitweb.compiz-fusion.org/?p=fusion/plugins/wall;a=commit;h=7c3265a7a984cfbf9e16913fd804f53d8b11894f
	warpPointer (s, -10000, -10000);
	warpPointer (s, foc->serverX + 10, foc->serverY + 10);
	// Also, this seems to fail every once in a while
	// havnt been able to find any pattern though
	moveInputFocusToWindow (foc);
	vs->nextFocus = NULL;
    }
    return;
}

static void
vigoMoveToMark (CompDisplay *d,
		char        *mark)
{
    CompWindow *w;
    CompWindow *hit = NULL;
    CompScreen *s;
    for (s = d->screens; s; s = s->next)
    {
	for (w = s->windows; w; w = w->next)
	{
	    VIGO_WINDOW (w);
	    if (!strcmp (mark, vw->mark))
	    {
		hit = w;
		break;
	    }
	}
	if (hit)
	    break;
    }
    if (hit)
    {
	s = hit->screen;
	int x, y;
	defaultViewportForWindow (hit, &x, &y);
	if (x != hit->screen->x || y != hit->screen->y)
	{
	    VIGO_SCREEN (s);
	    vs->nextFocus = hit;
	    WRAP (vs, s, activateWindow, vigoRefocus);
	}
	sendWindowActivationRequest (s, hit->id);
    }
}


static void
vigoRemoveMark (CompDisplay *d,
		char        *mark)
{
    CompScreen *s;
    CompWindow *h;
    for (s = d->screens; s; s = s->next)
    {
	for (h = s->windows; h; h = h->next)
	{
	    VIGO_WINDOW (h);
	    if (!strcmp (mark, vw->mark))
		strcpy (vw->mark, "");
	}
    }
}

static void
vigoMarkWindow (CompDisplay *d,
		CompWindow  *w,
		char        *mark)
{
    vigoRemoveMark (d, mark);
    VIGO_WINDOW (w);
    strcpy (vw->mark, mark);
}

static void
vigoHandleEvent (CompDisplay *d,
		 XEvent      *event)
{
    VIGO_DISPLAY (d);
    char kstr[VIGO_KSLEN];
    Bool dropEvent = FALSE;

    if (event->type == KeyPress)
    {
	CompScreen *s = findScreenAtDisplay (d, event->xkey.root);
	CompWindow *w = findWindowAtDisplay (d, event->xfocus.window);
	if (s && w)
	{
	    VIGO_SCREEN (s);
	    vigoGrabRelease (s, vs);
	    XKeyEvent *keyEvent = (XKeyEvent *) event;
	    int n = XLookupString (keyEvent, kstr, VIGO_KSLEN - 1, NULL, NULL);
	    kstr[n] = '\0';
	    if (n && vs->grabWin)
	    {
		switch (vs->grab)
		{
		    case VIGO_GRAB_MARK:
			vigoMarkWindow (d, vs->grabWin, kstr);
			dropEvent = TRUE;
			break;
		    case VIGO_GRAB_GOTO:
			dropEvent = TRUE;
			vigoMoveToMark (d, kstr);
			break;
		}
	    }
	}
    }
    if (!dropEvent)
    {
	UNWRAP (vd, d, handleEvent);
	(*d->handleEvent) (d, event);
	WRAP (vd, d, handleEvent, vigoHandleEvent);
    }
}

static Bool
vigoGotoMark (CompDisplay     *d,
	      CompAction      *action,
	      CompActionState state,
	      CompOption      *option,
	      int             nOption)
{
    GET_DATA;
    vigoGrabScreen (s, VIGO_GRAB_GOTO, w);
    return TRUE;
}

static Bool
vigoMark (CompDisplay     *d,
	  CompAction      *action,
	  CompActionState state,
	  CompOption      *option,
	  int             nOption)
{
    GET_DATA;
    vigoGrabScreen (s, VIGO_GRAB_MARK, w);
    return TRUE;
}


// Called a bit after we've raised the window, and
// the s->windows list has hopefully been updated, so check
// if anything changed
static Bool
vigoROL2 (void *rd_)
{
    VigoROLData *rd = (VigoROLData *) rd_;
    if (rd)
    {
	CompWindow *w;
	// Make sure the window still exists
	for (w = rd->s->windows; w; w = w->next)
	{
	    if (w == rd->w)
	    {
		Region v = vigoVisible (rd->w);
		if (v)
		{
		    Bool e = XEqualRegion (v, rd->v);	
		    XDestroyRegion (v);
		    XDestroyRegion (rd->v);
		    if (e)
			lowerWindow (rd->w);
		    free (rd);
		    return FALSE;
		}
	    }
	}
	free (rd);
    }
    return FALSE;
}

// Raise Or Lower
static Bool
vigoROL (CompDisplay     *d,
	 CompAction      *action,
	 CompActionState state,
	 CompOption      *option,
	 int             nOption)
{
    GET_DATA;
    if (w)
    {
	VigoROLData *rd = malloc (sizeof (VigoROLData));
	if (!rd)
	    return FALSE;
	rd->w = w;
	rd->s = w->screen;
	rd->v = vigoVisible (w);
	if (!rd->v)
	{
	    free (rd);
	    return FALSE;
	}
	raiseWindow (w);
	// wait a bit and see if anything happened, if not, we want to lower it
	// uuuugly
	compAddTimeout (5, vigoROL2, rd);
    }

    return TRUE;
}

static Bool
vigoGoInOut (CompDisplay     *d,
	  CompAction      *action,
	  CompActionState state,
	  CompOption      *option,
	  int             nOption)
{
    GET_DATA;
    if (w)
    {
	CompWindow *tw = NULL;
	if (action == vigoGetMoveIn (w->screen->display))
	    tw = vigoFindOverlapping (w, False);
	if (action == vigoGetMoveOut (w->screen->display))
	    tw = vigoFindOverlapping (w, True);
	if (tw)
	    moveInputFocusToWindow (tw);
    }

    return TRUE;
}


static Bool
vigoGoto (CompDisplay     *d,
	  CompAction      *action,
	  CompActionState state,
	  CompOption      *option,
	  int             nOption)
{
    GET_DATA;
    if (w)
    {
	CompWindow *nw = NULL;
	if (action == vigoGetMoveLeft (w->screen->display))
	    nw = vigoGetWindow (w, VIGO_WEST);
	if (action == vigoGetMoveRight (w->screen->display))
	    nw = vigoGetWindow (w, VIGO_EAST);
	if (action == vigoGetMoveUp (w->screen->display))
	    nw = vigoGetWindow (w, VIGO_NORTH);
	if (action == vigoGetMoveDown (w->screen->display))
	    nw = vigoGetWindow (w, VIGO_SOUTH);
	if (nw)
	    moveInputFocusToWindow (nw);
    }

    return TRUE;
}


static Bool
vigoInitDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    VigoDisplay *vd;

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

    vd = malloc (sizeof (VigoDisplay));
    if (!vd)
	return FALSE;

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

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

    vigoSetRaiseOrLowerInitiate (d, vigoROL);
    vigoSetMoveInInitiate (d, vigoGoInOut);
    vigoSetMoveOutInitiate (d, vigoGoInOut);
    vigoSetMoveLeftInitiate (d, vigoGoto);
    vigoSetMoveRightInitiate (d, vigoGoto);
    vigoSetMoveUpInitiate (d, vigoGoto);
    vigoSetMoveDownInitiate (d, vigoGoto);
    vigoSetMarkWindowInitiate (d, vigoMark);
    vigoSetMarkGotoInitiate (d, vigoGotoMark);

    WRAP (vd, d, handleEvent, vigoHandleEvent);

    return TRUE;
}

static Bool
vigoInitWindow (CompPlugin *p,
		CompWindow *w)
{
    VigoWindow *vw;
    VIGO_SCREEN (w->screen);
    vw = malloc (sizeof (VigoWindow));
    if (!vw)
	return FALSE;
    w->base.privates[vs->windowPrivateIndex].ptr = vw;
    return TRUE;
}

static void
vigoFiniDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    VIGO_DISPLAY (d);
    UNWRAP (vd, d, handleEvent);
    freeScreenPrivateIndex (d, vd->screenPrivateIndex);
    free (vd);
}

static void
vigoFiniWindow (CompPlugin *p,
		CompWindow *w)
{
    VIGO_WINDOW (w);
    free (vw);
}

static Bool
vigoInitScreen (CompPlugin *p,
		CompScreen *s) {
    VigoScreen *vs;
    VIGO_DISPLAY (s->display);
    vs = malloc (sizeof (VigoScreen));
    if (!vs)
	return FALSE;
    vs->windowPrivateIndex = allocateWindowPrivateIndex (s);
    vs->grabIndex = 0;
    vs->grab = VIGO_GRAB_NONE;
    vs->grabWin = NULL;
    vs->nextFocus = NULL;
    if (vs->windowPrivateIndex < 0)
    {
	free (vs);
	return FALSE;
    }
    s->base.privates[vd->screenPrivateIndex].ptr = vs;
    return TRUE;
}

static void
vigoFiniScreen (CompPlugin *p,
		CompScreen *s)
{
    VIGO_SCREEN (s);
    freeWindowPrivateIndex (s, vs->windowPrivateIndex);
    free (vs);
}

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

    return TRUE;
}

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

static CompBool
vigoInitObject (CompPlugin *p,
		CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) 0, /* InitCore */
	(InitPluginObjectProc) vigoInitDisplay,
	(InitPluginObjectProc) vigoInitScreen,
	(InitPluginObjectProc) vigoInitWindow,
    };
    RETURN_DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), TRUE, (p, o));
}

static void
vigoFiniObject (CompPlugin *p,
		CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) 0, /* FiniCore */
	(FiniPluginObjectProc) vigoFiniDisplay,
	(FiniPluginObjectProc) vigoFiniScreen,
	(FiniPluginObjectProc) vigoFiniWindow,
    };

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

CompPluginVTable vigoVTable = {
    "vigo",
    0,
    vigoInit,
    vigoFini,
    vigoInitObject,
    vigoFiniObject,
    0,
    0
};

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

// vim: formatoptions=croqlt ts=8 softtabstop=4 shiftwidth=4 cindent tw=80 cino=(0,t0