summaryrefslogtreecommitdiff
path: root/anaglyph.c
blob: c161a3042499a706a78104374968562611a26476 (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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/**
 * Compiz-plugin:   anaglyph
 * Author:          Patryk Kowalczyk <wodor@wodor.org>
 * created:         September 2007
 * License:         GPL
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <compiz-core.h>
#include "anaglyph_options.h"


#define GET_ANAGLYPH_CORE(c) \
	((AnaglyphCore *) (c)->base.privates[corePrivateIndex].ptr)

#define ANAGLYPH_CORE(c) \
	AnaglyphCore *ac = GET_ANAGLYPH_CORE (c)

#define GET_ANAGLYPH_DISPLAY(d) \
	((AnaglyphDisplay *) (d)->base.privates[displayPrivateIndex].ptr)

#define ANAGLYPH_DISPLAY(d)  \
	AnaglyphDisplay *ad = GET_ANAGLYPH_DISPLAY (d)

#define GET_ANAGLYPH_SCREEN(s, ad)  \
	((AnaglyphScreen *) (s)->base.privates[(ad)->screenPrivateIndex].ptr)

#define ANAGLYPH_SCREEN(s)  \
	AnaglyphScreen *as = GET_ANAGLYPH_SCREEN (s, GET_ANAGLYPH_DISPLAY (s->display))

#define GET_ANAGLYPH_WINDOW(w, as) \
	((AnaglyphWindow *) (w)->base.privates[(as)->windowPrivateIndex].ptr)

#define ANAGLYPH_WINDOW(w) \
	AnaglyphWindow *aw = GET_ANAGLYPH_WINDOW (w,  \
				GET_ANAGLYPH_SCREEN (w->screen,  \
				GET_ANAGLYPH_DISPLAY (w->screen->display)))


#define NUM_OPTIONS(s) (sizeof ((s)->opt) / sizeof (CompOption))

/*
 * pointer to display list
 *
 */
static int displayPrivateIndex;
static int corePrivateIndex;


typedef struct _AnaglyphCore {
	ObjectAddProc			objectAdd;
} AnaglyphCore;

typedef struct _AnaglyphDisplay {
	int				screenPrivateIndex;
} AnaglyphDisplay;

typedef struct _AnaglyphScreen {
	int				windowPrivateIndex;
	PaintWindowProc 		paintWindow;
//	PreparePaintScreenProc		preparePaintScreen;
	DonePaintScreenProc		donePaintScreen;
	PaintOutputProc			paintOutput;
//	PaintTransformedOutputProc 	paintTransformedOutput;
	DamageWindowRectProc		damageWindowRect;
	Bool isAnaglyph;
	Bool isDamage;
} AnaglyphScreen;

typedef struct _AnaglyphWindow {
	Bool isAnaglyph;
} AnaglyphWindow;


static void 
toggleAnaglyphWindow (CompWindow *w)
{

	ANAGLYPH_WINDOW(w);

	aw->isAnaglyph = !aw->isAnaglyph;	

	if (matchEval (anaglyphGetExcludeMatch(w->screen), w))
		aw->isAnaglyph = FALSE;

	if(w->redirected && !aw->isAnaglyph)
		damageScreen(w->screen);
	
	addWindowDamage(w);
}

 
static void 
toggleAnaglyphScreen (CompScreen *s)
{
	CompWindow *w;

	ANAGLYPH_SCREEN(s);
	//	compLogMessage (s->display, "anaglyph", CompLogLevelInfo, "info");
	as->isAnaglyph = !as->isAnaglyph;
	
	for (w = s->windows; w; w = w->next)
		if (w)
			toggleAnaglyphWindow (w);

}


static Bool
anaglyphAnaglyphWindow (CompDisplay *d,
			CompAction *action,
			CompActionState state,
			CompOption *option,
			int nOption)
{
	CompWindow *w;
	Window xid;
	
	xid = getIntOptionNamed(option, nOption, "window", 0);
	
	w = findWindowAtDisplay(d, xid);


	if (w){
		if (w->attrib.override_redirect)
			return FALSE;

		toggleAnaglyphWindow(w);
	}
	return FALSE;
}

static Bool
anaglyphAnaglyphScreen (CompDisplay *d,
			CompAction *action,
			CompActionState state,
			CompOption *option,
			int nOption)
{
	CompScreen *s;
	Window     xid;

	xid = getIntOptionNamed (option, nOption, "root", 0);

	s = findScreenAtDisplay (d, xid);

    	if (s){
		toggleAnaglyphScreen (s);
		return TRUE;
	}
	return FALSE;
}

//----------------------------------------------------------------------- MASTER FUNCTION

static Bool anaglyphDrawWindow(CompWindow * w,
					 const WindowPaintAttrib  *attrib,
					 const CompTransform *transform,
					 Region	region,
					 unsigned int mask)
{	
	Bool status;
	CompScreen* s = w->screen;
	ANAGLYPH_SCREEN(s);
	ANAGLYPH_WINDOW(w);
	
	//if (((aw->isAnaglyph != as->isAnaglyph) || (as->isAnaglyph && aw->isNew)) && w->texture->pixmap)
	if(aw->isAnaglyph && w->texture->pixmap)
	{
		//if (as->isAnaglyph)
		//	aw->isAnaglyph = FALSE;

		int oldFilter = s->display->textureFilter;

		if (anaglyphGetMipmaps (s))	
			s->display->textureFilter = GL_LINEAR_MIPMAP_LINEAR;

		mask |= PAINT_WINDOW_TRANSFORMED_MASK;
		
		CompTransform sTransform = *transform;
		WindowPaintAttrib wa = *attrib;

		//Window xid;
		//CompWindow *activeWindow;
		//xid = getActiveWindow(s->display, s->root);
		//activeWindow=findWindowAtScreen(s, xid);

		float offset = anaglyphGetOffset (s);
		float desktopOffset = anaglyphGetDesktopOffset (s);

		if (anaglyphGetDesaturate (s))
			wa.saturation = 0.0f;

		UNWRAP(as, s, paintWindow);

		//BLUE and ...
		glColorMask(GL_FALSE,GL_TRUE,GL_TRUE,GL_FALSE);
			if (w->type & CompWindowTypeDesktopMask) //desktop 
				matrixTranslate (&sTransform, offset*desktopOffset, 0.0f, 0.0f);
			else if (w->type & CompWindowTypeDockMask) // dock
				matrixTranslate (&sTransform,  0.0f, 0.0f, 0.0f);
			else if (w->state & CompWindowStateStickyMask) // sticky
				matrixTranslate (&sTransform, offset*desktopOffset, 0.0f, 0.0f);
			else if (w->state & CompWindowStateBelowMask) //below
				matrixTranslate (&sTransform, offset, 0.0f, 0.0f);
			else if (w->state & CompWindowStateAboveMask) // above 
				matrixTranslate (&sTransform, -offset*4.0, 0.0f, 0.0f);
			else if (w->id == w->screen->display->activeWindow) // active window
				matrixTranslate (&sTransform, -offset*3.0, 0.0f, 0.0f);
			else //other windows
				matrixTranslate (&sTransform, -offset, 0.0f, 0.0f);
		status = (*s->paintWindow) (w, &wa, &sTransform, region, mask);
		
		//RED
		glColorMask(GL_TRUE,GL_FALSE,GL_FALSE,GL_FALSE);
			if (w->type & CompWindowTypeDesktopMask) //desktop 
				matrixTranslate (&sTransform, -offset*2.0*desktopOffset, 0.0f, 0.0f);
			else if (w->type & CompWindowTypeDockMask)// dock
				matrixTranslate (&sTransform, 0.0f, 0.0f, 0.0f);
			else if (w->state & CompWindowStateStickyMask) // sticky
				matrixTranslate (&sTransform, -offset*2.0*desktopOffset, 0.0f, 0.0f);
			else if (w->state & CompWindowStateBelowMask) //below
				matrixTranslate (&sTransform, -offset*2.0, 0.0f, 0.0f);
			else if (w->state & CompWindowStateAboveMask) //above
				matrixTranslate (&sTransform, offset*4.0, 0.0f, 0.0f);
			else if (w->id == w->screen->display->activeWindow) // active window
				matrixTranslate (&sTransform, offset*3.0, 0.0f, 0.0f);
			else //other windows
				matrixTranslate (&sTransform, offset*2.0, 0.0f, 0.0f);
		status = (*s->paintWindow) (w, &wa, &sTransform, region, mask);

		WRAP(as, s, paintWindow, anaglyphDrawWindow);

			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

		s->display->textureFilter = oldFilter;
	}
	else
	{
		UNWRAP(as, s, paintWindow);
			status = (*s->paintWindow) (w, attrib, transform, region, mask);
		WRAP(as, s, paintWindow, anaglyphDrawWindow);
	}
	return status;
}

//------------------------------------------------------------------------------DAMAGE FUNCTION

static Bool anaglyphDamageWindowRect(CompWindow *w,
					Bool initial,
					BoxPtr rect){

	Bool status = FALSE;
	
	ANAGLYPH_SCREEN(w->screen);
	ANAGLYPH_WINDOW(w);
	
/*	if (initial){
		damageScreen(w->screen);
		status = TRUE;	
	}	
	else*/ if (aw->isAnaglyph || as->isAnaglyph || as->isDamage)
	{
		as->isDamage = TRUE;
		if (!aw->isAnaglyph && !as->isAnaglyph)
			as->isDamage = FALSE;

		damageScreen(w->screen);
		status = TRUE;
	}

	UNWRAP(as, w->screen, damageWindowRect);
		status |= (*w->screen->damageWindowRect)(w, initial, rect);
	WRAP(as, w->screen, damageWindowRect, anaglyphDamageWindowRect);

	return status;
}

//----------------------------------------------------------------------------------- PAINT OUTPUT

static Bool anaglyphPaintOutput(CompScreen *s, const ScreenPaintAttrib *sAttrib, 
	const CompTransform *transform, Region region, CompOutput *output, unsigned int mask){

	Bool status;

	ANAGLYPH_SCREEN(s);

	mask |= PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS_MASK;

	UNWRAP(as, s, paintOutput);
		status = (*s->paintOutput)(s, sAttrib, transform, region, output, mask);
	WRAP(as, s, paintOutput, anaglyphPaintOutput);

	return status;
}

//-------------------------------------------------------------------------------

static void
anaglyphWindowAdd (CompScreen *s,
			CompWindow *w)
{
	ANAGLYPH_SCREEN (s);

    if (as->isAnaglyph && matchEval (anaglyphGetAnaglyphMatch (s), w))
	toggleAnaglyphWindow (w);
}

static void
anaglyphScreenOptionChanged (CompScreen       *s,
				CompOption       *opt,
				AnaglyphScreenOptions num)
{
    switch (num)
    {
    case AnaglyphScreenOptionAnaglyphMatch:
    case AnaglyphScreenOptionExcludeMatch:
	{
	    CompWindow *w;
	    ANAGLYPH_SCREEN (s);

	    for (w = s->windows; w; w = w->next)
	    {
		Bool isAnaglyph;
		ANAGLYPH_WINDOW (w);

		isAnaglyph = matchEval (anaglyphGetAnaglyphMatch (s), w);
		isAnaglyph = isAnaglyph && !matchEval (anaglyphGetExcludeMatch (s), w);

		if (isAnaglyph && as->isAnaglyph && !aw->isAnaglyph)
		    toggleAnaglyphWindow (w);
		else if (!isAnaglyph && aw->isAnaglyph)
		    toggleAnaglyphWindow (w);
	    }
	}
	break;
    default:
	break;
    }
}

// ------------------------------------------------------------------------------- OBJECT ADD

static void
anaglyphObjectAdd (CompObject *parent,
			CompObject *object)
{
    static ObjectAddProc dispTab[] = {
	(ObjectAddProc) 0, /* CoreAdd */
        (ObjectAddProc) 0, /* DisplayAdd */
        (ObjectAddProc) 0, /* ScreenAdd */
        (ObjectAddProc) anaglyphWindowAdd
    };

    ANAGLYPH_CORE (&core);

    UNWRAP (ac, &core, objectAdd);
    (*core.objectAdd) (parent, object);
    WRAP (ac, &core, objectAdd, anaglyphObjectAdd);

    DISPATCH (object, dispTab, ARRAY_SIZE (dispTab), (parent, object));
}

// ------------------------------------------------------------------------------- CORE

static Bool
anaglyphInitCore (CompPlugin *p,
  	     CompCore   *c)
{
    AnaglyphCore *ac;

    if (!checkPluginABI ("core", CORE_ABIVERSION))
        return FALSE;

    ac = malloc (sizeof (AnaglyphCore));
    if (!ac)
        return FALSE;

    displayPrivateIndex = allocateDisplayPrivateIndex ();
    if (displayPrivateIndex < 0)
    {
        free (ac);
        return FALSE;
    }

    WRAP (ac, c, objectAdd, anaglyphObjectAdd);

    c->base.privates[corePrivateIndex].ptr = ac;

    return TRUE;
}


static void
anaglyphFiniCore (CompPlugin *p,
			CompCore   *c)
{
    ANAGLYPH_CORE (c);

    freeDisplayPrivateIndex (displayPrivateIndex);

    UNWRAP (ac, c, objectAdd);

    free (ac);
}

// ------------------------------------------------------------------------------- DISPLAY

static Bool
anaglyphInitDisplay (CompPlugin *p, CompDisplay *d)
{
    AnaglyphDisplay *ad;

    ad = malloc (sizeof (AnaglyphDisplay));
    if (!ad)
        return FALSE;


	ad->screenPrivateIndex = allocateScreenPrivateIndex (d);	
	if (ad->screenPrivateIndex < 0)
	{
        	free (ad);
        	return FALSE;
	}

	anaglyphSetWindowToggleKeyInitiate (d, anaglyphAnaglyphWindow);
	anaglyphSetScreenToggleKeyInitiate (d, anaglyphAnaglyphScreen);
	anaglyphSetWindowToggleButtonInitiate (d, anaglyphAnaglyphWindow);
	anaglyphSetScreenToggleButtonInitiate (d, anaglyphAnaglyphScreen);

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

	return TRUE;
}

static void
anaglyphFiniDisplay (CompPlugin *p, CompDisplay *d)
{
    ANAGLYPH_DISPLAY (d);

    freeScreenPrivateIndex (d, ad->screenPrivateIndex);

    free (ad);
}

// ------------------------------------------------------------------------------- SCREEN

static Bool
anaglyphInitScreen (CompPlugin *p, CompScreen *s)
{
	AnaglyphScreen *as;

	ANAGLYPH_DISPLAY (s->display);

	as = malloc (sizeof (AnaglyphScreen));
	if (!as)
		return FALSE;

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

	anaglyphSetAnaglyphMatchNotify (s, anaglyphScreenOptionChanged);
	anaglyphSetExcludeMatchNotify (s, anaglyphScreenOptionChanged);

	WRAP(as, s, paintOutput, anaglyphPaintOutput);
	WRAP(as, s, paintWindow, anaglyphDrawWindow);
	WRAP(as, s, damageWindowRect, anaglyphDamageWindowRect);

	s->base.privates[ad->screenPrivateIndex].ptr = as;
	
	return TRUE;
}

static void
anaglyphFiniScreen (CompPlugin *p, CompScreen *s)
{
	ANAGLYPH_SCREEN (s);

	freeWindowPrivateIndex (s, as->windowPrivateIndex);
	
	UNWRAP(as, s, paintOutput);
	UNWRAP(as, s, paintWindow);
	UNWRAP(as, s, damageWindowRect);

	free (as);
}

// ------------------------------------------------------------------------------- WINDOW

static Bool anaglyphInitWindow(CompPlugin * p, CompWindow * w)
{
	AnaglyphWindow *aw;

	ANAGLYPH_SCREEN(w->screen);

	aw = malloc(sizeof(AnaglyphWindow));
	if (!aw)
		return FALSE;

	aw->isAnaglyph = FALSE;

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

	return TRUE;
}

static void anaglyphFiniWindow(CompPlugin * p, CompWindow * w)
{
	ANAGLYPH_WINDOW(w);

	free(aw);
}

// ------------------------------------------------------------------------------- INITFINI

/*
 * anaglyphInit
 *
 */

static Bool
anaglyphInit (CompPlugin *p)
{
	corePrivateIndex = allocateCorePrivateIndex ();
	if (corePrivateIndex < 0)
		return FALSE;

    return TRUE;
}

/*
 * anaglyphFini
 *
 */

static void
anaglyphFini (CompPlugin *p)
{
	
	if (corePrivateIndex >= 0)
		freeCorePrivateIndex (corePrivateIndex);

}

// ------------------------------------------------------------------------------- OBJECTS

static CompBool
anaglyphInitObjects(CompPlugin *p,
              CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
        (InitPluginObjectProc) anaglyphInitCore,
        (InitPluginObjectProc) anaglyphInitDisplay,
        (InitPluginObjectProc) anaglyphInitScreen,
	(InitPluginObjectProc) anaglyphInitWindow
        };

    RETURN_DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), TRUE, (p, o));

}

static void
anaglyphFiniObjects(CompPlugin *p,
               CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
        (FiniPluginObjectProc) anaglyphFiniCore,
        (FiniPluginObjectProc) anaglyphFiniDisplay,
        (FiniPluginObjectProc) anaglyphFiniScreen,
	(FiniPluginObjectProc) anaglyphFiniWindow
    };

    DISPATCH (o, dispTab, ARRAY_SIZE (dispTab), (p, o));
}

static CompPluginVTable anaglyphVTable = {
	"anaglyph",
	0,
    	anaglyphInit,
    	anaglyphFini,
    	anaglyphInitObjects,
    	anaglyphFiniObjects,
	0,
	0
};

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