Release 941030
[wine/multimedia.git] / loader / task.c
blob693612fbe5744fd3070079b692f2965393d1e327
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 "stddebug.h"
16 /* #define DEBUG_TASK */
17 /* #undef DEBUG_TASK */
18 #include "debug.h"
21 static LPWINETASKENTRY lpTaskList = NULL;
22 static int nTaskCount = 0;
24 /**********************************************************************
25 * GetCurrentTask [KERNEL.36]
27 HTASK GetCurrentTask()
29 LPWINETASKENTRY lpTask = lpTaskList;
30 int pid = getpid();
31 dprintf_task(stddeb,"GetCurrentTask() // unix_pid=%08X !\n", pid);
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 dprintf_task(stddeb,"GetCurrentTask() returned hTask=%04X !\n", lpTask->te.hTask);
39 return lpTask->te.hTask;
43 /**********************************************************************
44 * GetNumTasks [KERNEL.152]
46 WORD GetNumTasks()
48 dprintf_task(stddeb,"GetNumTasks() returned %d !\n", nTaskCount);
49 return nTaskCount;
53 /**********************************************************************
54 * GetWindowTask [USER.224]
56 HTASK GetWindowTask(HWND hWnd)
58 HWND *wptr;
59 int count;
60 LPWINETASKENTRY lpTask = lpTaskList;
61 dprintf_task(stddeb,"GetWindowTask(%04X) !\n", hWnd);
62 while (lpTask != NULL) {
63 wptr = lpTask->lpWndList;
64 if (wptr != NULL) {
65 count = 0;
66 while (++count < MAXWIN_PER_TASK) {
67 dprintf_task(stddeb,"GetWindowTask // searching %04X %04X !\n",
68 lpTask->te.hTask, *(wptr));
69 if (*(wptr) == hWnd) {
70 dprintf_task(stddeb,"GetWindowTask(%04X) found hTask=%04X !\n",
71 hWnd, lpTask->te.hTask);
72 return lpTask->te.hTask;
74 wptr++;
77 lpTask = lpTask->lpNextTask;
79 return 0;
83 /**********************************************************************
84 * EnumTaskWindows [USER.225]
86 BOOL EnumTaskWindows(HANDLE hTask, FARPROC lpEnumFunc, LONG lParam)
88 HWND *wptr, hWnd;
89 BOOL bRet;
90 int count = 0;
91 LPWINETASKENTRY lpTask = lpTaskList;
92 dprintf_task(stddeb,"EnumTaskWindows(%04X, %08X, %08X) !\n", hTask, lpEnumFunc, lParam);
93 while (TRUE) {
94 if (lpTask->te.hTask == hTask) break;
95 if (lpTask == NULL) {
96 dprintf_task(stddeb,"EnumTaskWindows // hTask=%04X not found !\n", hTask);
97 return FALSE;
99 lpTask = lpTask->lpNextTask;
101 dprintf_task(stddeb,"EnumTaskWindows // found hTask=%04X !\n", hTask);
102 wptr = lpTask->lpWndList;
103 if (wptr == NULL) return FALSE;
104 if (lpEnumFunc == NULL) return FALSE;
105 while ((hWnd = *(wptr++)) != 0) {
106 if (++count >= MAXWIN_PER_TASK) return FALSE;
107 dprintf_task(stddeb,"EnumTaskWindows // hWnd=%04X count=%d !\n", hWnd, count);
108 #ifdef WINELIB
109 bRet = (*lpEnumFunc)(hWnd, lParam);
110 #else
111 bRet = CallBack16(lpEnumFunc, 2, 0, (int)hWnd, 2, (int)lParam);
112 #endif
113 if (bRet == 0) break;
115 return TRUE;
119 /**********************************************************************
120 * CreateNewTask [internal]
122 HANDLE CreateNewTask(HINSTANCE hInst, HTASK hTaskParent)
124 HANDLE hTask;
125 LPWINETASKENTRY lpTask = lpTaskList;
126 LPWINETASKENTRY lpNewTask;
127 MODULEENTRY module;
129 module.dwSize = sizeof(module);
130 ModuleFindHandle(&module, hInst);
132 if (lpTask != NULL) {
133 while (TRUE) {
134 if (lpTask->lpNextTask == NULL) break;
135 lpTask = lpTask->lpNextTask;
138 hTask = GlobalAlloc(GMEM_MOVEABLE, sizeof(WINETASKENTRY));
139 lpNewTask = (LPWINETASKENTRY) GlobalLock(hTask);
140 dprintf_task(stddeb,"CreateNewTask entry allocated %08X\n", lpNewTask);
141 if (lpNewTask == NULL) return 0;
142 if (lpTaskList == NULL) {
143 lpTaskList = lpNewTask;
144 lpNewTask->lpPrevTask = NULL;
146 else {
147 lpTask->lpNextTask = lpNewTask;
148 lpTask->te.hNext = lpNewTask->te.hTask;
149 lpNewTask->lpPrevTask = lpTask;
151 lpNewTask->lpNextTask = NULL;
152 lpNewTask->hIcon = 0;
153 lpNewTask->te.dwSize = sizeof(TASKENTRY);
154 lpNewTask->te.hModule = 0;
155 lpNewTask->te.hInst = hInst;
156 lpNewTask->te.hTask = hTask;
157 lpNewTask->te.hTaskParent = hTaskParent;
158 lpNewTask->te.wSS = 0;
159 lpNewTask->te.wSP = 0;
160 lpNewTask->te.wStackTop = 0;
161 lpNewTask->te.wStackMinimum = 0;
162 lpNewTask->te.wStackBottom = 0;
163 lpNewTask->te.wcEvents = 0;
164 lpNewTask->te.hQueue = 0;
165 strcpy(lpNewTask->te.szModule, module.szModule);
166 lpNewTask->te.wPSPOffset = 0;
167 lpNewTask->unix_pid = getpid();
168 lpNewTask->lpWndList = (HWND *) malloc(MAXWIN_PER_TASK * sizeof(HWND));
169 if (lpNewTask->lpWndList != NULL)
170 memset((LPSTR)lpNewTask->lpWndList, 0, MAXWIN_PER_TASK * sizeof(HWND));
171 dprintf_task(stddeb,"CreateNewTask // unix_pid=%08X return hTask=%04X\n",
172 lpNewTask->unix_pid, hTask);
173 GlobalUnlock(hTask);
174 nTaskCount++;
175 return hTask;
179 /**********************************************************************
180 * AddWindowToTask [internal]
182 BOOL AddWindowToTask(HTASK hTask, HWND hWnd)
184 HWND *wptr;
185 int count = 0;
186 LPWINETASKENTRY lpTask = lpTaskList;
187 dprintf_task(stddeb,"AddWindowToTask(%04X, %04X); !\n", hTask, hWnd);
188 while (TRUE) {
189 if (lpTask->te.hTask == hTask) break;
190 if (lpTask == NULL) {
191 fprintf(stderr,"AddWindowToTask // hTask=%04X not found !\n", hTask);
192 return FALSE;
194 lpTask = lpTask->lpNextTask;
196 wptr = lpTask->lpWndList;
197 if (wptr == NULL) return FALSE;
198 while (*(wptr) != 0) {
199 if (++count >= MAXWIN_PER_TASK) return FALSE;
200 wptr++;
202 *wptr = hWnd;
203 dprintf_task(stddeb,"AddWindowToTask // window added, count=%d !\n", count);
204 return TRUE;
208 /**********************************************************************
209 * RemoveWindowFromTask [internal]
211 BOOL RemoveWindowFromTask(HTASK hTask, HWND hWnd)
213 HWND *wptr;
214 int count = 0;
215 LPWINETASKENTRY lpTask = lpTaskList;
216 dprintf_task(stddeb,"RemoveWindowFromTask (%04X, %04X); !\n", hTask, hWnd);
217 while (TRUE) {
218 if (lpTask->te.hTask == hTask) break;
219 if (lpTask == NULL) {
220 fprintf(stderr,"RemoveWindowFromTask // hTask=%04X not found !\n", hTask);
221 return FALSE;
223 lpTask = lpTask->lpNextTask;
225 wptr = lpTask->lpWndList;
226 if (wptr == NULL) return FALSE;
227 while (*(wptr) != hWnd) {
228 if (++count >= MAXWIN_PER_TASK) return FALSE;
229 wptr++;
231 while (*(wptr) != 0) {
232 *(wptr) = *(wptr + 1);
233 if (++count >= MAXWIN_PER_TASK) return FALSE;
234 wptr++;
236 dprintf_task(stddeb,"RemoveWindowFromTask // window removed, count=%d !\n", --count);
237 return TRUE;
240 BOOL TaskFirst(LPTASKENTRY lpTask)
242 dprintf_task(stddeb,"TaskFirst(%8x)\n", (int) lpTask);
243 if (lpTaskList) {
244 memcpy(lpTask, &lpTaskList->te, lpTask->dwSize);
245 return TRUE;
246 } else
247 return FALSE;
250 BOOL TaskNext(LPTASKENTRY lpTask)
252 LPWINETASKENTRY list;
253 dprintf_task(stddeb,"TaskNext(%8x)\n", (int) lpTask);
254 list = lpTaskList;
255 while (list) {
256 if (list->te.hTask == lpTask->hTask) {
257 list = list->lpNextTask;
258 if (list) {
259 memcpy(lpTask, &list->te, lpTask->dwSize);
260 return TRUE;
261 } else
262 return FALSE;
264 list = list->lpNextTask;
266 return FALSE;
269 BOOL TaskFindHandle(LPTASKENTRY lpTask, HTASK hTask)
271 static LPWINETASKENTRY list;
272 dprintf_task(stddeb,"TaskFindHandle(%8x,%4x)\n", (int) lpTask, hTask);
273 list = lpTaskList;
274 while (list) {
275 if (list->te.hTask == hTask) {
276 list = list->lpNextTask;
277 if (list) {
278 memcpy(lpTask, &list->te, lpTask->dwSize);
279 return TRUE;
280 } else
281 return FALSE;
283 list = list->lpNextTask;
285 return FALSE;