summaryrefslogtreecommitdiff
path: root/tile.cpp
blob: b77610d7446456efe94603e8d65e500613752ba5 (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
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
/**
 *
 * Compiz tile plugin
 *
 * tile.c
 *
 * 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.
 *
 * TODO
 *	- change vertical and horizontal tiling to similar behavior as Left
 *	- fix bugs
 *	- make vertical and horizontal maximization be saved when tiling
 *
 **/

/* TODO: Right now we are tiling immediately with no animation, fix that */
/* FIX: Should check if window can be resized to amount we want, otherwise
        don't include that window */

#include "tile.h"

COMPIZ_PLUGIN_20081216 (tile, TilePluginVTable);

/* window painting function, draws animation */
bool
TileWindow::glPaint (const GLWindowPaintAttrib &attrib,
		     const GLMatrix            &transform,
		     CompRegion                &region,
		     unsigned int              mask)
{
    bool status;
#if 0
    bool dontDraw = false;
    if (tw->animationType != NoAnimation)
    {
	WindowPaintAttrib wAttrib = *attrib;
	CompTransform     wTransform = *transform;
	float             progress;

	progress = (float)ts->msResizing /
	           (float)tileGetAnimationDuration (s->display);

	switch (tileGetAnimateType (s->display))
	{
	    /*
	       Drop animation
	       */
	    case AnimateTypeDropFromTop:
		matrixRotate (&wTransform,
			      (progress * 100.0f) - 100.0f,
			      0.0f, 0.0f, 1.0f);
		mask |= PAINT_WINDOW_TRANSFORMED_MASK;
		break;

	    /*
	       Zoom animation
	       */
	    case AnimateTypeZoom:
		matrixTranslate (&wTransform, 0, 0, progress - 1.0f);
		mask |= PAINT_WINDOW_TRANSFORMED_MASK;
		break;

	    /*
    	       Slide animation
	       */
	    case AnimateTypeSlide:
		if (progress < 0.75f)
		    wAttrib.opacity /= 2;
		else
		    wAttrib.opacity *= (0.5f + 2 * (progress - 0.75f));

		if (ts->msResizing > tw->animationNum * ts->oneDuration) 
		{
		    /* animation finished */
		    tw->animationType = AnimationDone;
		}
		else if (ts->msResizing > (tw->animationNum - 1) *
			 ts->oneDuration)
		{
		    int thisDur; /* ms spent animating this window */
		    thisDur = ts->msResizing % ts->oneDuration;

		    if (tw->animationNum % 2)
			matrixTranslate (&wTransform,
					 s->width - s->width *
					 (float)thisDur / ts->oneDuration,
					 0, 0);
		    else
			matrixTranslate (&wTransform,
					 -s->width + s->width *
					 (float)thisDur / ts->oneDuration,
					 0, 0);

		    mask |= PAINT_WINDOW_TRANSFORMED_MASK;
		}
		else
		    dontDraw = true;
		break;
	    /*
    	       Outline animation
	       */
	    case AnimateTypeFilledOutline:
		dontDraw = true;
		break;

	    /*
    	       Fade animation
	       */
	    case AnimateTypeFade:
		// first half of the animation, fade out
		if (progress < 0.4f)
		    wAttrib.opacity -= wAttrib.opacity * progress / 0.4f;
		else
		{
		    if (progress > 0.6f && tw->alreadyResized)
		    {
			// second half of animation, fade in
			wAttrib.opacity *= (progress - 0.6f) / 0.4f;
		    }
		    else
		    {
			if (tw->needConfigure)
			    tileSetNewWindowSize (w);
			dontDraw = true;
		    }
		}
		break;

    	    default:
		break;
	}

	if (dontDraw)
	    mask |= PAINT_WINDOW_NO_CORE_INSTANCE_MASK;

	UNWRAP (ts, s, paintWindow);
	status = (*s->paintWindow) (w, &wAttrib, &wTransform, region, mask);
	WRAP (ts, s, paintWindow, tilePaintWindow);
    }
    else // paint window as always
#endif

    status = gWindow->glPaint (attrib, transform, region, mask);

    return status;
}

void
TileScreen::preparePaint (int        msSinceLastPaint)
{
#if 0
    // add spent time
    if (state == In || state == Out)
	ts->msResizing += msSinceLastPaint;
#endif
    cScreen->preparePaint (msSinceLastPaint);
}
#if 0
void
TileScreen::glPaintScreen (CompOutput   *outputs,
     		 	   int          numOutputs,
		 	   unsigned int mask)
{
    if (state == In || state == Out)
    {
	/* We want to ensure that we are painting the fullscreen output */
	outputs = &screen->fullscreenOutput ();
	numOutputs = 1;
    }

    gScreen->glPaintScreen (outputs, numOutputs, mask);
}
#endif
void
TileScreen::donePaint ()
{
#if 0
    if (state == In || state == Out)
    {
	if (msResizing > tileGetAnimationDuration (s->display))
	{
    	    foreach (CompWindow *w, screen->windows ())
    	    {
    		TILE_WINDOW (w);
    		//tw->animationType = NoAnimation;
    	    }

    	    //ts->msResizing = 0;

	    if (state == In)
		state = Tiled;
	    else if (state == Out)
		state = Normal;
	}

	cScreen->damageScreen ();
    }
#endif
    cScreen->donePaint ();
}

bool
TileScreen::glPaintOutput (const GLScreenPaintAttrib &attrib,
		 	   const GLMatrix	     &transform,
		 	   CompRegion                &region,
		 	   CompOutput                *output,
		 	   unsigned int              mask)
{
    bool status;

    if (state == In || state == Out)
	mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS_MASK;

    status = gScreen->glPaintOutput (attrib, transform, region, output, mask);

    /* Check if animation is enabled, there is resizing
       on screen and only outline should be drawn */

#if 0
    if ((state == In || state == Out) && (output->id () == ~0) &&
	(optionGetAnimateType () == TileOptions::AnimateTypeFilledOutline))
    {
	GLMatrix      sTransform = transform;
	float         animationDuration = optionGetAnimationDuration ();
	int           x, y, width, height;

	sTransform.toScreenSpace (output, -DEFAULT_Z_CAMERA);
	glPushMatrix ();
	glLoadMatrixf (sTransform.getMatrix ());

	glLineWidth (4.0f);

	foreach (CompWindow *w, screen->windows ())
	{
	    TILE_WINDOW (w);
	    if (tw->animationType == Animating)
	    {
		/* Coordinate = start +            speed          * elapsedTime
		   Coordinate = start + (target - start)/interval * elapsedTime
		   Draw outline */

		x      = tw->tiler->previous.x () - w->input ().left +
		         (((float)(w->x () - tw->previous.x ())) *
			  ts->msResizing / animationDuration);
		y      = tw->previous.y () - w->input ().top +
		         (((float)(w->y () - tw->previous.y ())) *
			  ts->msResizing / animationDuration);
		width  = tw->previous.width () + w->input ().left + w->input ().right +
		         (((float)(w->width () - tw->previous.width ())) *
			  ts->msResizing / animationDuration);
		height = tw->previous.height () +
		         w->input ().top + w->input ().bottom +
			 (((float)(w->height () - tw->previous.height ())) *
			  ts->msResizing / animationDuration);

		glColor3us (tw->outlineColor[0] * 0.66,
			    tw->outlineColor[1] * 0.66,
			    tw->outlineColor[2] * 0.66);
		glRecti (x, y + height, x + width, y);

		glColor3usv (tw->outlineColor);

		glBegin (GL_LINE_LOOP);
		glVertex3f (x, y, 0.0f);
		glVertex3f (x + width, y, 0.0f);
		glVertex3f (x + width, y + height, 0.0f);
		glVertex3f (x, y + height, 0.0f);
		glEnd ();

		glColor4usv (defaultColor);
	    }
	}
	glPopMatrix ();
	glColor4usv (defaultColor);
	glLineWidth (1.0f);
    }
#endif

    return status;
}

/* Resize notify used when windows are tiled horizontally or vertically */
void
TileWindow::resizeNotify (int        dx,
		  	  int        dy,
		  	  int        dwidth,
		  	  int        dheight)
{
    window->resizeNotify (dx, dy, dwidth, dheight);

    if (!alreadyResized)
    {
	alreadyResized = true;
	return;
    }
#if 0 
//WE WILL MOVE THIS TO A DIFFERENT PLUGIN
    /* Dont do anything if joining is disabled or windows are being resized */
    if (tileGetTileJoin (w->screen->display) && !ts->grabIndex)
    {
	CompWindow *prev = NULL, *next = NULL, *cw;
	bool       windowSeen = false;

	/* determine previous and next tiled window */
	for (cw = w->screen->reverseWindows; cw; cw = cw->prev)
	{
	    if (windowSeen)
	    {
		next = cw;
		break;
	    }
	    else
	    {
		if (cw != w)
		    prev = cw;
		else
		    windowSeen = true;
	    }
	}

    	switch (ts->tileType)
	{
	case TileToggleTypeTile:
	    if (prev)
		placeWin (prev,
			  prev->attrib.x, prev->attrib.y,
			  w->attrib.x - prev->attrib.x -
			  w->input.left - prev->input.right,
			  prev->height);

	    if (next)
	    {
		int currentX;
		currentX = w->attrib.x + w->width +
		           w->input.right + next->input.left;
		placeWin (next, currentX, next->attrib.y,
			  next->width + next->attrib.x - currentX,
			  next->height);
	    }
	    break;

	case TileToggleTypeTileHorizontally:
	    if (prev)
		placeWin (prev,
			  prev->attrib.x, prev->attrib.y,
			  prev->width,
			  w->attrib.y - prev->attrib.y -
			  w->input.top - prev->input.bottom);

	    if (next)
    	    {
		int currentY;
		currentY = w->attrib.y + w->height +
		           w->input.bottom + next->input.top;
		placeWin (next, next->attrib.x,
			  currentY, next->width,
			  next->height + next->attrib.y - currentY);
	    }
	    break;
	case TileToggleTypeLeft:
	    if (!next && prev && dwidth) /* last window - on the left */
	    {
		XRectangle workArea;
		int        currentX;

		workArea = w->screen->workArea;

		for (cw = w->screen->windows; cw; cw = cw->next)
		{
		    TILE_WINDOW(cw);

		    if (!tw->isTiled || (cw->id == w->id))
			continue;

		    currentX = workArea.x + w->serverX +
			       w->serverWidth + w->input.right + cw->input.left;

		    placeWin (cw, currentX, cw->attrib.y,
			      workArea.width - currentX - w->input.right,
			      cw->attrib.height);
		}
	    }
	    else if (next) /* windows on the right */
	    {
		XRectangle workArea;
		bool       first = true;

		workArea = w->screen->workArea;

		for (cw = w->screen->windows; cw; cw = cw->next)
		{
		    TILE_WINDOW (cw);

		    if (!tw->isTiled || (cw->id == w->id))
			continue;

		    if (first)
		    {
			placeWin (cw,
				  workArea.x + cw->input.left, cw->attrib.y,
				  w->serverX - w->input.left -
				  cw->input.left - cw->input.right - workArea.x,
				  cw->attrib.height);

			first = false;
		    }
		    else
		    {
			int x = cw->attrib.x;
			int y = cw->attrib.y;
			int width = cw->attrib.width;
			int height = cw->attrib.height;

			if (prev && (cw->id == prev->id))
			    height = w->serverY - cw->attrib.y -
				     w->input.top - cw->input.bottom;
			else if (next && (cw->id == next->id))
			    y = w->serverY + w->serverHeight +
				w->input.bottom + cw->input.top;

			x = w->serverX;
			width = workArea.width + workArea.x -
			        w->serverX - w->input.right;

			placeWin (cw, x, y, width, height);
		    }
		}
	    }
	    break;

	default:
	    break;
	}
    }
#endif
}

/* this is resizeConstrainMinMax from resize.c,
   thanks to David Reveman/Nigel Cunningham */
void
TileWindow::constrainMinMax (int        width,
		 	     int        height,
		 	     int        &newWidth,
		 	     int        &newHeight)
{
    const XSizeHints *hints = &window->sizeHints ();
    int              min_width = 0;
    int              min_height = 0;
    int              max_width = MAXSHORT;
    int              max_height = MAXSHORT;

    if ((hints->flags & PBaseSize) && (hints->flags & PMinSize))
    {
	min_width = hints->min_width;
	min_height = hints->min_height;
    }
    else if (hints->flags & PBaseSize)
    {
	min_width = hints->base_width;
	min_height = hints->base_height;
    }
    else if (hints->flags & PMinSize)
    {
	min_width = hints->min_width;
	min_height = hints->min_height;
    }

    if (hints->flags & PMaxSize)
    {
	max_width = hints->max_width;
	max_height = hints->max_height;
    }
#define CLAMP(v, min, max) ((v) <= (min) ? (min) : (v) >= (max) ? (max) : (v))

    /* clamp width and height to min and max values */
    width = CLAMP (width, min_width, max_width);
    height = CLAMP (height, min_height, max_height);

#undef CLAMP

    newWidth = width;
    newHeight = height;
}

/* Moves window to [x,y] and resizes to width x height
   if no animation or starts animation */

bool
TileWindow::placeWin (int          x,
		      int          y,
		      unsigned int width,
		      unsigned int height)
{
    /* Workaround for g++ warnings */
    int i_width;
    int i_height;
    /* this checks if the window isnt smaller
       than minimum size it has defined */
    /*constrainMinMax (i_width, i_height, i_width, i_height);

    width = i_width;
    height = i_height;*/

    /* does the window need to be moved? */
    if (x == window->x () && y == window->y () &&
	width == window->width () && height == window->height ())
	return true;

    if (!tiler)
	return false;

    /* set previous coordinates for animation */
    tiler->previous.setGeometry (window->x (), window->x () + window->width (),
				 window->y (), window->y () + window->height ());

    /* set future coordinates for animation */
    tiler->current.setGeometry (x, x + width, y, y + height);

    alreadyResized = false; /* window is not resized now */
    needConfigure = true;
#if 0
    switch (tileGetAnimateType (w->screen->display))
    {
    case AnimateTypeNone:
	tileSetNewWindowSize (w);
	break;
    case AnimateTypeFilledOutline:
    case AnimateTypeSlide:
    case AnimateTypeZoom:
    case AnimateTypeDropFromTop:
	tileSetNewWindowSize (w);
	/* fall-through */
    case AnimateTypeFade:
	tw->animationType = Animating;
	break;
    default:
     	break;
    }
#endif
    return true;
}

bool
TileWindow::is ()
{
    TILE_SCREEN (screen);

    if (ts->optionGetExcludeMatch ().evaluate (window))
	return false;

    if (window->overrideRedirect ())
	return false;

    if (!window->focus ())
	return false;

    if (window->wmType () & (CompWindowTypeDockMask | CompWindowTypeDesktopMask))
	return false;

    if (window->state () & CompWindowStateSkipPagerMask)
	return false;

    if (window->minimized () || !window->placed ())
	return false;

    return true;
}

Tiler::Tiler () :
    state (Tiler::NotTiled),
    maximized (false),
    savedMaxState (0)
{
    TILE_SCREEN (screen);
    ts->tilers.push_back(this);
}

void
Tiler::save (CompWindow *w)
{
    saved.setGeometry (w->serverX (), w->serverX () + w->serverWidth (),
		       w->serverY (), w->serverY () + w->serverHeight ());

    maximized = w->state () & MAXIMIZE_STATE;

    savedMaxState = w->state () & MAXIMIZE_STATE;
}

bool
Tiler::configure (CompWindow           *w,
		  TileScreen::TileType type)
{
    XWindowChanges xwc;
    unsigned int   mask = CWX | CWY | CWWidth | CWHeight;
    unsigned int xwidth, xheight;

    TILE_WINDOW (w);

    xwc.x = current.x ();
    xwc.y = current.y ();
    xwc.width = current.width ();
    xwc.height = current.height ();

    if (type == TileScreen::Restore)
    {
	if (maximized)
	    w->maximize (savedMaxState); // ???
    }
    else
	w->maximize (0);

    xwidth = xwc.width;
    xheight = xwc.height;

    if (xwidth == w->serverWidth ())
	mask &= ~CWWidth;

    if (xheight == w->serverHeight ())
	mask &= ~CWHeight;

    if (w->mapNum () && (mask & (CWWidth | CWHeight)))
	w->sendSyncRequest ();

    w->configureXWindow (mask, &xwc);
    tw->needConfigure = false;

    return true;
}

void
TileScreen::restoreTile (CompWindowExtents &border,
			 XRectangle &workArea,
			 int        count)
{
    foreach (CompWindow *w, screen->windows ())
    {
	TILE_WINDOW (w);
	if (tw->tiler)
	{
	    tw->placeWin (tw->tiler->saved.x (), tw->tiler->saved.y (),
		          tw->tiler->saved.width (), tw->tiler->saved.height ());
	    tilers.remove (tw->tiler);
	    delete tw->tiler;
	}
    }
}


void
TileScreen::squareTile (CompWindowExtents &border,
			XRectangle &workArea,
			int        count)
{
    int countX = ceil (sqrt (count));
    int countY = ceil ((float)count / countX);
    int currentX = workArea.x;
    int currentY = workArea.y;
    int winWidth = workArea.width / countX;
    int winHeight = workArea.height / countY;
    int i = 0;

    foreach (CompWindow *w, screen->windows ())
    {
	TILE_WINDOW (w);

	if (!tw->tiler)
	    continue;
	/* Put the window at the current slot */
	tw->placeWin (currentX, currentY,
		      winWidth,
		      winHeight);

	/* If we have reached countX ... */
	if (!(i % countX))
	{
	    /* Reset currentX back to the workArea start
	     * and move current Y
	     */
	    currentX = workArea.x;
	    currentY += winHeight;
	}
	/* Otherwise, add winWidth to currentX */
	else
	    currentX += winWidth;

	i++;
    }
}

void
TileScreen::horizontalTile (CompWindowExtents &border,
			    XRectangle &workArea,
			    int        count)
{
}

void
TileScreen::verticalTile (CompWindowExtents &border,
			  XRectangle &workArea,
			  int        count)
{
}

/* Adapated from scale.c
 * Copyright (c) 2007 Novell Inc.
 */

void
TileScreen::evenTile (CompWindowExtents &border,
		      XRectangle &workArea,
		      int        count)
{
}

/* Adapted from maximumize.c
 * Copyright (c) 2007 Kristian Lyngstol
 */

void
TileScreen::expandTile (CompWindowExtents &border,
			XRectangle &workArea,
			int        count)
{
}

/* Adapted from presentwindows.cpp
 * Copyright (c) 2007 Rivo Laks
 * Copyright (c) 2008 Lucas Murray
 */

void
TileScreen::organicTile (CompWindowExtents &border,
			 XRectangle &workArea,
			 int        count)
{
}

void
TileScreen::cascadeTile (CompWindowExtents &border,
			 XRectangle &workArea,
			 int        count)
{
}

bool
TileScreen::applyTiling (CompAction           *action,
			 CompAction::State    state,
			 CompOption::Vector   options,
			 TileScreen::TileType type)
{
    int count = 0;
    XRectangle        workArea;
    CompWindowExtents border;

    workArea = screen->workArea ();
    memset (&border, 0, sizeof (CompWindowExtents));

    foreach (CompWindow *w, screen->windows ())
    {
	TILE_WINDOW (w);
	/* Set the largest border size as some windows may be maximized */
	if (tw->is ())
	{
	    if (w->input ().left > border.left)
		border.left = w->input ().left;
	    if (w->input ().right > border.right)
		border.right = w->input ().right;
	    if (w->input ().top > border.top)
		border.top = w->input ().top;
	    if (w->input ().bottom > border.bottom)
		border.bottom = w->input ().bottom;

	    if (!tw->tiler)
	    {
		tw->tiler = new Tiler;
		tw->tiler->save (w);
	    }

	    count++;
	}
    }

    /* TODO: Should probably be replaced by a propper extension manager */

    switch (type)
    {
	case Restore:
	    restoreTile (border, workArea, count);
	    break;
	case Square:
	    squareTile (border, workArea, count);
	    break;
	case Even:
	    evenTile (border, workArea, count);
	    break;
	case Horizontal:
	    horizontalTile (border, workArea, count);
	    break;
	case Vertical:
	    verticalTile (border, workArea, count);
	    break;
	case Expand:
	    expandTile (border, workArea, count);
	    break;
	case Organic:
	    organicTile (border, workArea, count);
	    break;
	case Cascade:
	    cascadeTile (border, workArea, count);
	    break;
    }

    foreach (CompWindow *w, screen->windows ())
    {
	TILE_WINDOW (w);
	if (tw->tiler)
	{
	    tw->tiler->configure (w, type);
	}
    }

    return true;
}

#if 0
/* Applies tiling/restoring */
static bool
applyTiling (CompScreen *s)
{
    CompWindow *w;
    int        count = 0;

    TILE_SCREEN (s);

    if (ts->grabIndex)
	return false;

    for (w = s->windows; w; w = w->next)
    {
	if (isTileWindow (w))
	    count++;
    }

    ts->oneDuration = tileGetAnimationDuration (s->display) / MAX (count, 1);

    if (count > 1)
    {
	memset (&border, 0, sizeof (CompWindowExtents));
	/* first get the largest border of the windows on this
	   screen - some of the windows in our list might be 
	   maximized now and not be maximized later, so 
	   their border information may be inaccurate */
	for (w = s->windows; w; w = w->next)
	{
	    if (w->input.left > border.left)
		border.left = w->input.left;
	    if (w->input.right > border.right)
		border.right = w->input.right;
	    if (w->input.top > border.top)
		border.top = w->input.top;
	    if (w->input.bottom > border.bottom)
		border.bottom = w->input.bottom;
	}

	workArea = s->workArea;

	switch (ts->tileType)
	{
	case TileToggleTypeTile:

	    break;
	case TileToggleTypeLeft:
    	    height = workArea.height / (count - 1);
	    occupancy = tileGetTileLeftOccupancy (s->display);
	    break;
	case TileToggleTypeTileVertically:
    	    winWidth = workArea.width / count;
	    winHeight = workArea.height;
	    y = workArea.y;
	    break;
	case TileToggleTypeTileHorizontally:
	    winWidth = workArea.width;
	    winHeight = workArea.height / count;
	    x = workArea.x;
	    break;
	case TileToggleTypeCascade:
	    delta = tileGetTileDelta (s->display);
	    currentX = workArea.x;
	    currentY = workArea.y;
	    winHeight = workArea.height - delta * (count - 1);
	    winWidth = workArea.width - delta * (count - 1);
	    break;
	default:
    	    break;
	}

	for (w = s->windows; w; w = w->next)
	{
	    if (!isTileWindow (w))
		continue;

    	    TILE_WINDOW (w);

	    if (!tw->savedValid)
		saveCoords (w);

	    switch (ts->tileType)
	    {
	    case TileToggleTypeTile:
    		placeWin(w,
			 currentX + border.left, currentY + border.top,
			 winWidth - (border.left + border.right),
			 winHeight - (border.top + border.bottom));
		tw->isTiled = true;
		break;
	    case TileToggleTypeLeft:
	      	if (first)
		{
	    	    x = workArea.x;
    		    y = workArea.y;
		    winWidth = workArea.width * occupancy / 100;
		    winHeight = workArea.height;
		    first = false;
		}
		else
		{
		    x = workArea.x + (workArea.width * occupancy / 100);
		    y = workArea.y + (i * height);
		    winWidth = (workArea.width * (100 - occupancy) / 100);
		    winHeight = height;
		}

		placeWin(w,
			 x + border.left, y + border.top,
			 winWidth - (border.left + border.right),
			 winHeight - (border.top + border.bottom));
		tw->isTiled = true;
		break;
	    case TileToggleTypeTileVertically:
		x = workArea.x + (winWidth * i);
		placeWin(w,
			 x + border.left, y + border.top,
			 winWidth - (border.left + border.right),
			 winHeight - (border.top + border.bottom));
		tw->isTiled = true;
		break;
	    case TileToggleTypeTileHorizontally:
		y = workArea.y + (winHeight * i);
		placeWin (w, x + border.left, y + border.top,
			  winWidth - (border.left + border.right),
			  winHeight - (border.top + border.bottom));
		tw->isTiled = true;
		break;
	    case TileToggleTypeCascade:
		placeWin (w,
			  currentX + border.left, currentY + border.top,
			  winWidth - (border.left + border.right),
			  winHeight - (border.top + border.bottom));
		tw->isTiled = true;
		break;
	    case -1:
		if (tw->isTiled)
		{
		    placeWin (w,
			      tw->savedCoords.x, tw->savedCoords.y,
			      tw->savedCoords.width, tw->savedCoords.height);
		    tw->savedValid = false;
		    tw->isTiled = false;;	}
		break;
	    default:
		break;
	    }

	    i++;
	    tw->animationNum = i;

	    switch (ts->tileType)
	    {
	    case TileToggleTypeTile:
		if (!(i % countX))
		{
		    currentX = workArea.x;
		    currentY += winHeight;
		}
		else
		    currentX += winWidth;
		break;
	    case TileToggleTypeCascade:
		currentX += delta;
		currentY += delta;
	    default:
		break;
	    }
	}

	if (!ts->grabIndex)
	    ts->grabIndex = pushScreenGrab (s, s->invisibleCursor, "tile");

	ts->msResizing = 0;
    }

    return true;
}
#endif

TileScreen::TileScreen (CompScreen *screen) :
    PrivateHandler <TileScreen, CompScreen> (screen),
    TileOptions (tileVTable->getMetadata ()),
    cScreen (CompositeScreen::get (screen)),
    gScreen (GLScreen::get (screen)),
    state (Normal),
    time (0),
    nextPosition (0, 0, 0, 0)
{
    ScreenInterface::setHandler (screen);
    CompositeScreenInterface::setHandler (cScreen, false);
    GLScreenInterface::setHandler (gScreen, false);

#define tileInitiate(opt, type)					       \
    optionSet##opt##Initiate (boost::bind (&TileScreen::applyTiling, this,    \
					    _1, _2, _3, type))

    tileInitiate (RestoreKey, Restore);
    tileInitiate (SquareKey, Square);
    tileInitiate (EvenKey, Restore);
    tileInitiate (HorizontalKey, Restore);
    tileInitiate (VerticalKey, Restore);
    tileInitiate (ExpandKey, Restore);
    tileInitiate (OrganicKey, Restore);
    tileInitiate (CascadeKey, Restore);

    tileInitiate (RestoreEdge, Restore);
    tileInitiate (SquareEdge, Square);
    tileInitiate (EvenEdge, Restore);
    tileInitiate (HorizontalEdge, Restore);
    tileInitiate (VerticalEdge, Restore);
    tileInitiate (ExpandEdge, Restore);
    tileInitiate (OrganicEdge, Restore);
    tileInitiate (CascadeEdge, Restore);

#undef tileInitiate
}

TileWindow::TileWindow (CompWindow *window) :
    PrivateHandler <TileWindow, CompWindow> (window),
    window (window),
    gWindow (GLWindow::get (window)),
    time (0),
    alreadyResized (false),
    needConfigure (false),
    tiler (NULL)
{
    WindowInterface::setHandler (window, false);
    GLWindowInterface::setHandler (gWindow, false);

    for (int i = 0; i < 3; i++)
	outlineColor[i] = 0;

}

TileWindow::~TileWindow ()
{
    if (tiler)
    {
	TILE_SCREEN (screen);
	ts->tilers.remove (tiler);
	delete tiler;
    }
}

bool
TilePluginVTable::init ()
{
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION) ||
	!CompPlugin::checkPluginABI ("composite", COMPIZ_COMPOSITE_ABI) ||
	!CompPlugin::checkPluginABI ("opengl", COMPIZ_OPENGL_ABI))
	return false; /* should set 'animate' vars'*/

    return true;
}