summaryrefslogtreecommitdiff
path: root/shelf.c
blob: 8b606beafd0c2d00b1eee70495e3f8ff809b4906 (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
/*
 * 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>
 *
 * 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
 *  - Handle WindowMove
 *  - Mouse-over?
 */

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

typedef struct { 
    float scale;
    float targetScale;
    float steps;
    Window ipw;
} shelfWindow;

typedef struct {
    PaintWindowProc paintWindow;
    DamageWindowRectProc damageWindowRect;
    PreparePaintScreenProc preparePaintScreen;
    WindowMoveNotifyProc windowMoveNotify;
    int windowPrivateIndex;
} shelfScreen;

typedef struct {
    HandleEventProc handleEvent;
} shelfDisplay;

static shelfDisplay sdtmp;
static shelfDisplay *sd = &sdtmp;
static int displayPrivateIndex;
static int screenPrivateIndex;

#define SHELF_SCREEN(s) \
    shelfScreen *ss = s->base.privates[screenPrivateIndex].ptr;
#define SHELF_WINDOW(w) \
    shelfWindow *sw = w->base.privates[ss->windowPrivateIndex].ptr;

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

/* 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)
{
    XRectangle rect;
    rect.x = 0;
    rect.y = 0;
    rect.width = 0;
    rect.height = 0;

    XShapeSelectInput (w->screen->display->display, w->id, NoEventMask);
    XShapeCombineRectangles  (w->screen->display->display, w->id, 
			      ShapeInput, 0, 0, &rect, 1,  ShapeSet, 0);
    
    if (w->frame)
	XShapeCombineRectangles  (w->screen->display->display, w->frame, 
				  ShapeInput, 0, 0, &rect, 1,  ShapeSet, 0);
    XShapeSelectInput (w->screen->display->display, w->id, ShapeNotify);
}

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;

    for (w = s->windows; w; w = w->next)
	((shelfWindow *)w->base.privates[ss->windowPrivateIndex].ptr)->steps
	    = steps;
    
    UNWRAP (ss, s, preparePaintScreen);
    (*s->preparePaintScreen) (s, msSinceLastPaint);
    WRAP (ss, s, preparePaintScreen, shelfPreparePaintScreen);
}

/* Adjust size and location of the input prevention window
 */
static void 
shelfAdjustIPW (CompWindow *w)
{
    SHELF_SCREEN (w->screen);
    SHELF_WINDOW (w);
    XWindowChanges xwc;

    if (!sw->ipw)
	return;
    if (sw->targetScale == 1.0f)
    {
	XDestroyWindow (w->screen->display->display, sw->ipw);
	sw->ipw = None;
	return;
    }
    xwc.x = w->attrib.x - w->input.left;
    xwc.y = w->attrib.y - w->input.top;
    xwc.width = (int) ((float) (w->width + 2 + w->attrib.border_width +
				w->input.left + w->input.right) * sw->targetScale);
    xwc.height = (int) ((float) (w->height + 2 + w->attrib.border_width + w->input.top +
				 w->input.bottom) * sw->targetScale);

    xwc.stack_mode = Above;
    xwc.sibling = w->id;
    XConfigureWindow (w->screen->display->display, sw->ipw,
		      CWSibling | CWStackMode | CWX | CWY |
		      CWWidth | CWHeight, &xwc);
    XMapWindow (w->screen->display->display, sw->ipw);
    return;

}

/* Create an input prevention window */
static void
shelfMkIPW (CompWindow *w)
{
    SHELF_SCREEN (w->screen);
    SHELF_WINDOW (w);

    if (sw->ipw)
	return;
    XSetWindowAttributes attrib;
    attrib.override_redirect = TRUE;
    sw->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);
}

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

    if (w->wmType & (CompWindowTypeDesktopMask | CompWindowTypeDockMask))
	return;
    sw->targetScale = scale;
    if (sw->targetScale > 1.0f)
	sw->targetScale = 1.0f;
    else if ((float)w->width * sw->targetScale < SHELF_MIN_SIZE )
	sw->targetScale = SHELF_MIN_SIZE / (float)w->width;
    shelfShapeInput (w);
    shelfMkIPW (w);
    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_SCREEN (w->screen);
    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;
}

