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>
25 #include <dali/integration-api/debug.h>
37 * Using Impl to hide away Android specific members
39 struct FileDescriptorMonitor::Impl
42 Impl( int fileDescriptor, CallbackBase* callback, int eventBitmask )
43 : mCallback( callback ),
44 mFileDescriptor( fileDescriptor ),
45 mEventsToMonitor( eventBitmask )
55 CallbackBase* mCallback;
62 * Called when the file descriptor receives an event.
64 static int EventDispatch( int fd, int events, void* data )
66 Impl* impl = reinterpret_cast<Impl*>(data);
68 // if we want read events, check to see if a read event is available
69 int type = FileDescriptorMonitor::FD_NO_EVENT;
71 if( impl->mEventsToMonitor & ALOOPER_EVENT_INPUT )
74 type = FileDescriptorMonitor::FD_READABLE;
77 // check if we want write events
78 if( impl->mEventsToMonitor & ALOOPER_EVENT_OUTPUT )
81 type |= FileDescriptorMonitor::FD_WRITABLE;
85 // if there is an event, execute the callback
86 if( type != FileDescriptorMonitor::FD_NO_EVENT )
88 CallbackBase::Execute( *impl->mCallback, static_cast< FileDescriptorMonitor::EventType >( type ), impl->mFileDescriptor );
91 return 1; // Continue receiving callbacks
95 FileDescriptorMonitor::FileDescriptorMonitor( int fileDescriptor, CallbackBase* callback, int eventBitmask )
97 mImpl = new Impl(fileDescriptor, callback, eventBitmask);
99 if (fileDescriptor >= 0)
102 if( eventBitmask & FD_READABLE)
104 events = ALOOPER_EVENT_INPUT;
106 if( eventBitmask & FD_WRITABLE)
108 events |= ALOOPER_EVENT_OUTPUT;
111 mImpl->mEventsToMonitor = events;
113 ALooper* looper = ALooper_forThread();
116 ALooper_addFd( looper, fileDescriptor, ALOOPER_POLL_CALLBACK, events, &Impl::EventDispatch, mImpl );
121 FileDescriptorMonitor::~FileDescriptorMonitor()
123 if( mImpl->mFileDescriptor )
125 ALooper* looper = ALooper_forThread();
128 ALooper_removeFd( looper, mImpl->mFileDescriptor );
136 } // namespace Adaptor
138 } // namespace Internal