summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerInterface/OSX/RunLoopStopper.h
blob: 298dcd4a80d3e4cbb927f0f18e39ea62c9127ec2 (plain)
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
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <Foundation/Foundation.h>

namespace ciface
{
namespace OSX
{
class RunLoopStopper
{
  CFRunLoopSourceRef m_source;
  CFRunLoopRef m_runloop = nullptr;

public:
  RunLoopStopper()
  {
    CFRunLoopSourceContext ctx = {.version = 0,
                                  .perform = [](void*) { CFRunLoopStop(CFRunLoopGetCurrent()); }};
    m_source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &ctx);
  }
  ~RunLoopStopper() { CFRelease(m_source); }
  void Signal()
  {
    CFRunLoopSourceSignal(m_source);
    if (m_runloop != nullptr)
      CFRunLoopWakeUp(m_runloop);
  }
  void AddToRunLoop(CFRunLoopRef runloop, CFStringRef mode)
  {
    m_runloop = runloop;
    CFRunLoopAddSource(runloop, m_source, mode);
  }
  void RemoveFromRunLoop(CFRunLoopRef runloop, CFStringRef mode)
  {
    m_runloop = nullptr;
    CFRunLoopRemoveSource(runloop, m_source, mode);
  }
};

}  // namespace ciface
}  // namespace OSX