summaryrefslogtreecommitdiff
path: root/workspacenames.c
blob: 195b3fce4c4f9af45678d1101ec80f6c8d3430bc (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
/*
 *
 * Compiz workspace name display plugin
 *
 * workspacenames.c
 *
 * Copyright : (C) 2008 by Danny Baumann
 * E-mail    : maniac@compiz-fusion.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 <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <compiz-core.h>
#include <compiz-text.h>
#include "workspacenames_options.h"

#define PI 3.1415926

static int WSNamesDisplayPrivateIndex;

typedef struct _WSNamesDisplay {
    int		    screenPrivateIndex;
    HandleEventProc handleEvent;
} WSNamesDisplay;

typedef struct _WSNamesScreen {
    PreparePaintScreenProc preparePaintScreen;
    DonePaintScreenProc    donePaintScreen;
    PaintOutputProc        paintOutput;

    CompTexture textTexture;
    Pixmap      textPixmap;
    int         textWidth;
    int         textHeight;

    CompTimeoutHandle timeoutHandle;
    int               timer;
} WSNamesScreen;

#define WSNAMES_DISPLAY(d) PLUGIN_DISPLAY(d, WSNames, w)
#define WSNAMES_SCREEN(s) PLUGIN_SCREEN(s, WSNames, w)

static void 
wsnamesFreeText (CompScreen *s)
{
    WSNAMES_SCREEN(s);

    if (!ws->textPixmap)
	return;

    releasePixmapFromTexture (s, &ws->textTexture);
    initTexture (s, &ws->textTexture);
    XFreePixmap (s->display->display, ws->textPixmap);
    ws->textPixmap = None;
}

static char *
wsnamesGetCurrentWSName (CompScreen *s)
{
    int           currentVp;
    int           listSize, i;
    CompListValue *names;
    CompListValue *vpNumbers;

    vpNumbers = workspacenamesGetViewports (s);
    names     = workspacenamesGetNames (s);

    currentVp = s->y * s->hsize + s->x + 1;
    listSize  = MIN (vpNumbers->nValue, names->nValue);

    for (i = 0; i < listSize; i++)
	if (vpNumbers->value[i].i == currentVp)
	    return names->value[i].s;

    return NULL;
}

static void
wsnamesRenderNameText (CompScreen *s)
{
    CompTextAttrib tA;
    int            stride;
    void           *data;
    char           *name;
    int            ox1, ox2, oy1, oy2;

    WSNAMES_SCREEN (s);

    wsnamesFreeText (s);

    name = wsnamesGetCurrentWSName (s);
    if (!name)
	return;

    getCurrentOutputExtents (s, &ox1, &oy1, &ox2, &oy2);

    /* 75% of the output device as maximum width */
    tA.maxWidth = (ox2 - ox1) * 3 / 4;
    tA.maxHeight = 100;
    tA.screen = s;
    tA.size = workspacenamesGetTextFontSize (s);
    tA.color[0] = workspacenamesGetFontColorRed (s);
    tA.color[1] = workspacenamesGetFontColorGreen (s);
    tA.color[2] = workspacenamesGetFontColorBlue (s);
    tA.color[3] = workspacenamesGetFontColorAlpha (s);
    tA.style = (workspacenamesGetBoldText (s)) ?
	       TEXT_STYLE_BOLD : TEXT_STYLE_NORMAL;
    tA.family = "Sans";
    tA.ellipsize = TRUE;

    tA.renderMode = TextRenderNormal;
    tA.data = (void *) name;

    initTexture (s, &ws->textTexture);

    if ((*s->display->fileToImage) (s->display, TEXT_ID, (char *)&tA,
			 	    &ws->textWidth, &ws->textHeight,
				    &stride, &data))
    {
	ws->textPixmap = (Pixmap) data;

	bindPixmapToTexture (s, &ws->textTexture, ws->textPixmap,
			     ws->textWidth, ws->textHeight, 32);
    }
    else 
    {
	ws->textPixmap = None;
	ws->textWidth  = 0;
	ws->textHeight = 0;
    }
}

