Merge "Changes needed for https://review.tizen.org/gerrit/#/c/191202/" into devel...
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / file-descriptor-monitor-ecore.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/system/common/file-descriptor-monitor.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 // INTERNAL INCLUDES
28 #include <dali/integration-api/debug.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace Adaptor
37 {
38
39 /**
40  * Using Impl to hide away EFL specific members
41  */
42 struct FileDescriptorMonitor::Impl
43 {
44   // Construction
45   Impl( int fileDescriptor, CallbackBase* callback, int eventsToMonitor)
46   : mFileDescriptor( fileDescriptor ),
47     mEventsToMonitor( eventsToMonitor ),
48     mCallback( callback ),
49     mHandler( NULL )
50   {
51   }
52
53   ~Impl()
54   {
55     delete mCallback;
56   }
57
58   // Data
59   int mFileDescriptor;
60   int mEventsToMonitor;              ///< what file descriptor events to monitor
61   CallbackBase* mCallback;
62   Ecore_Fd_Handler* mHandler;
63
64   // Static Methods
65
66   /**
67    * Called when the file descriptor receives an event.
68    */
69   static Eina_Bool EventDispatch(void* data, Ecore_Fd_Handler *handler)
70   {
71     Impl* impl = reinterpret_cast<Impl*>(data);
72
73     // if we want read events, check to see if a read event is available
74     int type = FileDescriptorMonitor::FD_NO_EVENT;
75
76     if( ecore_main_fd_handler_active_get( handler, ECORE_FD_ERROR) )
77     {
78       CallbackBase::Execute( *impl->mCallback, FileDescriptorMonitor::FD_ERROR);
79       DALI_LOG_ERROR("ECORE_FD_ERROR occurred on %d\n", impl->mFileDescriptor);
80
81       return ECORE_CALLBACK_CANCEL;
82     }
83
84     if( impl->mEventsToMonitor & ECORE_FD_READ )
85     {
86       if (ecore_main_fd_handler_active_get( handler, ECORE_FD_READ))
87       {
88         type = FileDescriptorMonitor::FD_READABLE;
89       }
90     }
91     // check if we want write events
92     if( impl->mEventsToMonitor & ECORE_FD_WRITE )
93     {
94       if (ecore_main_fd_handler_active_get( handler, ECORE_FD_WRITE))
95       {
96         type |= FileDescriptorMonitor::FD_WRITABLE;
97       }
98     }
99
100     // if there is an event, execute the callback
101     if( type != FileDescriptorMonitor::FD_NO_EVENT )
102     {
103       CallbackBase::Execute( *impl->mCallback, static_cast< FileDescriptorMonitor::EventType >(type ) );
104     }
105
106     return ECORE_CALLBACK_RENEW;
107   }
108 };
109
110 FileDescriptorMonitor::FileDescriptorMonitor( int fileDescriptor, CallbackBase* callback, int eventBitmask)
111 {
112   mImpl = new Impl(fileDescriptor, callback, eventBitmask);
113
114   if (fileDescriptor < 1)
115   {
116     DALI_ASSERT_ALWAYS( 0 && "Invalid File descriptor");
117     return;
118   }
119
120   int events = 0;
121   if( eventBitmask & FD_READABLE)
122   {
123     events = ECORE_FD_READ;
124   }
125   if( eventBitmask & FD_WRITABLE)
126   {
127     events |= ECORE_FD_WRITE;
128   }
129   mImpl->mEventsToMonitor = events;
130   mImpl->mHandler = ecore_main_fd_handler_add( fileDescriptor, static_cast<Ecore_Fd_Handler_Flags >( events ), &Impl::EventDispatch, mImpl, NULL, NULL );
131
132 }
133
134 FileDescriptorMonitor::~FileDescriptorMonitor()
135 {
136   if (mImpl->mHandler)
137   {
138     ecore_main_fd_handler_del(mImpl->mHandler);
139   }
140
141   delete mImpl;
142   mImpl = NULL;
143 }
144
145 } // namespace Adaptor
146
147 } // namespace Internal
148
149 } // namespace Dali
150
151 #pragma GCC diagnostic pop