summaryrefslogtreecommitdiff
path: root/beryl-plugins/src/miniview.c
blob: 5027a325c3c1706fcc75dd307665d4ad2ec55fac (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
 /**
 *
 * Beryl miniview plugin
 *
 * miniview.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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>

#include <beryl.h>

#define MINIVIEW_CREATE_KEY_DEFAULT "Tab"
#define MINIVIEW_CREATE_MODIFIERS_DEFAULT (ShiftMask)

#define MINIVIEW_MOVE_KEY_DEFAULT Button1
#define MINIVIEW_MOVE_MODIFIERS_DEFAULT ( ShiftMask   )

#define MINIVIEW_INCREASE_KEY_DEFAULT "i"
#define MINIVIEW_INCREASE_MODIFIERS_DEFAULT (CompSuperMask | CompAltMask)

#define MINIVIEW_DECREASE_KEY_DEFAULT "d"
#define MINIVIEW_DECREASE_MODIFIERS_DEFAULT (CompSuperMask | CompAltMask)


#define MINIVIEW_HIDE_KEY_DEFAULT "h"
#define MINIVIEW_HIDE_MODIFIERS_DEFAULT (ShiftMask | CompSuperMask)

/* Unimplemented increase/decrease */

static int displayPrivateIndex;

#define MINIVIEW_DISPLAY_OPTION_CREATE 0
#define MINIVIEW_DISPLAY_OPTION_MOVE 1
#define MINIVIEW_DISPLAY_OPTION_HIDE 2
#define MINIVIEW_DISPLAY_OPTION_NUM 3

typedef struct _MiniviewDisplay
{
	int screenPrivateIndex;
      HandleEventProc handleEvent;

	CompOption opt[MINIVIEW_DISPLAY_OPTION_NUM];
} MiniviewDisplay;

typedef struct _MiniviewScreen
{
	DonePaintScreenProc donePaintScreen;
	PaintScreenProc paintScreen;
	PreparePaintScreenProc preparePaintScreen;
	PaintTransformedScreenProc paintTransformedScreen;

	Bool showing;
	int miniX;
	int miniY;
	int miniViewport;
	int miniViewportY;
	int moveStep;
	float zCam;


	float size;

	Bool moving;


	int grabIndex;

} MiniviewScreen;

#define GET_MINIVIEW_DISPLAY(d) \
		((MiniviewDisplay *) (d)->privates[displayPrivateIndex].ptr)
#define MINIVIEW_DISPLAY(d) \
		MiniviewDisplay *md = GET_MINIVIEW_DISPLAY(d);

#define GET_MINIVIEW_SCREEN(s,md) \
		((MiniviewScreen *) (s)->privates[(md)->screenPrivateIndex].ptr)
#define MINIVIEW_SCREEN(s) \
		MiniviewScreen *ms = GET_MINIVIEW_SCREEN(s, GET_MINIVIEW_DISPLAY(s->display))

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


static void miniviewHandleEvent(CompDisplay * d, XEvent * event)
{
	MINIVIEW_DISPLAY(d);

	CompScreen *s;
	switch (event->type)
	{
		default:
			break;
	}
	UNWRAP(md,d,handleEvent);
	(*d->handleEvent)(d,event);
	WRAP(md,d,handleEvent,miniviewHandleEvent);
}

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

	xid = getIntOptionNamed(option, nOption, "root", 0);
	s = findScreenAtDisplay(d, xid);
	MINIVIEW_SCREEN(s);
	ms->showing = !ms->showing;
	ms->miniViewport = s->x;
	ms->miniViewportY = s->y;
	ms->miniX = d->pointerX;
	ms->miniY = d->pointerY;
	ms->zCam = 0;
	ms->moveStep = 1;
	damageScreen(s);
	return FALSE;
}

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

	xid = getIntOptionNamed(option, nOption, "root", 0);
	s = findScreenAtDisplay(d, xid);
	MINIVIEW_SCREEN(s);
	ms->moving = !ms->moving;
	damageScreen(s);


	return FALSE;
}

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

	xid = getIntOptionNamed(option, nOption, "root", 0);
	s = findScreenAtDisplay(d, xid);
	MINIVIEW_SCREEN(s);
	ms->showing = !ms->showing;
	damageScreen(s);


	return FALSE;
}


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

	MINIVIEW_SCREEN(s);
	if (ms->showing)
	{
		mask |= PAINT_SCREEN_TRANSFORMED_MASK;
	}
	UNWRAP(ms, s, paintScreen);
	status = (*s->paintScreen) (s, sAttrib, transform, region, output, mask);
	WRAP(ms, s, paintScreen, miniviewPaintScreen);

	return status;
}

