| 1 |
/* |
| 2 |
* Copyright (C) 2010-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 |
*/ |
| 8 |
|
| 9 |
#include "WinEventsAndroid.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "application/AppInboundProtocol.h" |
| 13 |
#include "guilib/GUIComponent.h" |
| 14 |
#include "guilib/GUIWindowManager.h" |
| 15 |
#include "input/InputManager.h" |
| 16 |
#include "input/keyboard/XBMC_vkeys.h" |
| 17 |
#include "utils/log.h" |
| 18 |
|
| 19 |
#include <mutex> |
| 20 |
|
| 21 |
#define ALMOST_ZERO 0.125f |
| 22 |
enum { |
| 23 |
EVENT_STATE_TEST, |
| 24 |
EVENT_STATE_HOLD, |
| 25 |
EVENT_STATE_REPEAT |
| 26 |
}; |
| 27 |
|
| 28 |
/************************************************************************/ |
| 29 |
/************************************************************************/ |
| 30 |
static bool different_event(XBMC_Event &curEvent, XBMC_Event &newEvent) |
| 31 |
{ |
| 32 |
// different type |
| 33 |
if (curEvent.type != newEvent.type) |
| 34 |
return true; |
| 35 |
|
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
/************************************************************************/ |
| 40 |
/************************************************************************/ |
| 41 |
CWinEventsAndroid::CWinEventsAndroid() |
| 42 |
: CThread("CWinEventsAndroid") |
| 43 |
{ |
| 44 |
CLog::Log(LOGDEBUG, "CWinEventsAndroid::CWinEventsAndroid"); |
| 45 |
Create(); |
| 46 |
} |
| 47 |
|
| 48 |
CWinEventsAndroid::~CWinEventsAndroid() |
| 49 |
{ |
| 50 |
m_bStop = true; |
| 51 |
StopThread(true); |
| 52 |
} |
| 53 |
|
| 54 |
void CWinEventsAndroid::MessagePush(XBMC_Event *newEvent) |
| 55 |
{ |
| 56 |
std::unique_lock<CCriticalSection> lock(m_eventsCond); |
| 57 |
|
| 58 |
m_events.push_back(*newEvent); |
| 59 |
} |
| 60 |
|
| 61 |
void CWinEventsAndroid::MessagePushRepeat(XBMC_Event *repeatEvent) |
| 62 |
{ |
| 63 |
std::unique_lock<CCriticalSection> lock(m_eventsCond); |
| 64 |
|
| 65 |
std::list<XBMC_Event>::iterator itt; |
| 66 |
for (itt = m_events.begin(); itt != m_events.end(); ++itt) |
| 67 |
{ |
| 68 |
// we have events pending, if we we just |
| 69 |
// repush, we might push the repeat event |
| 70 |
// in back of a canceling non-active event. |
| 71 |
// do not repush if pending are different event. |
| 72 |
if (different_event(*itt, *repeatEvent)) |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// is a repeat, push it |
| 77 |
m_events.push_back(*repeatEvent); |
| 78 |
} |
| 79 |
|
| 80 |
bool CWinEventsAndroid::MessagePump() |
| 81 |
{ |
| 82 |
bool ret = false; |
| 83 |
|
| 84 |
// Do not always loop, only pump the initial queued count events. else if ui keep pushing |
| 85 |
// events the loop won't finish then it will block xbmc main message loop. |
| 86 |
for (size_t pumpEventCount = GetQueueSize(); pumpEventCount > 0; --pumpEventCount) |
| 87 |
{ |
| 88 |
// Pop up only one event per time since in App::OnEvent it may init modal dialog which init |
| 89 |
// deeper message loop and call the deeper MessagePump from there. |
| 90 |
XBMC_Event pumpEvent; |
| 91 |
{ |
| 92 |
std::unique_lock<CCriticalSection> lock(m_eventsCond); |
| 93 |
if (m_events.empty()) |
| 94 |
return ret; |
| 95 |
pumpEvent = m_events.front(); |
| 96 |
m_events.pop_front(); |
| 97 |
} |
| 98 |
|
| 99 |
std::shared_ptr<CAppInboundProtocol> appPort = CServiceBroker::GetAppPort(); |
| 100 |
if (appPort) |
| 101 |
ret |= appPort->OnEvent(pumpEvent); |
| 102 |
|
| 103 |
if (pumpEvent.type == XBMC_MOUSEBUTTONUP) |
| 104 |
CServiceBroker::GetGUI()->GetWindowManager().SendMessage(GUI_MSG_UNFOCUS_ALL, 0, 0, 0, 0); |
| 105 |
} |
| 106 |
|
| 107 |
return ret; |
| 108 |
} |
| 109 |
|
| 110 |
size_t CWinEventsAndroid::GetQueueSize() |
| 111 |
{ |
| 112 |
std::unique_lock<CCriticalSection> lock(m_eventsCond); |
| 113 |
return m_events.size(); |
| 114 |
} |
| 115 |
|
| 116 |
void CWinEventsAndroid::Process() |
| 117 |
{ |
| 118 |
uint32_t timeout = 10; |
| 119 |
uint32_t holdTimeout = 500; |
| 120 |
uint32_t repeatTimeout = 100; |
| 121 |
uint32_t repeatDuration = 0; |
| 122 |
|
| 123 |
XBMC_Event cur_event; |
| 124 |
int state = EVENT_STATE_TEST; |
| 125 |
while (!m_bStop) |
| 126 |
{ |
| 127 |
// run a 10ms (timeout) wait cycle |
| 128 |
CThread::Sleep(std::chrono::milliseconds(timeout)); |
| 129 |
|
| 130 |
std::unique_lock<CCriticalSection> lock(m_lasteventCond); |
| 131 |
|
| 132 |
switch(state) |
| 133 |
{ |
| 134 |
default: |
| 135 |
case EVENT_STATE_TEST: |
| 136 |
// non-active event, eat it |
| 137 |
if (!m_lastevent.empty()) |
| 138 |
m_lastevent.pop(); |
| 139 |
break; |
| 140 |
|
| 141 |
case EVENT_STATE_HOLD: |
| 142 |
repeatDuration += timeout; |
| 143 |
if (!m_lastevent.empty()) |
| 144 |
{ |
| 145 |
if (different_event(cur_event, m_lastevent.front())) |
| 146 |
{ |
| 147 |
// different event, cycle back to test |
| 148 |
state = EVENT_STATE_TEST; |
| 149 |
break; |
| 150 |
} |
| 151 |
|
| 152 |
// same event, eat it |
| 153 |
m_lastevent.pop(); |
| 154 |
} |
| 155 |
|
| 156 |
if (repeatDuration >= holdTimeout) |
| 157 |
{ |
| 158 |
CLog::Log(LOGDEBUG, "hold ->repeat, size({}), repeatDuration({})", m_lastevent.size(), |
| 159 |
repeatDuration); |
| 160 |
state = EVENT_STATE_REPEAT; |
| 161 |
} |
| 162 |
break; |
| 163 |
|
| 164 |
case EVENT_STATE_REPEAT: |
| 165 |
repeatDuration += timeout; |
| 166 |
if (!m_lastevent.empty()) |
| 167 |
{ |
| 168 |
if (different_event(cur_event, m_lastevent.front())) |
| 169 |
{ |
| 170 |
// different event, cycle back to test |
| 171 |
state = EVENT_STATE_TEST; |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
// same event, eat it |
| 176 |
m_lastevent.pop(); |
| 177 |
} |
| 178 |
|
| 179 |
if (repeatDuration >= holdTimeout) |
| 180 |
{ |
| 181 |
// this is a repeat, push it |
| 182 |
MessagePushRepeat(&cur_event); |
| 183 |
// assuming holdTimeout > repeatTimeout, |
| 184 |
// just subtract the repeatTimeout |
| 185 |
// to get the next cycle time |
| 186 |
repeatDuration -= repeatTimeout; |
| 187 |
} |
| 188 |
break; |
| 189 |
} |
| 190 |
} |
| 191 |
} |