summaryrefslogtreecommitdiff
path: root/cubedbus.c
blob: e13a4293d2c0a852ee4c96ed89b5ec2da64730ed (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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
 *
 * Copyright : (C) 2007 by Joel Bosveld
 * E-mail    : Joel.Bosveld@gmail.com
 *
 * 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 <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 */

#include "cubedbusParticle.c.in"
#include "cubedbusModel.c.in"
#include "cubedbusWindow.c.in"
#include "cubedbusTexture.c.in"
#include "cubedbusColour.c.in"
#include "cubedbusObject.c.in"

#include "cubedbusStd.c.in"


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);
		if(!s) return FALSE;
	CUBEDBUS_SCREEN(s);
	
	CubedbusObject *head=gs->head;
	CubedbusObject *objptr=gs->head;
	
	CubedbusName *cubedbusName = malloc (sizeof(CubedbusName));
	int i;
	

	
	cubedbusGetName( getStringOptionNamed(option,nOption,"name",""), cubedbusName );
	
	/* sets the head pointer correctly */
	
	for(i=0;i<cubedbusName->n -1;i++) /* stop 1 before the object to be created */
		{
		while(1)
			{
			  if(objptr == NULL) {
			    free(cubedbusName);
			    return FALSE; 						
			  }
		
			if(!strcmp(objptr->name,cubedbusName->string[i]))
				{
				head=objptr;				
				CubedbusObj *data=objptr->data;
				objptr=data->firstChild;

				break;
				}
			else objptr=objptr->next;
			}
		}
		
	char name[16];
	
		if( (strcmp(cubedbusName->string[i],"") == 0) || ( strlen(cubedbusName->string[i]) >= 16)) 
			{
			compLogMessage (NULL,"cubedbus",CompLogLevelError, "error with name");
			free(cubedbusName);			
			return FALSE;
			}
		
		strcpy(name,cubedbusName->string[i]);
		
		/*
		for(i=0;i<cubedbusName->n;i++)
			{
			free(cubedbusName->string[i]);
			}
		*/
		free(cubedbusName->string);
		free(cubedbusName);
		


	return cubedbusAddStdObject (option, nOption, head, s, name);
}

static Bool 
cubedbusDeleteObject (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);
		if(!s) return FALSE;	
	CUBEDBUS_SCREEN(s);
	
	CubedbusObject *head=gs->head;
	CubedbusObject *objptr=gs->head;
	
	CubedbusName *cubedbusName = malloc (sizeof(CubedbusName));
	int i;
	
	if(!cubedbusGetName( getStringOptionNamed(option,nOption,"name",""), cubedbusName )) {
	  free(cubedbusName);
	    return FALSE;
	}
	/* sets the head pointer correctly */
	
	for(i=0;i<cubedbusName->n -1 ;i++) /* stop 1 before the object to be created */
		{
		while(1)
			{
			  if(objptr == NULL) {
			    free(cubedbusName);
			      return FALSE;
			  }
		
			if(!strcmp(objptr->name, cubedbusName->string[i]))
				{
				head=objptr;
				CubedbusObj *data=objptr->data;			
				objptr=data->firstChild;
				break;
				}
			else objptr=objptr->next;
			}
		}
		
	while(1)	
		{
		  if(objptr==NULL) {
		    free(cubedbusName);
		    return false;
		  }
		if(!strcmp(objptr->name,cubedbusName->string[i])) 
			{
			if(objptr->prev != NULL) objptr->prev->next=objptr->next;
			else 
				{
				CubedbusObj *data = head->data;
				data->firstChild=objptr->next;
				} 

			if(objptr->next != NULL) objptr->next->prev=objptr->prev;
			
			free(cubedbusName);
			return (objptr->delfunc) (objptr);
			}
		objptr=objptr->next;
		}
}
		
static Bool 
cubedbusModifyObject (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) return FALSE;
	CUBEDBUS_SCREEN(s);
	
	CubedbusObject *objptr;
	objptr=gs->head;
	char name[16];
	
	char *tempName=getStringOptionNamed (option,nOption,"name", "");

	if(strlen(tempName) >= 16)      /* should be array length */
		{
		compLogMessage (NULL,"cubedbus",CompLogLevelError, "name is too long");
		return FALSE;
		}
	strcpy(name,tempName);	
	
	while(1)
		{
		if(objptr==NULL) return FALSE;
		if(!strcmp(objptr->name,name)) return (objptr->modfunc) (objptr, option, nOption);
		objptr=objptr->next;
		}	
	
}		


