summaryrefslogtreecommitdiff
path: root/src/opacify.cpp
blob: f7600c310767061d23669e9df97520fc4d1f2abb (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
/**
 * Compiz Opacify
 *
 * opacify.cpp
 *
 * Copyright (c) 2006 Kristian LyngstĂžl <kristian@beryl-project.org>
 * Ported to Compiz and BCOP usage by Danny Baumann <maniac@beryl-project.org>
 * Ported to Compiz 0.9 by Sam Spilsbury <smspillaz@gmail.com>
 *
 * 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.
 *
 *
 * Opacify increases opacity on targeted windows and reduces it on
 * blocking windows, making whatever window you are targeting easily
 * visible.
 *
 */

#include "opacify.h"

COMPIZ_PLUGIN_20090315 (opacify, OpacifyPluginVTable);

void
setFunctions (bool enabled)
{
    OPACIFY_SCREEN (screen);

    screen->handleEventSetEnabled (os, enabled);

    foreach (CompWindow *w, screen->windows ())
    {
	OPACIFY_WINDOW (w);

	ow->gWindow->glPaintSetEnabled (ow, enabled);
    }
}

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

/* Sets the real opacity and damages the window if actual opacity and
 * requested opacity differs. */

void
OpacifyWindow::setOpacity (int fOpacity)
{
    if (opacified || (gWindow->paintAttrib ().opacity != opacity))
	cWindow->addDamage ();

    opacified = true;
    opacity = fOpacity;
}

/* Resets the Window to the original opacity if it still exists.
*/

void
OpacifyScreen::resetOpacity (Window  id)
{
    CompWindow *w;

    w = screen->findWindow (id);
    if (!w)
	return;

    OPACIFY_WINDOW (w);

    ow->opacified = false;
    ow->cWindow->addDamage ();
}

/* Resets the opacity of windows on the passive list.
*/
void
OpacifyScreen::clearPassive ()
{
    passive.clear ();
}

/* Dim an (inactive) window. Place it on the passive list and
 * update passiveNum. Then change the opacity.
 */

void
OpacifyWindow::dim ()
{
    OPACIFY_SCREEN (screen);

    os->passive.push_back (window->id ());

    setOpacity (MIN (OPAQUE * os->optionGetPassiveOpacity () / 100,
		     gWindow->paintAttrib ().opacity));
}

/* Walk through all windows, skip until we've passed the active
 * window, skip if it's invisible, hidden or minimized, skip if
 * it's not a window type we're looking for.
 * Dim it if it intersects.
 *
 * Returns number of changed windows.
 */

int
OpacifyScreen::passiveWindows (CompRegion     fRegion)
{
    bool       flag = false;
    int        i = 0;

    /* Clear the list first to prevent memleaks */
    foreach (Window xid, passive)
    {
	CompWindow *win = screen->findWindow (xid);

	if (!win)
	    continue;

	OPACIFY_WINDOW (win);

	resetOpacity (xid);
	ow->setOpacity (MAX (OPAQUE * optionGetActiveOpacity () / 100,
			ow->gWindow->paintAttrib ().opacity));
    }
    passive.clear ();

    foreach (CompWindow *w, screen->windows ())
    {
	if (w->id () == active)
	{
	    flag = true;
	    continue;
	}
	if (!flag)
	    continue;
	if (!optionGetWindowMatch ().evaluate (w))
	    continue;
	if (!w->isViewable () || w->minimized ())
	    continue;

	intersect = w->region ().intersected (fRegion);
	if (!intersect.isEmpty ())
	{
	    OpacifyWindow::get (w)->dim ();
	    i++;
	}
    }

    return i;
}

/* Check if we switched active window, reset the old passive windows
 * if we did. If we have an active window and switched: reset that too.
 * If we have a window (w is true), update the active id and
 * passive list. justMoved is to make sure we recalculate opacity after
 * moving. We can't reset before moving because if we're using a delay
 * and the window being moved is not the active but overlapping, it will
 * be reset, which would conflict with move's opacity change.
 */
void
OpacifyWindow::handleEnter ()
{
    OPACIFY_SCREEN (screen);

    if (screen->otherGrabExist (NULL))
    {
	if (!screen->otherGrabExist ("move", NULL))
	{
	    os->justMoved = true;
	    return;
	}

	os->clearPassive ();
	os->resetOpacity (os->active);
	os->active = 0;
	return;
    }

    if (!window || os->active != window->id () || os->justMoved)
    {
	os->justMoved = false;
	os->resetOpacity (os->active);
	os->active = 0;
    }

    if (!window)
	return;

    if (window->id () != os->active && !window->shaded () &&
	os->optionGetWindowMatch ().evaluate (window))
    {
	int num;

	os->active = window->id ();
	num = os->passiveWindows (window->region ());

	if (num || os->optionGetOnlyIfBlock ())
	    setOpacity (MAX (OPAQUE * os->optionGetActiveOpacity () / 100,
				gWindow->paintAttrib ().opacity));
    }
}

/* Timeout-time! Unset the timeout handler, make sure we're on the same
 * screen, handle the event.
 */
bool
OpacifyScreen::handleTimeout ()
{
    if (newActive)
        OpacifyWindow::get (newActive)->handleEnter ();

    return false;
}

/* Checks whether we should delay or not.
 * Returns true if immediate execution.
 */
bool
OpacifyScreen::checkDelay ()
{
    if (optionGetFocusInstant () && newActive &&
	(newActive->id () == screen->activeWindow ()))
	return true;
    if (!optionGetTimeout ())
	return true;
    if (!newActive || (newActive->id () == screen->root ()))
	return false;
    if (newActive->type () & (CompWindowTypeDesktopMask |
			      CompWindowTypeDockMask))
    {
	return false;
    }
    if (optionGetNoDelayChange () && passive.size ())
	return true;

    return false;
}

bool
OpacifyWindow::glPaint (const GLWindowPaintAttrib &attrib,
			const GLMatrix		  &transform,
			const CompRegion	  &region,
			unsigned int		  mask)
{
    if (opacified)
    {
	GLWindowPaintAttrib wAttrib = attrib;

	wAttrib.opacity = opacity;

	return gWindow->glPaint (wAttrib, transform, region, mask);
    }
    else
    {
	return gWindow->glPaint (attrib, transform, region, mask);
    }

    return false;
}

/* Takes the inital event.
 * If we were configured, recalculate the opacify-windows if
 * it was our window.
 * If a window was entered: call upon handle_timeout after od->timeout
 * micro seconds, or directly if od->timeout is 0 (no delay).
 *
 */

void
OpacifyScreen::handleEvent (XEvent *event)
{

    screen->handleEvent (event);

    if (!isToggle)
	return;

    switch (event->type) {
    case EnterNotify:
	Window id;

	id = event->xcrossing.window;
	    newActive = screen->findTopLevelWindow (id);

	if (timeoutHandle.active ())
	    timeoutHandle.stop ();

	if (checkDelay ())
	    handleTimeout ();
	else
	    timeoutHandle.start ();
     	break;
    case ConfigureNotify:

	if (active != event->xconfigure.window)
	    break;

	clearPassive ();
	if (active)
	{
	    CompWindow *w;

	    w = screen->findWindow (active);
	    if (w)
		passiveWindows (w->region ());
	}
     	break;
    default:
	break;
    }
}

/* Toggle opacify on/off. We are in Display-context, make sure we handle all
 * screens.
 */

bool
OpacifyScreen::toggle (CompAction         *action,
		       CompAction::State  state,
		       CompOption::Vector options)
{
    isToggle = !isToggle;
    if (!isToggle && optionGetToggleReset ())
    {
	if (active)
	{
	    clearPassive ();
	    resetOpacity (active);
	    active = 0;
	}
    }

    setFunctions (isToggle);

    return true;
}

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

/** This is called when an option changes. The only option we are looking for
 *  here is 'init_toggle' so when that changes, we adjust our internal values
 *  appropriately
 */

void
OpacifyScreen::optionChanged (CompOption              *option,
			      OpacifyOptions::Options num)
{
    switch (num) {
    case OpacifyOptions::InitToggle:
	isToggle = option->value ().b ();
	setFunctions (isToggle);
	if (active)
	{
	    clearPassive ();
	    resetOpacity (active);
	    active = 0;
	}
	break;
	case OpacifyOptions::Timeout:
	timeoutHandle.setTimes (optionGetTimeout (), optionGetTimeout () * 1.2);
	break;
    default:
	break;
    }
}

/** Constructor for OpacifyWindow. This is called whenever a new window
 *  is created and we set our custom variables to it and also register to
 *  paint this window
 */

OpacifyWindow::OpacifyWindow (CompWindow *window) :
    PluginClassHandler <OpacifyWindow, CompWindow> (window),
    window (window),
    cWindow (CompositeWindow::get (window)),
    gWindow (GLWindow::get (window)),
    opacified (false),
    opacity (100)
{
    GLWindowInterface::setHandler (gWindow, false);
}

void
OpacifyScreen::postLoad ()
{
    setFunctions (isToggle);
}

/** Constructor for OpacifyScreen. This is called whenever a new screen
 *  is created and we set our custom variables to it and also register to
 *  handle X.org events when they come through
 */

OpacifyScreen::OpacifyScreen (CompScreen *screen) :
    PluginClassHandler <OpacifyScreen, CompScreen> (screen),
    PluginStateWriter <OpacifyScreen> (this, screen->root ()),
    isToggle (true),
    newActive (NULL),
    active (screen->activeWindow ()),
    intersect (emptyRegion),
    justMoved (false)
{
    ScreenInterface::setHandler (screen, false);

    timeoutHandle.setTimes (optionGetTimeout (), optionGetTimeout () * 1.2);
    timeoutHandle.setCallback (boost::bind (&OpacifyScreen::handleTimeout,
									 this));

    optionSetToggleKeyInitiate (boost::bind (&OpacifyScreen::toggle, this, _1,
								       _2, _3));
    optionSetInitToggleNotify (boost::bind (&OpacifyScreen::optionChanged,
								 this, _1, _2));
    optionSetTimeoutNotify (boost::bind (&OpacifyScreen::optionChanged,
								 this, _1, _2));

    screen->handleEventSetEnabled (this, optionGetInitToggle ());
}

OpacifyScreen::~OpacifyScreen ()
{
    writeSerializedData ();
}

bool
OpacifyPluginVTable::init ()
{
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION))
	return false;
    if (!CompPlugin::checkPluginABI ("composite", COMPIZ_COMPOSITE_ABI))
	return false;
    if (!CompPlugin::checkPluginABI ("opengl", COMPIZ_OPENGL_ABI))
	return false;


    return true;
}