summaryrefslogtreecommitdiff
path: root/shelf.c
blob: 42da8e222fbde135bb2ddb083f90d1a4556968ff (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
/*
 * 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: Kristian Lyngstøl <kristian@bohemians.org>
 *
 * TODO: 
 *  - Mem leaks (no malloc ()'s are ever freed)
 *  - Unwrap on unload
 *  - Input mask
 *  - Animation
 *  - Floating resize (on scroll wheel for instance)
 *  - Misc
 */

#include <compiz-core.h>
#include "shelf_options.h"

typedef struct { 
    Window id;
    float scale;
} miniWindow;

typedef struct {
    PaintWindowProc paintWindow;
    DamageWindowRectProc damageWindowRect;
    int windowPrivateIndex;
} miniScreen;

static int displayPrivateIndex;
static int screenPrivateIndex;

#define MINI_SCREEN(s) \
    miniScreen *ms = s->base.privates[screenPrivateIndex].ptr;
#define MINI_WINDOW(w) \
    miniWindow *mw = w->base.privates[ms->windowPrivateIndex].ptr;

/* Initially triggered keybinding.
 */
static Bool
miniwin2Trigger(CompDisplay     *d,
		 CompAction      *action,
		 CompActionState state,
		 CompOption      *option,
		 int             nOption)
{
    CompWindow *w = findWindowAtDisplay(d, d->activeWindow);
    if (!w)
	return TRUE;
    MINI_SCREEN (w->screen);
    MINI_WINDOW (w);
    if (mw->scale == 1.0f)
	mw->scale = 0.5f;
    else if (mw->scale == 0.5f)
	mw->scale = 0.25f;
    else 
	mw->scale = 1.00f;
    damageScreen (w->screen);
    return TRUE;
}

/* Fixme:
 * This should not cause total screen damage, but only adjust the rect
 * correctly.
 */
static Bool
miniDamageWindowRect (CompWindow *w,
                       Bool       initial,
                       BoxPtr     rect)
{
    Bool status = FALSE;

    MINI_SCREEN (w->screen);
    MINI_WINDOW (w);

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

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

    return status;
}

/* Scale the window if it is supposed to be scaled. 
 */
static Bool
mwPaintWindow (CompWindow *w,
	       const WindowPaintAttrib *attrib,
	       const CompTransform *transform,
	       Region region,
	       unsigned int mask)
{
    Bool status;
    CompScreen *s = w->screen;
    MINI_SCREEN (s);
    MINI_WINDOW (w);

    if (mw->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, mw->scale, mw->scale, 0);
	matrixTranslate (&mTransform, -xOrigin, -yOrigin,  0);
	mask |= PAINT_WINDOW_TRANSFORMED_MASK;
	UNWRAP (ms, s, paintWindow);
	status = (*s->paintWindow) (w, attrib, &mTransform, region, mask);
	WRAP (ms, s, paintWindow, mwPaintWindow);
    }
    else
    {
	UNWRAP (ms, s, paintWindow);
	status = (*s->paintWindow) (w, attrib, transform, region, mask);
	WRAP (ms, s, paintWindow, mwPaintWindow);
    }
    return status;
}

/* Configuration, initialization, boring stuff. --------------------- */
static void
miniwin2FiniScreen (CompPlugin *p,
		    CompScreen *s)
{
    MINI_SCREEN (s);
    if (!ms)
	return ;
    UNWRAP (ms, s, paintWindow);
    UNWRAP (ms, s, damageWindowRect);
    if (ms->windowPrivateIndex)
	freeWindowPrivateIndex (s, ms->windowPrivateIndex);
    free (ms);
}
static Bool
miniwin2InitScreen (CompPlugin *p,
		    CompScreen *s)
{
    miniScreen *ms;
    ms = malloc (sizeof (miniScreen));
    if (!ms)
	return FALSE; // fixme: error message.
    ms->windowPrivateIndex = allocateWindowPrivateIndex (s);
    s->base.privates[screenPrivateIndex].ptr = ms;
    WRAP (ms, s, paintWindow, mwPaintWindow); 
    WRAP (ms, s, damageWindowRect, miniDamageWindowRect);
    return TRUE; 
}

static void
miniwin2FiniDisplay (CompPlugin  *p,
		      CompDisplay *d)
{
    if (screenPrivateIndex >= 0)
        freeScreenPrivateIndex (d, screenPrivateIndex);
}

static Bool
miniwin2InitDisplay (CompPlugin  *p,
		      CompDisplay *d)
{
    if (!checkPluginABI ("core", CORE_ABIVERSION))
	return FALSE;
    screenPrivateIndex = allocateScreenPrivateIndex (d); 
    if (screenPrivateIndex < 0)
	return FALSE;
    shelfSetTriggerKeyInitiate (d, miniwin2Trigger);
    return TRUE;
}

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

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

static void
miniwin2FiniWindow (CompPlugin *p,
		    CompWindow *w)
{
    MINI_SCREEN (w->screen);
    MINI_WINDOW (w);
    if (mw)
	free (mw);
    return;
}

static Bool
miniwin2InitWindow (CompPlugin *p,
		    CompWindow *w)
{
    MINI_SCREEN(w->screen);

    miniWindow *mw;
    mw = malloc (sizeof (miniWindow));
    if (!mw)
	return FALSE;

    mw->id = w->id;
    mw->scale = 1.0f;
    w->base.privates[ms->windowPrivateIndex].ptr = mw;
    return TRUE;
}

static CompBool
miniwin2InitObject (CompPlugin *p,
		     CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) miniwin2Init, /* InitCore */
	(InitPluginObjectProc) miniwin2InitDisplay,
	(InitPluginObjectProc) miniwin2InitScreen, 
	(InitPluginObjectProc) miniwin2InitWindow
    };

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

static void
miniwin2FiniObject (CompPlugin *p,
		     CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) miniwin2Fini, /* InitCore */
	(FiniPluginObjectProc) miniwin2FiniDisplay,
	(FiniPluginObjectProc) miniwin2FiniScreen, 
	(FiniPluginObjectProc) miniwin2FiniWindow
    };

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

CompPluginVTable miniwin2VTable = {
    "shelf",
    0,
    0,
    0,
    miniwin2InitObject,
    miniwin2FiniObject,
    0,
    0
};

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