static void miniviewPreparePaintScreen(CompScreen *s, int ICouldCareLess)
{
	MINIVIEW_SCREEN(s);
	if ((ms->moving && ms->showing))
	{
		int winX, winY;
		int rootX, rootY;
		unsigned int mask_return;
		Window root_return;
		Window child_return;

		XQueryPointer(s->display->display, s->root,
					  &root_return, &child_return,
					  &rootX, &rootY, &winX, &winY, &mask_return);

		ms->miniX = rootX;
		ms->miniY = rootY;

	}
	UNWRAP(ms,s,preparePaintScreen);
	 (*s->preparePaintScreen) (s,ICouldCareLess);
	WRAP(ms,s,preparePaintScreen,miniviewPreparePaintScreen);
}

static void miniviewPaintTransformedScreen(CompScreen * s,
										   const ScreenPaintAttrib * sAttrib,
										   const CompTransform    *transform,
										   Region region, int output,
										   unsigned int mask)
{
	MINIVIEW_SCREEN(s);

	UNWRAP(ms, s, paintTransformedScreen);
	(*s->paintTransformedScreen) (s, sAttrib, transform, region, output, mask);
	if (ms->showing )
	{
		//mask &= PAINT_SCREEN_TRANSFORMED_MASK;
		int oldFilter = s->display->textureFilter;
		s->display->textureFilter = GL_LINEAR_MIPMAP_LINEAR;

		if (ms->zCam > -ms->size)
			ms->zCam -= .075;

		CompTransform     sTransform = *transform;
		ScreenPaintAttrib * sa = sAttrib;

		matrixScale(&sTransform, 1.0f,1.0f,1.0f*-ms->zCam);
		matrixTranslate(&sTransform, -1.0,1.0,0);

		float compensation;
		compensation = ms->size - 2.0f;
		compensation /= 2;
		matrixTranslate(&sTransform, -compensation,compensation,0);

		if (ms->moveStep <= 0.95)
			ms->moveStep += 0.05;

		matrixTranslate(&sTransform, (1/((float)s->width/(float)ms->miniX)*-ms->zCam),
		(-1/((float)s->height/(float)ms->miniY)*-ms->zCam),
		0);
		int move = 0;
		int ymove = 0;

		while (s->x != ms->miniViewport)
		{
			moveScreenViewport(s, 1, 0, FALSE);
			move++;
		}
		while (s->y != ms->miniViewport)
		{
			moveScreenViewport(s,0,1,FALSE);
			ymove++;
		}
		(*s->paintTransformedScreen) (s, sa, &sTransform, &s->region, output, mask);
		s->display->textureFilter = oldFilter;

		moveScreenViewport(s,-move,-ymove,FALSE);

	}


	WRAP(ms, s, paintTransformedScreen, miniviewPaintTransformedScreen);

}

static void miniviewDonePaintScreen(CompScreen * s)
{
	MINIVIEW_SCREEN(s);

	if (ms->showing)
		damageScreen(s);



	UNWRAP(ms, s, donePaintScreen);
	(*s->donePaintScreen) (s);
	WRAP(ms, s, donePaintScreen, miniviewDonePaintScreen);
}

static void miniviewDisplayInitOptions(MiniviewDisplay * md)
{
	CompOption *o;

	o = &md->opt[MINIVIEW_DISPLAY_OPTION_CREATE];
	o->advanced = False;
	o->name = "create";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Create");
	o->longDesc = N_("Create thumbnail of current viewport");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = miniviewCreate;
	o->value.action.terminate = 0;
	o->value.action.bell = 0;
	o->value.action.edgeMask = 0;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.type = CompBindingTypeKey;
	o->value.action.key.modifiers = MINIVIEW_CREATE_MODIFIERS_DEFAULT;
	o->value.action.key.keysym = XStringToKeysym(MINIVIEW_CREATE_KEY_DEFAULT);

	o = &md->opt[MINIVIEW_DISPLAY_OPTION_HIDE];
	o->advanced = False;
	o->name = "Hide";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Hide");
	o->longDesc = N_("Engage miniview hide mode.");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = miniviewHide;
	o->value.action.terminate = 0;
	o->value.action.bell = 0;
	o->value.action.edgeMask = 0;
	o->value.action.state = CompActionStateInitKey;
	o->value.action.type = CompBindingTypeKey;
	o->value.action.key.modifiers = MINIVIEW_HIDE_MODIFIERS_DEFAULT;
	o->value.action.key.keysym = XStringToKeysym(MINIVIEW_HIDE_KEY_DEFAULT);

	o = &md->opt[MINIVIEW_DISPLAY_OPTION_MOVE];
	o->advanced = False;
	o->name = "move";
	o->group = N_("Misc. Settings");
	o->subGroup = N_("");
	o->displayHints = "";
	o->shortDesc = N_("Move preview");
	o->longDesc = N_("Move current preview to mouse location");
	o->type = CompOptionTypeAction;
	o->value.action.initiate = miniviewMove;
	o->value.action.terminate = 0;
	o->value.action.bell = 0;
	o->value.action.edgeMask = 0;
	o->value.action.state =
			 CompActionStateInitButton;
	o->value.action.type = CompBindingTypeButton;
	//o->value.action.key.modifiers = MINIVIEW_MOVE_MODIFIERS_DEFAULT;
	//o->value.action.key.keysym = XStringToKeysym(MINIVIEW_MOVE_KEY_DEFAULT);
	o->value.action.button.modifiers = MINIVIEW_MOVE_MODIFIERS_DEFAULT;
	o->value.action.button.button = MINIVIEW_MOVE_KEY_DEFAULT;

}