static void
wsnamesDrawText (CompScreen *s)
{
    GLboolean wasBlend;
    GLint     oldBlendSrc, oldBlendDst;
    GLfloat   alpha;
    float     width, height, border;
    int       ox1, ox2, oy1, oy2;
    int       k;
    float     x, y;

    WSNAMES_SCREEN(s);

    width  = ws->textWidth;
    height = ws->textHeight;
    border = 10.0f;

    getCurrentOutputExtents (s, &ox1, &oy1, &ox2, &oy2);

    x = ox1 + ((ox2 - ox1) / 2) - (ws->textWidth / 2);

    /* assign y (for the lower corner!) according to the setting */
    switch (workspacenamesGetTextPlacement (s))
    {
	case TextPlacementCenteredOnScreen:
	    y = oy1 + ((oy2 - oy1) / 2) + (height / 2);
	    break;
	case TextPlacementTopOfScreen:
	case TextPlacementBottomOfScreen:
	    {
		XRectangle workArea;
		getWorkareaForOutput (s, s->currentOutputDev, &workArea);

	    	if (workspacenamesGetTextPlacement (s) == 
		    TextPlacementTopOfScreen)
    		    y = oy1 + workArea.y + (2 * border) + height;
		else
		    y = oy1 + workArea.y + workArea.height - (2 * border);
	    }
	    break;
	default:
	    return;
	    break;
    }

    x = floor (x);
    y = floor (y);

    glGetIntegerv (GL_BLEND_SRC, &oldBlendSrc);
    glGetIntegerv (GL_BLEND_DST, &oldBlendDst);
    wasBlend = glIsEnabled (GL_BLEND);

    if (!wasBlend)
	glEnable (GL_BLEND);
    glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

    if (ws->timer)
	alpha = ws->timer / (workspacenamesGetFadeTime (s) * 1000.0f);
    else
	alpha = 1.0f;

    glColor4us (workspacenamesGetBackColorRed (s),
		workspacenamesGetBackColorGreen (s),
		workspacenamesGetBackColorBlue (s),
		workspacenamesGetBackColorAlpha (s) * alpha);

    glPushMatrix ();

    glTranslatef (x, y - height, 0.0f);
    glRectf (0.0f, height, width, 0.0f);
    glRectf (0.0f, 0.0f, width, -border);
    glRectf (0.0f, height + border, width, height);
    glRectf (-border, height, 0.0f, 0.0f);
    glRectf (width, height, width + border, 0.0f);
    glTranslatef (-border, -border, 0.0f);

#define CORNER(a,b) \
    for (k = a; k < b; k++) \
    {\
	float rad = k * (PI / 180.0f);\
	glVertex2f (0.0f, 0.0f);\
	glVertex2f (cos (rad) * border, sin (rad) * border);\
	glVertex2f (cos ((k - 1) * (PI / 180.0f)) * border, \
		    sin ((k - 1) * (PI / 180.0f)) * border);\
    }

    /* Rounded corners */

    glTranslatef (border, border, 0.0f);
    glBegin (GL_TRIANGLES);
    CORNER (180, 270) glEnd ();
    glTranslatef (-border, -border, 0.0f);

    glTranslatef (width + border, border, 0.0f);
    glBegin (GL_TRIANGLES);
    CORNER (270, 360) glEnd ();
    glTranslatef (-(width + border), -border, 0.0f);

    glTranslatef (border, height + border, 0.0f);
    glBegin (GL_TRIANGLES);
    CORNER (90, 180) glEnd ();
    glTranslatef (-border, -(height + border), 0.0f);

    glTranslatef (width + border, height + border, 0.0f);
    glBegin (GL_TRIANGLES);
    CORNER (0, 90) glEnd ();
    glTranslatef (-(width + border), -(height + border), 0.0f);

    glPopMatrix ();

#undef CORNER

    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glColor4f (alpha, alpha, alpha, alpha);

    enableTexture (s, &ws->textTexture,COMP_TEXTURE_FILTER_GOOD);

    CompMatrix *m = &ws->textTexture.matrix;

    glBegin (GL_QUADS);

    glTexCoord2f (COMP_TEX_COORD_X (m, 0), COMP_TEX_COORD_Y (m ,0));
    glVertex2f (x, y - height);
    glTexCoord2f (COMP_TEX_COORD_X (m, 0), COMP_TEX_COORD_Y (m, height));
    glVertex2f (x, y);
    glTexCoord2f (COMP_TEX_COORD_X (m, width), COMP_TEX_COORD_Y (m, height));
    glVertex2f (x + width, y);
    glTexCoord2f (COMP_TEX_COORD_X (m, width), COMP_TEX_COORD_Y (m, 0));
    glVertex2f (x + width, y - height);

    glEnd ();

    disableTexture (s, &ws->textTexture);
    glColor4usv (defaultColor);

    if (!wasBlend)
	glDisable (GL_BLEND);
    glBlendFunc (oldBlendSrc, oldBlendDst);
}

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

    WSNAMES_SCREEN (s);

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

    if (ws->textPixmap)
    {
	CompTransform sTransform = *transform;

	transformToScreenSpace (s, output, -DEFAULT_Z_CAMERA, &sTransform);
	glPushMatrix ();
	glLoadMatrixf (sTransform.m);

	wsnamesDrawText (s);

	glPopMatrix ();
    }
	
    return status;
}

static void
wsnamesPreparePaintScreen (CompScreen *s,
			   int	      msSinceLastPaint)
{
    WSNAMES_SCREEN (s);

    if (ws->timer)
    {
	ws->timer -= msSinceLastPaint;
	ws->timer = MAX (ws->timer, 0);

	if (!ws->timer)
	    wsnamesFreeText (s);
    }

    UNWRAP (ws, s, preparePaintScreen);
    (*s->preparePaintScreen) (s, msSinceLastPaint);
    WRAP (ws, s, preparePaintScreen, wsnamesPreparePaintScreen);
}

