summaryrefslogtreecommitdiff
path: root/fakeargb.c
blob: 8b69e85986e8c47a41ef254a0bc5ede40cf98a6f (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
/**
 *
 * Beryl useless transparent terminals eyecandy plugin plugin
 *
 * fakeargb.c
 *
 * Copyright (c) 2006 Robert Carr <racarr@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.
 *
 **/

#include <compiz-core.h>
#include "fakeargb_options.h"

static int displayPrivateIndex;

typedef struct _FakeDisplay
{
    int screenPrivateIndex;
} FakeDisplay;

typedef struct _FakeScreen
{
    int windowPrivateIndex;

    DrawWindowTextureProc drawWindowTexture;

    int  handle;
    Bool black;
} FakeScreen;

typedef struct _FakeWindow
{
    Bool isFaked;
} FakeWindow;

#define GET_FAKE_DISPLAY(d) \
    ((FakeDisplay *) (d)->base.privates[displayPrivateIndex].ptr)
#define FAKE_DISPLAY(d) \
    FakeDisplay *fd = GET_FAKE_DISPLAY (d)

#define GET_FAKE_SCREEN(s, nd) \
    ((FakeScreen *) (s)->base.privates[(nd)->screenPrivateIndex].ptr)
#define FAKE_SCREEN(s) \
    FakeScreen *fs = GET_FAKE_SCREEN (s, GET_FAKE_DISPLAY (s->display))

#define GET_FAKE_WINDOW(w, ns) \
    ((FakeWindow *) (w)->base.privates[(ns)->windowPrivateIndex].ptr)
#define FAKE_WINDOW(w) \
    FakeWindow *fw = GET_FAKE_WINDOW (w,                   \
		     GET_FAKE_SCREEN  (w->screen,          \
		     GET_FAKE_DISPLAY (w->screen->display)))

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

static void
fakeToggleWindow (CompWindow *w)
{
    FAKE_WINDOW (w);
    FAKE_SCREEN (w->screen);

    if (fs->black)
	fw->isFaked = !fw->isFaked;
    if (fw->isFaked)
    {
	fs->handle = 0;
	fs->black = !fs->black;
    }

    addWindowDamage(w);
}

static Bool
fakeToggle (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)
	fakeToggleWindow (w);

    return TRUE;
}

static int
getFakeFragmentFunction (CompScreen  *s,
			 CompTexture *texture)
{
    CompFunctionData *data;
    int              target;

    FAKE_SCREEN (s);

    if (texture->target == GL_TEXTURE_2D)
	target = COMP_FETCH_TARGET_2D;
    else
	target = COMP_FETCH_TARGET_RECT;

    if (fs->handle)
	return fs->handle;

    data = createFunctionData ();
    if (data)
    {
	Bool ok = TRUE;
	int  handle = 0;

	ok &= addTempHeaderOpToFunctionData (data, "neg" );
	ok &= addTempHeaderOpToFunctionData(data, "temp");

	ok &= addFetchOpToFunctionData (data, "output", NULL, target);
	ok &= addDataOpToFunctionData (data, "RCP neg.a, output.a;");
 		
	ok &= addDataOpToFunctionData (data,
				       "MUL output.rgb, output.a, output;");
	 		
	ok &= addDataOpToFunctionData(data, "MOV temp, output;");
		
	if (!fs->black)
    	    ok &= addDataOpToFunctionData(data, "SUB temp.rgb, 1.0, temp;");
		
	ok &= addDataOpToFunctionData(data, "ADD output.a, 0, temp.r;");
	ok &= addDataOpToFunctionData(data, "ADD output.a, output.a, temp.g;");
	ok &= addDataOpToFunctionData(data, "ADD output.a, output.a, temp.b;");
	ok &= addDataOpToFunctionData(data, "MUL output.a, output.a, 0.33;");
	ok &= addDataOpToFunctionData (data,
				       "MUL output.rgb, output.a, output;");

	ok &= addColorOpToFunctionData (data, "output", "output");

	if (!ok)
	{
	    destroyFunctionData (data);
	    return 0;
	}

	handle = createFragmentFunction (s, "fake", data);
	fs->handle = handle;

	destroyFunctionData (data);

	return handle;
    }

    return 0;
}

