summaryrefslogtreecommitdiff
path: root/shelf.c
blob: e2059aa6e7e3a1e4c4628e7b52dec0ba32c6740e (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
/*
 * Compiz Fusion Shelf plugin
 *
 * Copyright (C) 2007  Canonical Ltd.
 *
 * 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.
 *
 * Author(s): 
 * Kristian LyngstĂžl <kristian@bohemians.org>
 * Danny Baumann <maniac@opencompositing.org>
 *
 * Description:
 *
 * This plugin visually resizes a window to allow otherwise obtrusive
 * windows to be visible in a monitor-fashion. Use case: Anything with
 * progress bars, notification programs, etc.
 *
 * Todo: 
 *  - Check for XShape events
 *  - Handle input in a sane way
 *  - Correct damage handeling
 *  - Mouse-over?
 */

#include <compiz-core.h>
#include <X11/extensions/shape.h>
#include <X11/cursorfont.h>
#include <math.h>
#include "shelf_options.h"

typedef struct _ShelfedWindowInfo {
    CompWindow                *w;
    struct _ShelfedWindowInfo *next;

    Window     ipw;

    XRectangle *inputRects;
    int        nInputRects;
    int        inputRectOrdering;

    XRectangle *frameInputRects;
    int        frameNInputRects;
    int        frameInputRectOrdering;
} ShelfedWindowInfo;

typedef struct { 
    float scale;
    float targetScale;
    float steps;

    ShelfedWindowInfo *info;
} ShelfWindow;

typedef struct {
    int windowPrivateIndex;

    int grabIndex;
    Window grabbedWindow;

    Cursor moveCursor;

    Bool noLastPointer;
    int  lastPointerX;
    int  lastPointerY;

    ShelfedWindowInfo *shelfedWindows;

    PaintWindowProc        paintWindow;    
    DamageWindowRectProc   damageWindowRect;
    PreparePaintScreenProc preparePaintScreen;
    WindowMoveNotifyProc   windowMoveNotify;
} ShelfScreen;

typedef struct {
    int screenPrivateIndex;

    HandleEventProc handleEvent;
} ShelfDisplay;

static int displayPrivateIndex;

#define GET_SHELF_DISPLAY(d) \
    ((ShelfDisplay *) (d)->base.privates[displayPrivateIndex].ptr)
#define SHELF_DISPLAY(d) \
    ShelfDisplay *sd = GET_SHELF_DISPLAY (d)
#define GET_SHELF_SCREEN(s, sd) \
    ((ShelfScreen *) (s)->base.privates[(sd)->screenPrivateIndex].ptr)
#define SHELF_SCREEN(s) \
    ShelfScreen *ss = GET_SHELF_SCREEN (s, GET_SHELF_DISPLAY (s->display))
#define GET_SHELF_WINDOW(w, ss) \
    ((ShelfWindow *) (w)->base.privates[(ss)->windowPrivateIndex].ptr)
#define SHELF_WINDOW(w) \
    ShelfWindow *sw = GET_SHELF_WINDOW  (w,                  \
		      GET_SHELF_SCREEN  (w->screen,          \
		      GET_SHELF_DISPLAY (w->screen->display)))

#define SHELF_MIN_SIZE 50.0f // Minimum pixelsize a window can be scaled to

/* Checks if w is a ipw and returns the real window */
static CompWindow *
shelfGetRealWindow (CompWindow *w)
{
    ShelfedWindowInfo *run;

    SHELF_SCREEN (w->screen);

    for (run = ss->shelfedWindows; run; run = run->next)
    {
	if (w->id == run->ipw)
	    return run->w;
    }

    return NULL;
}

static void
shelfSaveInputShape (CompWindow *w,
		     XRectangle **retRects,
		     int        *retCount,
		     int        *retOrdering)
{
    XRectangle *rects;
    int        count = 0, ordering;
    Display    *dpy = w->screen->display->display;

    rects = XShapeGetRectangles (dpy, w->id, ShapeInput, &count, &ordering);

    /* check if the returned shape exactly matches the window shape -
       if that is true, the window currently has no set input shape */
    if ((count == 1) &&
	(rects[0].x == -w->serverBorderWidth) &&
	(rects[0].y == -w->serverBorderWidth) &&
	(rects[0].width == (w->serverWidth + w->serverBorderWidth)) &&
	(rects[0].height == (w->serverHeight + w->serverBorderWidth)))
    {
	count = 0;
    }
    
    *retRects    = rects;
    *retCount    = count;
    *retOrdering = ordering;
}

/* Shape the input of the window when scaled.
 * Since the IPW will be dealing with the input, removing input
 * from the window entirely is a perfectly good solution. */
static void
shelfShapeInput (CompWindow *w)
{
    CompWindow *fw;
    Display    *dpy = w->screen->display->display;

    SHELF_WINDOW (w);

    /* save old shape */
    shelfSaveInputShape (w, &sw->info->inputRects,
			 &sw->info->nInputRects, &sw->info->inputRectOrdering);

    fw = findWindowAtDisplay (w->screen->display, w->frame);
    if (fw)
    {
	shelfSaveInputShape(fw, &sw->info->frameInputRects,
			    &sw->info->frameNInputRects,
			    &sw->info->frameInputRectOrdering);
    }
    else
    {
	sw->info->frameInputRects        = NULL;
	sw->info->frameNInputRects       = -1;
	sw->info->frameInputRectOrdering = 0;
    }

    /* clear shape */
    XShapeSelectInput (dpy, w->id, NoEventMask);
    XShapeCombineRectangles  (dpy, w->id, ShapeInput, 0, 0,
			      NULL, 0, ShapeSet, 0);
    
    if (w->frame)
	XShapeCombineRectangles  (dpy, w->frame, ShapeInput, 0, 0,
				  NULL, 0, ShapeSet, 0);

    XShapeSelectInput (dpy, w->id, ShapeNotify);
}

static void
shelfUnshapeInput (CompWindow *w)
{
    Display *dpy = w->screen->display->display;

    SHELF_WINDOW (w);

    if (sw->info->nInputRects)
    {
	XShapeCombineRectangles (dpy, w->id, ShapeInput, 0, 0,
				 sw->info->inputRects, sw->info->nInputRects,
				 ShapeSet, sw->info->inputRectOrdering);
    }
    else
    {
	XShapeCombineMask (dpy, w->id, ShapeInput, 0, 0, None, ShapeSet);
    }

    if (sw->info->frameNInputRects >= 0)
    {
	if (sw->info->frameInputRects)
	{
	    XShapeCombineRectangles (dpy, w->frame, ShapeInput, 0, 0,
				     sw->info->frameInputRects,
				     sw->info->frameNInputRects,
				     ShapeSet,
				     sw->info->frameInputRectOrdering);
	}
	else
	{
	    XShapeCombineMask (dpy, w->frame, ShapeInput, 0, 0, None, ShapeSet);
	}
    }
}

static void
shelfPreparePaintScreen (CompScreen *s,
			 int	    msSinceLastPaint)
{
    CompWindow *w;
    float      steps;

    SHELF_SCREEN (s);

    steps =  (float)msSinceLastPaint / (float)shelfGetAnimtime(s->display);

    if (steps < 0.005)
	steps = 0.005;

    /* FIXME: should only loop over all windows if at least one animation
       is running */
    for (w = s->windows; w; w = w->next)
	GET_SHELF_WINDOW (w, ss)->steps = steps;
    
    UNWRAP (ss, s, preparePaintScreen);
    (*s->preparePaintScreen) (s, msSinceLastPaint);
    WRAP (ss, s, preparePaintScreen, shelfPreparePaintScreen);
}

static void
shelfAddWindowToList (ShelfedWindowInfo *info)
{
    CompScreen        *s = info->w->screen;
    ShelfedWindowInfo *run;

    SHELF_SCREEN (s);

    run = ss->shelfedWindows;
    if (!run)
	ss->shelfedWindows = info;
    else
    {
	for (; run->next; run = run->next);
	run->next = info;
    }
}

static void
shelfRemoveWindowFromList (ShelfedWindowInfo *info)
{
    CompScreen        *s = info->w->screen;
    ShelfedWindowInfo *run;

    SHELF_SCREEN (s);

    if (!ss->shelfedWindows)
	return;

    if (ss->shelfedWindows == info)
	ss->shelfedWindows = info->next;
    else
    {
	for (run = ss->shelfedWindows; run->next; run = run->next)
	{
	    if (run->next == info)
	    {
		run->next = info->next;
		break;
	    }
	}
    }
}

/* Adjust size and location of the input prevention window
 */
static void 
shelfAdjustIPW (CompWindow *w)
{
    XWindowChanges xwc;
    float          width, height;

    SHELF_WINDOW (w);

    if (!sw->info || !sw->info->ipw)
	return;

    width  = w->width + 2 * w->attrib.border_width +
	     w->input.left + w->input.right + 2.0f;
    width  *= sw->targetScale;
    height = w->height + 2 * w->attrib.border_width +
	     w->input.top + w->input.bottom + 2.0f;
    height *= sw->targetScale;

    xwc.x          = w->attrib.x - w->input.left;
    xwc.y          = w->attrib.y - w->input.top;
    xwc.width      = (int) width;
    xwc.height     = (int) height;
    xwc.stack_mode = Above;
    xwc.sibling    = w->id;

    XConfigureWindow (w->screen->display->display, sw->info->ipw,
		      CWSibling | CWStackMode | CWX | CWY | CWWidth | CWHeight,
		      &xwc);

    XMapWindow (w->screen->display->display, sw->info->ipw);
}

static void
shelfAdjustIPWStacking (CompScreen *s)
{
    ShelfedWindowInfo *run;

    SHELF_SCREEN (s);

    for (run = ss->shelfedWindows; run; run = run->next)
    {
	if (!run->w->next || run->w->next->id != run->ipw)
	    shelfAdjustIPW (run->w);
    }
}

/* Create an input prevention window */
static void
shelfCreateIPW (CompWindow *w)
{
    Window               ipw;
    XSetWindowAttributes attrib;

    SHELF_WINDOW (w);

    if (!sw->info || sw->info->ipw)
	return;

    attrib.override_redirect = TRUE;
    ipw = XCreateWindow (w->screen->display->display,
			 w->screen->root,
			 w->serverX - w->input.left,
			 w->serverY - w->input.top,
			 w->serverWidth + w->input.left + w->input.right,
			 w->serverHeight + w->input.top + w->input.bottom,
			 0, CopyFromParent, InputOnly, CopyFromParent,
			 CWOverrideRedirect, &attrib);
    
    sw->info->ipw = ipw;
}

static Bool
shelfHandleShelfInfo (CompWindow *w)
{
    SHELF_WINDOW (w);

    if (sw->targetScale == 1.0f && sw->info)
    {
	if (sw->info->ipw)
	    XDestroyWindow (w->screen->display->display, sw->info->ipw);

	shelfUnshapeInput (w);
	shelfRemoveWindowFromList (sw->info);

	free (sw->info);
	sw->info = NULL;

	return FALSE;
    }
    else if (sw->targetScale != 1.0f && !sw->info)
    {
	sw->info = calloc (1, sizeof (ShelfedWindowInfo));
	if (!sw->info)
	    return FALSE;

	sw->info->w = w;
	shelfShapeInput (w);
	shelfCreateIPW (w);
	shelfAddWindowToList (sw->info);
    }

    return TRUE;
}

/* Sets the scale level and adjust the shape */
static void
shelfScaleWindow (CompWindow *w,
		  float      scale)
{
    SHELF_WINDOW (w);

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

    sw->targetScale = MIN (scale, 1.0f);

    if ((float) w->width * sw->targetScale < SHELF_MIN_SIZE)
	sw->targetScale = SHELF_MIN_SIZE / (float) w->width;

    if (!shelfHandleShelfInfo (w))
	return;

    shelfAdjustIPW (w);

    damageScreen (w->screen);
}

/* Binding for toggle mode. 
 * Toggles through three preset scale levels, 
 * currently hard coded to 1.0f (no scale), 0.5f and 0.25f.
 */
static Bool
shelfTrigger (CompDisplay     *d,
	      CompAction      *action,
	      CompActionState state,
	      CompOption      *option,
	      int             nOption)
{
    CompWindow *w = findWindowAtDisplay (d, d->activeWindow);
    if (!w)
	return TRUE;

    SHELF_WINDOW (w);

    if (sw->targetScale > 0.5f)
	shelfScaleWindow (w, 0.5f);
    else if (sw->targetScale <= 0.5f && sw->targetScale > 0.25)
	shelfScaleWindow (w, 0.25f);
    else 
	shelfScaleWindow (w, 1.0f);

    return TRUE;
}

/* Returns the ratio to multiply by to get a window that's 1/ration the
 * size of the screen.
 */
static inline float
shelfRat (CompWindow *w,
	  float      ratio)
{
    float winHeight    = (float) w->height;
    float winWidth     = (float) w->width;
    float screenHeight = (float) w->screen->height;
    float screenWidth  = (float) w->screen->width;
    float ret;

    if (winHeight / screenHeight < winWidth / screenWidth)
	ret = screenWidth / winWidth;
    else
	ret = screenHeight / winHeight;

    return ret / ratio;
}

static Bool
shelfTriggerScreen (CompDisplay *d,
		    CompAction *action,
		    CompActionState state,
		    CompOption      *option,
		    int             nOption)
{
    CompWindow *w = findWindowAtDisplay (d, d->activeWindow);
    if (!w)
	return TRUE;

    SHELF_WINDOW (w);

    /* FIXME: better should save calculated ratio and reuse it */
    if (sw->targetScale > shelfRat (w, 2.0f))
	shelfScaleWindow (w, shelfRat (w, 2.0f));
    else if (sw->targetScale <= shelfRat (w, 2.0f) && 
	     sw->targetScale > shelfRat (w, 3.0f))
	shelfScaleWindow (w, shelfRat (w, 3.0f));
    else if (sw->targetScale <= shelfRat (w, 3.0f) && 
	     sw->targetScale > shelfRat (w, 6.0f))
	shelfScaleWindow (w, shelfRat (w, 6.0f));
    else 
	shelfScaleWindow (w, 1.0f);

    return TRUE;
}

/* shelfInc and shelfDec are matcing functions and bindings;
 * They increase and decrease the scale factor by 'interval'.
 */
static Bool
shelfInc (CompDisplay     *d,
	  CompAction      *action,
	  CompActionState state,
	  CompOption      *option,
	  int             nOption)
{
    CompWindow *w = findWindowAtDisplay (d, d->activeWindow);
    if (!w)
	return TRUE;

    SHELF_WINDOW (w);

    shelfScaleWindow (w, sw->targetScale / shelfGetInterval (d));

    return TRUE;
}

static Bool
shelfDec (CompDisplay     *d,
	  CompAction      *action,
	  CompActionState state,
	  CompOption      *option,
	  int             nOption)
{
    CompWindow *w = findWindowAtDisplay (d, d->activeWindow);
    if (!w)
	return TRUE;

    SHELF_WINDOW (w);

    shelfScaleWindow (w, sw->targetScale * shelfGetInterval (d));

    return TRUE;
}

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

    SHELF_SCREEN (s);

    if (!otherScreenGrabExist (s, "shelf", 0))
    {
	activateWindow (w);
	ss->grabbedWindow = w->id;
	ss->grabIndex = pushScreenGrab (s, ss->moveCursor, "shelf");
    }
}