static CompOption *miniviewGetDisplayOptions(CompDisplay * d, int *count)
{
	if (d)
	{
		MINIVIEW_DISPLAY(d);
		*count = NUM_OPTIONS(md);
		return md->opt;
	}
	else
	{
		MiniviewDisplay *md = malloc(sizeof(MiniviewDisplay));

		miniviewDisplayInitOptions(md);
		*count = NUM_OPTIONS(md);
		return md->opt;
	}
}

static Bool
miniviewSetDisplayOption(CompDisplay * d, char *name, CompOptionValue * value)
{
	CompOption *o;
	int index;

	MINIVIEW_DISPLAY(d);

	o = compFindOption(md->opt, NUM_OPTIONS(md), name, &index);

	if (!o)
		return FALSE;

	switch (index)
	{
	case MINIVIEW_DISPLAY_OPTION_CREATE:
	case MINIVIEW_DISPLAY_OPTION_MOVE:
	case MINIVIEW_DISPLAY_OPTION_HIDE:
		if (setDisplayAction(d, o, value))
			return TRUE;
	default:
		break;
	}
	return FALSE;
}

static Bool miniviewInitDisplay(CompPlugin * p, CompDisplay * d)
{
	MiniviewDisplay *md;

	md = malloc(sizeof(MiniviewDisplay));
	if (!md)
		return FALSE;

	md->screenPrivateIndex = allocateScreenPrivateIndex(d);

	if (md->screenPrivateIndex < 0)
	{
		free(md);
		return FALSE;
	}

	miniviewDisplayInitOptions(md);

	WRAP(md,d,handleEvent,miniviewHandleEvent);
	d->privates[displayPrivateIndex].ptr = md;

	return TRUE;
}

static void miniviewFiniDisplay(CompPlugin * p, CompDisplay * d)
{
	MINIVIEW_DISPLAY(d);


	UNWRAP(md,d,handleEvent);
	freeScreenPrivateIndex(d, md->screenPrivateIndex);
	free(md);
}

static Bool miniviewInitScreen(CompPlugin * p, CompScreen * s)
{
	MiniviewScreen *ms;

	MINIVIEW_DISPLAY(s->display);

	ms = malloc(sizeof(MiniviewScreen));

	if (!ms)
		return FALSE;

	ms->grabIndex  = ms->moving = ms->miniX = ms->miniY = ms->miniViewport = ms->showing = ms->zCam =
			ms->moveStep = 0;
	ms->size = 3.0;
	addScreenAction(s, &md->opt[MINIVIEW_DISPLAY_OPTION_CREATE].value.action);
	addScreenAction(s, &md->opt[MINIVIEW_DISPLAY_OPTION_MOVE].value.action);
	addScreenAction(s, &md->opt[MINIVIEW_DISPLAY_OPTION_HIDE].value.action);

	WRAP(ms, s, paintScreen, miniviewPaintScreen);
	WRAP(ms, s, donePaintScreen, miniviewDonePaintScreen);
	WRAP(ms, s, paintTransformedScreen, miniviewPaintTransformedScreen);
	WRAP(ms, s, preparePaintScreen, miniviewPreparePaintScreen);

	s->privates[md->screenPrivateIndex].ptr = ms;

	return TRUE;
}

static void miniviewFiniScreen(CompPlugin * p, CompScreen * s)
{
	MINIVIEW_SCREEN(s);
	MINIVIEW_DISPLAY(s->display);

	removeScreenAction(s, &md->opt[MINIVIEW_DISPLAY_OPTION_CREATE].value.action);
	removeScreenAction(s, &md->opt[MINIVIEW_DISPLAY_OPTION_MOVE].value.action);
	removeScreenAction(s, &md->opt[MINIVIEW_DISPLAY_OPTION_HIDE].value.action);
	UNWRAP(ms, s, paintScreen);
	UNWRAP(ms, s, donePaintScreen);
	UNWRAP(ms, s, paintTransformedScreen);
	UNWRAP(ms, s, preparePaintScreen);
	free(ms);
}

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

static void miniviewFini(CompPlugin * p)
{
	if (displayPrivateIndex >= 0)
		freeDisplayPrivateIndex(displayPrivateIndex);
}



CompPluginVTable miniviewVTable = {
	"miniview",
	N_("Mini-viewport"),
	N_("Viewport preview plugin"),
	miniviewInit,
	miniviewFini,
	miniviewInitDisplay,
	miniviewFiniDisplay,
	miniviewInitScreen,
	miniviewFiniScreen,
	0,
	0,
	miniviewGetDisplayOptions,
	miniviewSetDisplayOption,
	0,
	0,
	0,
	0,
	0,
	0,
	BERYL_ABI_INFO,
	"beryl-plugins",
	"misc",
	0,
	0,
	TRUE,
};

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