Release 940815
[wine/gsoc-2012-control.git] / loader / task.c
blobd2276400ff1ac0722c6f1d97914ed88fa77d3d84
1 /*
2 * Tasks functions
3 */
4 static char Copyright[] = "Copyright Martin Ayotte, 1994";
6 /*
7 #define DEBUG_TASK
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include "windows.h"
16 #include "wine.h"
17 #include "task.h"
19 static LPWINETASKENTRY lpTaskList = NULL;
20 static int nTaskCount = 0;
22 /**********************************************************************
23 * GetCurrentTask [KERNEL.36]
25 HTASK GetCurrentTask()
27 LPWINETASKENTRY lpTask = lpTaskList;
28 int pid = getpid();
29 #ifdef DEBUG_TASK
30 printf("GetCurrentTask() // unix_pid=%08X !\n", pid);
31 #endif
32 if (lpTask == NULL) return 0;
33 while (TRUE) {
34 if (lpTask->unix_pid == pid) break;
35 if (lpTask->lpNextTask == NULL) return 0;
36 lpTask = lpTask->lpNextTask;
38 #ifdef DEBUG_TASK
39 printf("GetCurrentTask() returned hTask=%04X !\n", lpTask->te.hTask);
40 #endif
41 return lpTask->te.hTask;
45 /**********************************************************************
46 * GetNumTasks [KERNEL.152]
48 WORD GetNumTasks()
50 printf("GetNumTasks() returned %d !\n", nTaskCount);
51 return nTaskCount;
55 /**********************************************************************
56 * GetWindowTask [USER.224]
58 HTASK GetWindowTask(HWND hWnd)
60 HWND *wptr;
61 int count;
62 LPWINETASKENTRY lpTask = lpTaskList;
63 printf("GetWindowTask(%04X) !\n", hWnd);
64 while (lpTask != NULL) {
65 wptr = lpTask->lpWndList;
66 if (wptr != NULL) {
67 count = 0;
68 while (++count < MAXWIN_PER_TASK) {
69 printf("GetWindowTask // searching %04X %04X !\n",
70 lpTask->te.hTask, *(wptr));
71 if (*(wptr) == hWnd) {
72 printf("GetWindowTask(%04X) found hTask=%04X !\n",
73 hWnd, lpTask->te.hTask);
74 return lpTask->te.hTask;
76 wptr++;
79 lpTask = lpTask->lpNextTask;
81 return 0;
85 /**********************************************************************
86 * EnumTaskWindows [USER.225]
88 BOOL EnumTaskWindows(HANDLE hTask, FARPROC lpEnumFunc, LONG lParam)
90 HWND *wptr, hWnd;
91 BOOL bRet;
92 int count = 0;
93 LPWINETASKENTRY lpTask = lpTaskList;
94 printf("EnumTaskWindows(%04X, %08X, %08X) !\n", hTask, lpEnumFunc, lParam);
95 while (TRUE) {
96 if (lpTask->te.hTask == hTask) break;
97 if (lpTask == NULL) {
98 printf("EnumTaskWindows // hTask=%04X not found !\n", hTask);
99 return FALSE;
101 lpTask = lpTask->lpNextTask;
103 printf("EnumTaskWindows // found hTask=%04X !\n", hTask);
104 wptr = lpTask->lpWndList;
105 if (wptr == NULL) return FALSE;
106 if (lpEnumFunc == NULL) return FALSE;
107 while ((hWnd = *(wptr++)) != 0) {
108 if (++count >= MAXWIN_PER_TASK) return FALSE;
109 printf("EnumTaskWindows // hWnd=%04X count=%d !\n", hWnd, count);
110 #ifdef WINELIB
111 bRet = (*lpEnumFunc)(hWnd, lParam);
112 #else
113 bRet = CallBack16(lpEnumFunc, 2, 0, (int)hWnd, 2, (int)lParam);
114 #endif
115 if (bRet == 0) break;
117 return TRUE;
121 /**********************************************************************
122 * CreateNewTask [internal]
124 HANDLE CreateNewTask(HINSTANCE hInst, HTASK hTaskParent)
126 HANDLE hTask;
127 LPWINETASKENTRY lpTask = lpTaskList;
128 LPWINETASKENTRY lpNewTask;
129 MODULEENTRY module;
131 module.dwSize = sizeof(module);
132 ModuleFindHandle(&module, hInst);
134 if (lpTask != NULL) {
135 while (TRUE) {
136 if (lpTask->lpNextTask == NULL) break;
137 lpTask = lpTask->lpNextTask;
140 hTask = GlobalAlloc(GMEM_MOVEABLE, sizeof(WINETASKENTRY));
141 lpNewTask = (LPWINETASKENTRY) GlobalLock(hTask);
142 #ifdef DEBUG_TASK
143 printf("CreateNewTask entry allocated %08X\n", lpNewTask);
144 #endif
145 if (lpNewTask == NULL) return 0;
146 if (lpTaskList == NULL) {
147 lpTaskList = lpNewTask;
148 lpNewTask->lpPrevTask = NULL;
150 else {
151 lpTask->lpNextTask = lpNewTask;
152 lpTask->te.hNext = lpNewTask->te.hTask;
153 lpNewTask->lpPrevTask = lpTask;
155 lpNewTask->lpNextTask = NULL;
156 lpNewTask->hIcon = 0;
157 lpNewTask->te.dwSize = sizeof(TASKENTRY);
158 lpNewTask->te.hModule = 0;
159 lpNewTask->te.hInst = hInst;
160 lpNewTask->te.hTask = hTask;
161 lpNewTask->te.hTaskParent = hTaskParent;
162 lpNewTask->te.wSS = 0;
163 lpNewTask->te.wSP = 0;
164 lpNewTask->te.wStackTop = 0;
165 lpNewTask->te.wStackMinimum = 0;
166 lpNewTask->te.wStackBottom = 0;
167 lpNewTask->te.wcEvents = 0;
168 lpNewTask->te.hQueue = 0;
169 strcpy(lpNewTask->te.szModule, module.szModule);
170 lpNewTask->te.wPSPOffset = 0;
171 lpNewTask->unix_pid = getpid();
172 lpNewTask->lpWndList = (HWND *) malloc(MAXWIN_PER_TASK * sizeof(HWND));
173 if (lpNewTask->lpWndList != NULL)
174 memset((LPSTR)lpNewTask->lpWndList, 0, MAXWIN_PER_TASK * sizeof(HWND));
175 #ifdef DEBUG_TASK
176 printf("CreateNewTask // unix_pid=%08X return hTask=%04X\n",
177 lpNewTask->unix_pid, hTask);
178 #endif
179 GlobalUnlock(hTask);
180 nTaskCount++;
181 return hTask;
185 /**********************************************************************
186 * AddWindowToTask [internal]
188 BOOL AddWindowToTask(HTASK hTask, HWND hWnd)
190 HWND *wptr;
191 int count = 0;
192 LPWINETASKENTRY lpTask = lpTaskList;
193 #ifdef DEBUG_TASK
194 printf("AddWindowToTask(%04X, %04X); !\n", hTask, hWnd);
195 #endif
196 while (TRUE) {
197 if (lpTask->te.hTask == hTask) break;
198 if (lpTask == NULL) {
199 printf("AddWindowToTask // hTask=%04X not found !\n", hTask);
200 return FALSE;
202 lpTask = lpTask->lpNextTask;
204 wptr = lpTask->lpWndList;
205 if (wptr == NULL) return FALSE;
206 while (*(wptr) != 0) {
207 if (++count >= MAXWIN_PER_TASK) return FALSE;
208 wptr++;
210 *wptr = hWnd;
211 #ifdef DEBUG_TASK
212 printf("AddWindowToTask // window added, count=%d !\n", count);
213 #endif
214 return TRUE;
218 /**********************************************************************
219 * RemoveWindowFromTask [internal]
221 BOOL RemoveWindowFromTask(HTASK hTask, HWND hWnd)
223 HWND *wptr;
224 int count = 0;
225 LPWINETASKENTRY lpTask = lpTaskList;
226 #ifdef DEBUG_TASK
227 printf("RemoveWindowToTask(%04X, %04X); !\n", hTask, hWnd);
228 #endif
229 while (TRUE) {
230 if (lpTask->te.hTask == hTask) break;
231 if (lpTask == NULL) {
232 printf("RemoveWindowFromTask // hTask=%04X not found !\n", hTask);
233 return FALSE;
235 lpTask = lpTask->lpNextTask;
237 wptr = lpTask->lpWndList;
238 if (wptr == NULL) return FALSE;
239 while (*(wptr) != hWnd) {
240 if (++count >= MAXWIN_PER_TASK) return FALSE;
241 wptr++;
243 while (*(wptr) != 0) {
244 *(wptr) = *(wptr + 1);
245 if (++count >= MAXWIN_PER_TASK) return FALSE;
246 wptr++;
248 #ifdef DEBUG_TASK
249 printf("RemoveWindowFromTask // window removed, count=%d !\n", --count);
250 #endif
251 return TRUE;
254 BOOL TaskFirst(LPTASKENTRY lpTask)
256 printf("TaskFirst(%8x)\n", (int) lpTask);
258 if (lpTaskList) {
259 memcpy(lpTask, &lpTaskList->te, lpTask->dwSize);
260 return TRUE;
261 } else
262 return FALSE;
265 BOOL TaskNext(LPTASKENTRY lpTask)
267 LPWINETASKENTRY list;
269 printf("TaskNext(%8x)\n", (int) lpTask);
271 list = lpTaskList;
272 while (list) {
273 if (list->te.hTask == lpTask->hTask) {
274 list = list->lpNextTask;
275 if (list) {
276 memcpy(lpTask, &list->te, lpTask->dwSize);
277 return TRUE;
278 } else
279 return FALSE;
281 list = list->lpNextTask;
283 return FALSE;
286 BOOL TaskFindHandle(LPTASKENTRY lpTask, HTASK hTask)
288 static LPWINETASKENTRY list;
290 printf("TaskFindHandle(%8x,%4x)\n", (int) lpTask, hTask);
292 list = lpTaskList;
293 while (list) {
294 if (list->te.hTask == hTask) {
295 list = list->lpNextTask;
296 if (list) {
297 memcpy(lpTask, &list->te, lpTask->dwSize);
298 return TRUE;
299 } else
300 return FALSE;
302 list = list->lpNextTask;
304 return FALSE;