summaryrefslogtreecommitdiff
path: root/cubedbus.c
blob: c9cb93c0a9de6a641dc2c3671874eae2c0362174 (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

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <math.h>

#include <compiz-core.h>
#include <compiz-cube.h>

#include "cubedbus_options.h"
#include "cubedbus.h"

#define PI 3.14159

static int displayPrivateIndex;

static int cubeDisplayPrivateIndex;

typedef struct _CubedbusDisplay
{
    int screenPrivateIndex;

}
CubedbusDisplay;

typedef struct _CubedbusScreen
{
    DonePaintScreenProc    donePaintScreen;
    PreparePaintScreenProc preparePaintScreen;

    CubeClearTargetOutputProc clearTargetOutput;
    CubePaintInsideProc       paintInside;

    Bool damage;
    
    CubedbusObject *head;

}
CubedbusScreen;

#define GET_CUBEDBUS_DISPLAY(d) \
    ((CubedbusDisplay *) (d)->base.privates[displayPrivateIndex].ptr)
#define CUBEDBUS_DISPLAY(d) \
    CubedbusDisplay *gd = GET_CUBEDBUS_DISPLAY(d);                            /* Macros to get the gd pointer to CubedbusDisplay struct */

#define GET_CUBEDBUS_SCREEN(s, gd) \
    ((CubedbusScreen *) (s)->base.privates[(gd)->screenPrivateIndex].ptr)
#define CUBEDBUS_SCREEN(s) \
    CubedbusScreen *gs = GET_CUBEDBUS_SCREEN(s, GET_CUBEDBUS_DISPLAY(s->display))     /* Macros to get the gs pointer to CubedbusScreen struct */


static Bool 
cubedbusAddObject (CompDisplay     *d,
					CompAction      *action,
					CompActionState state,
					CompOption      *option,
					int             nOption)
{
	//get gs pointer
	CompScreen *s;
	Window     xid;
	xid = getIntOptionNamed (option, nOption, "root", 0);
	s = findScreenAtDisplay (d, xid);
	CUBEDBUS_SCREEN(s);
	
	compLogMessage (NULL, "cubedbus", CompLogLevelWarn, "Adding Object");

				GLint err;
				while((err=glGetError())){
				compLogMessage (NULL, "cubedbus", CompLogLevelError, "before %d",err);}
				
					
	CubedbusObject *objptr;
	objptr=gs->head;

	if (gs->head == NULL)
		{
		gs->head = malloc (sizeof(CubedbusObject));
		if(gs->head == NULL) return FALSE;
		objptr=gs->head;
		objptr->prev = NULL;
		}
	else
		{
		while(1)
			{
			if(objptr->next == NULL )
				{
				objptr->next = malloc (sizeof(CubedbusObject));
				if(objptr->next == NULL) return FALSE;	
				objptr->next->prev=objptr;
				objptr=objptr->next; /* objptr now points to newly created object */			
				}
			else objptr=objptr->next;
			}
		}
		
	/* have now created CubedbusObject */
	
	objptr->next=NULL;
	objptr->type=getIntOptionNamed (option,nOption, "type", 0);
		if(objptr->type == 0) return FALSE;
	objptr->name=getStringOptionNamed (option,nOption,"name", "");
		if(strcmp(objptr->name,"") == 0) return FALSE;
	
	switch (objptr->type)
		{
		case CUBEDBUS_COLOUR_OBJECT:
			{
				compLogMessage (NULL, "cubedbus", CompLogLevelWarn, "Adding ColourObject");
				
			objptr->func=&cubedbusDrawColourObject;
			objptr->data= malloc( sizeof(CubedbusColourObject) );
				if(objptr->data == NULL) return FALSE;
				
			CubedbusColourObject *colourData=objptr->data;	
			
			colourData->rotate[0]=getFloatOptionNamed (option,nOption, "rotateAngle", 0.0);
			colourData->rotate[1]=getFloatOptionNamed (option,nOption, "rotateX", 1.0);
			colourData->rotate[2]=getFloatOptionNamed (option,nOption, "rotateY", 0.0);
			colourData->rotate[3]=getFloatOptionNamed (option,nOption, "rotateZ", 0.0);
			colourData->translate[0]=getFloatOptionNamed (option,nOption, "translateX", 0.0);
			colourData->translate[1]=getFloatOptionNamed (option,nOption, "translateY", 0.0);
			colourData->translate[2]=getFloatOptionNamed (option,nOption, "translateZ", 0.0);
			colourData->scale[0]=getFloatOptionNamed (option,nOption, "scaleX", 1.0);
			colourData->scale[1]=getFloatOptionNamed (option,nOption, "scaleY", 1.0);
			colourData->scale[2]=getFloatOptionNamed (option,nOption, "scaleZ", 1.0);
			colourData->order=getBoolOptionNamed (option, nOption, "order", TRUE);
	
		compLogMessage (NULL, "cubedbus", CompLogLevelWarn, "translate %f", colourData->translate[0]);
				
			/* Code to make dList */
						
			colourData->dList=glGenLists (1);
			glNewList (colourData->dList, GL_COMPILE);

				
				int i;
				float r,g,b,a;
				float x,y,z;
				char string[4];

				glBegin(GL_POLYGON);

				for(i=0;i</*num*/3;i++)
					{
						sprintf(string,"r%d",i);
					r=getFloatOptionNamed (option, nOption, string, 1.0 );
						sprintf(string,"g%d",i);
					g=getFloatOptionNamed (option, nOption, string, 0.0 );
						sprintf(string,"b%d",i);
					b=getFloatOptionNamed (option, nOption, string, 0.0 );
						sprintf(string,"a%d",i);
					a=getFloatOptionNamed (option, nOption, string, 1.0 );
						sprintf(string,"x%d",i);
					x=getFloatOptionNamed (option, nOption, string, 0.0);
						sprintf(string,"y%d",i);
					y=getFloatOptionNamed (option, nOption, string, 0.0);
						sprintf(string,"z%d",i);
					z=getFloatOptionNamed (option, nOption, string, 0.0 );
					

					glColor4f(r,g,b,a);
					glVertex3f( x, y, z);
					}
				glEnd();
				
			glEndList();

				
			return TRUE;
			break;
			}
		case CUBEDBUS_TEXTURE_OBJECT:
			{
			return TRUE;
			break;
			}			
		}
		
	return FALSE;
}

/* Drawing functions */

static bool
cubedbusDrawColourObject (CubedbusObject *obj)
{
	glShadeModel (GL_SMOOTH);
	
	CubedbusColourObject *data=obj->data;
	
			if(data->order) glTranslatef (data->translate[0], data->translate[1], data->translate[2]);
		glRotatef ( data->rotate[0], data->rotate[1], data->rotate[2], data->rotate[3]);
			if(!(data->order)) glTranslatef (data->translate[0], data->translate[1], data->translate[2]); 
		
		glScalef( data->scale[0],data->scale[1], data->scale[2]); /* Rotate, translate and scale  */



	glCallList(data->dList);		

					

					
	return TRUE;
}

static bool
cubedbusDrawTextureObject (CubedbusObject *obj)
{
	return TRUE;
}

/* Standard plugin 'stuff' */

static void
cubedbusClearTargetOutput (CompScreen *s,    /* Doesn't do anything except glClear(GL_DEPTH_BUFFER_BIT)? - cube must change it? */
			float      xRotate,
			float      vRotate)
{
    CUBEDBUS_SCREEN (s);
    CUBE_SCREEN (s);

    UNWRAP (gs, cs, clearTargetOutput);
    (*cs->clearTargetOutput) (s, xRotate, vRotate);          
    WRAP (gs, cs, clearTargetOutput, cubedbusClearTargetOutput);

    glClear (GL_DEPTH_BUFFER_BIT);
}

static void                       /* draws the cubedbus, then calls the cube function */
cubedbusPaintInside (CompScreen              *s,
		  const ScreenPaintAttrib *sAttrib,
		  const CompTransform     *transform,
		  CompOutput              *output,
		  int                     size)
{
    CUBEDBUS_SCREEN (s);
    CUBE_SCREEN (s);

	Bool enabled;

    ScreenPaintAttrib sA = *sAttrib;

    sA.yRotate += (360.0f / size) * (cs->xRotations - (s->x * cs->nOutput)); /*?*/

    CompTransform mT = *transform;

    (*s->applyScreenTransform) (s, &sA, output, &mT);

    glPushMatrix();
    glLoadMatrixf (mT.m);
    glTranslatef (cs->outputXOffset, -cs->outputYOffset, 0.0f);   /* OpenGL stuff to set correct transformation due to cube rotation? */
    glScalef (cs->outputXScale, cs->outputYScale, 1.0f);

    glPushAttrib (GL_COLOR_BUFFER_BIT | GL_TEXTURE_BIT);
	
	enabled = glIsEnabled (GL_CULL_FACE);
	
	glDisable(GL_CULL_FACE);
    
    
	glPushMatrix();
    
    	glDisable (GL_LIGHTING);
    	glDisable (GL_COLOR_MATERIAL);
    	glEnable (GL_DEPTH_TEST);
    	
/*********************************************/ 
		
		CubedbusObject *obj=gs->head;
		
		while(true)
			{
			if(obj == NULL) break; /* pull out of loop if finished drawing all objects */
			
			glPushMatrix();
				
				if(!( (obj->func) (obj) )) compLogMessage(NULL, "cubedbus", CompLogLevelWarn, "error calling function") ; /* Call function */
				
			glPopMatrix();
			
			obj = obj->next;
			}
			
/**********************************************/ 
    
    glPopMatrix();

    glEnable (GL_COLOR_MATERIAL);
    
    if (s->lighting)
	glEnable (GL_LIGHTING);

    glDisable (GL_DEPTH_TEST);
    glDisable (GL_BLEND);

    if (enabled) glEnable (GL_CULL_FACE);     
    else glDisable (GL_CULL_FACE);    /* resets settings to original */

    glPopMatrix();
    glPopAttrib();

    gs->damage = TRUE;

    UNWRAP (gs, cs, paintInside);
    (*cs->paintInside) (s, sAttrib, transform, output, size);
    WRAP (gs, cs, paintInside, cubedbusPaintInside);
}

static void            
cubedbusPreparePaintScreen (CompScreen *s,
			 int        ms)
{
    CUBEDBUS_SCREEN (s);
    
	/* check for messages */
          
    UNWRAP (gs, s, preparePaintScreen);
    (*s->preparePaintScreen) (s, ms);
    WRAP (gs, s, preparePaintScreen, cubedbusPreparePaintScreen);
}

static void /* Calls damageScreen ? */
cubedbusDonePaintScreen (CompScreen * s)
{
    CUBEDBUS_SCREEN (s);

    if (gs->damage)
    {
	damageScreen (s);
	gs->damage = FALSE;
    }

    UNWRAP (gs, s, donePaintScreen);
    (*s->donePaintScreen) (s);
    WRAP (gs, s, donePaintScreen, cubedbusDonePaintScreen);
}

/* Inits display */

static Bool
cubedbusInitDisplay (CompPlugin  *p,
		  CompDisplay *d)
{
    CubedbusDisplay *gd;

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

    if (!getPluginDisplayIndex (d, "cube", &cubeDisplayPrivateIndex))
	return FALSE;

    gd = malloc (sizeof (CubedbusDisplay));    /* creates data struct - remainder is error checking */

    if (!gd)
	return FALSE;

    gd->screenPrivateIndex = allocateScreenPrivateIndex (d); 

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

    d->base.privates[displayPrivateIndex].ptr = gd;  /* allows use of CUBEDBUS_DISPLAY macro */

	cubedbusSetAddInitiate (d, cubedbusAddObject);
				
					
    return TRUE;
}

static void
cubedbusFiniDisplay (CompPlugin  *p,
		  CompDisplay *d)
{
    CUBEDBUS_DISPLAY (d);

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


/* Function which is run when screen is initialized */

static Bool
cubedbusInitScreen (CompPlugin *p,
		 CompScreen *s)
{
    CubedbusScreen *gs;

    CUBEDBUS_DISPLAY (s->display);    /* sets gd pointer used below */

    CUBE_SCREEN (s);

    gs = malloc (sizeof (CubedbusScreen) );            /* creates gearScreen struct */

    if (!gs)
	return FALSE;

    s->base.privates[gd->screenPrivateIndex].ptr = gs;           /* For the CUBEDBUS_SCREEN macro to work  */
	
	gs->head= NULL;

	 
    WRAP (gs, s, donePaintScreen, cubedbusDonePaintScreen);                 /*Functions version of ... is called instead of cores (which then also calls core function) */
    WRAP (gs, s, preparePaintScreen, cubedbusPreparePaintScreen);
    WRAP (gs, cs, clearTargetOutput, cubedbusClearTargetOutput);
    WRAP (gs, cs, paintInside, cubedbusPaintInside);
    
    return TRUE;

}

/* Fuction is run when screen is uninitialized */

static void
cubedbusFiniScreen (CompPlugin *p,
		 CompScreen *s)
{
    CUBEDBUS_SCREEN (s);                     /*Sets the pointers cs gs */
    CUBE_SCREEN (s);

	
	
	
    UNWRAP (gs, s, donePaintScreen);                /* gs is pointer to structure GearScreen, s is pointer to structure screen? - these functions are core functions*/
    UNWRAP (gs, s, preparePaintScreen);

    UNWRAP (gs, cs, clearTargetOutput);            /* ... cs is pointer to structure CubeScreen? - these two functions are part of the Cube plugin*/
    UNWRAP (gs, cs, paintInside);



    free (gs);
}

/* Load and Unload plugin */

static Bool
cubedbusInit (CompPlugin * p)
{
    displayPrivateIndex = allocateDisplayPrivateIndex();

    if (displayPrivateIndex < 0)
	return FALSE;

    return TRUE;
}

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

/* Init Object - lists functions for Core, Display and Screen */

static CompBool
cubedbusInitObject (CompPlugin *p,
		 CompObject *o)
{
    static InitPluginObjectProc dispTab[] = {
	(InitPluginObjectProc) 0, /* InitCore */
	(InitPluginObjectProc) cubedbusInitDisplay,
	(InitPluginObjectProc) cubedbusInitScreen
    };

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

static void
cubedbusFiniObject (CompPlugin *p,
		 CompObject *o)
{
    static FiniPluginObjectProc dispTab[] = {
	(FiniPluginObjectProc) 0, /* FiniCore */
	(FiniPluginObjectProc) cubedbusFiniDisplay,
	(FiniPluginObjectProc) cubedbusFiniScreen
    };

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

/* VTable */

CompPluginVTable cubedbusVTable = {
    "cubedbus",
    0,          /* (?) */
    cubedbusInit,
    cubedbusFini,
    cubedbusInitObject,
    cubedbusFiniObject,
    0,         /* Get(?) Object Options */
    0          /* Set(?) Object Options */
};

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