summaryrefslogtreecommitdiff
path: root/src/addhelper.c
blob: 299f5b7ac8eeb1124922b4123e51391fd3fe0024 (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
/**
 * Beryl  ADD Helper. Makes it easier to concentrate.
 *
 * Copyright (c) 2007 Kristian LyngstĂžl <kristian@beryl-project.org>
 * Ported and highly modified by Patrick Niklaus <marex@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.
 *
 * THIS WAS _NOT_ PORTED TO COMPIZ BY KRISTIAN LYNGSTOL.
 * This was perfectly commented before the port, but isn't now.
 * Now, it's a mess. 
 * 
 * I spent hours and hours makeing sure the code was clean and well documented.
 * It isn't any more.
 * Please respect my work if you are going to contribute to it.
 * If you are going to change this: WRITE WHAT YOU CHANGE.
 * And if it's a big change, two lines about why would be appreciated. 
 *
 */

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

#include "addhelper_options.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))
#define GET_ADD_WINDOW(w, as) \
	((AddHelperWindow *) (w)->privates[ (as)->windowPrivateIndex].ptr)
#define ADD_WINDOW(w) \
	AddHelperWindow *aw = GET_ADD_WINDOW (w, GET_ADD_SCREEN  (w->screen, GET_ADD_DISPLAY (w->screen->display)))

static int displayPrivateIndex = 0;

typedef struct _AddHelperDisplay
{
	int screenPrivateIndex;
	int active_screen;

	GLushort opacity;
	GLushort brightness;
	GLushort saturation;

	CompMatch *match;
	Bool toggle;
	Window lastActive;

	HandleEventProc handleEvent;
} AddHelperDisplay;

typedef struct _AddHelperScreen
{
	int windowPrivateIndex;

	PaintWindowProc paintWindow;
} AddHelperScreen;

typedef struct _AddHelperWindow
{
	Bool dim;
} AddHelperWindow;

/* 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(CompDisplay *d)
{
	ADD_DISPLAY(d);
	
	CompScreen *s;
	CompWindow *w;
	for (s = d->screens; s; s = s->next)
	{
		for (w = s->windows; w; w = w->next)
		{
			ADD_WINDOW(w);

			aw->dim = FALSE;

			if (!ad->toggle)
				continue;

			if (w->id == d->activeWindow)
				continue;

			if(w->invisible || w->destroyed || w->hidden || w->minimized)
				continue;

			if (!matchEval(ad->match, w))
				continue;

			aw->dim = TRUE;
		}

		damageScreen(s);
	}
}

/* Checks if the window is dimmed and, if so, paints it with the modified
 * paint attributes.
 */
static Bool addhelperPaintWindow(CompWindow *w, const WindowPaintAttrib *attrib,
								  const CompTransform *transform,
								  Region region, unsigned int mask)
{
	ADD_SCREEN(w->screen);
	ADD_DISPLAY(w->screen->display);
	ADD_WINDOW(w);
	Bool status;

	if (aw->dim)
	{
		// copy the paint attribute
		WindowPaintAttrib wAttrib = *attrib;

		// applies the lowest value
		wAttrib.opacity = MIN(attrib->opacity, ad->opacity);
		wAttrib.brightness = MIN(attrib->brightness, ad->brightness);
		wAttrib.saturation = MIN(attrib->saturation, ad->brightness);

		// continue painting with the modified attribute
		UNWRAP(as, w->screen, paintWindow);
		status = (*w->screen->paintWindow) (w, &wAttrib, transform, region, mask);
		WRAP(as, w->screen, paintWindow, addhelperPaintWindow);
	}
	else
	{
		// the window is not dimmed, so its painted normal
		UNWRAP(as, w->screen, paintWindow);
		status = (*w->screen->paintWindow) (w, attrib, transform, region, mask);
		WRAP(as, w->screen, paintWindow, addhelperPaintWindow);
	}

	return status;
}

/* Takes the inital event. 
 * This checks for focus change and acts on it.
 */
static void addhelperHandleEvent(CompDisplay * d, XEvent * event)
{
	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)
	{
		walk_windows(d);
		ad->lastActive = d->activeWindow;
	}
}

/* Configuration, initialization, boring stuff. ----------------------- */

/* Takes the action and toggles us.
 */
static Bool addhelperToggle(CompDisplay * d, CompAction * ac,
						   CompActionState state, CompOption * option,
						   int nOption)
{
	ADD_DISPLAY(d);

	ad->toggle = !ad->toggle;
	walk_windows(d);

	return TRUE;
}

/* Change notify for bcop */
static void addhelperDisplayOptionChanged(CompDisplay *d, CompOption *opt, AddhelperDisplayOptions num)
{
	ADD_DISPLAY(d);

	switch (num)
	{
	case AddhelperDisplayOptionBrightness:
		ad->brightness = (addhelperGetBrightness(d) * 0xffff) / 100;
		break;
	case AddhelperDisplayOptionSaturation:
		ad->saturation = (addhelperGetSaturation(d) * 0xffff) / 100;
		break;
	case AddhelperDisplayOptionOpacity:
		ad->opacity = (addhelperGetOpacity(d) * 0xffff) / 100;
		break;
	case AddhelperDisplayOptionWindowTypes:
		ad->match = addhelperGetWindowTypes(d);
		break;
	default:
		break;
	}

}

static Bool addhelperInitWindow(CompPlugin * p, CompWindow * w)
{
	ADD_SCREEN(w->screen);

	AddHelperWindow *aw = (AddHelperWindow *) malloc(sizeof(AddHelperWindow));

	w->privates[as->windowPrivateIndex].ptr = aw;

	aw->dim = FALSE;

	return TRUE;
}

static void addhelperFiniWindow(CompPlugin * p, CompWindow * w)
{
	ADD_WINDOW(w);

	free(aw);
}

static Bool addhelperInitScreen(CompPlugin * p, CompScreen * s)
{
	ADD_DISPLAY(s->display);

	AddHelperScreen *as = (AddHelperScreen*) malloc(sizeof(AddHelperScreen));

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

	WRAP(as, s, paintWindow, addhelperPaintWindow);

	s->privates[ad->screenPrivateIndex].ptr = as;

	return TRUE;
}

static void addhelperFiniScreen(CompPlugin * p, CompScreen * s)
{
	ADD_SCREEN(s);

	UNWRAP(as, s, paintWindow);
	
	free(as);
}

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

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

	addhelperSetToggleInitiate(d, addhelperToggle);
	addhelperSetBrightnessNotify(d, addhelperDisplayOptionChanged);
	addhelperSetOpacityNotify(d, addhelperDisplayOptionChanged);
	addhelperSetSaturationNotify(d, addhelperDisplayOptionChanged);

	ad->active_screen = d->screens->screenNum;
	ad->toggle = FALSE;
	ad->match = addhelperGetWindowTypes(d);
	ad->brightness = (addhelperGetBrightness(d) * 0xffff) / 100;
	ad->opacity = (addhelperGetOpacity(d) * 0xffff) / 100;
	ad->saturation = (addhelperGetSaturation(d) * 0xffff) / 100;
	ad->lastActive = None;

	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);
}

static int addhelperGetVersion(CompPlugin *p, int version)
{
	return ABIVERSION;
}

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

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