opengl32/tests: Do not pass NULL attrib list to wglCreatePBufferARB.
[wine] / dlls / mshtml / task.c
1 /*
2  * Copyright 2006 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30
31 #include "wine/debug.h"
32
33 #include "mshtml_private.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36
37 #define WM_PROCESSTASK 0x8008
38 #define TIMER_ID 0x3000
39
40 typedef struct {
41     HTMLDocument *doc;
42     DWORD id;
43     DWORD time;
44     DWORD interval;
45     IDispatch *disp;
46
47     struct list entry;
48 } task_timer_t;
49
50 static void default_task_destr(task_t *task)
51 {
52     heap_free(task);
53 }
54
55 void push_task(task_t *task, task_proc_t proc, task_proc_t destr, LONG magic)
56 {
57     thread_data_t *thread_data = get_thread_data(TRUE);
58
59     task->target_magic = magic;
60     task->proc = proc;
61     task->destr = destr ? destr : default_task_destr;
62     task->next = NULL;
63
64     if(thread_data->task_queue_tail)
65         thread_data->task_queue_tail->next = task;
66     else
67         thread_data->task_queue_head = task;
68
69     thread_data->task_queue_tail = task;
70
71     PostMessageW(thread_data->thread_hwnd, WM_PROCESSTASK, 0, 0);
72 }
73
74 static task_t *pop_task(void)
75 {
76     thread_data_t *thread_data = get_thread_data(TRUE);
77     task_t *task = thread_data->task_queue_head;
78
79     if(!task)
80         return NULL;
81
82     thread_data->task_queue_head = task->next;
83     if(!thread_data->task_queue_head)
84         thread_data->task_queue_tail = NULL;
85
86     return task;
87 }
88
89 static void release_task_timer(HWND thread_hwnd, task_timer_t *timer)
90 {
91     list_remove(&timer->entry);
92
93     IDispatch_Release(timer->disp);
94
95     heap_free(timer);
96 }
97
98 void remove_target_tasks(LONG target)
99 {
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;
104
105     if(!thread_data)
106         return;
107
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);
112     }
113
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);
117     }
118
119     while(thread_data->task_queue_head && thread_data->task_queue_head->target_magic == target) {
120         iter = pop_task();
121         iter->destr(iter);
122     }
123
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);
129         }
130
131         if(!iter->next)
132             thread_data->task_queue_tail = iter;
133     }
134 }
135
136 LONG get_task_target_magic(void)
137 {
138     static LONG magic = 0x10000000;
139     return InterlockedIncrement(&magic);
140 }
141
142 static BOOL queue_timer(thread_data_t *thread_data, task_timer_t *timer)
143 {
144     task_timer_t *iter;
145
146     list_remove(&timer->entry);
147
148     if(list_empty(&thread_data->timer_list)
149        || LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry)->time > timer->time) {
150
151         list_add_head(&thread_data->timer_list, &timer->entry);
152         return TRUE;
153     }
154
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;
159         }
160     }
161
162     list_add_tail(&thread_data->timer_list, &timer->entry);
163     return FALSE;
164 }
165
166 DWORD set_task_timer(HTMLDocument *doc, DWORD msec, BOOL interval, IDispatch *disp)
167 {
168     thread_data_t *thread_data = get_thread_data(TRUE);
169     task_timer_t *timer;
170     DWORD tc = GetTickCount();
171
172     static DWORD id_cnt = 0x20000000;
173
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);
180
181     IDispatch_AddRef(disp);
182     timer->disp = disp;
183
184     if(queue_timer(thread_data, timer))
185         SetTimer(thread_data->thread_hwnd, TIMER_ID, msec, NULL);
186
187     return timer->id;
188 }
189
190 HRESULT clear_task_timer(HTMLDocument *doc, BOOL interval, DWORD id)
191 {
192     thread_data_t *thread_data = get_thread_data(FALSE);
193     task_timer_t *iter;
194
195     if(!thread_data)
196         return S_OK;
197
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;
202         }
203     }
204
205     WARN("timet not found\n");
206     return S_OK;
207 }
208
209 static void call_timer_disp(IDispatch *disp)
210 {
211     DISPPARAMS dp = {NULL, NULL, 0, 0};
212     EXCEPINFO ei;
213     VARIANT res;
214     HRESULT hres;
215
216     V_VT(&res) = VT_EMPTY;
217     memset(&ei, 0, sizeof(ei));
218
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);
225
226     VariantClear(&res);
227 }
228
229 static LRESULT process_timer(void)
230 {
231     thread_data_t *thread_data = get_thread_data(TRUE);
232     IDispatch *disp;
233     DWORD tc;
234     task_timer_t *timer;
235
236     TRACE("\n");
237
238     while(!list_empty(&thread_data->timer_list)) {
239         timer = LIST_ENTRY(list_head(&thread_data->timer_list), task_timer_t, entry);
240
241         tc = GetTickCount();
242         if(timer->time > tc) {
243             SetTimer(thread_data->thread_hwnd, TIMER_ID, timer->time-tc, NULL);
244             return 0;
245         }
246
247         disp = timer->disp;
248         IDispatch_AddRef(disp);
249
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);
255         }
256
257         call_timer_disp(disp);
258
259         IDispatch_Release(disp);
260     }
261
262     KillTimer(thread_data->thread_hwnd, TIMER_ID);
263     return 0;
264 }
265
266 static LRESULT WINAPI hidden_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
267 {
268     switch(msg) {
269     case WM_PROCESSTASK:
270         while(1) {
271             task_t *task = pop_task();
272             if(!task)
273                 break;
274
275             task->proc(task);
276             task->destr(task);
277         }
278
279         return 0;
280     case WM_TIMER:
281         return process_timer();
282     }
283
284     if(msg > WM_USER)
285         FIXME("(%p %d %lx %lx)\n", hwnd, msg, wParam, lParam);
286
287     return DefWindowProcW(hwnd, msg, wParam, lParam);
288 }
289
290 static HWND create_thread_hwnd(void)
291 {
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};
295
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
303         };
304
305         hidden_wnd_class = RegisterClassExW(&wndclass);
306     }
307
308     return CreateWindowExW(0, wszInternetExplorer_Hidden, NULL, WS_POPUP,
309                            0, 0, 0, 0, NULL, NULL, hInst, NULL);
310 }
311
312 HWND get_thread_hwnd(void)
313 {
314     thread_data_t *thread_data = get_thread_data(TRUE);
315
316     if(!thread_data->thread_hwnd)
317         thread_data->thread_hwnd = create_thread_hwnd();
318
319     return thread_data->thread_hwnd;
320 }
321
322 thread_data_t *get_thread_data(BOOL create)
323 {
324     thread_data_t *thread_data;
325
326     if(mshtml_tls == TLS_OUT_OF_INDEXES) {
327         DWORD tls;
328
329         if(!create)
330             return NULL;
331
332         tls = TlsAlloc();
333         if(tls == TLS_OUT_OF_INDEXES)
334             return NULL;
335
336         tls = InterlockedCompareExchange((LONG*)&mshtml_tls, tls, TLS_OUT_OF_INDEXES);
337         if(tls != mshtml_tls)
338             TlsFree(tls);
339     }
340
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);
346     }
347
348     return thread_data;
349 }