gphoto2.ds: Set supported groups.
[wine.git] / dlls / mshtml / task.c
blob07f68753020d13b9ab3eb59fe45a7d7dcb49c249
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 "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <assert.h>
25 #define COBJMACROS
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "ole2.h"
32 #include "wine/debug.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 #define WM_PROCESSTASK 0x8008
39 #define TIMER_ID 0x3000
41 typedef struct {
42 HTMLInnerWindow *window;
43 DWORD id;
44 DWORD time;
45 DWORD interval;
46 IDispatch *disp;
48 struct list entry;
49 } task_timer_t;
51 static void default_task_destr(task_t *task)
53 heap_free(task);
56 HRESULT push_task(task_t *task, task_proc_t proc, task_proc_t destr, LONG magic)
58 thread_data_t *thread_data;
60 thread_data = get_thread_data(TRUE);
61 if(!thread_data) {
62 if(destr)
63 destr(task);
64 else
65 heap_free(task);
66 return E_OUTOFMEMORY;
69 task->target_magic = magic;
70 task->proc = proc;
71 task->destr = destr ? destr : default_task_destr;
73 list_add_tail(&thread_data->task_list, &task->entry);
75 PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
76 return S_OK;
79 static task_t *pop_task(void)
81 thread_data_t *thread_data;
82 task_t *task;
84 thread_data = get_thread_data(FALSE);
85 if(!thread_data)
86 return NULL;
88 if(list_empty(&thread_data->task_list))
89 return NULL;
91 task = LIST_ENTRY(thread_data->task_list.next, task_t, entry);
92 list_remove(&task->entry);
93 return task;
96 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
98 list_remove(&timer->entry);
100 IDispatch_Release(timer->disp);
102 heap_free(timer);
105 void flush_pending_tasks(LONG target)
107 thread_data_t *thread_data = get_thread_data(FALSE);
108 struct list *liter, *ltmp;
109 task_t *task;
111 if(!thread_data)
112 return;
114 LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->task_list) {
115 task = LIST_ENTRY(liter, task_t, entry);
116 if(task->target_magic == target) {
117 list_remove(&task->entry);
118 task->proc(task);
119 task->destr(task);
124 void remove_target_tasks(LONG target)
126 thread_data_t *thread_data = get_thread_data(FALSE);
127 struct list *liter, *ltmp;
128 task_timer_t *timer;
129 task_t *task;
131 if(!thread_data)
132 return;
134 LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
135 timer = LIST_ENTRY(liter, task_timer_t, entry);
136 if(timer->window->task_magic == target)
137 release_task_timer(thread_data->thread_hwnd, timer);
140 if(!list_empty(&thread_data->timer_list)) {
141 DWORD tc = GetTickCount();
143 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
144 SetTimer(thread_data->thread_hwnd, TIMER_ID, max( (int)(timer->time - tc), 0 ), NULL);
147 LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->task_list) {
148 task = LIST_ENTRY(liter, task_t, entry);
149 if(task->target_magic == target) {
150 list_remove(&task->entry);
151 task->destr(task);
156 LONG get_task_target_magic(void)
158 static LONG magic = 0x10000000;
159 return InterlockedIncrement(&magic);
162 static BOOL queue_timer(thread_data_t *thread_data, task_timer_t *timer)
164 task_timer_t *iter;
166 list_remove(&timer->entry);
168 if(list_empty(&thread_data->timer_list)
169 || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
171 list_add_head(&thread_data->timer_list, &timer->entry);
172 return TRUE;
175 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
176 if(iter->time > timer->time) {
177 list_add_tail(&iter->entry, &timer->entry);
178 return FALSE;
182 list_add_tail(&thread_data->timer_list, &timer->entry);
183 return FALSE;
186 HRESULT set_task_timer(HTMLInnerWindow *window, LONG msec, BOOL interval, IDispatch *disp, LONG *id)
188 thread_data_t *thread_data;
189 task_timer_t *timer;
190 DWORD tc = GetTickCount();
192 static DWORD id_cnt = 0x20000000;
194 thread_data = get_thread_data(TRUE);
195 if(!thread_data)
196 return E_OUTOFMEMORY;
198 timer = heap_alloc(sizeof(task_timer_t));
199 if(!timer)
200 return E_OUTOFMEMORY;
202 if(msec < 1)
203 msec = 1;
205 timer->id = id_cnt++;
206 timer->window = window;
207 timer->time = tc + msec;
208 timer->interval = interval ? msec : 0;
209 list_init(&timer->entry);
211 IDispatch_AddRef(disp);
212 timer->disp = disp;
214 if(queue_timer(thread_data, timer))
215 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
217 *id = timer->id;
218 return S_OK;
221 HRESULT clear_task_timer(HTMLInnerWindow *window, DWORD id)
223 thread_data_t *thread_data = get_thread_data(FALSE);
224 task_timer_t *iter;
226 if(!thread_data)
227 return S_OK;
229 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
230 if(iter->id == id && iter->window == window) {
231 release_task_timer(thread_data->thread_hwnd, iter);
232 return S_OK;
236 WARN("timet not found\n");
237 return S_OK;
240 static void call_timer_disp(IDispatch *disp)
242 DISPPARAMS dp = {NULL, NULL, 0, 0};
243 EXCEPINFO ei;
244 VARIANT res;
245 HRESULT hres;
247 V_VT(&res) = VT_EMPTY;
248 memset(&ei, 0, sizeof(ei));
250 TRACE(">>>\n");
251 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
252 if(hres == S_OK)
253 TRACE("<<<\n");
254 else
255 WARN("<<< %08x\n", hres);
257 VariantClear(&res);
260 static LRESULT process_timer(void)
262 thread_data_t *thread_data;
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);
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);
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;
339 static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
340 ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
342 if(!hidden_wnd_class) {
343 WNDCLASSEXW wndclass = {
344 sizeof(WNDCLASSEXW), 0,
345 hidden_proc,
346 0, 0, hInst, NULL, NULL, NULL, NULL,
347 wszInternetExplorer_Hidden,
348 NULL
351 hidden_wnd_class = RegisterClassExW(&wndclass);
354 return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
355 0, 0, 0, 0, NULL, NULL, hInst, NULL);
358 HWND get_thread_hwnd(void)
360 thread_data_t *thread_data;
362 thread_data = get_thread_data(TRUE);
363 if(!thread_data)
364 return NULL;
366 if(!thread_data->thread_hwnd)
367 thread_data->thread_hwnd = create_thread_hwnd();
369 return thread_data->thread_hwnd;
372 thread_data_t *get_thread_data(BOOL create)
374 thread_data_t *thread_data;
376 if(mshtml_tls == TLS_OUT_OF_INDEXES) {
377 DWORD tls;
379 if(!create)
380 return NULL;
382 tls = TlsAlloc();
383 if(tls == TLS_OUT_OF_INDEXES)
384 return NULL;
386 tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
387 if(tls != mshtml_tls)
388 TlsFree(tls);
391 thread_data = TlsGetValue(mshtml_tls);
392 if(!thread_data && create) {
393 thread_data = heap_alloc_zero(sizeof(thread_data_t));
394 if(!thread_data)
395 return NULL;
397 TlsSetValue(mshtml_tls, thread_data);
398 list_init(&thread_data->task_list);
399 list_init(&thread_data->timer_list);
402 return thread_data;