2 * Copyright 2006 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
36 #define WM_PROCESSTASK 0x8008
37 #define TIMER_ID 0x3000
40 HTMLInnerWindow
*window
;
50 static void default_task_destr(task_t
*task
)
55 HRESULT
push_task(task_t
*task
, task_proc_t proc
, task_proc_t destr
, LONG magic
)
57 thread_data_t
*thread_data
;
59 thread_data
= get_thread_data(TRUE
);
68 task
->target_magic
= magic
;
70 task
->destr
= destr
? destr
: default_task_destr
;
72 list_add_tail(&thread_data
->task_list
, &task
->entry
);
74 PostMessageW(thread_data
->thread_hwnd
, WM_PROCESSTASK
, 0, 0);
78 static task_t
*pop_task(void)
80 thread_data_t
*thread_data
;
83 thread_data
= get_thread_data(FALSE
);
87 if(list_empty(&thread_data
->task_list
))
90 task
= LIST_ENTRY(thread_data
->task_list
.next
, task_t
, entry
);
91 list_remove(&task
->entry
);
95 static void release_task_timer(HWND thread_hwnd
, task_timer_t
*timer
)
97 list_remove(&timer
->entry
);
99 IDispatch_Release(timer
->disp
);
104 void remove_target_tasks(LONG target
)
106 thread_data_t
*thread_data
= get_thread_data(FALSE
);
107 struct list
*liter
, *ltmp
;
114 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->timer_list
) {
115 timer
= LIST_ENTRY(liter
, task_timer_t
, entry
);
116 if(timer
->window
->task_magic
== target
)
117 release_task_timer(thread_data
->thread_hwnd
, timer
);
120 if(!list_empty(&thread_data
->timer_list
)) {
121 DWORD tc
= GetTickCount();
123 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
124 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, max( (int)(timer
->time
- tc
), 0 ), NULL
);
127 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->task_list
) {
128 task
= LIST_ENTRY(liter
, task_t
, entry
);
129 if(task
->target_magic
== target
) {
130 list_remove(&task
->entry
);
136 LONG
get_task_target_magic(void)
138 static LONG magic
= 0x10000000;
139 return InterlockedIncrement(&magic
);
142 static BOOL
queue_timer(thread_data_t
*thread_data
, task_timer_t
*timer
)
146 list_remove(&timer
->entry
);
148 if(list_empty(&thread_data
->timer_list
)
149 || LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
)->time
> timer
->time
) {
151 list_add_head(&thread_data
->timer_list
, &timer
->entry
);
155 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
156 if(iter
->time
> timer
->time
) {
157 list_add_tail(&iter
->entry
, &timer
->entry
);
162 list_add_tail(&thread_data
->timer_list
, &timer
->entry
);
166 HRESULT
set_task_timer(HTMLInnerWindow
*window
, LONG msec
, enum timer_type timer_type
, IDispatch
*disp
, LONG
*id
)
168 thread_data_t
*thread_data
;
170 DWORD tc
= GetTickCount();
172 static DWORD id_cnt
= 0x20000000;
174 thread_data
= get_thread_data(TRUE
);
176 return E_OUTOFMEMORY
;
178 timer
= heap_alloc(sizeof(task_timer_t
));
180 return E_OUTOFMEMORY
;
185 timer
->id
= id_cnt
++;
186 timer
->window
= window
;
187 timer
->time
= tc
+ msec
;
188 timer
->interval
= timer_type
== TIMER_INTERVAL
? msec
: 0;
189 timer
->type
= timer_type
;
190 list_init(&timer
->entry
);
192 IDispatch_AddRef(disp
);
195 if(queue_timer(thread_data
, timer
))
196 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, msec
, NULL
);
202 HRESULT
clear_task_timer(HTMLInnerWindow
*window
, DWORD id
)
204 thread_data_t
*thread_data
= get_thread_data(FALSE
);
210 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
211 if(iter
->id
== id
&& iter
->window
== window
) {
212 release_task_timer(thread_data
->thread_hwnd
, iter
);
217 WARN("timet not found\n");
221 static const char *debugstr_timer_type(enum timer_type type
)
224 case TIMER_TIMEOUT
: return "timeout";
225 case TIMER_INTERVAL
: return "interval";
226 case TIMER_ANIMATION_FRAME
: return "animation-frame";
231 static void call_timer_disp(IDispatch
*disp
, enum timer_type timer_type
)
233 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
239 V_VT(&res
) = VT_EMPTY
;
240 memset(&ei
, 0, sizeof(ei
));
242 if(timer_type
== TIMER_ANIMATION_FRAME
) {
244 dp
.rgvarg
= ×tamp
;
245 V_VT(×tamp
) = VT_R8
;
246 V_R8(×tamp
) = get_time_stamp();
249 TRACE("%p %s >>>\n", disp
, debugstr_timer_type(timer_type
));
250 hres
= IDispatch_Invoke(disp
, DISPID_VALUE
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, &res
, &ei
, NULL
);
252 TRACE("%p %s <<<\n", disp
, debugstr_timer_type(timer_type
));
254 WARN("%p %s <<< %08x\n", disp
, debugstr_timer_type(timer_type
), hres
);
259 static LRESULT
process_timer(void)
261 thread_data_t
*thread_data
;
262 enum timer_type timer_type
;
265 task_timer_t
*timer
=NULL
, *last_timer
;
269 thread_data
= get_thread_data(FALSE
);
270 assert(thread_data
!= NULL
);
272 if(list_empty(&thread_data
->timer_list
)) {
273 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
277 last_timer
= LIST_ENTRY(list_tail(&thread_data
->timer_list
), task_timer_t
, entry
);
280 if(timer
== last_timer
) {
281 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
282 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
>tc
? timer
->time
-tc
: 0, NULL
);
286 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
288 if(timer
->time
> tc
) {
289 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
-tc
, NULL
);
294 IDispatch_AddRef(disp
);
295 timer_type
= timer
->type
;
297 if(timer
->interval
) {
298 timer
->time
+= timer
->interval
;
299 queue_timer(thread_data
, timer
);
301 release_task_timer(thread_data
->thread_hwnd
, timer
);
304 call_timer_disp(disp
, timer_type
);
306 IDispatch_Release(disp
);
307 }while(!list_empty(&thread_data
->timer_list
));
309 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
313 static LRESULT WINAPI
hidden_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
318 task_t
*task
= pop_task();
328 return process_timer();
332 FIXME("(%p %d %lx %lx)\n", hwnd
, msg
, wParam
, lParam
);
334 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
337 static HWND
create_thread_hwnd(void)
339 static ATOM hidden_wnd_class
= 0;
341 if(!hidden_wnd_class
) {
342 WNDCLASSEXW wndclass
= {
343 sizeof(WNDCLASSEXW
), 0,
345 0, 0, hInst
, NULL
, NULL
, NULL
, NULL
,
346 L
"Internet Explorer_Hidden",
350 hidden_wnd_class
= RegisterClassExW(&wndclass
);
353 return CreateWindowExW(0, L
"Internet Explorer_Hidden", NULL
, WS_POPUP
,
354 0, 0, 0, 0, NULL
, NULL
, hInst
, NULL
);
357 HWND
get_thread_hwnd(void)
359 thread_data_t
*thread_data
;
361 thread_data
= get_thread_data(TRUE
);
365 if(!thread_data
->thread_hwnd
)
366 thread_data
->thread_hwnd
= create_thread_hwnd();
368 return thread_data
->thread_hwnd
;
371 thread_data_t
*get_thread_data(BOOL create
)
373 thread_data_t
*thread_data
;
375 if(mshtml_tls
== TLS_OUT_OF_INDEXES
) {
382 if(tls
== TLS_OUT_OF_INDEXES
)
385 tls
= InterlockedCompareExchange((LONG
*)&mshtml_tls
, tls
, TLS_OUT_OF_INDEXES
);
386 if(tls
!= mshtml_tls
)
390 thread_data
= TlsGetValue(mshtml_tls
);
391 if(!thread_data
&& create
) {
392 thread_data
= heap_alloc_zero(sizeof(thread_data_t
));
396 TlsSetValue(mshtml_tls
, thread_data
);
397 list_init(&thread_data
->task_list
);
398 list_init(&thread_data
->timer_list
);
404 ULONGLONG
get_time_stamp(void)
408 /* 1601 to 1970 is 369 years plus 89 leap days */
409 const ULONGLONG time_epoch
= (ULONGLONG
)(369 * 365 + 89) * 86400 * 1000;
411 GetSystemTimeAsFileTime(&time
);
412 return (((ULONGLONG
)time
.dwHighDateTime
<< 32) + time
.dwLowDateTime
) / 10000 - time_epoch
;