1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "event_win.h"
12
13#include "Mmsystem.h"
14
15namespace webrtc {
16EventWindows::EventWindows()
17    : _event(::CreateEvent(NULL  /* security attributes */,
18                           FALSE /* manual reset */,
19                           FALSE /* initial state */,
20                           NULL  /* name of event */)),
21      _timerID(NULL)
22{
23}
24
25EventWindows::~EventWindows()
26{
27    CloseHandle(_event);
28}
29
30bool EventWindows::Set()
31{
32    // Note: setting an event that is already set has no effect.
33    return SetEvent(_event) == 1 ? true : false;
34}
35
36bool EventWindows::Reset()
37{
38    return ResetEvent(_event) == 1 ? true : false;
39}
40
41EventTypeWrapper EventWindows::Wait(unsigned long maxTime)
42{
43    unsigned long res = WaitForSingleObject(_event, maxTime);
44    switch(res)
45    {
46    case WAIT_OBJECT_0:
47        return kEventSignaled;
48    case WAIT_TIMEOUT:
49        return kEventTimeout;
50    default:
51        return kEventError;
52    }
53}
54
55bool EventWindows::StartTimer(bool periodic, unsigned long time)
56{
57    if (_timerID != NULL)
58    {
59        timeKillEvent(_timerID);
60        _timerID=NULL;
61    }
62    if (periodic)
63    {
64        _timerID=timeSetEvent(time, 0,(LPTIMECALLBACK)HANDLE(_event),0,
65                              TIME_PERIODIC|TIME_CALLBACK_EVENT_PULSE);
66    } else {
67        _timerID=timeSetEvent(time, 0,(LPTIMECALLBACK)HANDLE(_event),0,
68                              TIME_ONESHOT|TIME_CALLBACK_EVENT_SET);
69    }
70
71    if (_timerID == NULL)
72    {
73        return false;
74    }
75    return true;
76}
77
78bool EventWindows::StopTimer()
79{
80    timeKillEvent(_timerID);
81    _timerID = NULL;
82    return true;
83}
84} // namespace webrtc
85