summaryrefslogtreecommitdiff
path: root/beryl-plugins/src/fade.c
blob: 53139382fac5d01e161c57172f980be73f0fe9fa (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
/*
 * Copyright © 2005 Novell, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Novell, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Novell, Inc. makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: David Reveman <davidr@novell.com>
 */

#include <stdlib.h>
#include <string.h>

#include <beryl.h>

#include <math.h>

#define FADE_SPEED_DEFAULT    7.0f
#define FADE_SPEED_MIN        0.1f
#define FADE_SPEED_MAX       10.0f
#define FADE_SPEED_PRECISION  0.1f

#define URGENT_SPEED_DEFAULT 15
#define URGENT_SPEED_MAX 50
#define URGENT_SPEED_MIN 5
#define URGENT_SPEED_O (fs->opt[FADE_SCREEN_OPTION_URGENT_SPEED].value.i*100)

#define URGENT_COUNT_DEFAULT 5
#define URGENT_COUNT_MIN 0
#define URGENT_COUNT_MAX 15
#define URGENT_COUNT_O fs->opt[FADE_SCREEN_OPTION_URGENT_COUNT].value.i

#define URGENT_CENTER_DEFAULT 70
#define URGENT_CENTER_MIN 0
#define URGENT_CENTER_MAX 100
#define URGENT_CENTER_O (fs->opt[FADE_SCREEN_OPTION_URGENT_CENTER].value.i/100.0)

#define URGENT_SWING_DEFAULT 20
#define URGENT_SWING_MIN 0
#define URGENT_SWING_MAX 100
#define URGENT_SWING_O (fs->opt[FADE_SCREEN_OPTION_URGENT_SWING].value.i/100.0)

#define URGENT_DEFAULT TRUE
#define URGENT_O fs->opt[FADE_SCREEN_OPTION_URGENT].value.b

#define FADE_VISUAL_BELL_DEFAULT FALSE

#define FADE_FULLSCREEN_VISUAL_BELL_DEFAULT FALSE

static int displayPrivateIndex;

typedef struct _FadeDisplay
{
	int screenPrivateIndex;
} FadeDisplay;

#define FADE_SCREEN_OPTION_FADE_SPEED          0
#define FADE_SCREEN_OPTION_NUM              1

typedef struct _FadeScreen
{
	int windowPrivateIndex;
	int fadeTime;

	int msec;

	CompOption opt[FADE_SCREEN_OPTION_NUM];

	PreparePaintScreenProc preparePaintScreen;
	PaintWindowProc paintWindow;

} FadeScreen;

typedef struct _FadeWindow
{
	GLushort opacity;
	GLushort brightness;
	GLushort saturation;

	int animStep;
	int count;

	int steps;
} FadeWindow;

/* Steps is in fade window to make sure painting only happens once per step */

#define GET_FADE_DISPLAY(d)                     \
    ((FadeDisplay *) (d)->privates[displayPrivateIndex].ptr)

#define FADE_DISPLAY(d)               \
    FadeDisplay *fd = GET_FADE_DISPLAY (d)

#define GET_FADE_SCREEN(s, fd)                     \
    ((FadeScreen *) (s)->privates[(fd)->screenPrivateIndex].ptr)

#define FADE_SCREEN(s)                            \
    FadeScreen *fs = GET_FADE_SCREEN (s, GET_FADE_DISPLAY (s->display))

#define GET_FADE_WINDOW(w, fs)                         \
    ((FadeWindow *) (w)->privates[(fs)->windowPrivateIndex].ptr)

#define FADE_WINDOW(w)                         \
    FadeWindow *fw = GET_FADE_WINDOW  (w,             \
            GET_FADE_SCREEN  (w->screen,         \
                GET_FADE_DISPLAY (w->screen->display)))

#define NUM_OPTIONS(s) (sizeof ((s)->opt) / sizeof (CompOption))

static void fadeScreenInitOptions(FadeScreen * fs)
{
	CompOption *o;

	o = &fs->opt[FADE_SCREEN_OPTION_FADE_SPEED];
	o->advanced = False;
	o->name = "fade_speed";
	o->group = N_("Misc. options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Fade Speed");
	o->longDesc = N_("How fast the windows Fade in and out.");
	o->type = CompOptionTypeFloat;
	o->value.f = FADE_SPEED_DEFAULT;
	o->rest.f.min = FADE_SPEED_MIN;
	o->rest.f.max = FADE_SPEED_MAX;
	o->rest.f.precision = FADE_SPEED_PRECISION;
}


static CompOption *fadeGetScreenOptions(CompScreen * screen, int *count)
{
	if (screen)
	{
		FADE_SCREEN(screen);

		*count = NUM_OPTIONS(fs);
		return fs->opt;
	}
	else
	{
		FadeScreen *fs = malloc(sizeof(FadeScreen));

		fadeScreenInitOptions(fs);
		*count = NUM_OPTIONS(fs);
		return fs->opt;
	}
}

static Bool
fadeSetScreenOption(CompScreen * screen, char *name, CompOptionValue * value)
{
	CompOption *o;
	int index;

	FADE_SCREEN(screen);

	o = compFindOption(fs->opt, NUM_OPTIONS(fs), name, &index);
	if (!o)
		return FALSE;

	switch (index)
	{
	case FADE_SCREEN_OPTION_FADE_SPEED:
		if (compSetFloatOption(o, value))
		{
			fs->fadeTime = 1000.0f / o->value.f;
			return TRUE;
		}
		break;
	default:
		break;
	}

	return FALSE;
}


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

	FADE_SCREEN(s);

	fs->msec = msSinceLastPaint;
	steps = (msSinceLastPaint * OPAQUE) / fs->fadeTime;
	if (steps < 12)
		steps = 12;
	for (w = s->windows; w; w = w->next)
	{
		GET_FADE_WINDOW(w, fs)->steps = steps;
	}

	UNWRAP(fs, s, preparePaintScreen);
	(*s->preparePaintScreen) (s, msSinceLastPaint);
	WRAP(fs, s, preparePaintScreen, fadePreparePaintScreen);
}


