1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "cast/standalone_receiver/sdl_glue.h"
6 
7 #include "platform/api/task_runner.h"
8 #include "platform/api/time.h"
9 #include "util/osp_logging.h"
10 
11 namespace openscreen {
12 namespace cast {
13 
SDLEventLoopProcessor(TaskRunner * task_runner,std::function<void ()> quit_callback)14 SDLEventLoopProcessor::SDLEventLoopProcessor(
15     TaskRunner* task_runner,
16     std::function<void()> quit_callback)
17     : alarm_(&Clock::now, task_runner),
18       quit_callback_(std::move(quit_callback)) {
19   alarm_.Schedule([this] { ProcessPendingEvents(); }, Alarm::kImmediately);
20 }
21 
22 SDLEventLoopProcessor::~SDLEventLoopProcessor() = default;
23 
ProcessPendingEvents()24 void SDLEventLoopProcessor::ProcessPendingEvents() {
25   // Process all pending events.
26   SDL_Event event;
27   while (SDL_PollEvent(&event)) {
28     if (event.type == SDL_QUIT) {
29       OSP_VLOG << "SDL_QUIT received, invoking quit callback...";
30       if (quit_callback_) {
31         quit_callback_();
32       }
33     }
34   }
35 
36   // Schedule a task to come back and process more pending events.
37   constexpr auto kEventPollPeriod = std::chrono::milliseconds(10);
38   alarm_.ScheduleFromNow([this] { ProcessPendingEvents(); }, kEventPollPeriod);
39 }
40 
41 }  // namespace cast
42 }  // namespace openscreen
43