Release 941017
[wine/gsoc-2012-control.git] / loader / task.c
blobd43e4048b837a2667ddd927bc34e914db5051f0c
1 /*
2 * Tasks functions
3 */
4 static char Copyright[] = "Copyright Martin Ayotte, 1994";
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include "windows.h"
13 #include "wine.h"
14 #include "task.h"
15 #include "debug.h"
18 /* #define DEBUG_TASK /* */
19 /* #undef DEBUG_TASK /* */
22 static LPWINETASKENTRY lpTaskList = NULL;
23 static int nTaskCount = 0;
25 /**********************************************************************
26 * GetCurrentTask [KERNEL.36]
28 HTASK GetCurrentTask()
30 LPWINETASKENTRY lpTask = lpTaskList;
31 int pid = getpid();
32 #ifdef DEBUG_TASK
33 fprintf(stddeb,"GetCurrentTask() // unix_pid=%08X !\n", pid);
34 #endif
35 if (lpTask == NULL) return 0;
36 while (TRUE) {
37 if (lpTask->unix_pid == pid) break;
38 if (lpTask->lpNextTask == NULL) return 0;
39 lpTask = lpTask->lpNextTask;
41 #ifdef DEBUG_TASK
42 fprintf(stddeb,"GetCurrentTask() returned hTask=%04X !\n", lpTask->te.hTask);
43 #endif
44 return lpTask->te.hTask;
48 /**********************************************************************
49 * GetNumTasks [KERNEL.152]
51 WORD GetNumTasks()
53 #ifdef DEBUG_TASK
54 fprintf(stddeb,"GetNumTasks() returned %d !\n", nTaskCount);
55 #endif
56 return nTaskCount;
60 /**********************************************************************
61 * GetWindowTask [USER.224]
63 HTASK GetWindowTask(HWND hWnd)
65 HWND *wptr;
66 int count;
67 LPWINETASKENTRY lpTask = lpTaskList;
68 #ifdef DEBUG_TASK
69 fprintf(stddeb,"GetWindowTask(%04X) !\n", hWnd);
70 #endif
71 while (lpTask != NULL) {
72 wptr = lpTask->lpWndList;
73 if (wptr != NULL) {
74 count = 0;
75 while (++count < MAXWIN_PER_TASK) {
76 #ifdef DEBUG_TASK
77 fprintf(stddeb,"GetWindowTask // searching %04X %04X !\n",
78 lpTask->te.hTask, *(wptr));
79 #endif
80 if (*(wptr) == hWnd) {
81 #ifdef DEBUG_TASK
82 fprintf(stddeb,"GetWindowTask(%04X) found hTask=%04X !\n",
83 hWnd, lpTask->te.hTask);
84 #endif
85 return lpTask->te.hTask;
87 wptr++;
90 lpTask = lpTask->lpNextTask;
92 return 0;
96 /**********************************************************************
97 * EnumTaskWindows [USER.225]
99 BOOL EnumTaskWindows(HANDLE hTask, FARPROC lpEnumFunc, LONG lParam)
101 HWND *wptr, hWnd;
102 BOOL bRet;
103 int count = 0;
104 LPWINETASKENTRY lpTask = lpTaskList;
105 #ifdef DEBUG_TASK
106 fprintf(stddeb,"EnumTaskWindows(%04X, %08X, %08X) !\n", hTask, lpEnumFunc, lParam);
107 #endif
108 while (TRUE) {
109 if (lpTask->te.hTask == hTask) break;
110 if (lpTask == NULL) {
111 #ifdef DEBUG_TASK
112 fprintf(stddeb,"EnumTaskWindows // hTask=%04X not found !\n", hTask);
113 #endif
114 return FALSE;
116 lpTask = lpTask->lpNextTask;
118 #ifdef DEBUG_TASK
119 fprintf(stddeb,"EnumTaskWindows // found hTask=%04X !\n", hTask);
120 #endif
121 wptr = lpTask->lpWndList;
122 if (wptr == NULL) return FALSE;
123 if (lpEnumFunc == NULL) return FALSE;
124 while ((hWnd = *(wptr++)) != 0) {
125 if (++count >= MAXWIN_PER_TASK) return FALSE;
126 #ifdef DEBUG_TASK
127 fprintf(stddeb,"EnumTaskWindows // hWnd=%04X count=%d !\n", hWnd, count);
128 #endif
129 #ifdef WINELIB
130 bRet = (*lpEnumFunc)(hWnd, lParam);
131 #else
132 bRet = CallBack16(lpEnumFunc, 2, 0, (int)hWnd, 2, (int)lParam);
133 #endif
134 if (bRet == 0) break;
136 return TRUE;
140 /**********************************************************************
141 * CreateNewTask [internal]
143 HANDLE CreateNewTask(HINSTANCE hInst, HTASK hTaskParent)
145 HANDLE hTask;
146 LPWINETASKENTRY lpTask = lpTaskList;
147 LPWINETASKENTRY lpNewTask;
148 MODULEENTRY module;
150 module.dwSize = sizeof(module);
151 ModuleFindHandle(&module, hInst);
153 if (lpTask != NULL) {
154 while (TRUE) {
155 if (lpTask->lpNextTask == NULL) break;
156 lpTask = lpTask->lpNextTask;
159 hTask = GlobalAlloc(GMEM_MOVEABLE, sizeof(WINETASKENTRY));
160 lpNewTask = (LPWINETASKENTRY) GlobalLock(hTask);
161 #ifdef DEBUG_TASK
162 fprintf(stddeb,"CreateNewTask entry allocated %08X\n", lpNewTask);
163 #endif
164 if (lpNewTask == NULL) return 0;
165 if (lpTaskList == NULL) {
166 lpTaskList = lpNewTask;
167 lpNewTask->lpPrevTask = NULL;
169 else {
170 lpTask->lpNextTask = lpNewTask;
171 lpTask->te.hNext = lpNewTask->te.hTask;
172 lpNewTask->lpPrevTask = lpTask;
174 lpNewTask->lpNextTask = NULL;
175 lpNewTask->hIcon = 0;
176 lpNewTask->te.dwSize = sizeof(TASKENTRY);
177 lpNewTask->te.hModule = 0;
178 lpNewTask->te.hInst = hInst;
179 lpNewTask->te.hTask = hTask;
180 lpNewTask->te.hTaskParent = hTaskParent;
181 lpNewTask->te.wSS = 0;
182 lpNewTask->te.wSP = 0;
183 lpNewTask->te.wStackTop = 0;
184 lpNewTask->te.wStackMinimum = 0;
185 lpNewTask->te.wStackBottom = 0;
186 lpNewTask->te.wcEvents = 0;
187 lpNewTask->te.hQueue = 0;
188 strcpy(lpNewTask->te.szModule, module.szModule);
189 lpNewTask->te.wPSPOffset = 0;
190 lpNewTask->unix_pid = getpid();
191 lpNewTask->lpWndList = (HWND *) malloc(MAXWIN_PER_TASK * sizeof(HWND));
192 if (lpNewTask->lpWndList != NULL)
193 memset((LPSTR)lpNewTask->lpWndList, 0, MAXWIN_PER_TASK * sizeof(HWND));
194 #ifdef DEBUG_TASK
195 fprintf(stddeb,"CreateNewTask // unix_pid=%08X return hTask=%04X\n",
196 lpNewTask->unix_pid, hTask);
197 #endif
198 GlobalUnlock(hTask);
199 nTaskCount++;
200 return hTask;
204 /**********************************************************************
205 * AddWindowToTask [internal]
207 BOOL AddWindowToTask(HTASK hTask, HWND hWnd)
209 HWND *wptr;
210 int count = 0;
211 LPWINETASKENTRY lpTask = lpTaskList;
212 #ifdef DEBUG_TASK
213 fprintf(stddeb,"AddWindowToTask(%04X, %04X); !\n", hTask, hWnd);
214 #endif
215 while (TRUE) {
216 if (lpTask->te.hTask == hTask) break;
217 if (lpTask == NULL) {
218 fprintf(stderr,"AddWindowToTask // hTask=%04X not found !\n", hTask);
219 return FALSE;
221 lpTask = lpTask->lpNextTask;
223 wptr = lpTask->lpWndList;
224 if (wptr == NULL) return FALSE;
225 while (*(wptr) != 0) {
226 if (++count >= MAXWIN_PER_TASK) return FALSE;
227 wptr++;
229 *wptr = hWnd;
230 #ifdef DEBUG_TASK
231 fprintf(stddeb,"AddWindowToTask // window added, count=%d !\n", count);
232 #endif
233 return TRUE;
237 /**********************************************************************
238 * RemoveWindowFromTask [internal]
240 BOOL RemoveWindowFromTask(HTASK hTask, HWND hWnd)
242 HWND *wptr;
243 int count = 0;
244 LPWINETASKENTRY lpTask = lpTaskList;
245 #ifdef DEBUG_TASK
246 fprintf(stddeb,"RemoveWindowToTask(%04X, %04X); !\n", hTask, hWnd);
247 #endif
248 while (TRUE) {
249 if (lpTask->te.hTask == hTask) break;
250 if (lpTask == NULL) {
251 fprintf(stderr,"RemoveWindowFromTask // hTask=%04X not found !\n", hTask);
252 return FALSE;
254 lpTask = lpTask->lpNextTask;
256 wptr = lpTask->lpWndList;
257 if (wptr == NULL) return FALSE;
258 while (*(wptr) != hWnd) {
259 if (++count >= MAXWIN_PER_TASK) return FALSE;
260 wptr++;
262 while (*(wptr) != 0) {
263 *(wptr) = *(wptr + 1);
264 if (++count >= MAXWIN_PER_TASK) return FALSE;
265 wptr++;
267 #ifdef DEBUG_TASK
268 fprintf(stddeb,"RemoveWindowFromTask // window removed, count=%d !\n", --count);
269 #endif
270 return TRUE;
273 BOOL TaskFirst(LPTASKENTRY lpTask)
275 #ifdef DEBUG_TASK
276 fprintf(stddeb,"TaskFirst(%8x)\n", (int) lpTask);
277 #endif
278 if (lpTaskList) {
279 memcpy(lpTask, &lpTaskList->te, lpTask->dwSize);
280 return TRUE;
281 } else
282 return FALSE;
285 BOOL TaskNext(LPTASKENTRY lpTask)
287 LPWINETASKENTRY list;
288 #ifdef DEBUG_TASK
289 fprintf(stddeb,"TaskNext(%8x)\n", (int) lpTask);
290 #endif
291 list = lpTaskList;
292 while (list) {
293 if (list->te.hTask == lpTask->hTask) {
294 list = list->lpNextTask;
295 if (list) {
296 memcpy(lpTask, &list->te, lpTask->dwSize);
297 return TRUE;
298 } else
299 return FALSE;
301 list = list->lpNextTask;
303 return FALSE;
306 BOOL TaskFindHandle(LPTASKENTRY lpTask, HTASK hTask)
308 static LPWINETASKENTRY list;
309 #ifdef DEBUG_TASK
310 fprintf(stddeb,"TaskFindHandle(%8x,%4x)\n", (int) lpTask, hTask);
311 #endif
312 list = lpTaskList;
313 while (list) {
314 if (list->te.hTask == hTask) {
315 list = list->lpNextTask;
316 if (list) {
317 memcpy(lpTask, &list->te, lpTask->dwSize);
318 return TRUE;
319 } else
320 return FALSE;
322 list = list->lpNextTask;
324 return FALSE;