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