static Bool
fadePaintWindow(CompWindow * w,
				const WindowPaintAttrib * attrib,
				const CompTransform     *transform,
				Region region, unsigned int mask)
{
	CompScreen *s = w->screen;
	Bool status;

	FADE_SCREEN(s);
	FADE_WINDOW(w);

	if (!w->screen->canDoSlightlySaturated)
		fw->saturation = attrib->saturation;

	if (fw->opacity != attrib->opacity ||
		fw->brightness != attrib->brightness ||
		fw->saturation != attrib->saturation)
	{
		GLint opacity;
		GLint brightness;
		GLint saturation;

		opacity = fw->opacity;
		if (attrib->opacity > fw->opacity)
		{
			opacity = fw->opacity + fw->steps;
			if (opacity > attrib->opacity)
				opacity = attrib->opacity;
		}
		else if (attrib->opacity < fw->opacity)
		{
			opacity = fw->opacity - fw->steps;
			if (opacity < attrib->opacity)
				opacity = attrib->opacity;
		}

		brightness = fw->brightness;
		if (attrib->brightness > fw->brightness)
		{
			brightness = fw->brightness + (fw->steps);
			if (brightness > attrib->brightness)
				brightness = attrib->brightness;
		}
		else if (attrib->brightness < fw->brightness)
		{
			brightness = fw->brightness - (fw->steps);
			if (brightness < attrib->brightness)
				brightness = attrib->brightness;
		}

		saturation = fw->saturation;
		if (attrib->saturation > fw->saturation)
		{
			saturation = fw->saturation + (fw->steps );
			if (saturation > attrib->saturation)
				saturation = attrib->saturation;
		}
		else if (attrib->saturation < fw->saturation)
		{
			saturation = fw->saturation - (fw->steps );
			if (saturation < attrib->saturation)
				saturation = attrib->saturation;
		}

		WindowPaintAttrib fAttrib = *attrib;

		fAttrib.opacity = opacity;
		fAttrib.brightness = brightness;
		fAttrib.saturation = saturation;

		UNWRAP(fs, s, paintWindow);
		status = (*s->paintWindow) (w, &fAttrib, transform, region, mask);
		WRAP(fs, s, paintWindow, fadePaintWindow);

		fw->opacity = opacity;
		fw->brightness = brightness;
		fw->saturation = saturation;
		fw->steps = 0;

		if (opacity != attrib->opacity ||
			brightness != attrib->brightness ||
			saturation != attrib->saturation)
			addWindowDamage(w);
	}
	else
	{
		UNWRAP(fs, s, paintWindow);
		status = (*s->paintWindow) (w, attrib, transform, region, mask);
		WRAP(fs, s, paintWindow, fadePaintWindow);
	}

	return status;
}



