summaryrefslogtreecommitdiff
path: root/beryl-plugins/src/addhelper.c
blob: 5af2e50cbef9f9f2fd3542d760ca11a5475740b7 (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
/**
 * Beryl  ADD Helper. Makes it easier to concentrate.
 *
 * Copyright (c) 2007 Kristian LyngstĂžl <kristian@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.
 *
 * This plugin provides a toggle-feature that dims all but the active
 * window. This makes it easier for people with lousy concentration
 * to focus. Like me.
 *
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrender.h>
#include <beryl.h>

#define GET_ADD_DISPLAY(d)                            \
	((AddHelperDisplay *) (d)->privates[displayPrivateIndex].ptr)
#define ADD_DISPLAY(d)                                \
    AddHelperDisplay *ad = GET_ADD_DISPLAY (d)
#define GET_ADD_SCREEN(s, ad)                         \
	((AddHelperScreen *) (s)->privates[(ad)->screenPrivateIndex].ptr)
#define ADD_SCREEN(s)                                 \
	AddHelperScreen *as = GET_ADD_SCREEN (s, GET_ADD_DISPLAY (s->display))

typedef enum _AhDisplayOptios {
	DOPT_TOGGLE = 0, 
	DOPT_OP,
	DOPT_BRI,
	DOPT_SAT,
	DOPT_WINDOW_TYPE,
	DOPT_NUM
} AhDisplayOptions;

#define ADD_TOGGLE_KEY "p"
#define ADD_TOGGLE_MOD CompSuperMask

typedef enum _AhScreenOptions {
	SOPT_NUM
} AhScreenOptions;

#define NUM_OPTIONS(s) (sizeof ((s)->opt) / sizeof (CompOption))
#define N_WIN_TYPE (sizeof (winType) / sizeof (winType[0]))

static int displayPrivateIndex = 0;

typedef enum _AhFocusBehavior {
	FOCUS_RESET,
	FOCUS_CHANGE,
	FOCUS_ADD
} AhFocusBehavior;

typedef struct _AddHelperDisplay
{
	int screenPrivateIndex;
	int active_screen;
	GLushort opacity;
	GLushort brightness;
	GLushort saturation;
	int wmMask;
	Bool toggle;
	Window lastActive;
	AhFocusBehavior behavior;

	HandleEventProc handleEvent;
	CompOption opt[DOPT_NUM];
} AddHelperDisplay;

static char *winType[] = {
	N_("Toolbar"),
	N_("Utility"),
	N_("Dialog"),
	N_("ModalDialog"),
	N_("Fullscreen"),
	N_("Normal")
};

/* Core addhelper functions. These do the real work. ---------------------*/

/* Reset a window */
static void reset_window(CompWindow * w)
{
	resetWindowOpacity(w, PL_TEMP_HELLO);
	resetWindowBrightness(w, PL_TEMP_HELLO);
	resetWindowSaturation(w,  PL_TEMP_HELLO);
}
/* Dim an (inactive) window. 
 */
static void dim_window(CompWindow * w)
{
	ADD_DISPLAY(w->screen->display);
	setWindowOpacity(w, MIN(ad->opacity, w->paint.opacity), PL_TEMP_HELLO);
	setWindowBrightness(w, MIN(ad->brightness, w->paint.brightness), PL_TEMP_HELLO);
	setWindowSaturation(w, MIN(ad->saturation, w->paint.saturation), PL_TEMP_HELLO);
}

/* Walk through all windows of the screen and adjust them if they
 * are not the active window. If reset is true, this will reset
 * the windows, including the active. Otherwise, it will dim 
 * and reset the active. 
 */
static void walk_windows(CompScreen * s, Bool reset)
{
	CompWindow *w;
	Window active = s->display->activeWindow;
	ADD_DISPLAY(s->display);

	for (w = s->windows; w; w = w->next)
	{
		if (!(w->type & ad->wmMask))
			continue;
		if (!reset && (w->invisible || w->destroyed || w->hidden || w->minimized))
			continue;
		if (!reset)
		{
			if (w->id == active)
				reset_window(w);
			else
				dim_window(w);
		}
		else
			reset_window(w);
			
	}
}

/* Takes the inital event. 
 * This checks for focus change and acts on it.
 */
static void addhelperHandleEvent(CompDisplay * d, XEvent * event)
{
	CompScreen *s;

	ADD_DISPLAY(d);

	UNWRAP(ad, d, handleEvent);
	(*d->handleEvent) (d, event);
	WRAP(ad, d, handleEvent, addhelperHandleEvent);
	
	if (!ad->toggle)
		return;
	if (event->type == PropertyNotify && ad->lastActive != d->activeWindow)
	{
		for (s = d->screens; s; s = s->next)
			walk_windows(s, False);
		ad->lastActive = d->activeWindow;
	}
}


/* Configuration, initialization, boring stuff. ----------------------- */
static void addhelperFiniScreen(CompPlugin * p, CompScreen * s)
{
	ADD_DISPLAY(s->display);

	removeScreenAction(s, &ad->opt[DOPT_TOGGLE].value.
					   action);
}




static Bool addhelperInitScreen(CompPlugin * p, CompScreen * s)
{
	ADD_DISPLAY(s->display);
	addScreenAction(s, &ad->opt[DOPT_TOGGLE].value.action);
	return TRUE;
}


/* Takes the action and toggles us.
 */
static Bool addhelper_toggle(CompDisplay * d, CompAction * ac,
						   CompActionState state, CompOption * option,
						   int nOption)
{
	ADD_DISPLAY(d);
	CompScreen * s;
	ad->toggle = !ad->toggle;
	for (s = d->screens; s; s = s->next)
		walk_windows(s, !ad->toggle);
	return TRUE;
}

static void addhelperDisplayInitOptions(AddHelperDisplay * ad)
{
	CompOption *o;
	int i;

	o = &ad->opt[DOPT_TOGGLE];
	o->name = "toggle";
	o->group = N_("Bindings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Toggle AddHelper");
	o->longDesc =
			N_
			("Use this to enable/disable AddHelper on the fly.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = addhelper_toggle;
	o->value.action.terminate = 0;
	o->value.action.bell = FALSE;
	o->value.action.edgeMask = 0;
	o->value.action.type = CompBindingTypeKey;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.key.modifiers = ADD_TOGGLE_MOD;
	o->value.action.key.keysym = XStringToKeysym(ADD_TOGGLE_KEY);
	o->advanced = False;
	ad->toggle = False;

	o = &ad->opt[DOPT_BRI];
	o->name = "bri";
	o->group = N_("Misc. options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Brightness");
	o->longDesc =
			N_
			("Brightness of faded windows");
	o->type = CompOptionTypeInt;
	o->value.i = 30;
	o->rest.i.min = 1;
	o->rest.i.max = 100;
	o->advanced = False;
	ad->brightness = BRIGHT * 0.30f;

	o = &ad->opt[DOPT_SAT];
	o->name = "sat";
	o->group = N_("Misc. options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Saturation");
	o->longDesc =
			N_
			("Saturation of faded windows");
	o->type = CompOptionTypeInt;
	o->value.i = 50;
	o->rest.i.min = 0;
	o->rest.i.max = 100;
	o->advanced = False;
	ad->saturation = 0.0f;

	o = &ad->opt[DOPT_OP];
	o->name = "op";
	o->group = N_("Misc. options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Opacity");
	o->longDesc =
			N_
			("Opacity of faded windows");
	o->type = CompOptionTypeInt;
	o->value.i = 100;
	o->rest.i.min = 1;
	o->rest.i.max = 100;
	o->advanced = False;
	ad->opacity = OPAQUE;

	o = &ad->opt[DOPT_WINDOW_TYPE];
	o->name = "window_types";
	o->group = N_("Misc. options");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Window Types");
	o->longDesc = N_("Window types that should be Opacified.");
	o->type = CompOptionTypeList;
	o->value.list.type = CompOptionTypeString;
	o->value.list.nValue = N_WIN_TYPE;
	o->value.list.value = malloc(sizeof(CompOptionValue) * N_WIN_TYPE);
	for (i = 0; i < N_WIN_TYPE; i++)
		o->value.list.value[i].s = strdup(winType[i]);
	o->rest.s.string = (char **)windowTypeString;
	o->rest.s.nString = nWindowTypeString;
	o->advanced = True;
	ad->wmMask = compWindowTypeMaskFromStringList(&o->value);
	
}

static Bool addhelperSetDisplayOptions(CompDisplay * display, char *name,
									 CompOptionValue * value)
{
	CompOption *o;
	int index;

	ADD_DISPLAY(display);

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

	switch (index)
	{
	case DOPT_TOGGLE:
		if (setDisplayAction(display, o, value))
			return TRUE;
		break;
	case DOPT_BRI:
		if (compSetIntOption(o, value))
		{
			ad->brightness = (o->value.i * 0xffff) / 100;
			return TRUE;
		}
		break;
	case DOPT_SAT:
		if (compSetIntOption(o, value))
		{
			ad->saturation = (o->value.i * 0xffff) / 100;
			return TRUE;
		}
		break;
	case DOPT_OP:
		if (compSetIntOption(o, value))
		{
			ad->opacity = (o->value.i * 0xffff) / 100;
			return TRUE;
		}
		break;
	case DOPT_WINDOW_TYPE:
		if (compSetOptionList(o, value))
		{
			ad->wmMask = compWindowTypeMaskFromStringList(&o->value);
			return TRUE;
		}
		break;
	default:
		break;
	}
	return FALSE;
}

static CompOption *addhelperGetDisplayOptions(CompDisplay * display, int *count)
{
	if (display)
	{
		ADD_DISPLAY(display);
		*count = NUM_OPTIONS(ad);
		return ad->opt;
	}
	else
	{
		AddHelperDisplay *ad = malloc(sizeof(AddHelperDisplay));

		addhelperDisplayInitOptions(ad);
		*count = NUM_OPTIONS(ad);
		return ad->opt;
	}
}

static Bool addhelperInitDisplay(CompPlugin * p, CompDisplay * d)
{
	AddHelperDisplay *ad = (AddHelperDisplay *) malloc(sizeof(AddHelperDisplay));

	ad->screenPrivateIndex = allocateScreenPrivateIndex(d);
	if (ad->screenPrivateIndex < 0)
	{
		free(ad);
		return FALSE;
	}
	addhelperDisplayInitOptions(ad);
	d->privates[displayPrivateIndex].ptr = ad;
	ad->active_screen = d->screens->screenNum;
	WRAP(ad, d, handleEvent, addhelperHandleEvent);
	return TRUE;
}

static void addhelperFiniDisplay(CompPlugin * p, CompDisplay * d)
{
	ADD_DISPLAY(d);
	UNWRAP(ad, d, handleEvent);
	freeScreenPrivateIndex(d, ad->screenPrivateIndex);
	free(ad);
}

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

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

CompPluginVTable addhelperVTable = {
	"addhelper",
	N_("AddHelper"),
	N_("Make it slightly easier to concentrate by dimming all but the active window."), 
	addhelperInit,
	addhelperFini,
	addhelperInitDisplay,
	addhelperFiniDisplay,
	addhelperInitScreen,
	addhelperFiniScreen,
	0,
	0,
	addhelperGetDisplayOptions,
	addhelperSetDisplayOptions,
	0, //addhelperGetScreenOptions,
	0, //addhelperSetScreenOptions,
	0,
	0,
	0,
	0,
	BERYL_ABI_INFO,
	"beryl-plugins",
	"accessibility",
	0,
	0,
	False,
};

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