2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/system/common/trigger-event.h>
22 #include <sys/eventfd.h>
25 #include <dali/integration-api/debug.h>
29 #include <dali/internal/system/common/file-descriptor-monitor.h>
37 TriggerEvent::TriggerEvent(CallbackBase* callback, TriggerEventInterface::Options options)
38 : mFileDescriptorMonitor(NULL),
43 // Create accompanying file descriptor.
44 mFileDescriptor = eventfd(0, EFD_NONBLOCK);
45 if(mFileDescriptor >= 0)
47 // Now Monitor the created event file descriptor
48 mFileDescriptorMonitor = new FileDescriptorMonitor(mFileDescriptor, MakeCallback(this, &TriggerEvent::Triggered), FileDescriptorMonitor::FD_READABLE);
52 DALI_LOG_ERROR("Unable to create TriggerEvent File descriptor\n");
56 TriggerEvent::~TriggerEvent()
58 delete mFileDescriptorMonitor;
61 if(mFileDescriptor >= 0)
63 close(mFileDescriptor);
68 void TriggerEvent::Trigger()
70 if(mFileDescriptor >= 0)
72 // Increment event counter by 1.
73 // Writing to the file descriptor triggers the Dispatch() method in the other thread
74 // (if in multi-threaded environment).
77 int size = write(mFileDescriptor, &data, sizeof(uint64_t));
79 if(size != sizeof(uint64_t))
81 DALI_LOG_ERROR("Unable to write to UpdateEvent File descriptor\n");
86 DALI_LOG_WARNING("Attempting to write to an invalid file descriptor\n");
90 void TriggerEvent::Triggered(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
92 if(!(eventBitMask & FileDescriptorMonitor::FD_READABLE))
94 DALI_ASSERT_ALWAYS(0 && "Trigger event file descriptor error");
98 // Reading from the file descriptor resets the event counter, we can ignore the count.
99 uint64_t receivedData;
101 size = read(mFileDescriptor, &receivedData, sizeof(uint64_t));
102 if(size != sizeof(uint64_t))
104 DALI_LOG_WARNING("Unable to read to UpdateEvent File descriptor\n");
107 // Save value to prevent duplicate deletion
108 TriggerEventInterface::Options options = mOptions;
110 // Call the connected callback
111 CallbackBase::Execute(*mCallback);
113 //check if we should delete ourselves after the trigger
114 if(options == TriggerEventInterface::DELETE_AFTER_TRIGGER)
120 } // namespace Adaptor
122 } // namespace Internal