Home | History | Annotate | Download | only in wince
      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 "WebCoreInstanceHandle.h"
     33 #include "Widget.h"
     34 #include <wtf/Assertions.h>
     35 #include <wtf/CurrentTime.h>
     36 #include <windows.h>
     37 
     38 namespace WebCore {
     39 
     40 enum {
     41     TimerIdNone = 0,
     42     TimerIdAuto,
     43     TimerIdManual,
     44 };
     45 static UINT timerID = TimerIdNone;
     46 
     47 static void (*sharedTimerFiredFunction)();
     48 
     49 static HWND timerWindowHandle = 0;
     50 const LPCWSTR kTimerWindowClassName = L"TimerWindowClass";
     51 
     52 LRESULT CALLBACK TimerWindowWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
     53 {
     54     if (message == WM_TIMER) {
     55         if (timerID != TimerIdNone)
     56             sharedTimerFiredFunction();
     57     } else if (message == WM_USER) {
     58         if (timerID = TimerIdManual) {
     59             sharedTimerFiredFunction();
     60             PostMessage(hWnd, WM_USER, 0, 0);
     61         }
     62     } else
     63         return DefWindowProc(hWnd, message, wParam, lParam);
     64 
     65     return 0;
     66 }
     67 
     68 static void initializeOffScreenTimerWindow()
     69 {
     70     if (timerWindowHandle)
     71         return;
     72 
     73     WNDCLASS wcex = {0};
     74     wcex.lpfnWndProc    = TimerWindowWndProc;
     75     wcex.hInstance      = WebCore::instanceHandle();
     76     wcex.lpszClassName  = kTimerWindowClassName;
     77     RegisterClass(&wcex);
     78 
     79     timerWindowHandle = CreateWindow(kTimerWindowClassName, 0, 0,
     80        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, WebCore::instanceHandle(), 0);
     81 }
     82 
     83 void setSharedTimerFiredFunction(void (*f)())
     84 {
     85     sharedTimerFiredFunction = f;
     86 }
     87 
     88 #define USER_TIMER_MAXIMUM  0x7FFFFFFF
     89 #define USER_TIMER_MINIMUM  0x0000000A
     90 
     91 void setSharedTimerFireTime(double fireTime)
     92 {
     93     ASSERT(sharedTimerFiredFunction);
     94 
     95     double interval = (fireTime - currentTime()) * 1000.;
     96     unsigned intervalInMS = interval < USER_TIMER_MINIMUM
     97         ? USER_TIMER_MINIMUM
     98         : interval > USER_TIMER_MAXIMUM
     99         ? USER_TIMER_MAXIMUM
    100         : static_cast<unsigned>(interval);
    101 
    102     if (timerID == TimerIdAuto) {
    103         KillTimer(timerWindowHandle, TimerIdAuto);
    104         timerID = TimerIdNone;
    105     }
    106 
    107     initializeOffScreenTimerWindow();
    108     if (SetTimer(timerWindowHandle, TimerIdAuto, intervalInMS, 0))
    109         timerID = TimerIdAuto;
    110     else if (timerID != TimerIdManual)
    111         PostMessage(timerWindowHandle, WM_USER, 0, 0);
    112 }
    113 
    114 void stopSharedTimer()
    115 {
    116     if (timerID == TimerIdAuto)
    117         KillTimer(timerWindowHandle, TimerIdAuto);
    118 
    119     timerID = TimerIdNone;
    120 }
    121 
    122 } // namespace WebCore
    123