summaryrefslogtreecommitdiff
path: root/src/locker.cpp
blob: 34355ca319e529b64c08f5698ed417a7ad30c152 (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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include "locker.h"
#include <cmath>
#include <limits.h>

COMPIZ_PLUGIN_20090315 (locker, LockerPluginVTable);

namespace {
    static LockerLockWindow     *mWindowDefault = NULL;
    static LockerLockBackground *mBackgroundDefault = NULL;
}

LockerLockWindow *
LockerLockWindow::Default ()
{
    return mWindowDefault;
}

void
LockerLockWindow::SetDefault (LockerLockWindow *l)
{
    mWindowDefault = l;
}

LockerLockBackground *
LockerLockBackground::Default ()
{
    return mBackgroundDefault;
}

void
LockerLockBackground::SetDefault (LockerLockBackground *b)
{
    mBackgroundDefault = b;
}

void
SimpleLockerWindow::hide ()
{
    mShowing = false;
}

void
SimpleLockerWindow::show ()
{
    mShowing = true;
}

bool
SimpleLockerWindow::visible ()
{
    return mShowing;
}

void
SimpleLockerWindow::paint (const GLMatrix &transform)
{
    /* Paint a white box in the center of the screen */

    glPushMatrix ();
    glLoadMatrixf (transform.getMatrix ());

    glColor4f (1.0f, 1.0f, 1.0f, 1.0f);

    glBegin (GL_QUADS);
    glVertex3f (x () + width () / 2.0f - 150, y () + height () / 2.0f + 150, 0.0f);
    glVertex3f (x () + width () / 2.0f + 150, y () + height ()  / 2.0f + 150, 0.0f);
    glVertex3f (x () + width () / 2.0f + 150, y () + height ()  / 2.0f - 150, 0.0f);
    glVertex3f (x () + width () / 2.0f - 150, y () + height ()  / 2.0f - 150, 0.0f);
    glEnd ();

    glPopMatrix ();

    mDamage += CompRect (x () + width () / 2.0f - 150, y () + height () / 2.0f - 150, 300, 300);
}

bool
SimpleLockerWindow::handleKeyPress (XKeyEvent *xk)
{
    return true;
}

bool
SimpleLockerWindow::handleKeyRelease (XKeyEvent *xk)
{
    return true;
}

bool
SimpleLockerWindow::handleButtonPress (XButtonEvent *xb)
{
    return true;
}

bool
SimpleLockerWindow::handleButtonRelease (XButtonEvent *xb)
{
    return true;
}

bool
SimpleLockerWindow::handleMotion (XMotionEvent *xm)
{
    return true;
}

void
SimpleLockerBackground::hide ()
{
    mShowing = false;
}

void
SimpleLockerBackground::show ()
{
    mShowing = true;
}

bool
SimpleLockerBackground::visible ()
{
    return mShowing;
}

CompRegion &
SimpleLockerBackground::damage ()
{
    return mDamage;
}

void
SimpleLockerBackground::paint (const GLMatrix &transform)
{
    /* Paint a multicolored box in the center of the screen */

    glPushMatrix ();
    glLoadMatrixf (transform.getMatrix ());

    glBegin (GL_QUADS);
    glColor4f (1.0f, 0.0f, 0.0f, 1.0f);
    glVertex3f (x (), y () + height (), 0.0f);
    glColor4f (0.0f, 1.0f, 0.0f, 1.0f);
    glVertex3f (x () + width (), y () + height (), 0.0f);
    glColor4f (0.0f, 0.0f, 1.0f, 1.0f);
    glVertex3f (x () + width (), y (), 0.0f);
    glColor4f (0.0f, 0.0f, 0.0f, 1.0f);
    glVertex3f (x (), y (), 0.0f);
    glEnd ();

    glPopMatrix ();

    mDamage += CompRect (x (), y (), width (), height ());
}


CompRegion &
SimpleLockerWindow::damage ()
{
    return mDamage;
}

void
LockerScreen::handleEvent (XEvent *event)
{
    LockerLockWindow *lockWindow = LockerLockWindow::Default ();
    bool		 processed = false;

    switch (event->type)
    {
    case ButtonPress:
	wake ();
	if (lockWindow && lockWindow->needsGrab () && mAnimationState == AnimationStateLocked)
	    processed = lockWindow->handleButtonPress ((XButtonEvent *) event);
	break;
    case ButtonRelease:
	wake ();
	if (lockWindow && lockWindow->needsGrab () && mAnimationState == AnimationStateLocked)
	    processed = lockWindow->handleButtonRelease ((XButtonEvent *) event);
	break;
    case KeyPress:
	wake ();
	if (lockWindow && lockWindow->needsGrab () && mAnimationState == AnimationStateLocked)
	    processed = lockWindow->handleKeyPress ((XKeyEvent *) event);
	break;
    case KeyRelease:
	wake ();
	if (lockWindow && lockWindow->needsGrab () && mAnimationState == AnimationStateLocked)
	    processed = lockWindow->handleKeyRelease ((XKeyEvent *) event);
	break;
    case MotionNotify:
	wake ();
	if (lockWindow && lockWindow->needsGrab () && mAnimationState == AnimationStateLocked)
	    processed = lockWindow->handleMotion ((XMotionEvent *) event);
	break;
    }

    if (!processed)
	screen->handleEvent (event);
}

void
LockerScreen::preparePaint (int ms)
{
    if (mAnimationState == AnimationStateLocking)
    {
	mAnimationProgress += (ms / (float) optionGetLockAnimationDuration ());

	if (mAnimationProgress >= 1.0f)
	    mAnimationProgress = 1.0f;

	foreach (CompWindow *w, screen->windows ())
	    LockerWindow::get (w)->setAnimationProgress (mAnimationProgress);
    }

    if (mAnimationState == AnimationStateUnlocking)
    {
	mAnimationProgress -= (ms / (float) optionGetUnlockAnimationDuration ());

	if (mAnimationProgress <= 0.0f)
	    mAnimationProgress = 0.0f;

	foreach (CompWindow *w, screen->windows ())
	    LockerWindow::get (w)->setAnimationProgress (mAnimationProgress);
    }

    cScreen->preparePaint (ms);
}

bool
LockerScreen::glPaintOutput (const GLScreenPaintAttrib &attrib,
			     const GLMatrix	       &transform,
			     const CompRegion	       &region,
			     CompOutput                *output,
			     unsigned int	       mask)
{
    LockerLockWindow *lockWindow = LockerLockWindow::Default ();
    LockerLockBackground *lockScreen = LockerLockBackground::Default ();
    bool		status;

    if (mAnimationState != AnimationStateIdle &&
	mAnimationState != AnimationStateLocked)
    {
	mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS_MASK;
	mask |= PAINT_SCREEN_TRANSFORMED_MASK;
	mask |= PAINT_SCREEN_CLEAR_MASK;
    }

    /* Nothing shall pass - clear the entire backbuffer and short circuit
     * the paint chain and then paint our locker windows, we absolutely do
     * not want anything else to go on top of us */
    if (mAnimationState == AnimationStateLocked)
    {
	GLint box[4];
	GLboolean scissoringEnabled = glIsEnabled (GL_SCISSOR_TEST);
	GLMatrix lTransform (transform);
	glGetIntegerv (GL_SCISSOR_BOX, box);
	if (!scissoringEnabled)
	    glEnable (GL_SCISSOR_TEST);

	lTransform.toScreenSpace (output, -DEFAULT_Z_CAMERA);

	glScissor (output->x (), output->y (), output->width (), output->height ());
	glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
	glClear (GL_COLOR_BUFFER_BIT);
	/* We could probably be a little smarter here, like binding
	 * an fbo and then never rendering it, it's not possible to break out
	 * of something like that */
	glScissor (0, 0, 0, 0);
	gScreen->glPaintOutputSetCurrentIndex (INT_MAX);
	status = gScreen->glPaintOutput (attrib, transform, region, output, mask);
	glScissor (box[0], box[1], box[2], box[3]);
	if (!scissoringEnabled)
	    glDisable (GL_SCISSOR_TEST);

	/* Now paint the lockers */
	if (lockScreen)
	{
	    CompRegion damage;
	    lockScreen->setGeometry (output->x (), output->y (),
				     output->width (), output->height ());
	    lockScreen->paint (lTransform);
	}

	if (lockWindow)
	{
	    CompRegion damage;
	    lockWindow->setGeometry (output->x (), output->y (),
				     output->width (), output->height ());
	    lockWindow->paint (lTransform);
	}

    }
    else
	status = gScreen->glPaintOutput (attrib, transform, region, output, mask);

    return status;
}

void
LockerScreen::donePaint ()
{
    LockerLockWindow *lockWindow = LockerLockWindow::Default ();
    LockerLockBackground *lockBackground = LockerLockBackground::Default ();

    if (mAnimationState == AnimationStateLocking ||
	mAnimationState == AnimationStateUnlocking)
    {
	if (mAnimationState == AnimationStateLocking &&
	    mAnimationProgress == 1.0f)
	{
	    mAnimationState = AnimationStateLocked;
	}

	if (mAnimationState == AnimationStateUnlocking &&
	    mAnimationProgress == 0.0f)
	{
	    mAnimationState = AnimationStateIdle;
	}

	cScreen->damageScreen ();
    }
    else if (mAnimationState == AnimationStateLocked)
    {
	if (lockWindow)
	{
	    cScreen->damageRegion (lockWindow->damage ());
	    lockWindow->damage () = CompRegion ();
	}

	if (lockBackground)
	{
	    cScreen->damageRegion (lockBackground->damage ());
	    lockBackground->damage () = CompRegion ();
	}
    }

    cScreen->donePaint ();
}

bool
LockerScreen::wake ()
{
    LockerLockWindow     *lockWindow = LockerLockWindow::Default ();
    LockerLockBackground *lockBackground = LockerLockBackground::Default ();

    if (mAnimationState == AnimationStateLocked)
    {
	/* No default locking implementation provided, just unlock
	 * the screen */
	if (!lockWindow)
	    unlock ();
	else
	{
	    mWaitTimer.setTimes (30000, 30000);
	    mWaitTimer.start ();

	    if (lockWindow)
		lockWindow->show ();

	    if (lockBackground)
		lockBackground->show ();
	}
    }
    else
    {
	mWaitTimer.setTimes (optionGetTimeout () * 1000, optionGetTimeout () * 1000);
    }

    return false;
}

bool
LockerScreen::lock ()
{
    LockerLockWindow *lockWindow = LockerLockWindow::Default ();

    if (mAnimationState != AnimationStateLocked ||
	mAnimationState != AnimationStateLocking)
	mAnimationState = AnimationStateLocking;

    if (lockWindow)
    {
	if (!mGrabIndex)
	{
	    if (lockWindow->needsGrab ())
	    {
		mGrabIndex = screen->pushGrab (screen->invisibleCursor (), "locker");
	    }
	}
    }

    return false;
}

bool
LockerScreen::unlock ()
{
    if (mAnimationState != AnimationStateUnlocking ||
	mAnimationState != AnimationStateIdle)
	mAnimationState = AnimationStateUnlocking;

    if (mGrabIndex)
    {
	screen->removeGrab (mGrabIndex, NULL);
	mGrabIndex = 0;
    }

    return false;
}
    

bool
LockerWindow::glPaint (const GLWindowPaintAttrib &attrib,
		       const GLMatrix	     &transform,
		       const CompRegion	     &region,
		       unsigned int		     mask)
{
    GLWindowPaintAttrib wAttrib (attrib);
    GLMatrix            wTransform (transform);

    /* A simple fade in / out will do */
    if (isAnimatedWindow ())
    {
	wAttrib.brightness = (int) ((1.0f - mAnimationProgress) * (float) attrib.brightness);
	wTransform.translate (0.0f, 0.0f, -mAnimationProgress * 0.05);

	mask |= PAINT_WINDOW_TRANSFORMED_MASK;
    }

    return gWindow->glPaint (wAttrib, wTransform, region, mask);
}

bool
LockerWindow::isAnimatedWindow ()
{
    if (window->isViewable ())
	return true;

    return false;
}

LockerScreen::LockerScreen (CompScreen *s) :
    PluginClassHandler <LockerScreen, CompScreen> (s),
    cScreen (CompositeScreen::get (screen)),
    gScreen (GLScreen::get (screen)),
    mAnimationProgress (0.0f),
    mAnimatedWindows (0),
    mAnimationState (AnimationStateIdle),
    mSLW (new SimpleLockerWindow ()),
    mSLB (new SimpleLockerBackground ())
{
    ScreenInterface::setHandler (screen);
    CompositeScreenInterface::setHandler (cScreen);
    GLScreenInterface::setHandler (gScreen);

    LockerLockWindow::SetDefault (mSLW);
    LockerLockBackground::SetDefault (mSLB);

    mWaitTimer.setCallback (boost::bind (&LockerScreen::lock, this));
    mWaitTimer.setTimes (optionGetTimeout () * 100, optionGetTimeout () * 100);
    mWaitTimer.start ();
}

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

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

    return true;
}