-
Notifications
You must be signed in to change notification settings - Fork 49
/
choc_MessageLoop.h
560 lines (458 loc) · 16.1 KB
/
choc_MessageLoop.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//
// ██████ ██ ██ ██████ ██████
// ██ ██ ██ ██ ██ ██ ** Classy Header-Only Classes **
// ██ ███████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ https://github.com/Tracktion/choc
// ██████ ██ ██ ██████ ██████
//
// CHOC is (C)2022 Tracktion Corporation, and is offered under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef CHOC_MESSAGELOOP_HEADER_INCLUDED
#define CHOC_MESSAGELOOP_HEADER_INCLUDED
#include <memory>
#include <string>
#include <functional>
#include <mutex>
#include <chrono>
#include "../platform/choc_Platform.h"
#include "../platform/choc_Assert.h"
//==============================================================================
/**
This namespace provides some bare-minimum event loop and message dispatch
functions.
Note that on Linux this uses GTK, so to build it you'll need to:
1. Install the libgtk-3-dev package.
2. Link the gtk+3.0 library in your build.
You might want to have a look inside choc/tests/CMakeLists.txt for
an example of how to add this packages to your build without too
much fuss.
*/
namespace choc::messageloop
{
/// On some platforms (yep, I mean Windows), it's only possible to send
/// messages after some initialisation has been run on the message thread
/// itself.
/// If you call choc::messageloop::run(), then that will automatically
/// set things up correctly and you don't need to call this function.
/// But if you're running in a program where there's a 3rd-party event
/// loop, you'll need to manually call choc::messageloop::initialise() on
/// your application's message thread at the start of your program to make
/// sure that any threaded calls to postMessage() work correctly.
void initialise();
/// Synchronously runs the system message loop.
void run();
/// Posts a message to make the message loop exit and terminate the app.
void stop();
/// Posts a function be invoked asynchronously by the message thread.
///
/// Tip: if you might need to cancel the callback after posting it,
/// you could use a `choc::threading::ThreadSafeFunctor` to wrap your
/// target function, which lets you safely nullify it.
void postMessage (std::function<void()>&&);
/// Returns true if the current thread is the message thread.
bool callerIsOnMessageThread();
//==============================================================================
/// Manages a periodic timer whose callbacks happen on the message loop.
///
/// You can create a Timer with a callback function and interval, and the
/// function will be repeatedly called (on the message thread) until the Timer
/// is deleted, or clear() is called, or the callback function returns false.
///
struct Timer
{
Timer() = default;
Timer (const Timer&) = delete;
Timer (Timer&&) = default;
Timer& operator= (Timer&&) = default;
~Timer() = default;
/// The callback function should return true to keep the
/// timer running, or false to stop it.
using Callback = std::function<bool()>;
/// Creates and starts a Timer for a particular interval (in
/// milliseconds) and a callback.
/// The callback will continue to be called at this interval
/// until either the Timer object is deleted, or clear() is
/// called, or the callback returns `false`.
Timer (uint32_t intervalMillisecs,
Callback&& callbackFunction);
/// Stops and clears the timer. (You can also clear a Timer
/// by assigning an empty Timer to it).
void clear();
/// Returns true if the Timer has been initialised with
/// a callback, or false if it's just an empty object.
operator bool() const { return pimpl != nullptr; }
private:
struct Pimpl;
std::unique_ptr<Pimpl> pimpl;
};
//==============================================================================
/// Triggers a one-shot timer callback of a given lambda function after a given
/// interval.
/// This uses the Timer class, but saves you needing to keep a Timer object alive,
/// if you just need a fire-and-forget event.
/// The callback function should just be a void lambda.
template <typename Callback>
void setTimeout (uint32_t intervalMillisecs, Callback&& callbackFunction);
}
//==============================================================================
// _ _ _ _
// __| | ___ | |_ __ _ (_)| | ___
// / _` | / _ \| __| / _` || || |/ __|
// | (_| || __/| |_ | (_| || || |\__ \ _ _ _
// \__,_| \___| \__| \__,_||_||_||___/(_)(_)(_)
//
// Code beyond this point is implementation detail...
//
//==============================================================================
#if CHOC_LINUX
#include <thread>
#include "../platform/choc_DisableAllWarnings.h"
#include <gtk/gtk.h>
#include "../platform/choc_ReenableAllWarnings.h"
namespace choc::messageloop
{
inline std::thread::id& getMainThreadIDRef()
{
static std::thread::id i;
return i;
}
inline void initialise()
{
getMainThreadIDRef() = std::this_thread::get_id();
}
inline void run()
{
initialise();
gtk_main();
}
inline void stop()
{
gtk_main_quit();
}
inline void postMessage (std::function<void()>&& fn)
{
g_idle_add_full (G_PRIORITY_HIGH_IDLE,
(GSourceFunc) ([](void* f) -> int
{
(*static_cast<std::function<void()>*>(f))();
return G_SOURCE_REMOVE;
}),
new std::function<void()> (std::move (fn)),
[] (void* f) { delete static_cast<std::function<void()>*>(f); });
}
inline bool callerIsOnMessageThread()
{
return getMainThreadIDRef() == std::this_thread::get_id();
}
struct Timer::Pimpl
{
Pimpl (Callback&& c, uint32_t interval)
{
sharedState = std::make_shared<SharedState>();
sharedState->callback = std::move (c);
handle = g_timeout_add (interval, staticCallback, this);
}
~Pimpl()
{
if (sharedState->isInCallback)
sharedState->isRunning = false;
else if (sharedState->isRunning)
g_source_remove (handle);
}
static gboolean staticCallback (gpointer context)
{
auto state = static_cast<Pimpl*> (context)->sharedState; // keep a local shared_ptr
return state->handleCallback();
}
struct SharedState : public std::enable_shared_from_this<SharedState>
{
Callback callback;
bool isInCallback = false, isRunning = true;
bool handleCallback()
{
isInCallback = true;
bool result = callback();
isInCallback = false;
if (! result)
isRunning = false;
return isRunning;
}
};
std::shared_ptr<SharedState> sharedState;
guint handle;
};
//==============================================================================
#elif CHOC_APPLE
#include <thread>
#include <unordered_set>
#include <dispatch/dispatch.h>
#include <type_traits>
#include "../platform/choc_ObjectiveCHelpers.h"
namespace choc::messageloop
{
inline std::thread::id& getMainThreadIDRef()
{
static std::thread::id i;
return i;
}
inline void initialise()
{
getMainThreadIDRef() = std::this_thread::get_id();
}
inline void run()
{
CHOC_AUTORELEASE_BEGIN
initialise();
objc::call<void> (objc::getSharedNSApplication(), "run");
CHOC_AUTORELEASE_END
}
inline void stop()
{
postMessage ([]
{
using namespace choc::objc;
static constexpr long NSEventTypeApplicationDefined = 15;
CHOC_AUTORELEASE_BEGIN
call<void> (getSharedNSApplication(), "stop:", (id) nullptr);
// After sending the stop message, we need to post a dummy event to
// kick the message loop, otherwise it can just sit there and hang
struct NSPoint { double x = 0, y = 0; };
id dummyEvent = callClass<id> ("NSEvent", "otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:",
NSEventTypeApplicationDefined, NSPoint(), 0, 0, 0, nullptr, (short) 0, 0, 0);
call<void> (getSharedNSApplication(), "postEvent:atStart:", dummyEvent, YES);
CHOC_AUTORELEASE_END
});
}
inline void postMessage (std::function<void()>&& fn)
{
dispatch_async_f (dispatch_get_main_queue(),
new std::function<void()> (std::move (fn)),
(dispatch_function_t) (+[](void* arg)
{
CHOC_AUTORELEASE_BEGIN
std::unique_ptr<std::function<void()>> f (static_cast<std::function<void()>*> (arg));
(*f)();
CHOC_AUTORELEASE_END
}));
}
inline bool callerIsOnMessageThread()
{
return getMainThreadIDRef() == std::this_thread::get_id();
}
struct Timer::Pimpl
{
Pimpl (Callback&& c, uint32_t i) : callback (std::move (c)), interval (i)
{
getList().add (this);
dispatch();
}
~Pimpl()
{
getList().remove (this);
}
static void staticCallback (void* context)
{
if (getList().invokeIfStillAlive (static_cast<Pimpl*> (context)))
{
CHOC_AUTORELEASE_BEGIN
static_cast<Pimpl*> (context)->dispatch();
CHOC_AUTORELEASE_END
}
}
void dispatch()
{
dispatch_after_f (dispatch_time (DISPATCH_TIME_NOW, interval * 1000000),
dispatch_get_main_queue(), this, staticCallback);
}
Callback callback;
const int64_t interval;
struct TimerList
{
std::recursive_mutex lock;
std::unordered_set<Pimpl*> timers;
bool invokeIfStillAlive (Pimpl* p)
{
std::scoped_lock l (lock);
// must check before AND after the call because the Pimpl
// may be deleted during the callback
return timers.find (p) != timers.end()
&& p->callback()
&& timers.find (p) != timers.end();
}
void add (Pimpl* p)
{
std::scoped_lock l (lock);
timers.insert (p);
}
void remove (Pimpl* p)
{
std::scoped_lock l (lock);
timers.erase (p);
}
};
static TimerList& getList()
{
static TimerList list;
return list;
}
};
//==============================================================================
#elif CHOC_WINDOWS
#undef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#undef NOMINMAX
#define NOMINMAX
#define Rectangle Rectangle_renamed_to_avoid_name_collisions
#include <windows.h>
#undef Rectangle
namespace choc::messageloop
{
struct MessageWindow
{
MessageWindow()
{
className = "choc_" + std::to_string (rand());
WNDCLASSEXA wc = {};
wc.cbSize = sizeof (wc);
wc.hInstance = GetModuleHandleA (nullptr);
wc.lpszClassName = className.c_str();
wc.lpfnWndProc = windowProc;
RegisterClassExA (std::addressof (wc));
hwnd = CreateWindowA (className.c_str(), "choc", 0, 0, 0, 0, 0,
nullptr, nullptr, wc.hInstance, nullptr);
}
~MessageWindow()
{
DestroyWindow (hwnd);
UnregisterClassA (className.c_str(), nullptr);
}
static LRESULT CALLBACK windowProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_APP && wParam == magicWParam)
{
std::unique_ptr<std::function<void()>> f (reinterpret_cast<std::function<void()>*> (lParam));
(*f)();
}
return DefWindowProc (h, message, wParam, lParam);
}
static inline constexpr WPARAM magicWParam = 0xc40cc40c;
HWND hwnd;
std::string className;
DWORD threadID = GetCurrentThreadId();
};
struct LockedMessageWindow
{
MessageWindow& window;
std::unique_lock<std::mutex> lock;
};
inline LockedMessageWindow getSharedMessageWindow (bool recreateIfWrongThread = false)
{
static std::unique_ptr<MessageWindow> window;
static std::mutex lock;
std::unique_lock<std::mutex> l (lock);
if (window == nullptr || (recreateIfWrongThread && window->threadID != GetCurrentThreadId()))
window = std::make_unique<MessageWindow>();
return LockedMessageWindow { *window, std::move (l) };
}
inline void initialise()
{
getSharedMessageWindow (true);
}
inline void run()
{
initialise();
for (;;)
{
MSG msg;
if (GetMessage (std::addressof (msg), nullptr, 0, 0) == -1)
break;
if (msg.message == WM_QUIT)
break;
if (msg.hwnd)
{
TranslateMessage (std::addressof (msg));
DispatchMessage (std::addressof (msg));
}
}
}
inline void stop()
{
postMessage ([] { PostQuitMessage (0); });
}
inline void postMessage (std::function<void()>&& fn)
{
PostMessageA (getSharedMessageWindow().window.hwnd, WM_APP, MessageWindow::magicWParam,
(LPARAM) new std::function<void()> (std::move (fn)));
}
inline bool callerIsOnMessageThread()
{
return getSharedMessageWindow().window.threadID == GetCurrentThreadId();
}
struct Timer::Pimpl
{
Pimpl (Callback&& c, uint32_t interval)
{
sharedState = std::make_shared<SharedState>();
sharedState->callback = std::move (c);
sharedState->timerID = SetTimer (getSharedMessageWindow().window.hwnd, reinterpret_cast<UINT_PTR> (this),
interval, (TIMERPROC) staticCallback);
}
static void staticCallback (HWND, UINT, UINT_PTR p, DWORD) noexcept
{
auto state = reinterpret_cast<Pimpl*>(p)->sharedState; // keep a local shared_ptr
return state->handleCallback();
}
struct SharedState : public std::enable_shared_from_this<SharedState>
{
~SharedState()
{
killTimer();
}
void killTimer()
{
if (timerID != 0)
{
KillTimer (getSharedMessageWindow().window.hwnd, timerID);
timerID = 0;
}
}
void handleCallback()
{
if (! callback())
killTimer();
}
Callback callback;
UINT_PTR timerID = 0;
};
std::shared_ptr<SharedState> sharedState;
};
#else
#error "choc::messageloop only supports OSX, Windows or Linux!"
#endif
inline Timer::Timer (uint32_t interval, Callback&& cb)
{
CHOC_ASSERT (cb != nullptr); // The callback must be a valid function!
pimpl = std::make_unique<Pimpl> (std::move (cb), interval);
}
template <typename Callback>
void setTimeout (uint32_t intervalMillisecs, Callback&& callback)
{
auto t = new Timer();
*t = Timer (intervalMillisecs, [t, c = std::move (callback)]
{
c();
postMessage ([t] { delete t; });
return false;
});
}
inline void Timer::clear() { pimpl.reset(); }
} // namespace choc::messageloop
#endif // CHOC_MESSAGELOOP_HEADER_INCLUDED