static void
handleMotionEvent (CompDisplay *d, XEvent *event)
{
    CompScreen *s;
    CompWindow *w;
    int        x,y;

    s = findScreenAtDisplay (d, event->xmotion.root);
    if (!s)
	return;

    SHELF_SCREEN (s);

    if (!ss->grabIndex)
	return;

    w = findWindowAtScreen (s, ss->grabbedWindow);
    if (!w)
	return;

    x = event->xmotion.x_root;
    y = event->xmotion.y_root;

    if (ss->noLastPointer)
    {
	ss->noLastPointer = FALSE;
	ss->lastPointerX = x;
	ss->lastPointerY = y;
	return;
    }

    moveWindow (w,
		-ss->lastPointerX + x,
		-ss->lastPointerY + y,
		TRUE, FALSE);
    syncWindowPosition (w);

    ss->lastPointerX = event->xmotion.x_root;
    ss->lastPointerY = event->xmotion.y_root;
}

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

    SHELF_SCREEN (s);

    ss->grabbedWindow = None;
    if (ss->grabIndex)
    {
	ss->noLastPointer = TRUE;
	ss->lastPointerX  = 0;
	ss->lastPointerY  = 0;

	moveInputFocusToWindow (w);
	removeScreenGrab (s, ss->grabIndex, NULL);
	ss->grabIndex = 0;
    }
}

