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
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
37 #define WM_PROCESSTASK 0x8008
38 #define TIMER_ID 0x3000
50 void push_task(task_t
*task
, task_proc_t proc
, LONG magic
)
52 thread_data_t
*thread_data
= get_thread_data(TRUE
);
54 task
->target_magic
= magic
;
58 if(thread_data
->task_queue_tail
)
59 thread_data
->task_queue_tail
->next
= task
;
61 thread_data
->task_queue_head
= task
;
63 thread_data
->task_queue_tail
= task
;
65 PostMessageW(thread_data
->thread_hwnd
, WM_PROCESSTASK
, 0, 0);
68 static task_t
*pop_task(void)
70 thread_data_t
*thread_data
= get_thread_data(TRUE
);
71 task_t
*task
= thread_data
->task_queue_head
;
76 thread_data
->task_queue_head
= task
->next
;
77 if(!thread_data
->task_queue_head
)
78 thread_data
->task_queue_tail
= NULL
;
83 static void release_task_timer(HWND thread_hwnd
, task_timer_t
*timer
)
85 list_remove(&timer
->entry
);
87 IDispatch_Release(timer
->disp
);
92 void remove_target_tasks(LONG target
)
94 thread_data_t
*thread_data
= get_thread_data(FALSE
);
95 struct list
*liter
, *ltmp
;
102 LIST_FOR_EACH_SAFE(liter
, ltmp
, &thread_data
->timer_list
) {
103 timer
= LIST_ENTRY(liter
, task_timer_t
, entry
);
104 if(timer
->doc
->task_magic
== target
)
105 release_task_timer(thread_data
->thread_hwnd
, timer
);
108 if(!list_empty(&thread_data
->timer_list
)) {
109 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
110 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
- GetTickCount(), NULL
);
113 while(thread_data
->task_queue_head
114 && thread_data
->task_queue_head
->target_magic
== target
)
117 for(iter
= thread_data
->task_queue_head
; iter
; iter
= iter
->next
) {
118 while(iter
->next
&& iter
->next
->target_magic
== target
) {
120 iter
->next
= tmp
->next
;
125 thread_data
->task_queue_tail
= iter
;
129 LONG
get_task_target_magic(void)
131 static LONG magic
= 0x10000000;
132 return InterlockedIncrement(&magic
);
135 static BOOL
queue_timer(thread_data_t
*thread_data
, task_timer_t
*timer
)
139 list_remove(&timer
->entry
);
141 if(list_empty(&thread_data
->timer_list
)
142 || LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
)->time
> timer
->time
) {
144 list_add_head(&thread_data
->timer_list
, &timer
->entry
);
148 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
149 if(iter
->time
> timer
->time
) {
150 list_add_tail(&iter
->entry
, &timer
->entry
);
155 list_add_tail(&thread_data
->timer_list
, &timer
->entry
);
159 DWORD
set_task_timer(HTMLDocument
*doc
, DWORD msec
, BOOL interval
, IDispatch
*disp
)
161 thread_data_t
*thread_data
= get_thread_data(TRUE
);
163 DWORD tc
= GetTickCount();
165 static DWORD id_cnt
= 0x20000000;
167 timer
= heap_alloc(sizeof(task_timer_t
));
168 timer
->id
= id_cnt
++;
170 timer
->time
= tc
+ msec
;
171 timer
->interval
= interval
? msec
: 0;
172 list_init(&timer
->entry
);
174 IDispatch_AddRef(disp
);
177 if(queue_timer(thread_data
, timer
))
178 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, msec
, NULL
);
183 HRESULT
clear_task_timer(HTMLDocument
*doc
, BOOL interval
, DWORD id
)
185 thread_data_t
*thread_data
= get_thread_data(FALSE
);
191 LIST_FOR_EACH_ENTRY(iter
, &thread_data
->timer_list
, task_timer_t
, entry
) {
192 if(iter
->id
== id
&& iter
->doc
== doc
&& (iter
->interval
== 0) == !interval
) {
193 release_task_timer(thread_data
->thread_hwnd
, iter
);
198 WARN("timet not found\n");
202 static void call_timer_disp(IDispatch
*disp
)
204 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
209 V_VT(&res
) = VT_EMPTY
;
210 memset(&ei
, 0, sizeof(ei
));
213 hres
= IDispatch_Invoke(disp
, DISPID_VALUE
, &IID_NULL
, 0, DISPATCH_METHOD
, &dp
, &res
, &ei
, NULL
);
217 WARN("<<< %08x\n", hres
);
222 static LRESULT
process_timer(void)
224 thread_data_t
*thread_data
= get_thread_data(TRUE
);
231 while(!list_empty(&thread_data
->timer_list
)) {
232 timer
= LIST_ENTRY(list_head(&thread_data
->timer_list
), task_timer_t
, entry
);
235 if(timer
->time
> tc
) {
236 SetTimer(thread_data
->thread_hwnd
, TIMER_ID
, timer
->time
-tc
, NULL
);
241 IDispatch_AddRef(disp
);
243 if(timer
->interval
) {
244 timer
->time
+= timer
->interval
;
245 queue_timer(thread_data
, timer
);
247 release_task_timer(thread_data
->thread_hwnd
, timer
);
250 call_timer_disp(disp
);
252 IDispatch_Release(disp
);
255 KillTimer(thread_data
->thread_hwnd
, TIMER_ID
);
259 static LRESULT WINAPI
hidden_proc(HWND hwnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
264 task_t
*task
= pop_task();
274 return process_timer();
278 FIXME("(%p %d %lx %lx)\n", hwnd
, msg
, wParam
, lParam
);
280 return DefWindowProcW(hwnd
, msg
, wParam
, lParam
);
283 static HWND
create_thread_hwnd(void)
285 static ATOM hidden_wnd_class
= 0;
286 static const WCHAR wszInternetExplorer_Hidden
[] = {'I','n','t','e','r','n','e','t',
287 ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
289 if(!hidden_wnd_class
) {
290 WNDCLASSEXW wndclass
= {
291 sizeof(WNDCLASSEXW
), 0,
293 0, 0, hInst
, NULL
, NULL
, NULL
, NULL
,
294 wszInternetExplorer_Hidden
,
298 hidden_wnd_class
= RegisterClassExW(&wndclass
);
301 return CreateWindowExW(0, wszInternetExplorer_Hidden
, NULL
, WS_POPUP
,
302 0, 0, 0, 0, NULL
, NULL
, hInst
, NULL
);
305 HWND
get_thread_hwnd(void)
307 thread_data_t
*thread_data
= get_thread_data(TRUE
);
309 if(!thread_data
->thread_hwnd
)
310 thread_data
->thread_hwnd
= create_thread_hwnd();
312 return thread_data
->thread_hwnd
;
315 thread_data_t
*get_thread_data(BOOL create
)
317 thread_data_t
*thread_data
;
319 if(mshtml_tls
== TLS_OUT_OF_INDEXES
) {
326 if(tls
== TLS_OUT_OF_INDEXES
)
329 tls
= InterlockedCompareExchange((LONG
*)&mshtml_tls
, tls
, TLS_OUT_OF_INDEXES
);
330 if(tls
!= mshtml_tls
)
334 thread_data
= TlsGetValue(mshtml_tls
);
335 if(!thread_data
&& create
) {
336 thread_data
= heap_alloc_zero(sizeof(thread_data_t
));
337 TlsSetValue(mshtml_tls
, thread_data
);
338 list_init(&thread_data
->timer_list
);