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
19 #include "mshtml_private.h"
21 #define WM_PROCESSTASK 0x8008
22 #define TIMER_ID 0x3000
25 HTMLInnerWindow
*window
;
34 static void default_task_destr(task_t
*task
)
39 HRESULT
push_task(task_t
*task
, task_proc_t proc
, task_proc_t destr
, LONG magic
)
41 thread_data_t
*thread_data
;
43 thread_data
= get_thread_data(TRUE
);
52 task
->target_magic
= magic
;
54 task
->destr
= destr
? destr
: default_task_destr
;
57 if(thread_data
->task_queue_tail
)
58 thread_data
->task_queue_tail
->next
= task
;
60 thread_data
->task_queue_head
= task
;
62 thread_data
->task_queue_tail
= task
;
64 PostMessageW(thread_data
->thread_hwnd
, WM_PROCESSTASK
, 0, 0);
68 static task_t
*pop_task(void)
70 thread_data_t
*thread_data
;
73 thread_data
= get_thread_data(FALSE
);
77 task
= thread_data
->task_queue_head
;
81 thread_data
->task_queue_head
= task
->next
;
82 if(!thread_data
->task_queue_head
)
83 thread_data
->task_queue_tail
= NULL
;
88 static void release_task_timer(HWND thread_hwnd
, task_timer_t
*timer
)
90 list_remove(&timer
->entry
);
92 IDispatch_Release(timer
->disp
);
97 void remove_target_tasks(LONG target
)
99 thread_data_t
*thread_data
= get_thread_data(FALSE
);
100 struct list
*liter
, *ltmp
;
107 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->timer_list
) {
108 timer
= LIST_ENTRY(liter
, task_timer_t
, entry
);
109 if(timer
->window
->task_magic
== target
)
110 release_task_timer(thread_data
->thread_hwnd
, timer
);
113 if(!list_empty(&thread_data
->timer_list
)) {
114 DWORD tc
= GetTickCount();
116 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
117 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, max( (int)(timer
->time
- tc
), 0 ), NULL
);
120 while(thread_data
->task_queue_head
&& thread_data
->task_queue_head
->target_magic
== target
) {
125 for(iter
= thread_data
->task_queue_head
; iter
; iter
= iter
->next
) {
126 while(iter
->next
&& iter
->next
->target_magic
== target
) {
128 iter
->next
= tmp
->next
;
133 thread_data
->task_queue_tail
= iter
;
137 LONG
get_task_target_magic(void)
139 static LONG magic
= 0x10000000;
140 return InterlockedIncrement(&magic
);
143 static BOOL
queue_timer(thread_data_t
*thread_data
, task_timer_t
*timer
)
147 list_remove(&timer
->entry
);
149 if(list_empty(&thread_data
->timer_list
)
150 || LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
)->time
> timer
->time
) {
152 list_add_head(&thread_data
->timer_list
, &timer
->entry
);
156 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
157 if(iter
->time
> timer
->time
) {
158 list_add_tail(&iter
->entry
, &timer
->entry
);
163 list_add_tail(&thread_data
->timer_list
, &timer
->entry
);
167 HRESULT
set_task_timer(HTMLInnerWindow
*window
, DWORD msec
, BOOL interval
, IDispatch
*disp
, LONG
*id
)
169 thread_data_t
*thread_data
;
171 DWORD tc
= GetTickCount();
173 static DWORD id_cnt
= 0x20000000;
175 thread_data
= get_thread_data(TRUE
);
177 return E_OUTOFMEMORY
;
179 timer
= heap_alloc(sizeof(task_timer_t
));
181 return E_OUTOFMEMORY
;
183 timer
->id
= id_cnt
++;
184 timer
->window
= window
;
185 timer
->time
= tc
+ msec
;
186 timer
->interval
= interval
? msec
: 0;
187 list_init(&timer
->entry
);
189 IDispatch_AddRef(disp
);
192 if(queue_timer(thread_data
, timer
))
193 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, msec
, NULL
);
199 HRESULT
clear_task_timer(HTMLInnerWindow
*window
, BOOL interval
, DWORD id
)
201 thread_data_t
*thread_data
= get_thread_data(FALSE
);
207 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
208 if(iter
->id
== id
&& iter
->window
== window
&& !iter
->interval
== !interval
) {
209 release_task_timer(thread_data
->thread_hwnd
, iter
);
214 WARN("timet not found\n");
218 static void call_timer_disp(IDispatch
*disp
)
220 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
225 V_VT(&res
) = VT_EMPTY
;
226 memset(&ei
, 0, sizeof(ei
));
229 hres
= IDispatch_Invoke(disp
, DISPID_VALUE
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, &res
, &ei
, NULL
);
233 WARN("<<< %08x\n", hres
);
238 static LRESULT
process_timer(void)
240 thread_data_t
*thread_data
;
243 task_timer_t
*timer
=NULL
, *last_timer
;
247 thread_data
= get_thread_data(FALSE
);
248 assert(thread_data
!= NULL
);
250 if(list_empty(&thread_data
->timer_list
)) {
251 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
255 last_timer
= LIST_ENTRY(list_tail(&thread_data
->timer_list
), task_timer_t
, entry
);
258 if(timer
== last_timer
) {
259 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
260 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
>tc
? timer
->time
-tc
: 0, NULL
);
264 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
266 if(timer
->time
> tc
) {
267 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
-tc
, NULL
);
272 IDispatch_AddRef(disp
);
274 if(timer
->interval
) {
275 timer
->time
+= timer
->interval
;
276 queue_timer(thread_data
, timer
);
278 release_task_timer(thread_data
->thread_hwnd
, timer
);
281 call_timer_disp(disp
);
283 IDispatch_Release(disp
);
284 }while(!list_empty(&thread_data
->timer_list
));
286 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
290 static LRESULT WINAPI
hidden_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
295 task_t
*task
= pop_task();
305 return process_timer();
309 FIXME("(%p %d %lx %lx)\n", hwnd
, msg
, wParam
, lParam
);
311 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
314 static HWND
create_thread_hwnd(void)
316 static ATOM hidden_wnd_class
= 0;
317 static const WCHAR wszInternetExplorer_Hidden
[] = {'I','n','t','e','r','n','e','t',
318 ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
320 if(!hidden_wnd_class
) {
321 WNDCLASSEXW wndclass
= {
322 sizeof(WNDCLASSEXW
), 0,
324 0, 0, hInst
, NULL
, NULL
, NULL
, NULL
,
325 wszInternetExplorer_Hidden
,
329 hidden_wnd_class
= RegisterClassExW(&wndclass
);
332 return CreateWindowExW(0, wszInternetExplorer_Hidden
, NULL
, WS_POPUP
,
333 0, 0, 0, 0, NULL
, NULL
, hInst
, NULL
);
336 HWND
get_thread_hwnd(void)
338 thread_data_t
*thread_data
;
340 thread_data
= get_thread_data(TRUE
);
344 if(!thread_data
->thread_hwnd
)
345 thread_data
->thread_hwnd
= create_thread_hwnd();
347 return thread_data
->thread_hwnd
;
350 thread_data_t
*get_thread_data(BOOL create
)
352 thread_data_t
*thread_data
;
354 if(mshtml_tls
== TLS_OUT_OF_INDEXES
) {
361 if(tls
== TLS_OUT_OF_INDEXES
)
364 tls
= InterlockedCompareExchange((LONG
*)&mshtml_tls
, tls
, TLS_OUT_OF_INDEXES
);
365 if(tls
!= mshtml_tls
)
369 thread_data
= TlsGetValue(mshtml_tls
);
370 if(!thread_data
&& create
) {
371 thread_data
= heap_alloc_zero(sizeof(thread_data_t
));
375 TlsSetValue(mshtml_tls
, thread_data
);
376 list_init(&thread_data
->timer_list
);