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
;
49 static void default_task_destr(task_t
*task
)
54 HRESULT
push_task(task_t
*task
, task_proc_t proc
, task_proc_t destr
, LONG magic
)
56 thread_data_t
*thread_data
;
58 thread_data
= get_thread_data(TRUE
);
67 task
->target_magic
= magic
;
69 task
->destr
= destr
? destr
: default_task_destr
;
71 list_add_tail(&thread_data
->task_list
, &task
->entry
);
73 PostMessageW(thread_data
->thread_hwnd
, WM_PROCESSTASK
, 0, 0);
77 static task_t
*pop_task(void)
79 thread_data_t
*thread_data
;
82 thread_data
= get_thread_data(FALSE
);
86 if(list_empty(&thread_data
->task_list
))
89 task
= LIST_ENTRY(thread_data
->task_list
.next
, task_t
, entry
);
90 list_remove(&task
->entry
);
94 static void release_task_timer(HWND thread_hwnd
, task_timer_t
*timer
)
96 list_remove(&timer
->entry
);
98 IDispatch_Release(timer
->disp
);
103 void remove_target_tasks(LONG target
)
105 thread_data_t
*thread_data
= get_thread_data(FALSE
);
106 struct list
*liter
, *ltmp
;
113 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->timer_list
) {
114 timer
= LIST_ENTRY(liter
, task_timer_t
, entry
);
115 if(timer
->window
->task_magic
== target
)
116 release_task_timer(thread_data
->thread_hwnd
, timer
);
119 if(!list_empty(&thread_data
->timer_list
)) {
120 DWORD tc
= GetTickCount();
122 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
123 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, max( (int)(timer
->time
- tc
), 0 ), NULL
);
126 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->task_list
) {
127 task
= LIST_ENTRY(liter
, task_t
, entry
);
128 if(task
->target_magic
== target
) {
129 list_remove(&task
->entry
);
135 LONG
get_task_target_magic(void)
137 static LONG magic
= 0x10000000;
138 return InterlockedIncrement(&magic
);
141 static BOOL
queue_timer(thread_data_t
*thread_data
, task_timer_t
*timer
)
145 list_remove(&timer
->entry
);
147 if(list_empty(&thread_data
->timer_list
)
148 || LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
)->time
> timer
->time
) {
150 list_add_head(&thread_data
->timer_list
, &timer
->entry
);
154 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
155 if(iter
->time
> timer
->time
) {
156 list_add_tail(&iter
->entry
, &timer
->entry
);
161 list_add_tail(&thread_data
->timer_list
, &timer
->entry
);
165 HRESULT
set_task_timer(HTMLInnerWindow
*window
, LONG msec
, BOOL interval
, IDispatch
*disp
, LONG
*id
)
167 thread_data_t
*thread_data
;
169 DWORD tc
= GetTickCount();
171 static DWORD id_cnt
= 0x20000000;
173 thread_data
= get_thread_data(TRUE
);
175 return E_OUTOFMEMORY
;
177 timer
= heap_alloc(sizeof(task_timer_t
));
179 return E_OUTOFMEMORY
;
184 timer
->id
= id_cnt
++;
185 timer
->window
= window
;
186 timer
->time
= tc
+ msec
;
187 timer
->interval
= interval
? msec
: 0;
188 list_init(&timer
->entry
);
190 IDispatch_AddRef(disp
);
193 if(queue_timer(thread_data
, timer
))
194 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, msec
, NULL
);
200 HRESULT
clear_task_timer(HTMLInnerWindow
*window
, DWORD id
)
202 thread_data_t
*thread_data
= get_thread_data(FALSE
);
208 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
209 if(iter
->id
== id
&& iter
->window
== window
) {
210 release_task_timer(thread_data
->thread_hwnd
, iter
);
215 WARN("timet not found\n");
219 static void call_timer_disp(IDispatch
*disp
)
221 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
226 V_VT(&res
) = VT_EMPTY
;
227 memset(&ei
, 0, sizeof(ei
));
230 hres
= IDispatch_Invoke(disp
, DISPID_VALUE
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, &res
, &ei
, NULL
);
234 WARN("<<< %08x\n", hres
);
239 static LRESULT
process_timer(void)
241 thread_data_t
*thread_data
;
244 task_timer_t
*timer
=NULL
, *last_timer
;
248 thread_data
= get_thread_data(FALSE
);
249 assert(thread_data
!= NULL
);
251 if(list_empty(&thread_data
->timer_list
)) {
252 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
256 last_timer
= LIST_ENTRY(list_tail(&thread_data
->timer_list
), task_timer_t
, entry
);
259 if(timer
== last_timer
) {
260 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
261 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
>tc
? timer
->time
-tc
: 0, NULL
);
265 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
267 if(timer
->time
> tc
) {
268 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
-tc
, NULL
);
273 IDispatch_AddRef(disp
);
275 if(timer
->interval
) {
276 timer
->time
+= timer
->interval
;
277 queue_timer(thread_data
, timer
);
279 release_task_timer(thread_data
->thread_hwnd
, timer
);
282 call_timer_disp(disp
);
284 IDispatch_Release(disp
);
285 }while(!list_empty(&thread_data
->timer_list
));
287 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
291 static LRESULT WINAPI
hidden_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
296 task_t
*task
= pop_task();
306 return process_timer();
310 FIXME("(%p %d %lx %lx)\n", hwnd
, msg
, wParam
, lParam
);
312 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
315 static HWND
create_thread_hwnd(void)
317 static ATOM hidden_wnd_class
= 0;
319 if(!hidden_wnd_class
) {
320 WNDCLASSEXW wndclass
= {
321 sizeof(WNDCLASSEXW
), 0,
323 0, 0, hInst
, NULL
, NULL
, NULL
, NULL
,
324 L
"Internet Explorer_Hidden",
328 hidden_wnd_class
= RegisterClassExW(&wndclass
);
331 return CreateWindowExW(0, L
"Internet Explorer_Hidden", NULL
, WS_POPUP
,
332 0, 0, 0, 0, NULL
, NULL
, hInst
, NULL
);
335 HWND
get_thread_hwnd(void)
337 thread_data_t
*thread_data
;
339 thread_data
= get_thread_data(TRUE
);
343 if(!thread_data
->thread_hwnd
)
344 thread_data
->thread_hwnd
= create_thread_hwnd();
346 return thread_data
->thread_hwnd
;
349 thread_data_t
*get_thread_data(BOOL create
)
351 thread_data_t
*thread_data
;
353 if(mshtml_tls
== TLS_OUT_OF_INDEXES
) {
360 if(tls
== TLS_OUT_OF_INDEXES
)
363 tls
= InterlockedCompareExchange((LONG
*)&mshtml_tls
, tls
, TLS_OUT_OF_INDEXES
);
364 if(tls
!= mshtml_tls
)
368 thread_data
= TlsGetValue(mshtml_tls
);
369 if(!thread_data
&& create
) {
370 thread_data
= heap_alloc_zero(sizeof(thread_data_t
));
374 TlsSetValue(mshtml_tls
, thread_data
);
375 list_init(&thread_data
->task_list
);
376 list_init(&thread_data
->timer_list
);