[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / trigger-event.cpp
1 /*
2  * Copyright (c) 2014 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/trigger-event.h>
20
21 // EXTERNAL INCLUDES
22 #include <sys/eventfd.h>
23 #include <unistd.h>
24
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/internal/system/common/file-descriptor-monitor.h>
29 #include <dali/internal/system/common/system-factory.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace Adaptor
36 {
37 TriggerEvent::TriggerEvent(CallbackBase* callback, TriggerEventInterface::Options options)
38 : mFileDescriptorMonitor(),
39   mCallback(callback),
40   mFileDescriptor(-1),
41   mOptions(options)
42 {
43   // Create accompanying file descriptor.
44   mFileDescriptor = eventfd(0, EFD_NONBLOCK);
45   if(mFileDescriptor >= 0)
46   {
47     // Now Monitor the created event file descriptor
48     mFileDescriptorMonitor = Dali::Internal::Adaptor::GetSystemFactory()->CreateFileDescriptorMonitor(mFileDescriptor, MakeCallback(this, &TriggerEvent::Triggered), FileDescriptorMonitor::FD_READABLE);
49   }
50   else
51   {
52     DALI_LOG_ERROR("Unable to create TriggerEvent File descriptor\n");
53   }
54 }
55
56 TriggerEvent::~TriggerEvent()
57 {
58   delete mCallback;
59
60   if(mFileDescriptor >= 0)
61   {
62     close(mFileDescriptor);
63     mFileDescriptor = 0;
64   }
65 }
66
67 void TriggerEvent::Trigger()
68 {
69   if(mFileDescriptor >= 0)
70   {
71     // Increment event counter by 1.
72     // Writing to the file descriptor triggers the Dispatch() method in the other thread
73     // (if in multi-threaded environment).
74
75     uint64_t data = 1;
76     int      size = write(mFileDescriptor, &data, sizeof(uint64_t));
77
78     if(size != sizeof(uint64_t))
79     {
80       DALI_LOG_ERROR("Unable to write to UpdateEvent File descriptor\n");
81     }
82   }
83   else
84   {
85     DALI_LOG_WARNING("Attempting to write to an invalid file descriptor\n");
86   }
87 }
88
89 void TriggerEvent::Triggered(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
90 {
91   if(!(eventBitMask & FileDescriptorMonitor::FD_READABLE))
92   {
93     DALI_ASSERT_ALWAYS(0 && "Trigger event file descriptor error");
94     return;
95   }
96
97   // Reading from the file descriptor resets the event counter, we can ignore the count.
98   uint64_t receivedData;
99   size_t   size;
100   size = read(mFileDescriptor, &receivedData, sizeof(uint64_t));
101   if(size != sizeof(uint64_t))
102   {
103     DALI_LOG_WARNING("Unable to read to UpdateEvent File descriptor\n");
104   }
105
106   // Save value to prevent duplicate deletion
107   TriggerEventInterface::Options options = mOptions;
108
109   // Call the connected callback
110   CallbackBase::Execute(*mCallback);
111
112   //check if we should delete ourselves after the trigger
113   if(options == TriggerEventInterface::DELETE_AFTER_TRIGGER)
114   {
115     delete this;
116   }
117 }
118
119 } // namespace Adaptor
120
121 } // namespace Internal
122
123 } // namespace Dali