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
32 #include "wine/debug.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
38 #define WM_PROCESSTASK 0x8008
39 #define TIMER_ID 0x3000
42 HTMLInnerWindow
*window
;
51 static void default_task_destr(task_t
*task
)
56 HRESULT
push_task(task_t
*task
, task_proc_t proc
, task_proc_t destr
, LONG magic
)
58 thread_data_t
*thread_data
;
60 thread_data
= get_thread_data(TRUE
);
69 task
->target_magic
= magic
;
71 task
->destr
= destr
? destr
: default_task_destr
;
73 list_add_tail(&thread_data
->task_list
, &task
->entry
);
75 PostMessageW(thread_data
->thread_hwnd
, WM_PROCESSTASK
, 0, 0);
79 static task_t
*pop_task(void)
81 thread_data_t
*thread_data
;
84 thread_data
= get_thread_data(FALSE
);
88 if(list_empty(&thread_data
->task_list
))
91 task
= LIST_ENTRY(thread_data
->task_list
.next
, task_t
, entry
);
92 list_remove(&task
->entry
);
96 static void release_task_timer(HWND thread_hwnd
, task_timer_t
*timer
)
98 list_remove(&timer
->entry
);
100 IDispatch_Release(timer
->disp
);
105 void flush_pending_tasks(LONG target
)
107 thread_data_t
*thread_data
= get_thread_data(FALSE
);
108 struct list
*liter
, *ltmp
;
114 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->task_list
) {
115 task
= LIST_ENTRY(liter
, task_t
, entry
);
116 if(task
->target_magic
== target
) {
117 list_remove(&task
->entry
);
124 void remove_target_tasks(LONG target
)
126 thread_data_t
*thread_data
= get_thread_data(FALSE
);
127 struct list
*liter
, *ltmp
;
134 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->timer_list
) {
135 timer
= LIST_ENTRY(liter
, task_timer_t
, entry
);
136 if(timer
->window
->task_magic
== target
)
137 release_task_timer(thread_data
->thread_hwnd
, timer
);
140 if(!list_empty(&thread_data
->timer_list
)) {
141 DWORD tc
= GetTickCount();
143 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
144 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, max( (int)(timer
->time
- tc
), 0 ), NULL
);
147 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->task_list
) {
148 task
= LIST_ENTRY(liter
, task_t
, entry
);
149 if(task
->target_magic
== target
) {
150 list_remove(&task
->entry
);
156 LONG
get_task_target_magic(void)
158 static LONG magic
= 0x10000000;
159 return InterlockedIncrement(&magic
);
162 static BOOL
queue_timer(thread_data_t
*thread_data
, task_timer_t
*timer
)
166 list_remove(&timer
->entry
);
168 if(list_empty(&thread_data
->timer_list
)
169 || LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
)->time
> timer
->time
) {
171 list_add_head(&thread_data
->timer_list
, &timer
->entry
);
175 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
176 if(iter
->time
> timer
->time
) {
177 list_add_tail(&iter
->entry
, &timer
->entry
);
182 list_add_tail(&thread_data
->timer_list
, &timer
->entry
);
186 HRESULT
set_task_timer(HTMLInnerWindow
*window
, DWORD msec
, BOOL interval
, IDispatch
*disp
, LONG
*id
)
188 thread_data_t
*thread_data
;
190 DWORD tc
= GetTickCount();
192 static DWORD id_cnt
= 0x20000000;
194 thread_data
= get_thread_data(TRUE
);
196 return E_OUTOFMEMORY
;
198 timer
= heap_alloc(sizeof(task_timer_t
));
200 return E_OUTOFMEMORY
;
202 timer
->id
= id_cnt
++;
203 timer
->window
= window
;
204 timer
->time
= tc
+ msec
;
205 timer
->interval
= interval
? msec
: 0;
206 list_init(&timer
->entry
);
208 IDispatch_AddRef(disp
);
211 if(queue_timer(thread_data
, timer
))
212 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, msec
, NULL
);
218 HRESULT
clear_task_timer(HTMLInnerWindow
*window
, BOOL interval
, DWORD id
)
220 thread_data_t
*thread_data
= get_thread_data(FALSE
);
226 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
227 if(iter
->id
== id
&& iter
->window
== window
&& !iter
->interval
== !interval
) {
228 release_task_timer(thread_data
->thread_hwnd
, iter
);
233 WARN("timet not found\n");
237 static void call_timer_disp(IDispatch
*disp
)
239 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
244 V_VT(&res
) = VT_EMPTY
;
245 memset(&ei
, 0, sizeof(ei
));
248 hres
= IDispatch_Invoke(disp
, DISPID_VALUE
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, &res
, &ei
, NULL
);
252 WARN("<<< %08x\n", hres
);
257 static LRESULT
process_timer(void)
259 thread_data_t
*thread_data
;
262 task_timer_t
*timer
=NULL
, *last_timer
;
266 thread_data
= get_thread_data(FALSE
);
267 assert(thread_data
!= NULL
);
269 if(list_empty(&thread_data
->timer_list
)) {
270 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
274 last_timer
= LIST_ENTRY(list_tail(&thread_data
->timer_list
), task_timer_t
, entry
);
277 if(timer
== last_timer
) {
278 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
279 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
>tc
? timer
->time
-tc
: 0, NULL
);
283 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
285 if(timer
->time
> tc
) {
286 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
-tc
, NULL
);
291 IDispatch_AddRef(disp
);
293 if(timer
->interval
) {
294 timer
->time
+= timer
->interval
;
295 queue_timer(thread_data
, timer
);
297 release_task_timer(thread_data
->thread_hwnd
, timer
);
300 call_timer_disp(disp
);
302 IDispatch_Release(disp
);
303 }while(!list_empty(&thread_data
->timer_list
));
305 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
309 static LRESULT WINAPI
hidden_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
314 task_t
*task
= pop_task();
324 return process_timer();
328 FIXME("(%p %d %lx %lx)\n", hwnd
, msg
, wParam
, lParam
);
330 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
333 static HWND
create_thread_hwnd(void)
335 static ATOM hidden_wnd_class
= 0;
336 static const WCHAR wszInternetExplorer_Hidden
[] = {'I','n','t','e','r','n','e','t',
337 ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
339 if(!hidden_wnd_class
) {
340 WNDCLASSEXW wndclass
= {
341 sizeof(WNDCLASSEXW
), 0,
343 0, 0, hInst
, NULL
, NULL
, NULL
, NULL
,
344 wszInternetExplorer_Hidden
,
348 hidden_wnd_class
= RegisterClassExW(&wndclass
);
351 return CreateWindowExW(0, wszInternetExplorer_Hidden
, NULL
, WS_POPUP
,
352 0, 0, 0, 0, NULL
, NULL
, hInst
, NULL
);
355 HWND
get_thread_hwnd(void)
357 thread_data_t
*thread_data
;
359 thread_data
= get_thread_data(TRUE
);
363 if(!thread_data
->thread_hwnd
)
364 thread_data
->thread_hwnd
= create_thread_hwnd();
366 return thread_data
->thread_hwnd
;
369 thread_data_t
*get_thread_data(BOOL create
)
371 thread_data_t
*thread_data
;
373 if(mshtml_tls
== TLS_OUT_OF_INDEXES
) {
380 if(tls
== TLS_OUT_OF_INDEXES
)
383 tls
= InterlockedCompareExchange((LONG
*)&mshtml_tls
, tls
, TLS_OUT_OF_INDEXES
);
384 if(tls
!= mshtml_tls
)
388 thread_data
= TlsGetValue(mshtml_tls
);
389 if(!thread_data
&& create
) {
390 thread_data
= heap_alloc_zero(sizeof(thread_data_t
));
394 TlsSetValue(mshtml_tls
, thread_data
);
395 list_init(&thread_data
->task_list
);
396 list_init(&thread_data
->timer_list
);