[Tizen] Support Ecore-Wayland2
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / ecore-server-connection.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/window-system/common/ecore-server-connection.h>
20
21 // EXTERNAL INCLUDES
22 // Ecore is littered with C style cast
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wold-style-cast"
25 #include <Ecore.h>
26
27 #include <dali/integration-api/debug.h>
28
29 // INTERNAL INCLUDES
30
31 namespace
32 {
33 // Copied from ecore_evas_extn_engine.h
34 // procotol version - change this as needed
35 const int MAJOR( 0x2011 );
36 }
37
38
39 namespace Dali
40 {
41 namespace Internal
42 {
43 namespace Adaptor
44 {
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gServerConnectionLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SERVER_CONNECTION");
47 #endif
48
49 ServerConnection::ServerConnection(
50   const char*                 serviceName,
51   int                         serviceNumber,
52   bool                        isSystem,
53   ServerConnection::Observer* observer)
54
55 : mConnected(false),
56   mObserver(observer)
57 {
58   Ecore_Ipc_Type ipctype = ECORE_IPC_LOCAL_USER;
59
60   ecore_ipc_init();
61   mService.name = eina_stringshare_add(serviceName);
62   mService.num = serviceNumber;
63   mService.isSystem = isSystem;
64
65   if( mService.isSystem )
66   {
67     ipctype = ECORE_IPC_LOCAL_SYSTEM;
68   }
69
70   DALI_LOG_INFO( gServerConnectionLogFilter, Debug::General, "ServerConnection: Connecting to %s %d\n", mService.name, mService.num );
71
72   mIpcServer = ecore_ipc_server_connect( ipctype, (char *)mService.name, mService.num, this );
73
74   if( !mIpcServer )
75   {
76     DALI_LOG_INFO( gServerConnectionLogFilter, Debug::General, "mIpcServer is null\n" );
77     ecore_ipc_shutdown();
78   }
79   else
80   {
81     mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_ADD,
82                                                      &ServerConnection::IpcServerAdd,
83                                                      this ) );
84
85     mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_DEL,
86                                                      &ServerConnection::IpcServerDel,
87                                                      this ) );
88
89     mIpcHandlers.push_back( ecore_event_handler_add( ECORE_IPC_EVENT_SERVER_DATA,
90                                                      &ServerConnection::IpcServerData,
91                                                      this));
92
93     mConnected = true;
94   }
95 }
96
97 ServerConnection::~ServerConnection()
98 {
99   CloseConnection();
100
101   if( mService.name != NULL )
102   {
103     eina_stringshare_del(mService.name);
104   }
105
106   for( Handlers::iterator iter = mIpcHandlers.begin(); iter != mIpcHandlers.end(); ++iter )
107   {
108     ecore_event_handler_del(*iter);
109   }
110   mIpcHandlers.clear();
111 }
112
113 bool ServerConnection::IsConnected()
114 {
115   return mConnected;
116 }
117
118 void ServerConnection::OnDisconnect()
119 {
120   mConnected = false;
121   mIpcServer = NULL;
122   ecore_ipc_shutdown();
123   if( mObserver )
124   {
125     mObserver->ConnectionClosed();
126   }
127 }
128
129 bool ServerConnection::SendEvent( int event, const void *data, int size )
130 {
131   return SendEvent(event, 0, 0, data, size);
132 }
133
134 bool ServerConnection::SendEvent( int event, int ref, int ref_to, const void *data, int size )
135 {
136   if( mIpcServer != NULL  && ecore_ipc_server_send(mIpcServer, MAJOR, event, ref, ref_to, 0, data, size) )
137   {
138     return true;
139   }
140   else
141   {
142     return false;
143   }
144 }
145
146 Eina_Bool ServerConnection::IpcServerAdd( void *data, int /*type*/, void *event )
147 {
148   DALI_LOG_INFO(gServerConnectionLogFilter, Debug::General, "ServerConnection: IpcServerAdd\n" );
149
150   return ECORE_CALLBACK_PASS_ON;
151 }
152
153 Eina_Bool ServerConnection::IpcServerDel( void *data, int /*type*/, void *event )
154 {
155   DALI_LOG_INFO( gServerConnectionLogFilter, Debug::General, "ServerConnection: IpcServerDel\n" );
156
157   Ecore_Ipc_Event_Server_Del *e = static_cast<Ecore_Ipc_Event_Server_Del *>( event );
158   ServerConnection* connection = static_cast<ServerConnection*>( data );
159
160   if( connection != NULL )
161   {
162     if( connection->mIpcServer == e->server)
163     {
164       // No longer have a server connection
165       connection->OnDisconnect();
166     }
167   }
168
169   return ECORE_CALLBACK_PASS_ON;
170 }
171
172 Eina_Bool ServerConnection::IpcServerData( void *data, int /*type*/, void *event )
173 {
174   DALI_LOG_INFO( gServerConnectionLogFilter, Debug::General, "ServerConnection: IpcServerData\n" );
175
176   Ecore_Ipc_Event_Server_Data *e = static_cast<Ecore_Ipc_Event_Server_Data *>( event );
177   ServerConnection* connection = static_cast<ServerConnection*>( data );
178
179   if( connection != NULL )
180   {
181     if( connection != ecore_ipc_server_data_get( e->server ) )
182     {
183       return ECORE_CALLBACK_PASS_ON;
184     }
185
186     if( e->major != MAJOR )
187     {
188       return ECORE_CALLBACK_PASS_ON;
189     }
190
191     if( connection->mObserver )
192     {
193       connection->mObserver->DataReceived( event );
194     }
195   }
196   return ECORE_CALLBACK_PASS_ON;
197 }
198
199 void ServerConnection::CloseConnection()
200 {
201   if( mConnected )
202   {
203     DALI_LOG_INFO( gServerConnectionLogFilter, Debug::General, "ServerConnection: CloseConnection\n" );
204
205     if( mIpcServer )
206     {
207       ecore_ipc_server_del( mIpcServer );
208       mIpcServer = NULL;
209     }
210
211     ecore_ipc_shutdown();
212     mConnected = false;
213   }
214 }
215
216 } // Adaptor
217
218 } // Internal
219
220 } // Dali
221
222 #pragma GCC diagnostic pop