summaryrefslogtreecommitdiffstats
path: root/xbmc/peripherals/EventScanner.h
blob: 009227879b9dc4aa043a309705c75e8c34174f87 (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
/*
 *  Copyright (C) 2016-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "EventLockHandle.h"
#include "EventPollHandle.h"
#include "PeripheralTypes.h"
#include "threads/CriticalSection.h"
#include "threads/Event.h"
#include "threads/Thread.h"

#include <chrono>
#include <set>

namespace PERIPHERALS
{
class IEventScannerCallback;

/*!
 * \brief Class to scan for peripheral events
 *
 * By default, a rate of 60 Hz is used. A client can obtain control over when
 * input is handled by registering for a polling handle.
 */
class CEventScanner : public IEventPollCallback, public IEventLockCallback, protected CThread
{
public:
  explicit CEventScanner(IEventScannerCallback& callback);

  ~CEventScanner() override = default;

  void Start();
  void Stop();

  EventPollHandlePtr RegisterPollHandle();

  /*!
   * \brief Acquire a lock that prevents event processing while held
   */
  EventLockHandlePtr RegisterLock();

  // implementation of IEventPollCallback
  void Activate(CEventPollHandle& handle) override;
  void Deactivate(CEventPollHandle& handle) override;
  void HandleEvents(bool bWait) override;
  void Release(CEventPollHandle& handle) override;

  // implementation of IEventLockCallback
  void ReleaseLock(CEventLockHandle& handle) override;

protected:
  // implementation of CThread
  void Process() override;

private:
  std::chrono::milliseconds GetScanIntervalMs() const;

  // Construction parameters
  IEventScannerCallback& m_callback;

  // Event parameters
  std::set<void*> m_activeHandles;
  std::set<void*> m_activeLocks;
  CEvent m_scanEvent;
  CEvent m_scanFinishedEvent;
  mutable CCriticalSection m_handleMutex;
  CCriticalSection m_lockMutex;
  CCriticalSection m_pollMutex; // Prevent two poll handles from polling at once
};
} // namespace PERIPHERALS