static void
wsnamesDonePaintScreen (CompScreen *s)
{
    WSNAMES_SCREEN (s);

    /* FIXME: better only damage paint region */
    if (ws->timer)
	damageScreen (s);

    UNWRAP (ws, s, donePaintScreen);
    (*s->donePaintScreen) (s);
    WRAP (ws, s, donePaintScreen, wsnamesDonePaintScreen);
}

static Bool
wsnamesHideTimeout (void *closure)
{
    CompScreen *s = (CompScreen *) closure;

    WSNAMES_SCREEN (s);

    ws->timer = workspacenamesGetFadeTime (s) * 1000;
    if (!ws->timer)
	wsnamesFreeText (s);

    damageScreen (s);

    ws->timeoutHandle = 0;

    return FALSE;
}

static void
wsnamesHandleEvent (CompDisplay *d,
		    XEvent      *event)
{
    WSNAMES_DISPLAY (d);

    UNWRAP (wd, d, handleEvent);
    (*d->handleEvent) (d, event);
    WRAP (wd, d, handleEvent, wsnamesHandleEvent);

    switch (event->type) {
    case PropertyNotify:
	if (event->xproperty.atom == d->desktopViewportAtom)
	{
	    CompScreen *s;
	    s = findScreenAtDisplay (d, event->xproperty.window);
	    if (s)
	    {
		int timeout;

		WSNAMES_SCREEN (s);

		wsnamesRenderNameText (s);
		if (ws->timeoutHandle)
		    compRemoveTimeout (ws->timeoutHandle);
		timeout = workspacenamesGetDisplayTime (s) * 1000;
		ws->timeoutHandle = compAddTimeout (timeout,
						    wsnamesHideTimeout, s);

		damageScreen (s);
	    }
	}
	break;
    }
}

static Bool
wsnamesInitDisplay (CompPlugin  *p,
		    CompDisplay *d)
{
    WSNamesDisplay *wd;

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

    if (!checkPluginABI ("text", TEXT_ABIVERSION))
	return FALSE;

    wd = malloc (sizeof (WSNamesDisplay));
    if (!wd)
	return FALSE;

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

    WRAP (wd, d, handleEvent, wsnamesHandleEvent);

    d->base.privates[WSNamesDisplayPrivateIndex].ptr = wd;

    return TRUE;
}

static void
wsnamesFiniDisplay (CompPlugin  *p,
		    CompDisplay *d)
{
    WSNAMES_DISPLAY (d);

    freeScreenPrivateIndex (d, wd->screenPrivateIndex);

    UNWRAP (wd, d, handleEvent);

    free (wd);
}

static Bool
wsnamesInitScreen (CompPlugin *p,
		   CompScreen *s)
{
    WSNamesScreen *ws;

    WSNAMES_DISPLAY (s->display);

    ws = malloc (sizeof (WSNamesScreen));
    if (!ws)
	return FALSE;

    ws->textPixmap = None;
    ws->textHeight = 0;
    ws->textWidth  = 0;

    ws->timeoutHandle = 0;
    ws->timer         = 0;

    WRAP (ws, s, preparePaintScreen, wsnamesPreparePaintScreen);
    WRAP (ws, s, donePaintScreen, wsnamesDonePaintScreen);
    WRAP (ws, s, paintOutput, wsnamesPaintOutput);

    s->base.privates[wd->screenPrivateIndex].ptr = ws;

    return TRUE;
}

static void
wsnamesFiniScreen (CompPlugin *p,
		   CompScreen *s)
{
    WSNAMES_SCREEN (s);

    UNWRAP (ws, s, preparePaintScreen);
    UNWRAP (ws, s, donePaintScreen);
    UNWRAP (ws, s, paintOutput);

    wsnamesFreeText (s);

    free (ws);
}

static CompBool
wsnamesInitObject (CompPlugin *p,
		   CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) 0, /* InitCore */
	(InitPluginObjectProc) wsnamesInitDisplay,
	(InitPluginObjectProc) wsnamesInitScreen
    };

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

static void
wsnamesFiniObject (CompPlugin *p,
		   CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) 0, /* FiniCore */
	(FiniPluginObjectProc) wsnamesFiniDisplay,
	(FiniPluginObjectProc) wsnamesFiniScreen
    };

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

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

    return TRUE;
}

static void
wsnamesFini (CompPlugin *p)
{
    freeDisplayPrivateIndex (WSNamesDisplayPrivateIndex);
}

CompPluginVTable wsnamesVTable = {
    "workspacenames",
    0,
    wsnamesInit,
    wsnamesFini,
    wsnamesInitObject,
    wsnamesFiniObject,
    0,
    0
};

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