static CompWindow *
shelfFindRealWindowID (CompDisplay *d,
		       Window      wid)
{
    CompWindow *orig;

    orig = findWindowAtDisplay (d, wid);
    if (!orig)
	return NULL;

    return shelfGetRealWindow (orig);
}

static void
shelfHandleEvent (CompDisplay *d,
		  XEvent      *event)
{
    CompWindow *w, *oldPrev, *oldNext;
    CompScreen *s;

    SHELF_DISPLAY (d);

    switch (event->type)
    {
	case ButtonPress:
	    w = shelfFindRealWindowID (d, event->xbutton.window);
	    if (w)
		handleButtonPress (w);
	    break;
	case ButtonRelease:
	    s = findScreenAtDisplay (d, event->xbutton.root);
	    if (s)
	    {
		SHELF_SCREEN (s);
		w = findWindowAtDisplay (d, ss->grabbedWindow);
		if (w)
		    handleButtonRelease (w);
	    }
	    break;
	case MotionNotify:
	    handleMotionEvent (d, event);
	    break;
	case ConfigureNotify:
	    w = findWindowAtDisplay (d, event->xconfigure.window);
	    if (w)
	    {
		oldPrev = w->prev;
		oldNext = w->next;
	    }
	    break;
    }

    UNWRAP (sd, d, handleEvent);
    (*d->handleEvent) (d, event);
    WRAP (sd, d, handleEvent, shelfHandleEvent);

    switch (event->type)
    {
	case ConfigureNotify:
	    if (w) /* already assigned above */
	    {
		if (w->prev != oldPrev || w->next != oldNext)
		{
		    /* restacking occured, ensure ipw stacking */
		    shelfAdjustIPWStacking (w->screen);
		}
	    }
	    break;
    }
}

