configure: Add a check for sys/ucontext.h and include it where appropriate.
[wine.git] / dlls / mshtml / task.c
blob6baac6388aaf10b33ecffd3adc2d1a4857f8fefd
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;
72 task->next = NULL;
74 if(thread_data->task_queue_tail)
75 thread_data->task_queue_tail->next = task;
76 else
77 thread_data->task_queue_head = task;
79 thread_data->task_queue_tail = task;
81 PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
82 return S_OK;
85 static task_t *pop_task(void)
87 thread_data_t *thread_data;
88 task_t *task;
90 thread_data = get_thread_data(FALSE);
91 if(!thread_data)
92 return NULL;
94 task = thread_data->task_queue_head;
95 if(!task)
96 return NULL;
98 thread_data->task_queue_head = task->next;
99 if(!thread_data->task_queue_head)
100 thread_data->task_queue_tail = NULL;
102 return task;
105 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
107 list_remove(&timer->entry);
109 IDispatch_Release(timer->disp);
111 heap_free(timer);
114 void remove_target_tasks(LONG target)
116 thread_data_t *thread_data = get_thread_data(FALSE);
117 struct list *liter, *ltmp;
118 task_timer_t *timer;
119 task_t *iter, *tmp;
121 if(!thread_data)
122 return;
124 LIST_FOR_EACH_SAFE(liter, ltmp, &thread_data->timer_list) {
125 timer = LIST_ENTRY(liter, task_timer_t, entry);
126 if(timer->window->task_magic == target)
127 release_task_timer(thread_data->thread_hwnd, timer);
130 if(!list_empty(&thread_data->timer_list)) {
131 DWORD tc = GetTickCount();
133 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
134 SetTimer(thread_data->thread_hwnd, TIMER_ID, max( (int)(timer->time - tc), 0 ), NULL);
137 while(thread_data->task_queue_head && thread_data->task_queue_head->target_magic == target) {
138 iter = pop_task();
139 iter->destr(iter);
142 for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
143 while(iter->next && iter->next->target_magic == target) {
144 tmp = iter->next;
145 iter->next = tmp->next;
146 tmp->destr(tmp);
149 if(!iter->next)
150 thread_data->task_queue_tail = iter;
154 LONG get_task_target_magic(void)
156 static LONG magic = 0x10000000;
157 return InterlockedIncrement(&magic);
160 static BOOL queue_timer(thread_data_t *thread_data, task_timer_t *timer)
162 task_timer_t *iter;
164 list_remove(&timer->entry);
166 if(list_empty(&thread_data->timer_list)
167 || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
169 list_add_head(&thread_data->timer_list, &timer->entry);
170 return TRUE;
173 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
174 if(iter->time > timer->time) {
175 list_add_tail(&iter->entry, &timer->entry);
176 return FALSE;
180 list_add_tail(&thread_data->timer_list, &timer->entry);
181 return FALSE;
184 HRESULT set_task_timer(HTMLInnerWindow *window, DWORD msec, BOOL interval, IDispatch *disp, LONG *id)
186 thread_data_t *thread_data;
187 task_timer_t *timer;
188 DWORD tc = GetTickCount();
190 static DWORD id_cnt = 0x20000000;
192 thread_data = get_thread_data(TRUE);
193 if(!thread_data)
194 return E_OUTOFMEMORY;
196 timer = heap_alloc(sizeof(task_timer_t));
197 if(!timer)
198 return E_OUTOFMEMORY;
200 timer->id = id_cnt++;
201 timer->window = window;
202 timer->time = tc + msec;
203 timer->interval = interval ? msec : 0;
204 list_init(&timer->entry);
206 IDispatch_AddRef(disp);
207 timer->disp = disp;
209 if(queue_timer(thread_data, timer))
210 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
212 *id = timer->id;
213 return S_OK;
216 HRESULT clear_task_timer(HTMLInnerWindow *window, BOOL interval, DWORD id)
218 thread_data_t *thread_data = get_thread_data(FALSE);
219 task_timer_t *iter;
221 if(!thread_data)
222 return S_OK;
224 LIST_FOR_EACH_ENTRY(iter, &thread_data->timer_list, task_timer_t, entry) {
225 if(iter->id == id && iter->window == window && !iter->interval == !interval) {
226 release_task_timer(thread_data->thread_hwnd, iter);
227 return S_OK;
231 WARN("timet not found\n");
232 return S_OK;
235 static void call_timer_disp(IDispatch *disp)
237 DISPPARAMS dp = {NULL, NULL, 0, 0};
238 EXCEPINFO ei;
239 VARIANT res;
240 HRESULT hres;
242 V_VT(&res) = VT_EMPTY;
243 memset(&ei, 0, sizeof(ei));
245 TRACE(">>>\n");
246 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
247 if(hres == S_OK)
248 TRACE("<<<\n");
249 else
250 WARN("<<< %08x\n", hres);
252 VariantClear(&res);
255 static LRESULT process_timer(void)
257 thread_data_t *thread_data;
258 IDispatch *disp;
259 DWORD tc;
260 task_timer_t *timer=NULL, *last_timer;
262 TRACE("\n");
264 thread_data = get_thread_data(FALSE);
265 assert(thread_data != NULL);
267 if(list_empty(&thread_data->timer_list)) {
268 KillTimer(thread_data->thread_hwnd, TIMER_ID);
269 return 0;
272 last_timer = LIST_ENTRY(list_tail(&thread_data->timer_list), task_timer_t, entry);
273 do {
274 tc = GetTickCount();
275 if(timer == last_timer) {
276 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
277 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time>tc ? timer->time-tc : 0, NULL);
278 return 0;
281 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
283 if(timer->time > tc) {
284 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
285 return 0;
288 disp = timer->disp;
289 IDispatch_AddRef(disp);
291 if(timer->interval) {
292 timer->time += timer->interval;
293 queue_timer(thread_data, timer);
294 }else {
295 release_task_timer(thread_data->thread_hwnd, timer);
298 call_timer_disp(disp);
300 IDispatch_Release(disp);
301 }while(!list_empty(&thread_data->timer_list));
303 KillTimer(thread_data->thread_hwnd, TIMER_ID);
304 return 0;
307 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
309 switch(msg) {
310 case WM_PROCESSTASK:
311 while(1) {
312 task_t *task = pop_task();
313 if(!task)
314 break;
316 task->proc(task);
317 task->destr(task);
320 return 0;
321 case WM_TIMER:
322 return process_timer();
325 if(msg > WM_USER)
326 FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
328 return DefWindowProcW(hwnd, msg, wParam, lParam);
331 static HWND create_thread_hwnd(void)
333 static ATOM hidden_wnd_class = 0;
334 static const WCHAR wszInternetExplorer_Hidden[] = {'I','n','t','e','r','n','e','t',
335 ' ','E','x','p','l','o','r','e','r','_','H','i','d','d','e','n',0};
337 if(!hidden_wnd_class) {
338 WNDCLASSEXW wndclass = {
339 sizeof(WNDCLASSEXW), 0,
340 hidden_proc,
341 0, 0, hInst, NULL, NULL, NULL, NULL,
342 wszInternetExplorer_Hidden,
343 NULL
346 hidden_wnd_class = RegisterClassExW(&wndclass);
349 return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
350 0, 0, 0, 0, NULL, NULL, hInst, NULL);
353 HWND get_thread_hwnd(void)
355 thread_data_t *thread_data;
357 thread_data = get_thread_data(TRUE);
358 if(!thread_data)
359 return NULL;
361 if(!thread_data->thread_hwnd)
362 thread_data->thread_hwnd = create_thread_hwnd();
364 return thread_data->thread_hwnd;
367 thread_data_t *get_thread_data(BOOL create)
369 thread_data_t *thread_data;
371 if(mshtml_tls == TLS_OUT_OF_INDEXES) {
372 DWORD tls;
374 if(!create)
375 return NULL;
377 tls = TlsAlloc();
378 if(tls == TLS_OUT_OF_INDEXES)
379 return NULL;
381 tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
382 if(tls != mshtml_tls)
383 TlsFree(tls);
386 thread_data = TlsGetValue(mshtml_tls);
387 if(!thread_data && create) {
388 thread_data = heap_alloc_zero(sizeof(thread_data_t));
389 if(!thread_data)
390 return NULL;
392 TlsSetValue(mshtml_tls, thread_data);
393 list_init(&thread_data->timer_list);
396 return thread_data;