winex11: Store the DC bounds rectangle as a pointer.
[wine/multimedia.git] / dlls / mshtml / task.c
bloba38437c87dc00d5cc17786cf3c46261021067d91
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>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
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
40 typedef struct {
41 HTMLDocument *doc;
42 DWORD id;
43 DWORD time;
44 DWORD interval;
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 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;
60 task->proc = proc;
61 task->destr = destr ? destr : default_task_destr;
62 task->next = NULL;
64 if(thread_data->task_queue_tail)
65 thread_data->task_queue_tail->next = task;
66 else
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;
79 if(!task)
80 return NULL;
82 thread_data->task_queue_head = task->next;
83 if(!thread_data->task_queue_head)
84 thread_data->task_queue_tail = NULL;
86 return task;
89 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
91 list_remove(&timer->entry);
93 IDispatch_Release(timer->disp);
95 heap_free(timer);
98 void remove_target_tasks(LONG target)
100 thread_data_t *thread_data = get_thread_data(FALSE);
101 struct list *liter, *ltmp;
102 task_timer_t *timer;
103 task_t *iter, *tmp;
105 if(!thread_data)
106 return;
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) {
120 iter = pop_task();
121 iter->destr(iter);
124 for(iter = thread_data->task_queue_head; iter; iter = iter->next) {
125 while(iter->next && iter->next->target_magic == target) {
126 tmp = iter->next;
127 iter->next = tmp->next;
128 tmp->destr(tmp);
131 if(!iter->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)
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 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
168 thread_data_t *thread_data = get_thread_data(TRUE);
169 task_timer_t *timer;
170 DWORD tc = GetTickCount();
172 static DWORD id_cnt = 0x20000000;
174 timer = heap_alloc(sizeof(task_timer_t));
175 timer->id = id_cnt++;
176 timer->doc = doc;
177 timer->time = tc + msec;
178 timer->interval = interval ? msec : 0;
179 list_init(&timer->entry);
181 IDispatch_AddRef(disp);
182 timer->disp = disp;
184 if(queue_timer(thread_data, timer))
185 SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
187 return timer->id;
190 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
192 thread_data_t *thread_data = get_thread_data(FALSE);
193 task_timer_t *iter;
195 if(!thread_data)
196 return S_OK;
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);
201 return S_OK;
205 WARN("timet not found\n");
206 return S_OK;
209 static void call_timer_disp(IDispatch *disp)
211 DISPPARAMS dp = {NULL, NULL, 0, 0};
212 EXCEPINFO ei;
213 VARIANT res;
214 HRESULT hres;
216 V_VT(&res) = VT_EMPTY;
217 memset(&ei, 0, sizeof(ei));
219 TRACE(">>>\n");
220 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, 0, DISPATCH_METHOD, &dp, &res, &ei, NULL);
221 if(hres == S_OK)
222 TRACE("<<<\n");
223 else
224 WARN("<<< %08x\n", hres);
226 VariantClear(&res);
229 static LRESULT process_timer(void)
231 thread_data_t *thread_data = get_thread_data(TRUE);
232 IDispatch *disp;
233 DWORD tc;
234 task_timer_t *timer;
236 TRACE("\n");
238 while(!list_empty(&thread_data->timer_list)) {
239 timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
241 tc = GetTickCount();
242 if(timer->time > tc) {
243 SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
244 return 0;
247 disp = timer->disp;
248 IDispatch_AddRef(disp);
250 if(timer->interval) {
251 timer->time += timer->interval;
252 queue_timer(thread_data, timer);
253 }else {
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);
263 return 0;
266 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
268 switch(msg) {
269 case WM_PROCESSTASK:
270 while(1) {
271 task_t *task = pop_task();
272 if(!task)
273 break;
275 task->proc(task);
276 task->destr(task);
279 return 0;
280 case WM_TIMER:
281 return process_timer();
284 if(msg > WM_USER)
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,
299 hidden_proc,
300 0, 0, hInst, NULL, NULL, NULL, NULL,
301 wszInternetExplorer_Hidden,
302 NULL
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) {
327 DWORD tls;
329 if(!create)
330 return NULL;
332 tls = TlsAlloc();
333 if(tls == TLS_OUT_OF_INDEXES)
334 return NULL;
336 tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
337 if(tls != mshtml_tls)
338 TlsFree(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);
348 return thread_data;