Initial Version of LayerManagementService added.
[profile/ivi/layer-management.git] / LayerManagerPlugins / Communicators / DBUSCommunicator / src / DBUSCommunicator.cpp
1 /***************************************************************************
2 *
3 * Copyright 2010 BMW Car IT GmbH
4 *
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *               http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ****************************************************************************/
19 #include <dbus/dbus.h>
20 #include <stdbool.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <string>
26
27 #include "DBUSCommunicator.h"
28 #include "Log.h"
29
30 #define CONNMAN_SERVICE                 "de.bmw.CompositingService"
31 #define CONNMAN_PATH                    "/de/bmw/CompositingService"
32
33 typedef void (DBUSCommunicator::*CallBackMethod) (DBusConnection *connection, DBusMessage *message);
34
35 typedef struct {
36       const char *name;
37       const char *signature;
38       const char *reply;
39       CallBackMethod function;
40 } MethodTable;
41
42 static MethodTable manager_methods[] = {
43                 { "Debug",              "b",    "",     &DBUSCommunicator::Debug },
44                 { "ListAllLayerIDS", "",    "au",   &DBUSCommunicator::ListAllLayerIDS},
45                 { "ListAllSurfaceIDS", "",    "au",   &DBUSCommunicator::ListAllSurfaceIDS},
46                 { "ListAllLayerGroupIDS", "",    "au",   &DBUSCommunicator::ListAllLayerGroupIDS},
47                 { "ListAllSurfaceGroupIDS", "",    "au",   &DBUSCommunicator::ListAllSurfaceGroupIDS},
48                 { "ListSurfacesOfSurfacegroup", "u",    "au",   &DBUSCommunicator::ListSurfacesOfSurfacegroup},
49                 { "ListLayersOfLayergroup", "u",    "au",   &DBUSCommunicator::ListLayersOfLayergroup},
50                 { "ListSurfaceofLayer", "u",    "au",   &DBUSCommunicator::ListSurfaceofLayer},
51                 { "getPropertiesOfSurface", "u",    "duuuuuuuuyb",   &DBUSCommunicator::getPropertiesOfSurface},
52                 { "getPropertiesOfLayer", "u",    "duuuuuuuuyb",  &DBUSCommunicator::getPropertiesOfLayer },
53                 { "CreateSurface", "uuuu",    "u",   &DBUSCommunicator::CreateSurface},
54                 { "RemoveSurface", "u",    "",  &DBUSCommunicator::RemoveSurface },
55                 { "CreateLayer", "",    "u",  &DBUSCommunicator::CreateLayer },
56                 { "RemoveLayer", "u",    "",  &DBUSCommunicator::RemoveLayer },
57                 { "AddSurfaceToSurfaceGroup", "uu",    "",  &DBUSCommunicator::AddSurfaceToSurfaceGroup },
58                 { "RemoveSurfaceFromSurfaceGroup", "uu",    "",  &DBUSCommunicator::RemoveSurfaceFromSurfaceGroup },
59                 { "AddLayerToLayerGroup", "uu",    "",   &DBUSCommunicator::AddLayerToLayerGroup},
60                 { "RemoveLayerFromLayerGroup",  "uu", "", &DBUSCommunicator::RemoveLayerFromLayerGroup },
61                 { "AddSurfaceToLayer", "uu",    "", &DBUSCommunicator::AddSurfaceToLayer  },
62                 { "RemoveSurfaceFromLayer", "uu",    "",   &DBUSCommunicator::RemoveSurfaceFromLayer},
63                 { "CreateSurfaceGroup", "",    "u",  &DBUSCommunicator::CreateSurfaceGroup },
64                 { "RemoveSurfaceGroup", "u",    "",   &DBUSCommunicator::RemoveSurfaceGroup},
65                 { "CreateLayerGroup", "",    "u",  &DBUSCommunicator::CreateLayerGroup },
66                 { "RemoveLayerGroup", "u",    "",  &DBUSCommunicator::RemoveLayerGroup },
67                 { "SetSurfaceSourceRegion", "uuuuu",    "",  &DBUSCommunicator::SetSurfaceSourceRegion },
68                 { "SetLayerSourceRegion", "uuuuu",    "",   &DBUSCommunicator::SetLayerSourceRegion},
69                 { "SetSurfaceDestinationRegion", "uuuuu",    "",   &DBUSCommunicator::SetSurfaceDestinationRegion},
70                 { "SetSurfacePosition", "uuu",    "",  &DBUSCommunicator::SetSurfacePosition },
71                 { "GetSurfacePosition", "u",    "uu",  &DBUSCommunicator::GetSurfacePosition },
72                 { "SetSurfaceDimension", "uuu",    "",   &DBUSCommunicator::SetSurfaceDimension},
73                 { "SetLayerDestinationRegion", "uuuuu",    "",   &DBUSCommunicator::SetLayerDestinationRegion},
74                 { "SetLayerPosition", "uuu",    "",   &DBUSCommunicator::SetLayerPosition},
75                 { "GetLayerPosition", "u",    "uu",   &DBUSCommunicator::GetLayerPosition},
76                 { "SetLayerDimension", "uuu",    "",   &DBUSCommunicator::SetLayerDimension},
77                 { "GetLayerDimension", "u",    "uu",   &DBUSCommunicator::GetLayerDimension},
78                 { "GetSurfaceDimension", "u",    "uu",   &DBUSCommunicator::GetSurfaceDimension},
79                 { "SetSurfaceOpacity", "ud",    "",   &DBUSCommunicator::SetSurfaceOpacity},
80                 { "SetLayerOpacity", "ud",    "",  &DBUSCommunicator::SetLayerOpacity },
81                 { "SetSurfacegroupOpacity", "ud",    "",  &DBUSCommunicator::SetSurfacegroupOpacity },
82                 { "SetLayergroupOpacity", "ud",    "",  &DBUSCommunicator::SetLayergroupOpacity },
83                 { "GetSurfaceOpacity", "u",    "d",  &DBUSCommunicator::GetSurfaceOpacity },
84                 { "GetLayerOpacity", "u",    "d", &DBUSCommunicator::GetLayerOpacity  },
85                 { "GetSurfacegroupOpacity", "u",    "d",  &DBUSCommunicator::GetSurfacegroupOpacity },
86                 { "GetLayergroupOpacity", "u",    "d",  &DBUSCommunicator::GetLayergroupOpacity },
87                 { "SetSurfaceOrientation", "uu",    "",  &DBUSCommunicator::SetSurfaceOrientation },
88                 { "SetLayerOrientation", "uu",    "",  &DBUSCommunicator::SetLayerOrientation },
89                 { "GetSurfacePixelformat", "u",    "u",  &DBUSCommunicator::GetSurfacePixelformat },
90                 { "SetSurfaceVisibility", "ub",    "",  &DBUSCommunicator::SetSurfaceVisibility },
91                 { "SetLayerVisibility", "ub",    "",  &DBUSCommunicator::SetLayerVisibility },
92                 { "GetSurfaceVisibility", "u",    "b", &DBUSCommunicator::GetSurfaceVisibility  },
93                 { "GetLayerVisibility", "u",    "b",   &DBUSCommunicator::GetLayerVisibility },
94                 { "SetSurfacegroupVisibility", "ub",    "",  &DBUSCommunicator::SetSurfacegroupVisibility },
95                 { "SetLayergroupVisibility", "ub",    "",  &DBUSCommunicator::SetLayergroupVisibility },
96                 { "SetRenderOrderOfLayers", "auu",    "",  &DBUSCommunicator::SetRenderOrderOfLayers },
97                 { "SetSurfaceRenderOrderWithinLayer", "uau",    "",  &DBUSCommunicator::SetSurfaceRenderOrderWithinLayer },
98                 { "GetLayerType", "u",    "u",&DBUSCommunicator::GetLayerType   },
99                 { "GetLayertypeCapabilities", "u",    "u",  &DBUSCommunicator::GetLayertypeCapabilities },
100                 { "GetLayerCapabilities", "u",    "u",  &DBUSCommunicator::GetLayerCapabilities },
101                 { "Exit", "",    "",  &DBUSCommunicator::Exit },
102                 { "CommitChanges", "",    "", &DBUSCommunicator::CommitChanges  },
103                 { "CreateShader", "ss",    "u",  &DBUSCommunicator::CreateShader },
104                 { "DestroyShader", "u",    "",   &DBUSCommunicator::DestroyShader},
105                 { "SetShader", "uu",    "",  &DBUSCommunicator::SetShader },
106                 { "SetUniforms", "uas",    "",  &DBUSCommunicator::SetUniforms },
107          { "","","",NULL}
108  };
109
110 #include <string>
111
112 DBusMessageIter iter;
113 DBusMessage* currentMsg;
114 DBusMessage* reply;
115 dbus_uint32_t serial = 0;
116 DBusConnection* conn;
117
118 char* getStringFromMessage(){
119    char* param;
120
121    if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&iter)){
122           LOG_ERROR("DBUSCommunicator", "Argument is not string!");
123    }else{
124           dbus_message_iter_get_basic(&iter, &param);
125    }
126    return param;
127 }
128
129 bool getBoolFromMessage(){
130         dbus_bool_t boolparam;
131
132    if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&iter)){
133            LOG_ERROR("DBUSCommunicator", "Argument is not bool!");
134    }else{
135           dbus_message_iter_get_basic(&iter, &boolparam);
136    }
137         bool b;
138         if (boolparam==0)
139                 b = false;
140         else
141                 b = true;
142    return b;
143 }
144
145 char getByteFromMessage(){
146    char param;
147
148    if (DBUS_TYPE_BYTE != dbus_message_iter_get_arg_type(&iter)){
149            LOG_ERROR("DBUSCommunicator", "Argument is not byte!");
150    }else{
151           dbus_message_iter_get_basic(&iter, &param);
152    }
153    return param;
154 }
155
156 uint getUIntFromMessage(){
157    uint param;
158
159    if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&iter)){
160            LOG_ERROR("DBUSCommunicator", "Argument is not uint32!");
161    }else{
162           dbus_message_iter_get_basic(&iter, &param);
163    }
164    return param;
165 }
166
167
168
169 double getDoubleFromMessage(){
170         double param;
171
172    if (DBUS_TYPE_DOUBLE != dbus_message_iter_get_arg_type(&iter)){
173            LOG_ERROR("DBUSCommunicator","Argument is not double!");
174    }else{
175           dbus_message_iter_get_basic(&iter, &param);
176    }
177    return param;
178 }
179
180
181 void appendBool(bool toAppend){
182         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &toAppend)) {
183                 LOG_ERROR("DBUSCommunicator", "Out Of Memory!");
184               exit(1);
185            }
186 }
187
188 void appendUint(uint toAppend){
189         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &toAppend)) {
190                 LOG_ERROR("DBUSCommunicator", "Out Of Memory!");
191               exit(1);
192            }
193 }
194
195 void appendDouble(double toAppend){
196         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_DOUBLE, &toAppend)) {
197                 LOG_ERROR("DBUSCommunicator", "Out Of Memory!");
198               exit(1);
199            }
200 }
201
202 void appendbyte(char toAppend){
203         if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BYTE, &toAppend)) {
204                 LOG_ERROR("DBUSCommunicator", "Out Of Memory!");
205               exit(1);
206            }
207 }
208
209 void initReceive(DBusMessage* msg){
210         currentMsg = msg;
211         if (!dbus_message_iter_init(currentMsg, &iter))
212                 LOG_ERROR("DBUSCommunicator", "Message has no arguments!");
213 }
214
215 void initReply(){
216         // create a reply from the message
217    reply = dbus_message_new_method_return(currentMsg);
218    dbus_message_iter_init_append(reply, &iter);
219 }
220
221 void closeReply(){
222         // send the reply && flush the connection
223    if (!dbus_connection_send(conn, reply, &serial)) {
224            LOG_ERROR("DBUSCommunicator", "Out Of Memory!");
225           exit(1);
226    }
227    dbus_connection_flush(conn);
228
229    // free the reply
230    dbus_message_unref(reply);
231 }
232
233
234
235 void addIntrospectHeader(std::string& str){
236         str.append("<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" \n \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\"> \n");
237 }
238
239 void addIntrospectNode(std::string& str, std::string nodename){
240         str.append("<node name=\"" + nodename  + "\">  \n");
241 }
242
243 void addIntrospectinterface(std::string& str, std::string interfacename){
244         str.append("<interface name=\"" + interfacename + "\">  \n");
245 }
246
247 void addIntrospectMethod(std::string& str, std::string methodname){
248         str.append("<method name=\"" + methodname + "\">  \n");
249 }
250
251 void addIntrospectArg(std::string& str, std::string argname,std::string direction,std::string type){
252         str.append("<arg name=\"" + argname + "\" direction=\"" + direction + "\" type=\"" + type + "\"/>  \n");
253 }
254
255 void closeIntrospectMethod(std::string& str){
256         str.append("</method>  \n");
257 }
258
259 void closeIntrospectInterface(std::string& str){
260         str.append("</interface>  \n");
261 }
262
263 void closeIntrospectNode(std::string& str){
264         str.append("</node>  \n");
265 }
266
267 std::string generateIntrospectionString(MethodTable* table){
268 LOG_DEBUG("DBUSCommunicator", "Generation introspection data");
269         std::string introspect;
270         addIntrospectHeader(introspect);
271            addIntrospectNode(introspect, "de.bmw.CompositingService");
272            addIntrospectinterface(introspect, "org.freedesktop.DBus.Introspectable");
273            addIntrospectMethod(introspect,"Introspect");
274            addIntrospectArg(introspect,"data","out","s");
275            closeIntrospectMethod(introspect);
276            closeIntrospectInterface(introspect);
277
278            addIntrospectinterface(introspect, "de.bmw.CompositingService");
279
280            int i=0;
281            while(strcmp(manager_methods[i].name,"")!=0){
282                    MethodTable entry = table[i];
283                    addIntrospectMethod(introspect,std::string(entry.name));
284                    std::string signatureIN = std::string(entry.signature);
285                    for (int c=0;c<signatureIN.length();c++){
286                            switch(signatureIN.at(c)){
287                                    case 'a': addIntrospectArg(introspect,"","in","a" + signatureIN.at(c+1));c++; break;
288                                    default: addIntrospectArg(introspect,"","in","i"); break;
289                            }
290                    }
291
292                    std::string reply = std::string(entry.reply);
293                    for (int c=0;c<reply.length();c++){
294                            switch(reply.at(c)){
295                                    case 'a':addIntrospectArg(introspect,"","out","a" + reply.at(c+1)); c++; break;
296                                    default:addIntrospectArg(introspect,"","out","i"); break;
297                            }
298                    }
299
300                    closeIntrospectMethod(introspect);
301                    i++;
302            }
303
304                 closeIntrospectInterface(introspect);
305
306            closeIntrospectNode(introspect);
307            LOG_DEBUG("DBUSCommunicator", "returning introspection data");
308            return introspect;
309 }
310
311 DBUSCommunicator* DBUSCommunicator::m_reference = NULL;
312
313 DBUSCommunicator::DBUSCommunicator(CommandExecutor* executor, ILayerList* ll) : BaseCommunicator(executor,ll){
314 }
315
316 DBUSCommunicator::~DBUSCommunicator(){
317
318 }
319
320 void DBUSCommunicator::setdebug(bool onoff){
321
322 }
323
324 bool DBUSCommunicator::start(){
325         LOG_INFO("DBUSCommunicator", "Starting up dbus connector");
326         DBusError err;
327         int ret;
328                 // initialise the error
329                 dbus_error_init(&err);
330
331                 // connect to the bus and check for errors
332                 LOG_INFO("DBUSCommunicator","get DBUS Session");
333                 conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
334                 if (dbus_error_is_set(&err)) {
335                         LOG_ERROR("DBUSCommunicator","Connection error");
336                   dbus_error_free(&err);
337                 }
338                 if (NULL == conn) {
339                         LOG_ERROR("DBUSCommunicator","Connection is null");
340                   exit(1);
341                 }
342                 LOG_INFO("DBUSCommunicator","request dbus name");
343                 ret = dbus_bus_request_name(conn, "de.bmw.CompositingService", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);
344                 if (dbus_error_is_set(&err)) {
345                         LOG_ERROR("DBUSCommunicator", "Name Error "<< err.message);
346                   dbus_error_free(&err);
347                 }
348                 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) {
349                         LOG_ERROR("DBUSCommunicator", "Not Primary Owner "<< ret);
350                   exit(1);
351                 }
352                 LOG_INFO("DBUSCommunicator","create thread");
353         this->m_running = true;
354         pthread_create(&m_currentThread, NULL, DBUSCommunicator::run, this);
355         LOG_INFO("DBUSCommunicator", "Started dbus connector");
356         return true;
357 }
358
359 void DBUSCommunicator::stop(){
360         LOG_INFO("DBUSCommunicator","stopping");
361         this->m_running = false;
362         pthread_join(m_currentThread,NULL);
363         DBusError err;
364         dbus_error_init(&err);
365         bool errorset = dbus_error_is_set(&err);
366         if (errorset)
367                 LOG_ERROR("DBUSCommunicator","there was an dbus error");
368         LOG_INFO("ask about owner name",0);
369         dbus_bus_name_has_owner(conn,"de.bmw.CompositingService",&err);
370         errorset = dbus_error_is_set(&err);
371         if (errorset)
372                 LOG_ERROR("DBUSCommunicator","there was an dbus error");
373         dbus_error_init(&err);
374         dbus_bus_release_name(conn, "de.bmw.CompositingService",&err);
375         LOG_INFO("DBUSCommunicator", "Stopped dbus connector");
376 }
377
378 void introspectCallback(DBusConnection* conn,DBusMessage* msg )
379 {
380    DBusMessage* reply;
381    DBusMessageIter args;
382    dbus_uint32_t serial = 0;
383    char* param;
384
385    // create a reply from the message
386    reply = dbus_message_new_method_return(msg);
387
388    std::string introspect = generateIntrospectionString(manager_methods);
389
390
391    const char* string = introspect.c_str();
392    // add the arguments to the reply
393    dbus_message_iter_init_append(reply, &args);
394    if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string)) {
395            LOG_ERROR("DBUSCommunicator", "Out Of Memory!");
396       exit(1);
397    }
398
399    // send the reply && flush the connection
400    if (!dbus_connection_send(conn, reply, &serial)) {
401            LOG_ERROR("DBUSCommunicator", "Out Of Memory!");
402       exit(1);
403    }
404    dbus_connection_flush(conn);
405
406    // free the reply
407    dbus_message_unref(reply);
408 }
409
410 void DBUSCommunicator::Debug(DBusConnection* conn,DBusMessage* msg )
411 {
412    initReceive(msg);
413    bool param = getBoolFromMessage();
414
415    m_reference->m_executor->execute(new DebugCommand(param));
416
417    initReply();
418    closeReply();
419 }
420
421 void DBUSCommunicator::ListAllLayerIDS(DBusConnection* conn,DBusMessage* msg )
422 {
423         currentMsg = msg;
424    int* array = NULL;
425    int length = 0;
426    m_reference->m_layerlist->getLayerIDs(&length,&array);
427    initReply();
428    DBusMessageIter arrayIter;
429    dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "u", &arrayIter);
430    for ( int i = 0; i< length;i++)
431            dbus_message_iter_append_basic(&arrayIter,DBUS_TYPE_UINT32,&array[i]);
432    dbus_message_iter_close_container(&iter,&arrayIter);
433    closeReply();
434 }
435
436 void DBUSCommunicator::ListAllSurfaceIDS(DBusConnection* conn,DBusMessage* msg )
437 {
438         currentMsg = msg;
439    int* array = NULL;
440    int length = 0;
441    m_reference->m_layerlist->getSurfaceIDs(&length,&array);
442    initReply();
443    DBusMessageIter arrayIter;
444    dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "u", &arrayIter);
445    for ( int i = 0; i< length;i++)
446            dbus_message_iter_append_basic(&arrayIter,DBUS_TYPE_UINT32,&array[i]);
447    dbus_message_iter_close_container(&iter,&arrayIter);
448    closeReply();
449 }
450
451 void DBUSCommunicator::ListAllLayerGroupIDS(DBusConnection* conn,DBusMessage* msg )
452 {
453         currentMsg = msg;
454            int* array = NULL;
455            int length = 0;
456            m_reference->m_layerlist->getLayerGroupIDs(&length,&array);
457            initReply();
458            DBusMessageIter arrayIter;
459            dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "u", &arrayIter);
460            for ( int i = 0; i< length;i++)
461                    dbus_message_iter_append_basic(&arrayIter,DBUS_TYPE_UINT32,&array[i]);
462            dbus_message_iter_close_container(&iter,&arrayIter);
463            closeReply();
464 }
465
466 void DBUSCommunicator::ListAllSurfaceGroupIDS(DBusConnection* conn,DBusMessage* msg )
467 {
468         currentMsg = msg;
469            int* array = NULL;
470            int length = 0;
471            m_reference->m_layerlist->getSurfaceGroupIDs(&length,&array);
472            initReply();
473            DBusMessageIter arrayIter;
474            dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "u", &arrayIter);
475            for ( int i = 0; i< length;i++)
476                    dbus_message_iter_append_basic(&arrayIter,DBUS_TYPE_UINT32,&array[i]);
477            dbus_message_iter_close_container(&iter,&arrayIter);
478            closeReply();
479 }
480
481 void DBUSCommunicator::ListSurfacesOfSurfacegroup(DBusConnection* conn,DBusMessage* msg )
482 {
483         initReceive(msg);
484         uint id = getUIntFromMessage();
485         SurfaceGroup* sg = m_reference->m_layerlist->getSurfaceGroup(id);
486         std::list<Surface*> surfaces = sg->getList();
487     initReply();
488         DBusMessageIter arrayIter;
489         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "u", &arrayIter);
490     for ( std::list<Surface*>::const_iterator it=surfaces.begin();it!=surfaces.end();it++){
491                    Surface* s = *it;
492                    int id = s->getID();
493                    dbus_message_iter_append_basic(&arrayIter,DBUS_TYPE_UINT32,&id);
494     }
495    dbus_message_iter_close_container(&iter,&arrayIter);
496    closeReply();
497 }
498
499 void DBUSCommunicator::ListLayersOfLayergroup(DBusConnection* conn,DBusMessage* msg )
500 {
501         initReceive(msg);
502         uint id = getUIntFromMessage();
503
504         LayerGroup* sg = m_reference->m_layerlist->getLayerGroup(id);
505         std::list<Layer*> layers = sg->getList();
506     initReply();
507         DBusMessageIter arrayIter;
508         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "u", &arrayIter);
509     for ( std::list<Layer*>::const_iterator it=layers.begin();it!=layers.end();it++){
510                    Layer* l = *it;
511                    int id = l->getID();
512                    dbus_message_iter_append_basic(&arrayIter,DBUS_TYPE_UINT32,&id);
513     }
514    dbus_message_iter_close_container(&iter,&arrayIter);
515    closeReply();
516 }
517
518 void DBUSCommunicator::ListSurfaceofLayer(DBusConnection* conn,DBusMessage* msg )
519 {
520         initReceive(msg);
521         uint id = getUIntFromMessage();
522
523         Layer* layer = m_reference->m_layerlist->getLayer(id);
524         std::list<Surface*> surfaces = layer->getAllSurfaces();
525     initReply();
526         DBusMessageIter arrayIter;
527         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "u", &arrayIter);
528     for ( std::list<Surface*>::const_iterator it=surfaces.begin();it!=surfaces.end();it++){
529                    Surface* s = *it;
530                    int id = s->getID();
531                    dbus_message_iter_append_basic(&arrayIter,DBUS_TYPE_UINT32,&id);
532     }
533    dbus_message_iter_close_container(&iter,&arrayIter);
534    closeReply();
535 }
536
537 void DBUSCommunicator::getPropertiesOfSurface(DBusConnection* conn,DBusMessage* msg )
538 {
539         initReceive(msg);
540         uint id = getUIntFromMessage();
541
542         Surface* surface = m_reference->m_layerlist->getSurface(id);
543
544         initReply();
545         appendDouble(surface->getOpacity());
546         Rectangle dest = surface->getDestinationRegion();
547         Rectangle src = surface->getDestinationRegion();
548         appendUint(src.x);
549         appendUint(src.y);
550         appendUint(src.width);
551         appendUint(src.height);
552         appendUint(dest.x);
553         appendUint(dest.y);
554         appendUint(dest.width);
555         appendUint(dest.height);
556         OrientationType orientation = surface->getOrientation();
557         appendbyte((char)orientation);
558         appendBool(surface->getVisibility());
559    closeReply();
560 }
561
562 void DBUSCommunicator::getPropertiesOfLayer(DBusConnection* conn,DBusMessage* msg )
563 {
564                 initReceive(msg);
565                 uint id = getUIntFromMessage();
566
567                 Layer* layer = m_reference->m_layerlist->getLayer(id);
568
569                 initReply();
570                 appendDouble(layer->getOpacity());
571                 Rectangle dest = layer->getDestinationRegion();
572                 Rectangle src = layer->getDestinationRegion();
573                 appendUint(src.x);
574                 appendUint(src.y);
575                 appendUint(src.width);
576                 appendUint(src.height);
577                 appendUint(dest.x);
578                 appendUint(dest.y);
579                 appendUint(dest.width);
580                 appendUint(dest.height);
581                 OrientationType orientation = layer->getOrientation();
582                 appendbyte((char)orientation);
583                 appendBool(layer->getVisibility());
584            closeReply();
585 }
586
587 void DBUSCommunicator::CreateSurface(DBusConnection* conn,DBusMessage* msg )
588 {
589         initReceive(msg);
590     uint handle = getUIntFromMessage();
591     dbus_message_iter_next(&iter);
592     uint pixelformat = getUIntFromMessage();
593     PixelFormat pf = (PixelFormat)pixelformat;
594
595     dbus_message_iter_next(&iter);
596     uint width = getUIntFromMessage();
597
598     dbus_message_iter_next(&iter);
599     uint height = getUIntFromMessage();
600     uint id;
601         m_reference->m_executor->execute(new CreateCommand(handle,TypeSurface,pf, width, height,&id));
602    initReply();
603    appendUint(id);
604    closeReply();
605 }
606
607 void DBUSCommunicator::RemoveSurface(DBusConnection* conn,DBusMessage* msg )
608 {
609            initReceive(msg);
610            uint param = getUIntFromMessage();
611            m_reference->m_executor->execute(new RemoveCommand(param,TypeSurface));
612            initReply();
613            closeReply();
614 }
615
616 void DBUSCommunicator::CreateLayer(DBusConnection* conn,DBusMessage* msg )
617 {
618         currentMsg = msg;
619     uint id;
620         m_reference->m_executor->execute(new CreateCommand(-1,TypeLayer,PIXELFORMAT_R8,0,0,&id));
621    initReply();
622    appendUint(id);
623    closeReply();
624 }
625
626 void DBUSCommunicator::RemoveLayer(DBusConnection* conn,DBusMessage* msg )
627 {
628            initReceive(msg);
629            uint param = getUIntFromMessage();
630            m_reference->m_executor->execute(new RemoveCommand(param,TypeLayer));
631            initReply();
632            closeReply();
633 }
634
635 void DBUSCommunicator::AddSurfaceToSurfaceGroup(DBusConnection* conn,DBusMessage* msg )
636 {
637            initReceive(msg);
638            uint surfaceid = getUIntFromMessage();
639            dbus_message_iter_next(&iter);
640            uint surfacegroupid = getUIntFromMessage();
641            m_reference->m_executor->execute(new SurfacegroupAddSurfaceCommand(surfacegroupid,surfaceid));
642            initReply();
643            closeReply();
644 }
645
646 void DBUSCommunicator::RemoveSurfaceFromSurfaceGroup(DBusConnection* conn,DBusMessage* msg )
647 {
648    initReceive(msg);
649    uint surfaceid = getUIntFromMessage();
650    dbus_message_iter_next(&iter);
651    uint surfacegroupid = getUIntFromMessage();
652    m_reference->m_executor->execute(new SurfacegroupRemoveSurfaceCommand(surfacegroupid,surfaceid));
653    initReply();
654    closeReply();
655 }
656
657 void DBUSCommunicator::AddLayerToLayerGroup(DBusConnection* conn,DBusMessage* msg )
658 {
659         initReceive(msg);
660    uint layerid = getUIntFromMessage();
661    dbus_message_iter_next(&iter);
662    uint layergroupid = getUIntFromMessage();
663    m_reference->m_executor->execute(new LayergroupAddLayerCommand(layergroupid,layerid));
664    initReply();
665    closeReply();
666 }
667
668 void DBUSCommunicator::RemoveLayerFromLayerGroup(DBusConnection* conn,DBusMessage* msg )
669 {
670    initReceive(msg);
671    uint layerid = getUIntFromMessage();
672    dbus_message_iter_next(&iter);
673    uint layergroupid = getUIntFromMessage();
674    m_reference->m_executor->execute(new LayergroupRemoveLayerCommand(layergroupid,layerid));
675    initReply();
676    closeReply();
677 }
678
679 void DBUSCommunicator::AddSurfaceToLayer(DBusConnection* conn,DBusMessage* msg )
680 {
681         initReceive(msg);
682    uint surfaceid = getUIntFromMessage();
683    dbus_message_iter_next(&iter);
684    uint layer = getUIntFromMessage();
685    m_reference->m_executor->execute(new LayerAddSurfaceCommand(layer,surfaceid));
686    initReply();
687    closeReply();
688 }
689
690 void DBUSCommunicator::RemoveSurfaceFromLayer(DBusConnection* conn,DBusMessage* msg )
691 {
692            initReceive(msg);
693            uint surfaceid = getUIntFromMessage();
694            dbus_message_iter_next(&iter);
695            uint layerid = getUIntFromMessage();
696            m_reference->m_executor->execute(new LayerRemoveSurfaceCommand(layerid,surfaceid));
697            initReply();
698            closeReply();
699 }
700
701 void DBUSCommunicator::CreateSurfaceGroup(DBusConnection* conn,DBusMessage* msg )
702 {
703         currentMsg = msg;
704    uint newID;
705    m_reference->m_executor->execute(new CreateCommand(-1,TypeSurfaceGroup,PIXELFORMAT_R8,0,0,&newID));
706    initReply();
707    appendUint(newID);
708    closeReply();
709 }
710
711
712 void DBUSCommunicator::RemoveSurfaceGroup(DBusConnection* conn,DBusMessage* msg )
713 {
714    initReceive(msg);
715    uint param = getUIntFromMessage();
716    m_reference->m_executor->execute(new RemoveCommand(param,TypeSurfaceGroup));
717    initReply();
718    closeReply();
719 }
720
721 void DBUSCommunicator::CreateLayerGroup(DBusConnection* conn,DBusMessage* msg )
722 {
723         currentMsg = msg;
724    uint newID;
725    m_reference->m_executor->execute(new CreateCommand(-1,TypeLayerGroup,PIXELFORMAT_R8,0,0,&newID));
726    initReply();
727    appendUint(newID);
728    closeReply();
729 }
730
731 void DBUSCommunicator::RemoveLayerGroup(DBusConnection* conn,DBusMessage* msg )
732 {
733            initReceive(msg);
734            uint param = getUIntFromMessage();
735            m_reference->m_executor->execute(new RemoveCommand(param,TypeLayerGroup));
736            initReply();
737            closeReply();
738 }
739
740 void DBUSCommunicator::SetSurfaceSourceRegion(DBusConnection* conn,DBusMessage* msg )
741 {
742         initReceive(msg);
743         uint id = getUIntFromMessage();
744         dbus_message_iter_next(&iter);
745         uint x = getUIntFromMessage();
746         dbus_message_iter_next(&iter);
747         uint y = getUIntFromMessage();
748         dbus_message_iter_next(&iter);
749         uint width = getUIntFromMessage();
750         dbus_message_iter_next(&iter);
751         uint height = getUIntFromMessage();
752         m_reference->m_executor->execute(new SetSourceRectangleCommand(id,TypeSurface,x,y,width,height));
753         initReply();
754         closeReply();
755 }
756
757 void DBUSCommunicator::SetLayerSourceRegion(DBusConnection* conn,DBusMessage* msg )
758 {
759         initReceive(msg);
760         uint id = getUIntFromMessage();
761         dbus_message_iter_next(&iter);
762         uint x = getUIntFromMessage();
763         dbus_message_iter_next(&iter);
764         uint y = getUIntFromMessage();
765         dbus_message_iter_next(&iter);
766         uint width = getUIntFromMessage();
767         dbus_message_iter_next(&iter);
768         uint height = getUIntFromMessage();
769         m_reference->m_executor->execute(new SetSourceRectangleCommand(id,TypeLayer,x,y,width,height));
770         initReply();
771         closeReply();
772 }
773
774 void DBUSCommunicator::SetSurfaceDestinationRegion(DBusConnection* conn,DBusMessage* msg )
775 {
776         initReceive(msg);
777         uint id = getUIntFromMessage();
778         dbus_message_iter_next(&iter);
779         uint x = getUIntFromMessage();
780         dbus_message_iter_next(&iter);
781         uint y = getUIntFromMessage();
782         dbus_message_iter_next(&iter);
783         uint width = getUIntFromMessage();
784         dbus_message_iter_next(&iter);
785         uint height = getUIntFromMessage();
786         m_reference->m_executor->execute(new SetDestinationRectangleCommand(id,TypeSurface,x,y,width,height));
787         initReply();
788         closeReply();
789 }
790
791 void DBUSCommunicator::SetSurfacePosition(DBusConnection* conn,DBusMessage* msg )
792 {
793         initReceive(msg);
794         uint id = getUIntFromMessage();
795         dbus_message_iter_next(&iter);
796         uint x = getUIntFromMessage();
797         dbus_message_iter_next(&iter);
798         uint y = getUIntFromMessage();
799         m_reference->m_executor->execute(new SetPositionCommand(id,TypeSurface,x,y));
800         initReply();
801         closeReply();
802 }
803
804 void DBUSCommunicator::GetSurfacePosition(DBusConnection* conn,DBusMessage* msg )
805 {
806         initReceive(msg);
807         uint id = getUIntFromMessage();
808         uint x;
809         uint y;
810         m_reference->m_executor->execute(new GetPositionCommand(id,TypeSurface,&x,&y));
811         initReply();
812         appendUint(x);
813         appendUint(y);
814         closeReply();
815 }
816
817 void DBUSCommunicator::SetSurfaceDimension(DBusConnection* conn,DBusMessage* msg )
818 {
819         initReceive(msg);
820         uint id = getUIntFromMessage();
821         dbus_message_iter_next(&iter);
822         uint width = getUIntFromMessage();
823         dbus_message_iter_next(&iter);
824         uint height = getUIntFromMessage();
825         m_reference->m_executor->execute(new SetDimensionCommand(id,TypeSurface,width,height));
826         initReply();
827         closeReply();
828 }
829
830 void DBUSCommunicator::SetLayerDestinationRegion(DBusConnection* conn,DBusMessage* msg )
831 {
832         initReceive(msg);
833         uint id = getUIntFromMessage();
834         dbus_message_iter_next(&iter);
835         uint x = getUIntFromMessage();
836         dbus_message_iter_next(&iter);
837         uint y = getUIntFromMessage();
838         dbus_message_iter_next(&iter);
839         uint width = getUIntFromMessage();
840         dbus_message_iter_next(&iter);
841         uint height = getUIntFromMessage();
842         m_reference->m_executor->execute(new SetDestinationRectangleCommand(id,TypeLayer,x,y,width,height));
843         initReply();
844         closeReply();
845 }
846
847 void DBUSCommunicator::SetLayerPosition(DBusConnection* conn,DBusMessage* msg )
848 {
849         initReceive(msg);
850         uint id = getUIntFromMessage();
851         dbus_message_iter_next(&iter);
852         uint x = getUIntFromMessage();
853         dbus_message_iter_next(&iter);
854         uint y = getUIntFromMessage();
855         m_reference->m_executor->execute(new SetPositionCommand(id,TypeLayer,x,y));
856         initReply();
857         closeReply();
858 }
859
860 void DBUSCommunicator::GetLayerPosition(DBusConnection* conn,DBusMessage* msg )
861 {
862         initReceive(msg);
863         uint id = getUIntFromMessage();
864         uint x;
865         uint y;
866         m_reference->m_executor->execute(new GetPositionCommand(id,TypeLayer,&x,&y));
867         initReply();
868         appendUint(x);
869         appendUint(y);
870         closeReply();
871 }
872
873 void DBUSCommunicator::SetLayerDimension(DBusConnection* conn,DBusMessage* msg )
874 {
875         initReceive(msg);
876         uint id = getUIntFromMessage();
877         dbus_message_iter_next(&iter);
878         uint width = getUIntFromMessage();
879         dbus_message_iter_next(&iter);
880         uint height = getUIntFromMessage();
881         m_reference->m_executor->execute(new SetDimensionCommand(id,TypeLayer,width,height));
882         initReply();
883         closeReply();
884 }
885
886 void DBUSCommunicator::GetLayerDimension(DBusConnection* conn,DBusMessage* msg )
887 {
888         initReceive(msg);
889         uint id = getUIntFromMessage();
890         uint width;
891         uint height;
892         m_reference->m_executor->execute(new GetDimensionCommand(id,TypeLayer,&width,&height));
893         initReply();
894         appendUint(width);
895         appendUint(height);
896         closeReply();
897 }
898
899 void DBUSCommunicator::GetSurfaceDimension(DBusConnection* conn,DBusMessage* msg )
900 {
901         initReceive(msg);
902         uint id = getUIntFromMessage();
903         uint width;
904         uint height;
905         m_reference->m_executor->execute(new GetDimensionCommand(id,TypeSurface,&width,&height));
906         initReply();
907         appendUint(width);
908         appendUint(height);
909         closeReply();
910 }
911
912 void DBUSCommunicator::SetSurfaceOpacity(DBusConnection* conn,DBusMessage* msg )
913 {
914            initReceive(msg);
915            uint id = getUIntFromMessage();
916            dbus_message_iter_next(&iter);
917            double param = getDoubleFromMessage();
918            m_reference->m_executor->execute(new SetOpacityCommand(id,TypeSurface,param));
919            initReply();
920            closeReply();
921 }
922
923 void DBUSCommunicator::SetLayerOpacity(DBusConnection* conn,DBusMessage* msg )
924 {
925            initReceive(msg);
926            uint id = getUIntFromMessage();
927            dbus_message_iter_next(&iter);
928            double param = getDoubleFromMessage();
929            m_reference->m_executor->execute(new SetOpacityCommand(id,TypeLayer,param));
930            initReply();
931            closeReply();
932 }
933
934 void DBUSCommunicator::SetSurfacegroupOpacity(DBusConnection* conn,DBusMessage* msg )
935 {
936            initReceive(msg);
937            uint id = getUIntFromMessage();
938            dbus_message_iter_next(&iter);
939            double param = getDoubleFromMessage();
940            m_reference->m_executor->execute(new SetOpacityCommand(id,TypeSurfaceGroup,param));
941            initReply();
942            closeReply();
943 }
944
945 void DBUSCommunicator::SetLayergroupOpacity(DBusConnection* conn,DBusMessage* msg )
946 {
947            initReceive(msg);
948            uint id = getUIntFromMessage();
949            dbus_message_iter_next(&iter);
950            double param = getDoubleFromMessage();
951            m_reference->m_executor->execute(new SetOpacityCommand(id,TypeLayerGroup,param));
952            initReply();
953            closeReply();
954 }
955
956 void DBUSCommunicator::GetSurfaceOpacity(DBusConnection* conn,DBusMessage* msg )
957 {
958            initReceive(msg);
959            uint id = getUIntFromMessage();
960            double param;
961            m_reference->m_executor->execute(new GetOpacityCommand(id,TypeSurface,&param));
962            initReply();
963            appendDouble(param);
964            closeReply();
965 }
966
967 void DBUSCommunicator::GetLayerOpacity(DBusConnection* conn,DBusMessage* msg )
968 {
969            initReceive(msg);
970            uint id = getUIntFromMessage();
971            double param;
972            m_reference->m_executor->execute(new GetOpacityCommand(id,TypeLayer,&param));
973            initReply();
974            appendDouble(param);
975            closeReply();
976 }
977
978 void DBUSCommunicator::GetSurfacegroupOpacity(DBusConnection* conn,DBusMessage* msg )
979 {
980            initReceive(msg);
981            uint id = getUIntFromMessage();
982            double param;
983            m_reference->m_executor->execute(new GetOpacityCommand(id,TypeSurfaceGroup,&param));
984            initReply();
985            appendDouble(param);
986            closeReply();
987 }
988
989 void DBUSCommunicator::GetLayergroupOpacity(DBusConnection* conn,DBusMessage* msg )
990 {
991            initReceive(msg);
992            uint id = getUIntFromMessage();
993            double param;
994            m_reference->m_executor->execute(new GetOpacityCommand(id,TypeLayerGroup,&param));
995            initReply();
996            appendDouble(param);
997            closeReply();
998 }
999
1000 void DBUSCommunicator::SetSurfaceOrientation(DBusConnection* conn,DBusMessage* msg )
1001 {
1002            initReceive(msg);
1003            uint id = getUIntFromMessage();
1004            dbus_message_iter_next(&iter);
1005            uint param = getUIntFromMessage();
1006            OrientationType o = (OrientationType) param;
1007            m_reference->m_executor->execute(new SetOrientationCommand(id,TypeSurface,o));
1008            initReply();
1009            closeReply();
1010 }
1011
1012 void DBUSCommunicator::SetLayerOrientation(DBusConnection* conn,DBusMessage* msg )
1013 {
1014            initReceive(msg);
1015            uint id = getUIntFromMessage();
1016            dbus_message_iter_next(&iter);
1017            uint param = getUIntFromMessage();
1018            OrientationType o = (OrientationType) param;
1019            m_reference->m_executor->execute(new SetOrientationCommand(id,TypeLayer,o));
1020            initReply();
1021            closeReply();
1022 }
1023
1024 void DBUSCommunicator::GetSurfacePixelformat(DBusConnection* conn,DBusMessage* msg )
1025 {
1026            initReceive(msg);
1027            uint id = getUIntFromMessage();
1028            PixelFormat param;
1029            m_reference->m_executor->execute(new GetPixelformatCommand(id,TypeSurface,&param));
1030            initReply();
1031            appendbyte(param);
1032            closeReply();
1033 }
1034
1035 void DBUSCommunicator::SetSurfaceVisibility(DBusConnection* conn,DBusMessage* msg )
1036 {
1037         initReceive(msg);
1038         const uint surfaceid = getUIntFromMessage();
1039         dbus_message_iter_next(&iter);
1040         bool newVis= getBoolFromMessage();
1041         m_reference->m_executor->execute(new SetVisibilityCommand(surfaceid,TypeSurface,newVis));
1042         initReply();
1043         closeReply();
1044 }
1045
1046 void DBUSCommunicator::SetLayerVisibility(DBusConnection* conn,DBusMessage* msg )
1047 {
1048            initReceive(msg);
1049            uint layerid = getUIntFromMessage();
1050            dbus_message_iter_next(&iter);
1051                    bool myparam = getBoolFromMessage();
1052            m_reference->m_executor->execute(new SetVisibilityCommand(layerid,TypeLayer,myparam));
1053            initReply();
1054            closeReply();
1055 }
1056
1057 void DBUSCommunicator::GetSurfaceVisibility(DBusConnection* conn,DBusMessage* msg )
1058 {
1059            initReceive(msg);
1060            uint id = getUIntFromMessage();
1061            bool param;
1062            m_reference->m_executor->execute(new GetVisibilityCommand(id,TypeSurface,&param));
1063            initReply();
1064            appendBool(param);
1065            closeReply();
1066 }
1067
1068 void DBUSCommunicator::GetLayerVisibility(DBusConnection* conn,DBusMessage* msg )
1069 {
1070            initReceive(msg);
1071            uint id = getUIntFromMessage();
1072            bool param;
1073            m_reference->m_executor->execute(new GetVisibilityCommand(id,TypeLayer,&param));
1074            initReply();
1075            appendBool(param);
1076            closeReply();
1077 }
1078
1079 void DBUSCommunicator::SetSurfacegroupVisibility(DBusConnection* conn,DBusMessage* msg )
1080 {
1081    initReceive(msg);
1082    uint groupid = getUIntFromMessage();
1083    dbus_message_iter_next(&iter);
1084            bool myparam = getBoolFromMessage();
1085    m_reference->m_executor->execute(new SetVisibilityCommand(groupid,TypeSurfaceGroup,myparam));
1086    initReply();
1087    closeReply();
1088 }
1089
1090 void DBUSCommunicator::SetLayergroupVisibility(DBusConnection* conn,DBusMessage* msg )
1091 {
1092            initReceive(msg);
1093            uint groupid = getUIntFromMessage();
1094            dbus_message_iter_next(&iter);
1095                    bool myparam = getBoolFromMessage();
1096            m_reference->m_executor->execute(new SetVisibilityCommand(groupid,TypeLayerGroup,myparam));
1097            initReply();
1098            closeReply();
1099 }
1100
1101 void DBUSCommunicator::SetRenderOrderOfLayers(DBusConnection* conn,DBusMessage* msg )
1102 {
1103    initReceive(msg);
1104    if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&iter))
1105            LOG_ERROR("DBUSCommunicator","Argument is not an array!");
1106    DBusMessageIter arrayIter;
1107    dbus_message_iter_recurse(&iter,&arrayIter);
1108    int* arr;
1109    int length;
1110    dbus_message_iter_get_fixed_array(&arrayIter,&arr,&length);
1111
1112    m_reference->m_executor->execute(new SetLayerOrderCommand(arr,length));
1113
1114    initReply();
1115    closeReply();
1116 }
1117
1118 void DBUSCommunicator::SetSurfaceRenderOrderWithinLayer(DBusConnection* conn,DBusMessage* msg )
1119 {
1120         initReceive(msg);
1121         uint layerid = getUIntFromMessage();
1122         dbus_message_iter_next(&iter);
1123
1124         if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&iter))
1125                 LOG_ERROR("DBUSCommunicator", "Argument is not an array!");
1126         DBusMessageIter arrayIter;
1127         dbus_message_iter_recurse(&iter,&arrayIter);
1128         int* arr;
1129         int length;
1130         dbus_message_iter_get_fixed_array(&arrayIter,&arr,&length);
1131
1132         m_reference->m_executor->execute(new SetOrderWithinLayerCommand(layerid,arr,length));
1133
1134         initReply();
1135         closeReply();
1136 }
1137
1138 void DBUSCommunicator::GetLayerType(DBusConnection* conn,DBusMessage* msg )
1139 {
1140            initReceive(msg);
1141            uint id = getUIntFromMessage();
1142            Layer* l = m_reference->m_layerlist->getLayer(id);
1143            initReply();
1144            appendUint(l->getLayerType());
1145            closeReply();
1146 }
1147
1148 void DBUSCommunicator::GetLayertypeCapabilities(DBusConnection* conn,DBusMessage* msg )
1149 {
1150
1151            initReceive(msg);
1152            uint id = getUIntFromMessage();
1153            LayerType type = (LayerType)id;
1154            uint capabilities = m_reference->m_executor->getLayerTypeCapabilities(type);
1155            initReply();
1156            appendUint(capabilities);
1157            closeReply();
1158 }
1159
1160 void DBUSCommunicator::GetLayerCapabilities(DBusConnection* conn,DBusMessage* msg )
1161 {
1162          initReceive(msg);
1163            uint id = getUIntFromMessage();
1164            Layer* l = m_reference->m_layerlist->getLayer(id);
1165            initReply();
1166            appendUint(l->getCapabilities());
1167            closeReply();
1168 }
1169
1170 void DBUSCommunicator::FadeIn(DBusConnection* conn,DBusMessage* msg )
1171 {
1172    initReceive(msg);
1173    initReply();
1174    closeReply();
1175 }
1176
1177 void DBUSCommunicator::SynchronizedFade(DBusConnection* conn,DBusMessage* msg )
1178 {
1179    initReceive(msg);
1180    initReply();
1181    closeReply();
1182 }
1183
1184 void DBUSCommunicator::FadeOut(DBusConnection* conn,DBusMessage* msg )
1185 {
1186    initReceive(msg);
1187    initReply();
1188    closeReply();
1189 }
1190
1191 void DBUSCommunicator::Exit(DBusConnection* conn,DBusMessage* msg )
1192 {
1193         currentMsg = msg;
1194         m_reference->m_executor->execute(new ExitCommand());
1195    initReply();
1196    closeReply();
1197 }
1198
1199 void DBUSCommunicator::CommitChanges(DBusConnection* conn,DBusMessage* msg )
1200 {
1201         currentMsg = msg;
1202         m_reference->m_executor->execute(new CommitCommand());
1203    initReply();
1204    closeReply();
1205 }
1206
1207 void DBUSCommunicator::CreateShader(DBusConnection* conn,DBusMessage* msg )
1208 {
1209         initReceive(msg);
1210         char* vertname = getStringFromMessage();
1211         dbus_message_iter_next(&iter);
1212         char* fragname = getStringFromMessage();
1213         uint id;
1214         m_reference->m_executor->execute(new CreateShaderCommand(vertname,fragname,&id));
1215    initReply();
1216    appendUint(id);
1217    closeReply();
1218 }
1219
1220 void DBUSCommunicator::DestroyShader(DBusConnection* conn,DBusMessage* msg )
1221 {
1222    initReceive(msg);
1223    uint shaderid = getUIntFromMessage();
1224    m_reference->m_executor->execute(new DestroyShaderCommand(shaderid));
1225    initReply();
1226    closeReply();
1227 }
1228
1229 void DBUSCommunicator::SetShader(DBusConnection* conn,DBusMessage* msg )
1230 {
1231    initReceive(msg);
1232    uint objectid = getUIntFromMessage();
1233    dbus_message_iter_next(&iter);
1234    uint shaderid = getUIntFromMessage();
1235    m_reference->m_executor->execute(new SetShaderCommand(objectid,shaderid));
1236    initReply();
1237    closeReply();
1238 }
1239
1240 void DBUSCommunicator::SetUniforms(DBusConnection* conn,DBusMessage* msg )
1241 {
1242         initReceive(msg);
1243         uint id = getUIntFromMessage();
1244         dbus_message_iter_next(&iter);
1245         if (DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type(&iter))
1246                 LOG_ERROR("DBUSCommunicator", "Argument is not an array!");
1247         std::vector<std::string> uniforms;
1248         DBusMessageIter arrayIter;
1249
1250         dbus_message_iter_recurse(&iter,&arrayIter);
1251         bool hasNext = true;
1252         while(hasNext){
1253           if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&arrayIter))
1254                   LOG_ERROR("DBUSCommunicator", "Argument is not an string!");
1255           char* param;
1256           dbus_message_iter_get_basic(&arrayIter,&param);
1257           uniforms.push_back(std::string(param));
1258           if (dbus_message_iter_has_next(&arrayIter))
1259                   dbus_message_iter_next(&arrayIter);
1260           else
1261                   hasNext = false;
1262         }
1263
1264         m_reference->m_executor->execute(new SetUniformsCommand(id,uniforms));
1265         initReply();
1266         closeReply();
1267 }
1268
1269 void* DBUSCommunicator::run(void * arg){
1270         LOG_INFO("DBUSCommunicator","Running..");
1271 //      pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL);
1272 //      pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
1273         m_reference = (DBUSCommunicator*)arg;
1274         while(m_reference->m_running){
1275 //              pthread_testcancel();
1276                 dbus_connection_read_write(conn, 50);
1277                 DBusMessage* msg = dbus_connection_pop_message(conn);
1278                 if (NULL!=msg){
1279                         const char *n = dbus_message_get_member (msg);
1280
1281                                   bool found = false;
1282                                   int i=0;
1283                                   while(!found && strcmp(manager_methods[i].name,"")!=0){
1284                                           if (n && strcmp(manager_methods[i].name,n) == 0){
1285                                                   MethodTable entry =  manager_methods[i];
1286                                                   LOG_DEBUG("DBUSC","called methodname:" << entry.name);
1287                                                   CallBackMethod m = entry.function;
1288                                                   (m_reference->*m)(conn,msg);
1289                                                   found = true;
1290                                                   }
1291                                           i++;
1292                                   }
1293                                   if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")){
1294                                                   LOG_DEBUG("DBUSCommunicator", "Introspection called");
1295                                                   introspectCallback(conn, msg);
1296                                                   found = true;
1297                                   }
1298                                   if (!found){
1299                                           DBusMessage* reply = dbus_message_new_method_return(msg);
1300                                           uint serial = 0;
1301                                            // send the reply && flush the connection
1302                                            if (!dbus_connection_send(conn, reply, &serial)) {
1303                                                    LOG_ERROR("DBUSCommunicator", "Out Of Memory!");
1304                                                   exit(1);
1305                                            }
1306                                            dbus_connection_flush(conn);
1307
1308                                            // free the reply
1309                                            dbus_message_unref(reply);
1310                                   }
1311
1312                         }
1313         }
1314
1315         LOG_DEBUG("DBUSCommunicator","ending thread");
1316         return 0;
1317 }
1318
1319 extern "C" DBUSCommunicator* createDBUSCommunicator(CommandExecutor* executor, LayerList* ll) {
1320         return new DBUSCommunicator(executor,ll);
1321 }
1322
1323 extern "C" void destroyDBUSCommunicator(DBUSCommunicator* p) {
1324         delete p;
1325 }
1326
1327