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 static void default_task_destr(task_t *task)
55 void push_task(task_t *task, task_proc_t proc, task_proc_t destr, LONG magic)
57 thread_data_t *thread_data = get_thread_data(TRUE);
59 task->target_magic = magic;
61 task->destr = destr ? destr : default_task_destr;
64 if(thread_data->task_queue_tail)
65 thread_data->task_queue_tail->next = task;
67 thread_data->task_queue_head = task;
69 thread_data->task_queue_tail = task;
71 PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
74 static task_t *pop_task(void)
76 thread_data_t *thread_data = get_thread_data(TRUE);
77 task_t *task = thread_data->task_queue_head;
82 thread_data->task_queue_head = task->next;
83 if(!thread_data->task_queue_head)
84 thread_data->task_queue_tail = NULL;
89 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
91 list_remove(&timer->entry);
93 IDispatch_Release(timer->disp);
98 void remove_target_tasks(LONG target)
100 thread_data_t *thread_data = get_thread_data(FALSE);
101 struct list *liter, *ltmp;
108 LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
109 timer = LIST_ENTRY(liter, task_timer_t, entry);
110 if(timer->doc->task_magic == target)
111 release_task_timer(thread_data->thread_hwnd, timer);
114 if(!list_empty(&thread_data->timer_list)) {
115 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
116 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time - GetTickCount(), NULL);
119 while(thread_data->task_queue_head && thread_data->task_queue_head->target_magic == target) {
124 for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
125 while(iter->next && iter->next->target_magic == target) {
127 iter->next = tmp->next;
132 thread_data->task_queue_tail = iter;
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 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
168 thread_data_t *thread_data = get_thread_data(TRUE);
170 DWORD tc = GetTickCount();
172 static DWORD id_cnt = 0x20000000;
174 timer = heap_alloc(sizeof(task_timer_t));
175 timer->id = id_cnt++;
177 timer->time = tc + msec;
178 timer->interval = interval ? msec : 0;
179 list_init(&timer->entry);
181 IDispatch_AddRef(disp);
184 if(queue_timer(thread_data, timer))
185 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
190 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
192 thread_data_t *thread_data = get_thread_data(FALSE);
198 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
199 if(iter->id == id && iter->doc == doc && (iter->interval == 0) == !interval) {
200 release_task_timer(thread_data->thread_hwnd, iter);
205 WARN("timet not found\n");
209 static void call_timer_disp(IDispatch *disp)
211 DISPPARAMS dp = {NULL, NULL, 0, 0};
216 V_VT(&res) = VT_EMPTY;
217 memset(&ei, 0, sizeof(ei));
220 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
224 WARN("<<< %08x\n", hres);
229 static LRESULT process_timer(void)
231 thread_data_t *thread_data = get_thread_data(TRUE);
238 while(!list_empty(&thread_data->timer_list)) {
239 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
242 if(timer->time > tc) {
243 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
248 IDispatch_AddRef(disp);
250 if(timer->interval) {
251 timer->time += timer->interval;
252 queue_timer(thread_data, timer);
254 release_task_timer(thread_data->thread_hwnd, timer);
257 call_timer_disp(disp);
259 IDispatch_Release(disp);
262 KillTimer(thread_data->thread_hwnd, TIMER_ID);
266 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
271 task_t *task = pop_task();
281 return process_timer();
285 FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
287 return DefWindowProcW(hwnd, msg, wParam, lParam);
290 static HWND create_thread_hwnd(void)
292 static ATOM hidden_wnd_class = 0;
293 static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
294 ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
296 if(!hidden_wnd_class) {
297 WNDCLASSEXW wndclass = {
298 sizeof(WNDCLASSEXW), 0,
300 0, 0, hInst, NULL, NULL, NULL, NULL,
301 wszInternetExplorer_Hidden,
305 hidden_wnd_class = RegisterClassExW(&wndclass);
308 return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
309 0, 0, 0, 0, NULL, NULL, hInst, NULL);
312 HWND get_thread_hwnd(void)
314 thread_data_t *thread_data = get_thread_data(TRUE);
316 if(!thread_data->thread_hwnd)
317 thread_data->thread_hwnd = create_thread_hwnd();
319 return thread_data->thread_hwnd;
322 thread_data_t *get_thread_data(BOOL create)
324 thread_data_t *thread_data;
326 if(mshtml_tls == TLS_OUT_OF_INDEXES) {
333 if(tls == TLS_OUT_OF_INDEXES)
336 tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
337 if(tls != mshtml_tls)
341 thread_data = TlsGetValue(mshtml_tls);
342 if(!thread_data && create) {
343 thread_data = heap_alloc_zero(sizeof(thread_data_t));
344 TlsSetValue(mshtml_tls, thread_data);
345 list_init(&thread_data->timer_list);