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