/* The window was damaged, adjust the damage to fit the actual area we
 * care about.
 *
 * FIXME:
 * This should not cause total screen damage, but only adjust the rect
 * correctly.
 */
static Bool
shelfDamageWindowRect (CompWindow *w,
                       Bool       initial,
                       BoxPtr     rect)
{
    Bool status = FALSE;
    SHELF_SCREEN (w->screen);
    SHELF_WINDOW (w);

    if (sw->scale != 1.0f)
    {
	damageScreen (w->screen);
	status = TRUE;
    }

    UNWRAP (ss, w->screen, damageWindowRect);
    status |= (*w->screen->damageWindowRect) (w, initial, rect);
    WRAP (ss, w->screen, damageWindowRect, shelfDamageWindowRect);

    return status;
}

/* Scale the window if it is supposed to be scaled.
 * Translate into place.
 *
 * FIXME: Merge the two translations.
 */
static Bool
shelfPaintWindow (CompWindow		    *w,
		  const WindowPaintAttrib   *attrib,
		  const CompTransform	    *transform,
		  Region		    region,
		  unsigned int		    mask)
{
    Bool       status;
    CompScreen *s = w->screen;

    SHELF_SCREEN (s);
    SHELF_WINDOW (w);

    if (sw->targetScale != sw->scale && sw->steps)
    {
	sw->scale += (float) sw->steps * (sw->targetScale - sw->scale);
	if (fabsf (sw->targetScale - sw->scale) < 0.005)
	    sw->scale = sw->targetScale;
    }

    if (sw->scale != 1.0f)
    {
	CompTransform mTransform = *transform;
	int           xOrigin, yOrigin;

	xOrigin = w->attrib.x - w->input.left;
	yOrigin = w->attrib.y - w->input.top;
	
	matrixTranslate (&mTransform, xOrigin, yOrigin,  0);
	matrixScale (&mTransform, sw->scale, sw->scale, 0);
	matrixTranslate (&mTransform, -xOrigin, -yOrigin,  0);
	
	mask |= PAINT_WINDOW_TRANSFORMED_MASK;

	/* FIXME: should better use DonePaintScreen for that */
	if (sw->scale != sw->targetScale)
	    addWindowDamage (w);	    

	UNWRAP (ss, s, paintWindow);
	status = (*s->paintWindow) (w, attrib, &mTransform, region, mask);
	WRAP (ss, s, paintWindow, shelfPaintWindow);
    }
    else
    {
	UNWRAP (ss, s, paintWindow);
	status = (*s->paintWindow) (w, attrib, transform, region, mask);
	WRAP (ss, s, paintWindow, shelfPaintWindow);
    }

    return status;
}

