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 "webrtc/system_wrappers/source/event_win.h"
12
13#include "Mmsystem.h"
14
15namespace webrtc {
16
17EventWindows::EventWindows()
18    : event_(::CreateEvent(NULL,    // security attributes
19                           FALSE,   // manual reset
20                           FALSE,   // initial state
21                           NULL)),  // name of event
22    timerID_(NULL) {
23}
24
25EventWindows::~EventWindows() {
26  StopTimer();
27  CloseHandle(event_);
28}
29
30bool EventWindows::Set() {
31  // Note: setting an event that is already set has no effect.
32  return SetEvent(event_) == 1;
33}
34
35bool EventWindows::Reset() {
36  return ResetEvent(event_) == 1;
37}
38
39EventTypeWrapper EventWindows::Wait(unsigned long max_time) {
40  unsigned long res = WaitForSingleObject(event_, max_time);
41  switch (res) {
42    case WAIT_OBJECT_0:
43      return kEventSignaled;
44    case WAIT_TIMEOUT:
45      return kEventTimeout;
46    default:
47      return kEventError;
48  }
49}
50
51bool EventWindows::StartTimer(bool periodic, unsigned long time) {
52  if (timerID_ != NULL) {
53    timeKillEvent(timerID_);
54    timerID_ = NULL;
55  }
56
57  if (periodic) {
58    timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
59                            TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
60  } else {
61    timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
62                            TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
63  }
64
65  return timerID_ != NULL;
66}
67
68bool EventWindows::StopTimer() {
69  if (timerID_ != NULL) {
70    timeKillEvent(timerID_);
71    timerID_ = NULL;
72  }
73
74  return true;
75}
76
77}  // namespace webrtc
78