static bool
cubedbusGenerateDList(CompOption *option, int nOption, GLuint *dList)
{
	*dList=glGenLists (1);
	glNewList (*dList, GL_COMPILE);
		
		int i;
		float r,g,b,a;
		float x,y,z;
		char string[5];

		glBegin(GL_TRIANGLES);
	
		int max=getIntOptionNamed(option, nOption, "numberOfPoints", 0);
		for(i=0;i<max;i++)
			{
					sprintf(string,"r%d",i);
				r=getFloatOptionNamed (option, nOption, string, 0.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;
}

/* Get name */
static bool
cubedbusGetName(char *name, CubedbusName *storage)
{
	int i;
	char **temp;

	storage->string=malloc (sizeof(char*)*3); /* 3 is the number of expected layers - root -> object -> colour */
	storage->string[0]=strsep(&name,".");

	
	for(i=1;;i++)
		{
		if(i>2)
			{
			temp=realloc(storage->string, sizeof(char*)*(i+1));
			if (temp==NULL)
				{
				free(storage->string);
				return FALSE;
				}
			storage->string=temp;
			}
		storage->string[i] = strsep(&name,".");

			if(storage->string[i]==NULL)
				{
				storage->n=i;
				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);
    
    static const float lmodel_ambient[] = { 0.4, 0.4, 0.4, 0.4 };
    static const float lmodel_localviewer[] = { 0.0 };
    

    Bool enabled;

    ScreenPaintAttrib sA = *sAttrib;

    sA.yRotate += cs->invert * (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 | GL_LIGHTING_BIT);
	
    glEnable (GL_BLEND);

	enabled = glIsEnabled (GL_CULL_FACE);
	
	glDisable(GL_CULL_FACE);
    
    
	glPushMatrix();
    

	glLightModelfv (GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
        glLightModelfv (GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_localviewer);

        glEnable (GL_NORMALIZE);
    	glEnable (GL_COLOR_MATERIAL);
    	glEnable (GL_DEPTH_TEST);
        glEnable (GL_LIGHTING);
        glEnable (GL_LIGHT1);
        glDisable (GL_LIGHT0);

    	glColor4usv (defaultColor);

        glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
     
/*********************************************/ 
		
		CubedbusObject *obj=gs->head;
		
		glScalef(0.5,0.5,0.5);


			if(obj == NULL) compLogMessage(NULL,"cubedbus", CompLogLevelWarn, "no root object to draw"); /* pull out of loop if finished drawing all objects */
			
			else
				{
				glPushMatrix();
				
					if(!( (obj->func) (obj) )) compLogMessage(NULL, "cubedbus", CompLogLevelWarn, "error calling function") ; /* Call function */
				
				glPopMatrix();
			
				glColor4usv (defaultColor);
				}

/**********************************************/ 
    
    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);
    
    timeSec += ms/1000.0f;
	/* 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);
	cubedbusSetDeleteInitiate (d, cubedbusDeleteObject);	
	cubedbusSetModifyInitiate (d, cubedbusModifyObject);			
					
    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  */
	
    timeSec = 0;
    
	/**/
	gs->head= malloc (sizeof(CubedbusObject));
		if(gs->head == NULL) return FALSE;
	gs->head->next=NULL;
	gs->head->prev=NULL;
	gs->head->type=CUBEDBUS_OBJECT;
	
	strcpy(gs->head->name,"root");
	
	gs->head->s=s;
			gs->head->func   =&cubedbusDrawObj;   /* function called when drawing */
			gs->head->delfunc=&cubedbusDeleteObj; /* function called when deleting */
			gs->head->modfunc=&cubedbusModifyObj; /* function called when modifying */
			gs->head->addfunc=&cubedbusAddObj;	/* function called when adding */		
	
	

	gs->head->data = malloc( sizeof(CubedbusObj) );
		if(gs->head->data == NULL) return FALSE;
		
	CubedbusObj *data=gs->head->data;	
	
	data->rotate[0]=0;
	data->rotate[1]=0;
	data->rotate[2]=0;
	data->rotate[3]=0;
	data->translate[0]=0;
	data->translate[1]=0;
	data->translate[2]=0;
	data->scale[0]=1;
	data->scale[1]=1;
	data->scale[2]=1;
	data->order=TRUE;
	data->rotateSpeed=0;
	data->firstChild=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;
}