static void
shelfWindowMoveNotify (CompWindow *w,
		       int        dx,
		       int        dy,
		       Bool       immediate)
{
    SHELF_SCREEN (w->screen);
    SHELF_WINDOW (w);

    if (sw->targetScale != 1.00f)
	shelfAdjustIPW (w);

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

/* Configuration, initialization, boring stuff. --------------------- */
static Bool
shelfInitScreen (CompPlugin *p,
		 CompScreen *s)
{
    ShelfScreen *ss;

    SHELF_DISPLAY (s->display);

    ss = malloc (sizeof (ShelfScreen));
    if (!ss)
	return FALSE; // fixme: error message.

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

    ss->moveCursor = XCreateFontCursor (s->display->display, XC_fleur);

    ss->lastPointerX  = 0;
    ss->lastPointerY  = 0;
    ss->noLastPointer = TRUE;

    ss->grabbedWindow  = None;
    ss->shelfedWindows = NULL;

    WRAP (ss, s, preparePaintScreen, shelfPreparePaintScreen);
    WRAP (ss, s, paintWindow, shelfPaintWindow); 
    WRAP (ss, s, damageWindowRect, shelfDamageWindowRect);
    WRAP (ss, s, windowMoveNotify, shelfWindowMoveNotify);

    s->base.privates[sd->screenPrivateIndex].ptr = ss;

    return TRUE; 
}

static void
shelfFiniScreen (CompPlugin *p,
		 CompScreen *s)
{
    SHELF_SCREEN (s);

    UNWRAP (ss, s, preparePaintScreen);
    UNWRAP (ss, s, paintWindow);
    UNWRAP (ss, s, damageWindowRect);
    UNWRAP (ss, s, windowMoveNotify);

    freeWindowPrivateIndex (s, ss->windowPrivateIndex);

    if (ss->moveCursor)
	XFreeCursor (s->display->display, ss->moveCursor);

    free (ss);
}

static Bool
shelfInitDisplay (CompPlugin  *p,
		  CompDisplay *d)
{
    ShelfDisplay *sd;

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

    if (!d->shapeExtension)
    {
	compLogMessage (d, "shelf", CompLogLevelError,
			"No Shape extension found. Shelfing not possible.\n");
	return FALSE;
    }

    sd = malloc (sizeof (ShelfDisplay));
    if (!sd)
	return FALSE;

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

    shelfSetTriggerKeyInitiate (d, shelfTrigger);
    shelfSetTriggerscreenKeyInitiate (d, shelfTriggerScreen);
    shelfSetIncButtonInitiate (d, shelfInc);
    shelfSetDecButtonInitiate (d, shelfDec);

    WRAP (sd, d, handleEvent, shelfHandleEvent);
    
    d->base.privates[displayPrivateIndex].ptr = sd;

    return TRUE;
}

static void
shelfFiniDisplay (CompPlugin  *p,
		  CompDisplay *d)
{
    SHELF_DISPLAY (d);

    freeScreenPrivateIndex (d, sd->screenPrivateIndex);

    UNWRAP (sd, d, handleEvent);

    free (sd);
}

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

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

    return TRUE;
}