/* 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_SCREEN (w->screen);
    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_SCREEN (w->screen);
    SHELF_WINDOW (w);
    shelfScaleWindow (w, sw->targetScale * shelfGetInterval (d));
    return TRUE;
}
static void
shelfHandleEvent (CompDisplay *d, XEvent *event)
{
    SHELF_SCREEN (d->screens);
    CompWindow *w;
    switch (event->type)
    {
	case ButtonPress:
	    for (w = d->screens->windows; w; w = w->next)
	    {
		SHELF_WINDOW (w);
		if (event->xbutton.window == sw->ipw)
		{
		    moveInputFocusToWindow (w);
		}
	    }
	    break;
    }
    UNWRAP (sd, d, handleEvent);
    (*d->handleEvent) (d, event);
    WRAP (sd, d, handleEvent, shelfHandleEvent);
}

/* 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;
	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;
}

/* Checks if w is a ipw and returns the real window */
static CompWindow *
shelfGetRealWindow (CompWindow *w)
{
    SHELF_SCREEN (w->screen);
    CompWindow *rw;
    for (rw = w->screen->windows; rw; rw = rw->next)
    {
	SHELF_WINDOW (rw);
	if (sw->ipw == w->id)
	    return rw;
    }
    return NULL;
}

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 void
shelfFiniScreen (CompPlugin *p,
		 CompScreen *s)
{
    SHELF_SCREEN (s);
    if (!ss)
	return ;
    UNWRAP (ss, s, preparePaintScreen);
    UNWRAP (ss, s, paintWindow);
    UNWRAP (ss, s, damageWindowRect);
    UNWRAP (ss, s, windowMoveNotify);
    if (ss->windowPrivateIndex)
	freeWindowPrivateIndex (s, ss->windowPrivateIndex);
    free (ss);
}

static Bool
shelfInitScreen (CompPlugin *p,
		 CompScreen *s)
{
    shelfScreen *ss;
    ss = malloc (sizeof (shelfScreen));
    if (!ss)
	return FALSE; // fixme: error message.
    ss->windowPrivateIndex = allocateWindowPrivateIndex (s);
    s->base.privates[screenPrivateIndex].ptr = ss;
    WRAP (ss, s, preparePaintScreen, shelfPreparePaintScreen);
    WRAP (ss, s, paintWindow, shelfPaintWindow); 
    WRAP (ss, s, damageWindowRect, shelfDamageWindowRect);
    WRAP (ss, s, windowMoveNotify, shelfWindowMoveNotify);
    return TRUE; 
}

static void
shelfFiniDisplay (CompPlugin  *p,
		  CompDisplay *d)
{
    if (screenPrivateIndex >= 0)
        freeScreenPrivateIndex (d, screenPrivateIndex);
    UNWRAP (sd, d, handleEvent);
}

static Bool
shelfInitDisplay (CompPlugin  *p,
		  CompDisplay *d)
{
    if (!checkPluginABI ("core", CORE_ABIVERSION))
	return FALSE;
    screenPrivateIndex = allocateScreenPrivateIndex (d); 
    if (screenPrivateIndex < 0)
	return FALSE;
    if (!d->shapeExtension)
    {
	printf ("shelf: No Shape extension found. Shelfing not possible.\n");
	freeScreenPrivateIndex (d, screenPrivateIndex);
	return FALSE;
    }
    shelfSetTriggerKeyInitiate (d, shelfTrigger);
    shelfSetIncButtonInitiate (d, shelfInc);
    shelfSetDecButtonInitiate (d, shelfDec);
    WRAP (sd, d, handleEvent, shelfHandleEvent);
    return TRUE;
}

static void
shelfFini (CompPlugin *p)
{
    if (displayPrivateIndex >= 0)
       freeDisplayPrivateIndex (displayPrivateIndex);
}

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

static void
shelfFiniWindow (CompPlugin *p,
		 CompWindow *w)
{
    SHELF_SCREEN (w->screen);
    SHELF_WINDOW (w);
    if (sw)
	free (sw);
}

static Bool
shelfInitWindow (CompPlugin *p,
		 CompWindow *w)
{
    SHELF_SCREEN(w->screen);

    shelfWindow *sw;
    sw = malloc (sizeof (shelfWindow));
    if (!sw)
	return FALSE;

    sw->scale = 1.0f;
    sw->targetScale = 1.0f;
    sw->ipw = None;
    w->base.privates[ss->windowPrivateIndex].ptr = sw;
    return TRUE;
}

static CompBool
shelfInitObject (CompPlugin *p,
		 CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	    (InitPluginObjectProc) shelfInit, /* 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) shelfFini, /* InitCore */
	(FiniPluginObjectProc) shelfFiniDisplay,
	(FiniPluginObjectProc) shelfFiniScreen, 
	(FiniPluginObjectProc) shelfFiniWindow
    };

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

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

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