• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2007-2008 Torch Mobile, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "config.h"
28 #include "SharedTimer.h"
29 
30 #include "Page.h"
31 #include "SystemTime.h"
32 #include "Widget.h"
33 #include <wtf/Assertions.h>
34 #include <wtf/CurrentTime.h>
35 #include <windows.h>
36 
37 namespace JSC {
38 extern void* g_stackBase;
39 }
40 
41 namespace WebCore {
42 
43 enum {
44     TimerIdNone = 0,
45     TimerIdAuto,
46     TimerIdManual,
47 };
48 static UINT timerID = TimerIdNone;
49 
50 static void (*sharedTimerFiredFunction)();
51 
52 static HWND timerWindowHandle = 0;
53 const LPCWSTR kTimerWindowClassName = L"TimerWindowClass";
54 
TimerWindowWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)55 LRESULT CALLBACK TimerWindowWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
56 {
57     int dummy;
58     JSC::g_stackBase = &dummy;
59 
60     if (message == WM_TIMER) {
61         if (timerID != TimerIdNone)
62             sharedTimerFiredFunction();
63     } else if (message == WM_USER)    {
64         if (timerID = TimerIdManual) {
65             sharedTimerFiredFunction();
66             PostMessage(hWnd, WM_USER, 0, 0);
67         }
68     } else {
69         JSC::g_stackBase = 0;
70         return DefWindowProc(hWnd, message, wParam, lParam);
71     }
72     JSC::g_stackBase = 0;
73     return 0;
74 }
75 
initializeOffScreenTimerWindow()76 static void initializeOffScreenTimerWindow()
77 {
78     if (timerWindowHandle)
79         return;
80 
81     WNDCLASS wcex = {0};
82     wcex.lpfnWndProc    = TimerWindowWndProc;
83     wcex.hInstance      = Page::instanceHandle();
84     wcex.lpszClassName  = kTimerWindowClassName;
85     RegisterClass(&wcex);
86 
87     timerWindowHandle = CreateWindow(kTimerWindowClassName, 0, 0,
88        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, Page::instanceHandle(), 0);
89 }
90 
setSharedTimerFiredFunction(void (* f)())91 void setSharedTimerFiredFunction(void (*f)())
92 {
93     sharedTimerFiredFunction = f;
94 }
95 
96 #define USER_TIMER_MAXIMUM  0x7FFFFFFF
97 #define USER_TIMER_MINIMUM  0x0000000A
98 
setSharedTimerFireTime(double fireTime)99 void setSharedTimerFireTime(double fireTime)
100 {
101     ASSERT(sharedTimerFiredFunction);
102 
103     double interval = (fireTime - currentTime()) * 1000.;
104     unsigned intervalInMS = interval < USER_TIMER_MINIMUM
105         ? USER_TIMER_MINIMUM
106         : interval > USER_TIMER_MAXIMUM
107         ? USER_TIMER_MAXIMUM
108         : static_cast<unsigned>(interval);
109 
110     if (timerID == TimerIdAuto) {
111         KillTimer(timerWindowHandle, TimerIdAuto);
112         timerID = TimerIdNone;
113     }
114 
115     initializeOffScreenTimerWindow();
116     if (SetTimer(timerWindowHandle, TimerIdAuto, intervalInMS, 0))
117         timerID = TimerIdAuto;
118     else if (timerID != TimerIdManual)
119         PostMessage(timerWindowHandle, WM_USER, 0, 0);
120 }
121 
stopSharedTimer()122 void stopSharedTimer()
123 {
124     if (timerID == TimerIdAuto)
125         KillTimer(timerWindowHandle, TimerIdAuto);
126 
127     timerID = TimerIdNone;
128 }
129 
130 }
131