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