static void
fakeDrawWindowTexture (CompWindow           *w,
		       CompTexture          *texture,
		       const FragmentAttrib *attrib,
		       unsigned int         mask)
{
    CompScreen *s = w->screen;

    FAKE_SCREEN (s);
    FAKE_WINDOW (w);
	
    if (fw->isFaked && (texture->name == w->texture->name) &&
	s->fragmentProgram)
    {
	FragmentAttrib fa = *attrib;
	int            function;

	function = getFakeFragmentFunction (s, texture);
	if (function)
	    addFragmentFunction (&fa, function);

	UNWRAP (fs, s, drawWindowTexture);
	(*s->drawWindowTexture) (w, texture, &fa, mask);
	WRAP (fs, s, drawWindowTexture, fakeDrawWindowTexture);
    }
    else
    {
	UNWRAP (fs, s, drawWindowTexture);
	(*s->drawWindowTexture) (w, texture, attrib, mask);
	WRAP(fs, s, drawWindowTexture, fakeDrawWindowTexture);
    }
}

static Bool
fakeInitDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    FakeDisplay *fd;

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

    fd = malloc (sizeof (FakeDisplay));
    if (!fd)
	return FALSE;

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

    fakeargbSetWindowToggleKeyInitiate (d, fakeToggle);
    fakeargbSetWindowToggleButtonInitiate (d, fakeToggle);

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

    return TRUE;
}

static void
fakeFiniDisplay (CompPlugin  *p,
		 CompDisplay *d)
{
    FAKE_DISPLAY (d);

    freeScreenPrivateIndex (d, fd->screenPrivateIndex);
    free (fd);
}

static Bool
fakeInitScreen (CompPlugin *p,
		CompScreen *s)
{
    FakeScreen *fs;

    FAKE_DISPLAY (s->display);

    fs = malloc (sizeof (FakeScreen));
    if (!fs)
	return FALSE;

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

    fs->handle = 0;

    WRAP (fs, s, drawWindowTexture, fakeDrawWindowTexture);

    s->base.privates[fd->screenPrivateIndex].ptr = fs;

    fs->black = TRUE;

    return TRUE;
}

static void
fakeFiniScreen (CompPlugin *p,
		CompScreen *s)
{
    FAKE_SCREEN (s);

    freeWindowPrivateIndex (s, fs->windowPrivateIndex);

    UNWRAP (fs, s, drawWindowTexture);
	
    free (fs);
}

static Bool
fakeInitWindow (CompPlugin *p,
		CompWindow *w)
{
    FakeWindow *fw;

    FAKE_SCREEN (w->screen);

    fw = malloc (sizeof (FakeWindow));
    if (!fw)
	return FALSE;

    fw->isFaked = FALSE;

    w->base.privates[fs->windowPrivateIndex].ptr = fw;

    return TRUE;
}

static void
fakeFiniWindow (CompPlugin *p,
		CompWindow *w)
{
    FAKE_WINDOW (w);

    free (fw);
}

static Bool
fakeInit (CompPlugin *p)
{
    displayPrivateIndex = allocateDisplayPrivateIndex ();
    if (displayPrivateIndex < 0)
	return FALSE;

    return TRUE;
}

static void
fakeFini (CompPlugin *p)
{
    freeDisplayPrivateIndex (displayPrivateIndex);
}

static CompBool
fakeInitObject (CompPlugin *p,
		CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) 0, /* InitCore */
	(InitPluginObjectProc) fakeInitDisplay,
	(InitPluginObjectProc) fakeInitScreen,
	(InitPluginObjectProc) fakeInitWindow
    };

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

static void
fakeFiniObject (CompPlugin *p,
		CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) 0, /* FiniCore */
	(FiniPluginObjectProc) fakeFiniDisplay,
	(FiniPluginObjectProc) fakeFiniScreen,
	(FiniPluginObjectProc) fakeFiniWindow
    };

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

CompPluginVTable fakeVTable = {
    "fakeargb",
    0,
    fakeInit,
    fakeFini,
    fakeInitObject,
    fakeFiniObject,
    0,
    0
};

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