Audacity 3.2.0
RunLoop.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file RunLoop.cpp
6
7 @author Vitaly Sverchinsky
8
9 @brief Part of Audacity VST3 module
10
11**********************************************************************/
12
13#include "RunLoop.h"
14
15using namespace internal::x11;
16
18{
19 Steinberg::IPtr<Steinberg::Linux::IEventHandler> handler;
20 GIOChannel* channel {nullptr};
21 guint id{0};
22
24 {
25 if(id != 0)
26 g_source_remove(id);
27 if(channel != nullptr)
28 g_io_channel_unref(channel);
29 }
30};
31
33{
34 Steinberg::IPtr<Steinberg::Linux::ITimerHandler> handler;
35 guint id {0};
36
38 {
39 if(id != 0)
40 g_source_remove(id);
41 }
42};
43
44IMPLEMENT_FUNKNOWN_METHODS(RunLoop, Steinberg::Linux::IRunLoop, Steinberg::Linux::IRunLoop::iid);
45
46gboolean RunLoop::OnEvent(GIOChannel *source, GIOCondition condition, gpointer data)
47{
48 auto handler = static_cast<Steinberg::Linux::IEventHandler*>(data);
49 handler->onFDIsSet(g_io_channel_unix_get_fd(source));
50 return G_SOURCE_REMOVE;
51}
52
53gboolean RunLoop::OnTimer(gpointer data)
54{
55 auto handler = static_cast<Steinberg::Linux::ITimerHandler*>(data);
56 handler->onTimer();
57 return G_SOURCE_CONTINUE;
58}
59
61 : mDisplay(display)
62{
63 FUNKNOWN_CTOR;
64}
65
67{
68 FUNKNOWN_DTOR;
69}
70
72 Steinberg::Linux::IEventHandler* handler,
73 Steinberg::Linux::FileDescriptor fd)
74{
75 using namespace Steinberg;
76
77 if(handler == nullptr)
78 return kInvalidArgument;
79
80 if(XConnectionNumber(mDisplay) == fd)
81 return kResultOk;
82
83 auto eventHandler = std::make_unique<EventHandler>();
84 eventHandler->handler = handler;
85
86 if(auto channel = g_io_channel_unix_new(fd))
87 {
88 eventHandler->channel = channel;
89 eventHandler->id = g_io_add_watch(
90 channel,
91 (GIOCondition)(G_IO_IN | G_IO_OUT | G_IO_ERR | G_IO_HUP),
94 );
95 mEventHandlers.emplace_back(std::move(eventHandler));
96 return kResultTrue;
97 }
98 return kResultFalse;
99}
100
102 Steinberg::Linux::IEventHandler* handler)
103{
104 using namespace Steinberg;
105
106 for(auto it = mEventHandlers.begin(), end = mEventHandlers.end(); it != end; ++it)
107 {
108 if((*it)->handler.get() == handler)
109 {
110 mEventHandlers.erase(it);
111 return kResultTrue;
112 }
113 }
114 return kResultFalse;
115}
116
117Steinberg::tresult RunLoop::registerTimer (Steinberg::Linux::ITimerHandler* handler,
118 Steinberg::Linux::TimerInterval milliseconds)
119{
120 using namespace Steinberg;
121
122 if(!handler || milliseconds == 0)
123 return kInvalidArgument;
124
125 auto timerHandler = std::make_unique<TimerHandler>();
126 timerHandler->handler = handler;
127 timerHandler->id = g_timeout_add(milliseconds, &RunLoop::OnTimer, handler);
128
129 mTimerHandlers.emplace_back(std::move(timerHandler));
130
131 return Steinberg::kResultOk;
132}
133
134Steinberg::tresult RunLoop::unregisterTimer (Steinberg::Linux::ITimerHandler* handler)
135{
136 using namespace Steinberg;
137
138 for(auto it = mTimerHandlers.begin(), end = mTimerHandlers.end(); it != end; ++it)
139 {
140 if((*it)->handler.get() == handler)
141 {
142 mTimerHandlers.erase(it);
143 return kResultTrue;
144 }
145 }
146 return kResultFalse;
147}
IMPLEMENT_FUNKNOWN_METHODS(RunLoop, Steinberg::Linux::IRunLoop, Steinberg::Linux::IRunLoop::iid)
Steinberg::tresult PLUGIN_API registerEventHandler(Steinberg::Linux::IEventHandler *handler, Steinberg::Linux::FileDescriptor fd) override
Definition: RunLoop.cpp:71
Display * mDisplay
Definition: RunLoop.h:37
Steinberg::tresult PLUGIN_API unregisterEventHandler(Steinberg::Linux::IEventHandler *handler) override
Definition: RunLoop.cpp:101
RunLoop(::Display *display)
Definition: RunLoop.cpp:60
std::vector< std::unique_ptr< EventHandler > > mEventHandlers
Definition: RunLoop.h:32
static gboolean OnEvent(GIOChannel *source, GIOCondition condition, gpointer data)
Definition: RunLoop.cpp:46
std::vector< std::unique_ptr< TimerHandler > > mTimerHandlers
Definition: RunLoop.h:35
Steinberg::tresult PLUGIN_API registerTimer(Steinberg::Linux::ITimerHandler *handler, Steinberg::Linux::TimerInterval milliseconds) override
Definition: RunLoop.cpp:117
Steinberg::tresult PLUGIN_API unregisterTimer(Steinberg::Linux::ITimerHandler *handler) override
Definition: RunLoop.cpp:134
static gboolean OnTimer(gpointer data)
Definition: RunLoop.cpp:53
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
Steinberg::IPtr< Steinberg::Linux::IEventHandler > handler
Definition: RunLoop.cpp:19
Steinberg::IPtr< Steinberg::Linux::ITimerHandler > handler
Definition: RunLoop.cpp:34