1 /*
2  * Copyright © 2007 Novell, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without
6  * fee, provided that the above copyright notice appear in all copies
7  * and that both that copyright notice and this permission notice
8  * appear in supporting documentation, and that the name of
9  * Novell, Inc. not be used in advertising or publicity pertaining to
10  * distribution of the software without specific, written prior permission.
11  * Novell, Inc. makes no representations about the suitability of this
12  * software for any purpose. It is provided "as is" without express or
13  * implied warranty.
14  *
15  * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17  * NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author: David Reveman <davidr@novell.com>
24  */
25 
26 #include <compiz-core.h>
27 
28 typedef CompBool (*AllocObjectPrivateIndexProc) (CompObject *parent);
29 
30 typedef void (*FreeObjectPrivateIndexProc) (CompObject *parent,
31 					    int	       index);
32 
33 typedef CompBool (*ForEachObjectProc) (CompObject	  *parent,
34 				       ObjectCallBackProc proc,
35 				       void		  *closure);
36 
37 typedef char *(*NameObjectProc) (CompObject *object);
38 
39 typedef CompObject *(*FindObjectProc) (CompObject *parent,
40 				       const char *name);
41 
42 struct _CompObjectInfo {
43     const char			*name;
44     AllocObjectPrivateIndexProc allocPrivateIndex;
45     FreeObjectPrivateIndexProc  freePrivateIndex;
46     ForEachObjectProc		forEachObject;
47     NameObjectProc		nameObject;
48     FindObjectProc		findObject;
49 } objectInfo[] = {
50     {
51 	"core",
52 	allocCoreObjectPrivateIndex,
53 	freeCoreObjectPrivateIndex,
54 	forEachCoreObject,
55 	nameCoreObject,
56 	findCoreObject
57     }, {
58 	"display",
59 	allocDisplayObjectPrivateIndex,
60 	freeDisplayObjectPrivateIndex,
61 	forEachDisplayObject,
62 	nameDisplayObject,
63 	findDisplayObject
64     }, {
65 	"screen",
66 	allocScreenObjectPrivateIndex,
67 	freeScreenObjectPrivateIndex,
68 	forEachScreenObject,
69 	nameScreenObject,
70 	findScreenObject
71     }, {
72 	"window",
73 	allocWindowObjectPrivateIndex,
74 	freeWindowObjectPrivateIndex,
75 	forEachWindowObject,
76 	nameWindowObject,
77 	findWindowObject
78     }
79 };
80 
81 void
compObjectInit(CompObject * object,CompPrivate * privates,CompObjectType type)82 compObjectInit (CompObject     *object,
83 		CompPrivate    *privates,
84 		CompObjectType type)
85 {
86     object->type     = type;
87     object->privates = privates;
88     object->parent   = NULL;
89 }
90 
91 int
compObjectAllocatePrivateIndex(CompObject * parent,CompObjectType type)92 compObjectAllocatePrivateIndex (CompObject     *parent,
93 				CompObjectType type)
94 {
95     return (*objectInfo[type].allocPrivateIndex) (parent);
96 }
97 
98 void
compObjectFreePrivateIndex(CompObject * parent,CompObjectType type,int index)99 compObjectFreePrivateIndex (CompObject     *parent,
100 			    CompObjectType type,
101 			    int	           index)
102 {
103     (*objectInfo[type].freePrivateIndex) (parent, index);
104 }
105 
106 CompBool
compObjectForEach(CompObject * parent,CompObjectType type,ObjectCallBackProc proc,void * closure)107 compObjectForEach (CompObject	      *parent,
108 		   CompObjectType     type,
109 		   ObjectCallBackProc proc,
110 		   void		      *closure)
111 {
112     return (*objectInfo[type].forEachObject) (parent, proc, closure);
113 }
114 
115 CompBool
compObjectForEachType(CompObject * parent,ObjectTypeCallBackProc proc,void * closure)116 compObjectForEachType (CompObject	      *parent,
117 		       ObjectTypeCallBackProc proc,
118 		       void		      *closure)
119 {
120     int i;
121 
122     for (i = 0; i < sizeof (objectInfo) / sizeof (objectInfo[0]); i++)
123 	if (!(*proc) (i, parent, closure))
124 	    return FALSE;
125 
126     return TRUE;
127 }
128 
129 const char *
compObjectTypeName(CompObjectType type)130 compObjectTypeName (CompObjectType type)
131 {
132     return objectInfo[type].name;
133 }
134 
135 char *
compObjectName(CompObject * object)136 compObjectName (CompObject *object)
137 {
138     return (*objectInfo[object->type].nameObject) (object);
139 }
140 
141 CompObject *
compObjectFind(CompObject * parent,CompObjectType type,const char * name)142 compObjectFind (CompObject     *parent,
143 		CompObjectType type,
144 		const char     *name)
145 {
146     return (*objectInfo[type].findObject) (parent, name);
147 }
148