Merge pull request #26312 from garbear/update-controllers
[xbmc.git] / xbmc / windowing / tvos / WinEventsTVOS.mm
bloba16c4f0d1f33dc9b1e37a92dea9dec9ee53d5f38
1 /*
2  *  Copyright (C) 2012-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
9 #include "WinEventsTVOS.h"
11 #include "ServiceBroker.h"
12 #include "application/AppInboundProtocol.h"
13 #include "guilib/GUIWindowManager.h"
14 #include "input/InputManager.h"
15 #include "input/keyboard/XBMC_vkeys.h"
16 #include "threads/CriticalSection.h"
17 #include "utils/log.h"
19 #include <list>
20 #include <mutex>
22 static CCriticalSection g_inputCond;
24 static std::list<XBMC_Event> events;
26 CWinEventsTVOS::CWinEventsTVOS() : CThread("CWinEventsTVOS")
28   CLog::Log(LOGDEBUG, "CWinEventsTVOS::CWinEventsTVOS");
29   Create();
32 CWinEventsTVOS::~CWinEventsTVOS()
34   m_bStop = true;
35   StopThread(true);
38 void CWinEventsTVOS::MessagePush(XBMC_Event* newEvent)
40   std::unique_lock<CCriticalSection> lock(m_eventsCond);
42   m_events.push_back(*newEvent);
45 size_t CWinEventsTVOS::GetQueueSize()
47   std::unique_lock<CCriticalSection> lock(g_inputCond);
48   return events.size();
52 bool CWinEventsTVOS::MessagePump()
54   bool ret = false;
55   std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort();
57   // Do not always loop, only pump the initial queued count events. else if ui keep pushing
58   // events the loop won't finish then it will block xbmc main message loop.
59   for (size_t pumpEventCount = GetQueueSize(); pumpEventCount > 0; --pumpEventCount)
60   {
61     // Pop up only one event per time since in App::OnEvent it may init modal dialog which init
62     // deeper message loop and call the deeper MessagePump from there.
63     XBMC_Event pumpEvent;
64     {
65       std::unique_lock<CCriticalSection> lock(g_inputCond);
66       if (events.empty())
67         return ret;
68       pumpEvent = events.front();
69       events.pop_front();
70     }
72     if (appPort)
73       ret = appPort->OnEvent(pumpEvent);
74   }
75   return ret;