static Bool fadeInitDisplay(CompPlugin * p, CompDisplay * d)
{
	FadeDisplay *fd;

	fd = malloc(sizeof(FadeDisplay));
	if (!fd)
		return FALSE;

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

	d->privates[displayPrivateIndex].ptr = fd;

	return TRUE;
}

static void fadeFiniDisplay(CompPlugin * p, CompDisplay * d)
{
	FADE_DISPLAY(d);

	freeScreenPrivateIndex(d, fd->screenPrivateIndex);

	free(fd);
}

static Bool fadeInitScreen(CompPlugin * p, CompScreen * s)
{
	FadeScreen *fs;

	FADE_DISPLAY(s->display);

	fs = malloc(sizeof(FadeScreen));
	if (!fs)
		return FALSE;

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


	fs->fadeTime = 1000.0f / FADE_SPEED_DEFAULT;

	fadeScreenInitOptions(fs);

	WRAP(fs, s, preparePaintScreen, fadePreparePaintScreen);
	WRAP(fs, s, paintWindow, fadePaintWindow);

	s->privates[fd->screenPrivateIndex].ptr = fs;

	return TRUE;
}

static void fadeFiniScreen(CompPlugin * p, CompScreen * s)
{
	FADE_SCREEN(s);

	freeWindowPrivateIndex(s, fs->windowPrivateIndex);

	UNWRAP(fs, s, preparePaintScreen);
	UNWRAP(fs, s, paintWindow);

	free(fs);
}

static Bool fadeInitWindow(CompPlugin * p, CompWindow * w)
{
	FadeWindow *fw;

	FADE_SCREEN(w->screen);

	fw = malloc(sizeof(FadeWindow));
	if (!fw)
		return FALSE;

	fw->opacity = w->paint.opacity;
	fw->brightness = w->paint.brightness;
	fw->saturation = w->paint.saturation;

	w->privates[fs->windowPrivateIndex].ptr = fw;

	return TRUE;
}

static void fadeFiniWindow(CompPlugin * p, CompWindow * w)
{
	FADE_WINDOW(w);
	free(fw);
}

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

	return TRUE;
}

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

CompPluginDep fadeDeps[] = {
	{CompPluginRuleAfter, "animation"}
	,
	{CompPluginRuleAfter, "decoration"}
	,
	{CompPluginRuleAfter, "wobbly"}
	,
};

static CompPluginVTable fadeVTable = {
	"fade",
	N_("Fading Windows"),
	N_("Fade in windows when mapped and fade out windows when unmapped"),
	fadeInit,
	fadeFini,
	fadeInitDisplay,
	fadeFiniDisplay,
	fadeInitScreen,
	fadeFiniScreen,
	fadeInitWindow,
	fadeFiniWindow,
	0,							/* GetDisplayOptions */
	0,							/* SetDisplayOption */
	fadeGetScreenOptions,
	fadeSetScreenOption,
	fadeDeps,
	sizeof(fadeDeps) / sizeof(fadeDeps[0]),
	0,
	0,
	BERYL_ABI_INFO,
	"beryl-plugins",
	"effects",
	0,
	0,
	True,
};

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