aboutsummaryrefslogtreecommitdiff
path: root/cast/standalone_receiver/sdl_glue.h
blob: 7e13607441ad6a23b25fe73269b27e791d273be3 (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
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CAST_STANDALONE_RECEIVER_SDL_GLUE_H_
#define CAST_STANDALONE_RECEIVER_SDL_GLUE_H_

#include <stdint.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#include <SDL2/SDL.h>
#pragma GCC diagnostic pop

#include <functional>
#include <memory>
#include <utility>
#include <vector>

#include "util/alarm.h"

namespace openscreen {

class TaskRunner;

namespace cast {

template <uint32_t subsystem>
class ScopedSDLSubSystem {
 public:
  ScopedSDLSubSystem() { SDL_InitSubSystem(subsystem); }
  ~ScopedSDLSubSystem() { SDL_QuitSubSystem(subsystem); }
};

// Macro that, for an SDL_Foo, generates code for:
//
//  using SDLFooUniquePtr = std::unique_ptr<SDL_Foo, SDLFooDestroyer>;
//  SDLFooUniquePtr MakeUniqueSDLFoo(...args...);
#define DEFINE_SDL_UNIQUE_PTR(name)                          \
  struct SDL##name##Destroyer {                              \
    void operator()(SDL_##name* obj) const {                 \
      if (obj) {                                             \
        SDL_Destroy##name(obj);                              \
      }                                                      \
    }                                                        \
  };                                                         \
  using SDL##name##UniquePtr =                               \
      std::unique_ptr<SDL_##name, SDL##name##Destroyer>;     \
  template <typename... Args>                                \
  SDL##name##UniquePtr MakeUniqueSDL##name(Args&&... args) { \
    return SDL##name##UniquePtr(                             \
        SDL_Create##name(std::forward<Args>(args)...));      \
  }

DEFINE_SDL_UNIQUE_PTR(Window);
DEFINE_SDL_UNIQUE_PTR(Renderer);
DEFINE_SDL_UNIQUE_PTR(Texture);

#undef DEFINE_SDL_UNIQUE_PTR

// A looping mechanism that runs the SDL event loop by scheduling periodic tasks
// to the given TaskRunner. Looping continues indefinitely, until the instance
// is destroyed. A client-provided quit callback is invoked whenever a SDL_QUIT
// event is received.
class SDLEventLoopProcessor {
 public:
  SDLEventLoopProcessor(TaskRunner* task_runner,
                        std::function<void()> quit_callback);
  ~SDLEventLoopProcessor();

  using KeyboardEventCallback = std::function<void(const SDL_KeyboardEvent&)>;
  void RegisterForKeyboardEvent(KeyboardEventCallback cb);

 private:
  void ProcessPendingEvents();

  Alarm alarm_;
  std::function<void()> quit_callback_;
  std::vector<KeyboardEventCallback> keyboard_callbacks_;
};

}  // namespace cast
}  // namespace openscreen

#endif  // CAST_STANDALONE_RECEIVER_SDL_GLUE_H_