static Bool
shelfInitWindow (CompPlugin *p,
		 CompWindow *w)
{
    ShelfWindow *sw;

    SHELF_SCREEN (w->screen);

    sw = malloc (sizeof (ShelfWindow));
    if (!sw)
	return FALSE;

    sw->scale       = 1.0f;
    sw->targetScale = 1.0f;

    sw->info = NULL;

    w->base.privates[ss->windowPrivateIndex].ptr = sw;

    return TRUE;
}

static void
shelfFiniWindow (CompPlugin *p,
		 CompWindow *w)
{
    SHELF_WINDOW (w);

    if (sw->info)
    {
	sw->targetScale = 1.0f;
	/* implicitly frees sw->info */
	shelfHandleShelfInfo (w);
    }

    free (sw);
}

static CompBool
shelfInitObject (CompPlugin *p,
		 CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	    (InitPluginObjectProc) 0, /* InitCore */
	    (InitPluginObjectProc) shelfInitDisplay,
	    (InitPluginObjectProc) shelfInitScreen, 
	    (InitPluginObjectProc) shelfInitWindow
    };

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

static void
shelfFiniObject (CompPlugin *p,
		 CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) 0, /* InitCore */
	(FiniPluginObjectProc) shelfFiniDisplay,
	(FiniPluginObjectProc) shelfFiniScreen, 
	(FiniPluginObjectProc) shelfFiniWindow
    };

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

CompPluginVTable shelfVTable = {
    "shelf",
    0,
    shelfInit,
    shelfFini,
    shelfInitObject,
    shelfFiniObject,
    0,
    0
};

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