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 "trigger-event.h"
22 #include <sys/eventfd.h>
23 #include <boost/bind.hpp>
25 #include <dali/integration-api/debug.h>
29 #include <file-descriptor-monitor.h>
40 TriggerEvent::TriggerEvent( boost::function<void()> functor, TriggerEventInterface::Options options )
41 : mFileDescriptorMonitor(NULL),
46 // Create accompanying file descriptor.
47 mFileDescriptor = eventfd(0, EFD_NONBLOCK);
48 if (mFileDescriptor >= 0)
50 // Now Monitor the created event file descriptor
51 mFileDescriptorMonitor = new FileDescriptorMonitor(mFileDescriptor, boost::bind(&TriggerEvent::Triggered, this));
55 DALI_LOG_ERROR("Unable to create TriggerEvent File descriptor\n");
59 TriggerEvent::~TriggerEvent()
61 if (mFileDescriptorMonitor)
63 delete mFileDescriptorMonitor;
64 mFileDescriptorMonitor = NULL;
67 if (mFileDescriptor >= 0)
69 close(mFileDescriptor);
74 void TriggerEvent::Trigger()
76 if (mFileDescriptor >= 0)
78 // Increment event counter by 1.
79 // Writing to the file descriptor triggers the Dispatch() method in the other thread
80 // (if in multi-threaded environment).
83 int size = write(mFileDescriptor, &data, sizeof(uint64_t));
85 if (size != sizeof(uint64_t))
87 DALI_LOG_ERROR("Unable to write to UpdateEvent File descriptor\n");
92 DALI_LOG_WARNING("Attempting to write to an invalid file descriptor\n");
96 void TriggerEvent::Triggered()
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 // Call the connected boost function.
110 //check if we should delete ourselves after the trigger
111 if( mOptions == TriggerEventInterface::DELETE_AFTER_TRIGGER )
117 } // namespace Adaptor
119 } // namespace Internal