2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/system/common/file-descriptor-monitor.h>
22 // Ecore is littered with C style cast
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wold-style-cast"
28 #include <dali/integration-api/debug.h>
40 * Using Impl to hide away EFL specific members
42 struct FileDescriptorMonitor::Impl
45 Impl( int fileDescriptor, CallbackBase* callback, int eventsToMonitor)
46 : mFileDescriptor( fileDescriptor ),
47 mEventsToMonitor( eventsToMonitor ),
48 mCallback( callback ),
60 int mEventsToMonitor; ///< what file descriptor events to monitor
61 CallbackBase* mCallback;
62 Ecore_Fd_Handler* mHandler;
67 * Called when the file descriptor receives an event.
69 static Eina_Bool EventDispatch(void* data, Ecore_Fd_Handler *handler)
71 Impl* impl = reinterpret_cast<Impl*>(data);
73 // if we want read events, check to see if a read event is available
74 int type = FileDescriptorMonitor::FD_NO_EVENT;
76 if( ecore_main_fd_handler_active_get( handler, ECORE_FD_ERROR) )
78 CallbackBase::Execute( *impl->mCallback, FileDescriptorMonitor::FD_ERROR);
79 DALI_LOG_ERROR("ECORE_FD_ERROR occurred on %d\n", impl->mFileDescriptor);
81 return ECORE_CALLBACK_CANCEL;
84 if( impl->mEventsToMonitor & ECORE_FD_READ )
86 if (ecore_main_fd_handler_active_get( handler, ECORE_FD_READ))
88 type = FileDescriptorMonitor::FD_READABLE;
91 // check if we want write events
92 if( impl->mEventsToMonitor & ECORE_FD_WRITE )
94 if (ecore_main_fd_handler_active_get( handler, ECORE_FD_WRITE))
96 type |= FileDescriptorMonitor::FD_WRITABLE;
100 // if there is an event, execute the callback
101 if( type != FileDescriptorMonitor::FD_NO_EVENT )
103 CallbackBase::Execute( *impl->mCallback, static_cast< FileDescriptorMonitor::EventType >(type ) );
106 return ECORE_CALLBACK_RENEW;
110 FileDescriptorMonitor::FileDescriptorMonitor( int fileDescriptor, CallbackBase* callback, int eventBitmask)
112 mImpl = new Impl(fileDescriptor, callback, eventBitmask);
114 if (fileDescriptor < 1)
116 DALI_ASSERT_ALWAYS( 0 && "Invalid File descriptor");
121 if( eventBitmask & FD_READABLE)
123 events = ECORE_FD_READ;
125 if( eventBitmask & FD_WRITABLE)
127 events |= ECORE_FD_WRITE;
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 );
134 FileDescriptorMonitor::~FileDescriptorMonitor()
138 ecore_main_fd_handler_del(mImpl->mHandler);
145 } // namespace Adaptor
147 } // namespace Internal
151 #pragma GCC diagnostic pop