summaryrefslogtreecommitdiff
path: root/src/winreflect.cpp
blob: 2c7a7e255623ae7a4f94b83c997c036c01d17c1c (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
/*
 *
 * Compiz Window reflection plugin
 *
 * winreflect.h
 *
 * Copyright : (C) 2007 by Sam Spilsbury
 * Email     : <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.
 *
 */

#include "winreflect.h"

COMPIZ_PLUGIN_20090315 (winreflect, WinrelfectPluginVTable);

void
WinreflectScreen::updateReflectionProperty (Window id)
{
    Atom actual;
    int	 result, format;
    unsigned long n, left;
    unsigned char *propData;
    bool	  validProperty = false;
    bool	  found = false;
    CompRegion	 reg (emptyRegion);
  
    result = XGetWindowProperty (screen->dpy (), id,
				 mReflectedRegionAtom, 0, 32768,
				 false, AnyPropertyType, &actual, &format,
				 &n, &left, &propData);

    if (result == Success && propData)
    {
	if (format == 32 && actual == XA_CARDINAL &&
	    n > 0 && (n % 4 == 0))
	{
	    long	 *data = (long *) propData;
	    unsigned int nBox = n / 4;

	    while (nBox--)
	    {
		int x, y, w, h;
		x = *data++;
		y = *data++;
		w = *data++;
		h = *data++;

		CompRect r (x, y, w, h);
		
		reg += r;
	    }
	    
	    validProperty = true;
	}

	XFree (propData);
    }
    
    foreach (ReflectionRegion r, mReflectedRegions)
    {
	if (r.id == id)
	{
	    ReflectionRegion nR (reg);
	    
	    nR.id = id;
	    
	    found = true;
	    mReflectedRegions.remove (r);
	    
	    if (validProperty)
		mReflectedRegions.push_back (nR);
	    
	    break;
	}
    }
    
    if (!found)
    {
	ReflectionRegion nR (reg);
	
	nR.id = id;
	
	mReflectedRegions.push_back (nR);
    }

}

void
WinreflectScreen::optionChange (CompOption *opt,
				Options	   num)
{  
    switch (num)
    {
	case WinreflectOptions::ReflectDocks:
	{
	    if (opt->value ().b ())
	    {
		foreach (CompWindow *w, screen->windows ())
		{
		    if (w->type () == CompWindowTypeDockMask)
		    {
			CompRect in (w->inputRect ());
			ReflectionRegion r (in);
			
			r.id = w->id ();
			
			mReflectedRegions.push_back (r);
		    }
		}
	    }
	    else
	    {
		std::list <ReflectionRegion>::iterator it = mReflectedRegions.begin ();
		
		while (it != mReflectedRegions.end ())
		{
		    ReflectionRegion r = *it;
		    CompWindow *w = screen->findWindow (r.id);
		    
		    if (w && w->type () == CompWindowTypeDockMask)
		    {
			mReflectedRegions.remove (r);
			it = mReflectedRegions.begin ();
		    }
		    it++;
		}
	    }
	}
	default:
	  break;
    }
}

void
WinreflectScreen::handleEvent (XEvent *event)
{
    switch (event->type)
    {
	case PropertyNotify:
	    if (event->xproperty.atom == mReflectedRegionAtom)
		updateReflectionProperty (event->xproperty.window);
	    break;
	case DestroyNotify:
	    foreach (ReflectionRegion &r, mReflectedRegions)
	    {
		if (event->xdestroywindow.window == r.id)
		{
		    mReflectedRegions.remove (r);
		    break;
		}
	    }
	    break;
	case ConfigureNotify:
	    foreach (ReflectionRegion &r, mReflectedRegions)
	    {
		if (event->xconfigure.window = r.id)
		{
		    CompWindow *w = screen->findWindow (r.id);
		    
		    if (w->type () == CompWindowTypeDockMask &&
		        optionGetReflectDocks ())
		    {
			CompRect	 in (w->inputRect ());
			ReflectionRegion nR (in);
		      
			mReflectedRegions.remove (r);
			
			nR.id = w->id ();
			
			mReflectedRegions.push_back (nR);
			
			break;
		    }
		}
	    }
	    break;
	default:
	    break;
    }
    
    screen->handleEvent (event);
}

bool
WinreflectScreen::glPaintOutput (const GLScreenPaintAttrib &attrib,
				 const GLMatrix		   &transform,
				 const CompRegion	   &region,
				 CompOutput		   *output,
				 unsigned int		   mask)
{
    mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS_MASK;
    
    mIsTransformed = false;
    
    return gScreen->glPaintOutput (attrib, transform, region, output, mask);
}

void
WinreflectScreen::glPaintTransformedOutput (const GLScreenPaintAttrib &attrib,
					    const GLMatrix	      &transform,
					    const CompRegion	      &region,
					    CompOutput		      *output,
					    unsigned int	      mask)
{
    mIsTransformed = true;
    
    return gScreen->glPaintTransformedOutput (attrib, transform, region, output, mask);
}

bool
WinreflectWindow::glPaint (const GLWindowPaintAttrib	&attrib,
			   const GLMatrix		&transform,
			   const CompRegion		&region,
			   unsigned int			mask)
{
    WINREFLECT_SCREEN (screen);
  
    GLFragment::Attrib fA (attrib);
    GLMatrix	       wTransform (transform);
    int            cull, cullInv;
    bool status = false;
    glGetIntegerv (GL_CULL_FACE_MODE, &cull);
    cullInv = (cull == GL_BACK)? GL_FRONT : GL_BACK;   
    
    status |= gWindow->glPaint (attrib, transform, region, mask);
    
    if (mask & PAINT_WINDOW_OCCLUSION_DETECTION_MASK ||
        mask & PAINT_WINDOW_NO_CORE_INSTANCE_MASK)
	return status;
    
    if (ws->mIsTransformed)
	return status;
    
    foreach (ReflectionRegion reg, ws->mReflectedRegions)
    {
	CompRegion	   reflectiveRegion ((CompRegion &) reg);
	
	reflectiveRegion.translate (0, -reflectiveRegion.boundingRect ().height ());
	reflectiveRegion = reflectiveRegion.united ((CompRegion &) reg);
	
	/* Don't bother if the window doesn't intersect the reflective region */
	if (!(CompRegion (window->outputRect ()).intersects (reflectiveRegion)))
	    continue;
	
	fA.setOpacity (fA.getOpacity () * ((float) ws->optionGetOpacity () / 100.0f));
	
	wTransform.translate (0.0, reg.boundingRect ().top () * 2, 0.0f);
	wTransform.scale (1.0f, -1.0f, 1.0f);

	if (window->alpha () || fA.getOpacity () != OPAQUE)
	    mask |= PAINT_WINDOW_TRANSLUCENT_MASK;

	mask |= PAINT_WINDOW_TRANSFORMED_MASK;
	
	glPushMatrix ();
	glLoadMatrixf (wTransform.getMatrix ());
	
	glPushAttrib (GL_SCISSOR_BIT);
	glEnable (GL_SCISSOR_TEST);
	glCullFace (cullInv);
	
	/* For every single rect that the client gave us, draw another clipped reflected window
	* FIXME: this approach really sucks and is a waste of CPU time for complicated shapes
	* is there some other clever GL function we can use here?
	*/
	
	foreach (const CompRect &r, reg.rects ())
	{
	    CompRect clipBox (r);
	    
	    glScissor (clipBox.x1 (), screen->height () - clipBox.y2 (), clipBox.width (), clipBox.height ());
	    
	    status = gWindow->glDraw (wTransform, fA, region, mask);
	}
	
	glCullFace (cull);
	glDisable (GL_SCISSOR_TEST);
	glPopAttrib ();
	glPopMatrix ();

    }
    
    return status;
}

bool
WinreflectWindow::damageRect (bool initial, const CompRect &rect)
{    
    CompRegion r = window->region ();
    
    r = r.united (r.translated (0, r.boundingRect ().height ()));
    r.shrink (-100, -100);
    
    WinreflectScreen::get (screen)->cScreen->damageRegion (r);
    
    return cWindow->damageRect (initial, rect);
}

WinreflectWindow::WinreflectWindow (CompWindow *w) :
    PluginClassHandler <WinreflectWindow, CompWindow> (w),
    window (w),
    cWindow (CompositeWindow::get (w)),
    gWindow (GLWindow::get (w))
{
    CompositeWindowInterface::setHandler (cWindow);
    GLWindowInterface::setHandler (gWindow);
}

WinreflectScreen::WinreflectScreen (CompScreen *s) :
    PluginClassHandler <WinreflectScreen, CompScreen> (s),
    cScreen (CompositeScreen::get (s)),
    gScreen (GLScreen::get (s)),
    mReflectedRegionAtom (XInternAtom (s->dpy (), "_COMPIZ_NET_CM_REFLECTED_REGION", FALSE)),
    mIsTransformed (false)
{    
    ScreenInterface::setHandler (screen);
    GLScreenInterface::setHandler (gScreen);
    
    optionSetReflectDocksNotify (boost::bind (&WinreflectScreen::optionChange, this, _1, _2));
}

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