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