user32: Remove _wassert workaround.
[wine.git] / dlls / mshtml / task.c
blob67b7633a5b456ad3bc450954d6b1bee0658d9f66
1 /*
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 <stdarg.h>
20 #include <stdio.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
35 #define WM_PROCESSTASK 0x8008
36 #define TIMER_ID 0x3000
38 typedef struct {
39 HTMLInnerWindow *window;
40 DWORD id;
41 DWORD time;
42 DWORD interval;
43 enum timer_type type;
44 IDispatch *disp;
46 struct list entry;
47 } task_timer_t;
49 static void default_task_destr(task_t *task)
51 heap_free(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);
59 if(!thread_data) {
60 if(destr)
61 destr(task);
62 else
63 heap_free(task);
64 return E_OUTOFMEMORY;
67 task->target_magic = magic;
68 task->proc = proc;
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);
74 return S_OK;
77 static task_t *pop_task(void)
79 thread_data_t *thread_data;
80 task_t *task;
82 thread_data = get_thread_data(FALSE);
83 if(!thread_data)
84 return NULL;
86 if(list_empty(&thread_data->task_list))
87 return NULL;
89 task = LIST_ENTRY(thread_data->task_list.next, task_t, entry);
90 list_remove(&task->entry);
91 return task;
94 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
96 list_remove(&timer->entry);
98 IDispatch_Release(timer->disp);
100 heap_free(timer);
103 void remove_target_tasks(LONG target)
105 thread_data_t *thread_data = get_thread_data(FALSE);
106 struct list *liter, *ltmp;
107 task_timer_t *timer;
108 task_t *task;
110 if(!thread_data)
111 return;
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);
130 task->destr(task);
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)
143 task_timer_t *iter;
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);
151 return TRUE;
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);
157 return FALSE;
161 list_add_tail(&thread_data->timer_list, &timer->entry);
162 return FALSE;
165 HRESULT set_task_timer(HTMLInnerWindow *window, LONG msec, enum timer_type timer_type, IDispatch *disp, LONG *id)
167 thread_data_t *thread_data;
168 task_timer_t *timer;
169 DWORD tc = GetTickCount();
171 static DWORD id_cnt = 0x20000000;
173 thread_data = get_thread_data(TRUE);
174 if(!thread_data)
175 return E_OUTOFMEMORY;
177 timer = heap_alloc(sizeof(task_timer_t));
178 if(!timer)
179 return E_OUTOFMEMORY;
181 if(msec < 1)
182 msec = 1;
184 timer->id = id_cnt++;
185 timer->window = window;
186 timer->time = tc + msec;
187 timer->interval = timer_type == TIMER_INTERVAL ? msec : 0;
188 timer->type = timer_type;
189 list_init(&timer->entry);
191 IDispatch_AddRef(disp);
192 timer->disp = disp;
194 if(queue_timer(thread_data, timer))
195 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
197 *id = timer->id;
198 return S_OK;
201 HRESULT clear_task_timer(HTMLInnerWindow *window, DWORD id)
203 thread_data_t *thread_data = get_thread_data(FALSE);
204 task_timer_t *iter;
206 if(!thread_data)
207 return S_OK;
209 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
210 if(iter->id == id && iter->window == window) {
211 release_task_timer(thread_data->thread_hwnd, iter);
212 return S_OK;
216 WARN("timet not found\n");
217 return S_OK;
220 static const char *debugstr_timer_type(enum timer_type type)
222 switch(type) {
223 case TIMER_TIMEOUT: return "timeout";
224 case TIMER_INTERVAL: return "interval";
225 case TIMER_ANIMATION_FRAME: return "animation-frame";
226 DEFAULT_UNREACHABLE;
230 static void call_timer_disp(IDispatch *disp, enum timer_type timer_type)
232 DISPPARAMS dp = {NULL, NULL, 0, 0};
233 VARIANT timestamp;
234 EXCEPINFO ei;
235 VARIANT res;
236 HRESULT hres;
238 V_VT(&res) = VT_EMPTY;
239 memset(&ei, 0, sizeof(ei));
241 if(timer_type == TIMER_ANIMATION_FRAME) {
242 dp.cArgs = 1;
243 dp.rgvarg = &timestamp;
244 V_VT(&timestamp) = VT_R8;
245 V_R8(&timestamp) = get_time_stamp();
248 TRACE("%p %s >>>\n", disp, debugstr_timer_type(timer_type));
249 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
250 if(hres == S_OK)
251 TRACE("%p %s <<<\n", disp, debugstr_timer_type(timer_type));
252 else
253 WARN("%p %s <<< %08x\n", disp, debugstr_timer_type(timer_type), hres);
255 VariantClear(&res);
258 static LRESULT process_timer(void)
260 thread_data_t *thread_data;
261 enum timer_type timer_type;
262 IDispatch *disp;
263 DWORD tc;
264 task_timer_t *timer=NULL, *last_timer;
266 TRACE("\n");
268 thread_data = get_thread_data(FALSE);
269 assert(thread_data != NULL);
271 if(list_empty(&thread_data->timer_list)) {
272 KillTimer(thread_data->thread_hwnd, TIMER_ID);
273 return 0;
276 last_timer = LIST_ENTRY(list_tail(&thread_data->timer_list), task_timer_t, entry);
277 do {
278 tc = GetTickCount();
279 if(timer == last_timer) {
280 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
281 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time>tc ? timer->time-tc : 0, NULL);
282 return 0;
285 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
287 if(timer->time > tc) {
288 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
289 return 0;
292 disp = timer->disp;
293 IDispatch_AddRef(disp);
294 timer_type = timer->type;
296 if(timer->interval) {
297 timer->time += timer->interval;
298 queue_timer(thread_data, timer);
299 }else {
300 release_task_timer(thread_data->thread_hwnd, timer);
303 call_timer_disp(disp, timer_type);
305 IDispatch_Release(disp);
306 }while(!list_empty(&thread_data->timer_list));
308 KillTimer(thread_data->thread_hwnd, TIMER_ID);
309 return 0;
312 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
314 switch(msg) {
315 case WM_PROCESSTASK:
316 while(1) {
317 task_t *task = pop_task();
318 if(!task)
319 break;
321 task->proc(task);
322 task->destr(task);
325 return 0;
326 case WM_TIMER:
327 return process_timer();
330 if(msg > WM_USER)
331 FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
333 return DefWindowProcW(hwnd, msg, wParam, lParam);
336 static HWND create_thread_hwnd(void)
338 static ATOM hidden_wnd_class = 0;
340 if(!hidden_wnd_class) {
341 WNDCLASSEXW wndclass = {
342 sizeof(WNDCLASSEXW), 0,
343 hidden_proc,
344 0, 0, hInst, NULL, NULL, NULL, NULL,
345 L"Internet Explorer_Hidden",
346 NULL
349 hidden_wnd_class = RegisterClassExW(&wndclass);
352 return CreateWindowExW(0, L"Internet Explorer_Hidden", NULL, WS_POPUP,
353 0, 0, 0, 0, NULL, NULL, hInst, NULL);
356 HWND get_thread_hwnd(void)
358 thread_data_t *thread_data;
360 thread_data = get_thread_data(TRUE);
361 if(!thread_data)
362 return NULL;
364 if(!thread_data->thread_hwnd)
365 thread_data->thread_hwnd = create_thread_hwnd();
367 return thread_data->thread_hwnd;
370 thread_data_t *get_thread_data(BOOL create)
372 thread_data_t *thread_data;
374 if(mshtml_tls == TLS_OUT_OF_INDEXES) {
375 DWORD tls;
377 if(!create)
378 return NULL;
380 tls = TlsAlloc();
381 if(tls == TLS_OUT_OF_INDEXES)
382 return NULL;
384 tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
385 if(tls != mshtml_tls)
386 TlsFree(tls);
389 thread_data = TlsGetValue(mshtml_tls);
390 if(!thread_data && create) {
391 thread_data = heap_alloc_zero(sizeof(thread_data_t));
392 if(!thread_data)
393 return NULL;
395 TlsSetValue(mshtml_tls, thread_data);
396 list_init(&thread_data->task_list);
397 list_init(&thread_data->timer_list);
400 return thread_data;
403 ULONGLONG get_time_stamp(void)
405 FILETIME time;
407 /* 1601 to 1970 is 369 years plus 89 leap days */
408 const ULONGLONG time_epoch = (ULONGLONG)(369 * 365 + 89) * 86400 * 1000;
410 GetSystemTimeAsFileTime(&time);
411 return (((ULONGLONG)time.dwHighDateTime << 32) + time.dwLowDateTime) / 10000 - time_epoch;