hidclass.sys: Support parsing of explicit usage page.
[wine.git] / dlls / mshtml / task.c
blob28cb88ab5f1cf402255f6c8700024ee4e368d75a
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>
21 #include <assert.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
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
39 typedef struct {
40 HTMLInnerWindow *window;
41 DWORD id;
42 DWORD time;
43 DWORD interval;
44 enum timer_type type;
45 IDispatch *disp;
47 struct list entry;
48 } task_timer_t;
50 static void default_task_destr(task_t *task)
52 heap_free(task);
55 HRESULT push_task(task_t *task, task_proc_t proc, task_proc_t destr, LONG magic)
57 thread_data_t *thread_data;
59 thread_data = get_thread_data(TRUE);
60 if(!thread_data) {
61 if(destr)
62 destr(task);
63 else
64 heap_free(task);
65 return E_OUTOFMEMORY;
68 task->target_magic = magic;
69 task->proc = proc;
70 task->destr = destr ? destr : default_task_destr;
72 list_add_tail(&thread_data->task_list, &task->entry);
74 PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
75 return S_OK;
78 static task_t *pop_task(void)
80 thread_data_t *thread_data;
81 task_t *task;
83 thread_data = get_thread_data(FALSE);
84 if(!thread_data)
85 return NULL;
87 if(list_empty(&thread_data->task_list))
88 return NULL;
90 task = LIST_ENTRY(thread_data->task_list.next, task_t, entry);
91 list_remove(&task->entry);
92 return task;
95 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
97 list_remove(&timer->entry);
99 IDispatch_Release(timer->disp);
101 heap_free(timer);
104 void remove_target_tasks(LONG target)
106 thread_data_t *thread_data = get_thread_data(FALSE);
107 struct list *liter, *ltmp;
108 task_timer_t *timer;
109 task_t *task;
111 if(!thread_data)
112 return;
114 LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
115 timer = LIST_ENTRY(liter, task_timer_t, entry);
116 if(timer->window->task_magic == target)
117 release_task_timer(thread_data->thread_hwnd, timer);
120 if(!list_empty(&thread_data->timer_list)) {
121 DWORD tc = GetTickCount();
123 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
124 SetTimer(thread_data->thread_hwnd, TIMER_ID, max( (int)(timer->time - tc), 0 ), NULL);
127 LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->task_list) {
128 task = LIST_ENTRY(liter, task_t, entry);
129 if(task->target_magic == target) {
130 list_remove(&task->entry);
131 task->destr(task);
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)
144 task_timer_t *iter;
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);
152 return TRUE;
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);
158 return FALSE;
162 list_add_tail(&thread_data->timer_list, &timer->entry);
163 return FALSE;
166 HRESULT set_task_timer(HTMLInnerWindow *window, LONG msec, enum timer_type timer_type, IDispatch *disp, LONG *id)
168 thread_data_t *thread_data;
169 task_timer_t *timer;
170 DWORD tc = GetTickCount();
172 static DWORD id_cnt = 0x20000000;
174 thread_data = get_thread_data(TRUE);
175 if(!thread_data)
176 return E_OUTOFMEMORY;
178 timer = heap_alloc(sizeof(task_timer_t));
179 if(!timer)
180 return E_OUTOFMEMORY;
182 if(msec < 1)
183 msec = 1;
185 timer->id = id_cnt++;
186 timer->window = window;
187 timer->time = tc + msec;
188 timer->interval = timer_type == TIMER_INTERVAL ? msec : 0;
189 timer->type = timer_type;
190 list_init(&timer->entry);
192 IDispatch_AddRef(disp);
193 timer->disp = disp;
195 if(queue_timer(thread_data, timer))
196 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
198 *id = timer->id;
199 return S_OK;
202 HRESULT clear_task_timer(HTMLInnerWindow *window, DWORD id)
204 thread_data_t *thread_data = get_thread_data(FALSE);
205 task_timer_t *iter;
207 if(!thread_data)
208 return S_OK;
210 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
211 if(iter->id == id && iter->window == window) {
212 release_task_timer(thread_data->thread_hwnd, iter);
213 return S_OK;
217 WARN("timet not found\n");
218 return S_OK;
221 static const char *debugstr_timer_type(enum timer_type type)
223 switch(type) {
224 case TIMER_TIMEOUT: return "timeout";
225 case TIMER_INTERVAL: return "interval";
226 case TIMER_ANIMATION_FRAME: return "animation-frame";
227 DEFAULT_UNREACHABLE;
231 static void call_timer_disp(IDispatch *disp, enum timer_type timer_type)
233 DISPPARAMS dp = {NULL, NULL, 0, 0};
234 VARIANT timestamp;
235 EXCEPINFO ei;
236 VARIANT res;
237 HRESULT hres;
239 V_VT(&res) = VT_EMPTY;
240 memset(&ei, 0, sizeof(ei));
242 if(timer_type == TIMER_ANIMATION_FRAME) {
243 dp.cArgs = 1;
244 dp.rgvarg = &timestamp;
245 V_VT(&timestamp) = VT_R8;
246 V_R8(&timestamp) = get_time_stamp();
249 TRACE("%p %s >>>\n", disp, debugstr_timer_type(timer_type));
250 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
251 if(hres == S_OK)
252 TRACE("%p %s <<<\n", disp, debugstr_timer_type(timer_type));
253 else
254 WARN("%p %s <<< %08x\n", disp, debugstr_timer_type(timer_type), hres);
256 VariantClear(&res);
259 static LRESULT process_timer(void)
261 thread_data_t *thread_data;
262 enum timer_type timer_type;
263 IDispatch *disp;
264 DWORD tc;
265 task_timer_t *timer=NULL, *last_timer;
267 TRACE("\n");
269 thread_data = get_thread_data(FALSE);
270 assert(thread_data != NULL);
272 if(list_empty(&thread_data->timer_list)) {
273 KillTimer(thread_data->thread_hwnd, TIMER_ID);
274 return 0;
277 last_timer = LIST_ENTRY(list_tail(&thread_data->timer_list), task_timer_t, entry);
278 do {
279 tc = GetTickCount();
280 if(timer == last_timer) {
281 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
282 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time>tc ? timer->time-tc : 0, NULL);
283 return 0;
286 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
288 if(timer->time > tc) {
289 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
290 return 0;
293 disp = timer->disp;
294 IDispatch_AddRef(disp);
295 timer_type = timer->type;
297 if(timer->interval) {
298 timer->time += timer->interval;
299 queue_timer(thread_data, timer);
300 }else {
301 release_task_timer(thread_data->thread_hwnd, timer);
304 call_timer_disp(disp, timer_type);
306 IDispatch_Release(disp);
307 }while(!list_empty(&thread_data->timer_list));
309 KillTimer(thread_data->thread_hwnd, TIMER_ID);
310 return 0;
313 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
315 switch(msg) {
316 case WM_PROCESSTASK:
317 while(1) {
318 task_t *task = pop_task();
319 if(!task)
320 break;
322 task->proc(task);
323 task->destr(task);
326 return 0;
327 case WM_TIMER:
328 return process_timer();
331 if(msg > WM_USER)
332 FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
334 return DefWindowProcW(hwnd, msg, wParam, lParam);
337 static HWND create_thread_hwnd(void)
339 static ATOM hidden_wnd_class = 0;
341 if(!hidden_wnd_class) {
342 WNDCLASSEXW wndclass = {
343 sizeof(WNDCLASSEXW), 0,
344 hidden_proc,
345 0, 0, hInst, NULL, NULL, NULL, NULL,
346 L"Internet Explorer_Hidden",
347 NULL
350 hidden_wnd_class = RegisterClassExW(&wndclass);
353 return CreateWindowExW(0, L"Internet Explorer_Hidden", NULL, WS_POPUP,
354 0, 0, 0, 0, NULL, NULL, hInst, NULL);
357 HWND get_thread_hwnd(void)
359 thread_data_t *thread_data;
361 thread_data = get_thread_data(TRUE);
362 if(!thread_data)
363 return NULL;
365 if(!thread_data->thread_hwnd)
366 thread_data->thread_hwnd = create_thread_hwnd();
368 return thread_data->thread_hwnd;
371 thread_data_t *get_thread_data(BOOL create)
373 thread_data_t *thread_data;
375 if(mshtml_tls == TLS_OUT_OF_INDEXES) {
376 DWORD tls;
378 if(!create)
379 return NULL;
381 tls = TlsAlloc();
382 if(tls == TLS_OUT_OF_INDEXES)
383 return NULL;
385 tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
386 if(tls != mshtml_tls)
387 TlsFree(tls);
390 thread_data = TlsGetValue(mshtml_tls);
391 if(!thread_data && create) {
392 thread_data = heap_alloc_zero(sizeof(thread_data_t));
393 if(!thread_data)
394 return NULL;
396 TlsSetValue(mshtml_tls, thread_data);
397 list_init(&thread_data->task_list);
398 list_init(&thread_data->timer_list);
401 return thread_data;
404 ULONGLONG get_time_stamp(void)
406 FILETIME time;
408 /* 1601 to 1970 is 369 years plus 89 leap days */
409 const ULONGLONG time_epoch = (ULONGLONG)(369 * 365 + 89) * 86400 * 1000;
411 GetSystemTimeAsFileTime(&time);
412 return (((ULONGLONG)time.dwHighDateTime << 32) + time.dwLowDateTime) / 10000 - time_epoch;