summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/AlarmClock.h
blob: 392ba0265b1969e514302af178b57d659dbccf8e (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
/*
 *  Copyright (C) 2005-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 "Stopwatch.h"
#include "threads/CriticalSection.h"
#include "threads/Thread.h"

#include <map>
#include <string>

struct SAlarmClockEvent
{
  CStopWatch watch;
  double m_fSecs;
  std::string m_strCommand;
  bool m_loop;
};

class CAlarmClock : public CThread
{
public:
  CAlarmClock();
  ~CAlarmClock() override;
  void Start(const std::string& strName, float n_secs, const std::string& strCommand, bool bSilent = false, bool bLoop = false);
  inline bool IsRunning() const
  {
    return m_bIsRunning;
  }

  inline bool HasAlarm(const std::string& strName)
  {
    // note: strName should be lower case only here
    //       No point checking it at the moment due to it only being called
    //       from GUIInfoManager (which is always lowercase)
    //    CLog::Log(LOGDEBUG,"checking for {}",strName);
    return (m_event.find(strName) != m_event.end());
  }

  double GetRemaining(const std::string& strName)
  {
    std::map<std::string,SAlarmClockEvent>::iterator iter;
    if ((iter=m_event.find(strName)) != m_event.end())
    {
      return iter->second.m_fSecs - static_cast<double>(iter->second.watch.IsRunning()
                                                            ? iter->second.watch.GetElapsedSeconds()
                                                            : 0.f);
    }

    return 0.0;
  }

  void Stop(const std::string& strName, bool bSilent = false);
  void Process() override;
private:
  std::map<std::string,SAlarmClockEvent> m_event;
  CCriticalSection m_events;

  bool m_bIsRunning = false;
};

extern CAlarmClock g_alarmClock;