Release 950122
[wine/multimedia.git] / loader / task.c
blobde42cc3ebf6a92f3b0e064c4b7c6f30e0e633338
1 /*
2 * Tasks functions
4 static char Copyright[] = "Copyright Martin Ayotte, 1994";
5 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #include "windows.h"
12 #include "if1632.h"
13 #include "task.h"
14 #include "stddebug.h"
15 #include "debug.h"
17 static LPWINETASKENTRY lpTaskList = NULL;
18 static int nTaskCount = 0;
20 /**********************************************************************
21 * GetCurrentTask [KERNEL.36]
23 HTASK GetCurrentTask(void)
25 LPWINETASKENTRY lpTask = lpTaskList;
26 int pid = getpid();
27 dprintf_task(stddeb,"GetCurrentTask() // unix_pid=%08X !\n", pid);
28 if (lpTask == NULL) return 0;
29 while (TRUE) {
30 if (lpTask->unix_pid == pid) break;
31 if (lpTask->lpNextTask == NULL) return 0;
32 lpTask = lpTask->lpNextTask;
34 dprintf_task(stddeb,"GetCurrentTask() returned hTask=%04X !\n", lpTask->te.hTask);
35 return lpTask->te.hTask;
39 /**********************************************************************
40 * GetNumTasks [KERNEL.152]
42 WORD GetNumTasks(void)
44 dprintf_task(stddeb,"GetNumTasks() returned %d !\n", nTaskCount);
45 return nTaskCount;
49 /**********************************************************************
50 * GetWindowTask [USER.224]
52 HTASK GetWindowTask(HWND hWnd)
54 HWND *wptr;
55 int count;
56 LPWINETASKENTRY lpTask = lpTaskList;
57 dprintf_task(stddeb,"GetWindowTask(%04X) !\n", hWnd);
58 while (lpTask != NULL) {
59 wptr = lpTask->lpWndList;
60 if (wptr != NULL) {
61 count = 0;
62 while (++count < MAXWIN_PER_TASK) {
63 dprintf_task(stddeb,"GetWindowTask // searching %04X %04X !\n",
64 lpTask->te.hTask, *(wptr));
65 if (*(wptr) == hWnd) {
66 dprintf_task(stddeb,"GetWindowTask(%04X) found hTask=%04X !\n",
67 hWnd, lpTask->te.hTask);
68 return lpTask->te.hTask;
70 wptr++;
73 lpTask = lpTask->lpNextTask;
75 return 0;
79 /**********************************************************************
80 * EnumTaskWindows [USER.225]
82 BOOL EnumTaskWindows(HANDLE hTask, FARPROC lpEnumFunc, LONG lParam)
84 HWND *wptr, hWnd;
85 BOOL bRet;
86 int count = 0;
87 LPWINETASKENTRY lpTask = lpTaskList;
88 dprintf_task(stddeb,"EnumTaskWindows(%04X, %08X, %08X) !\n", hTask,
89 (unsigned int) lpEnumFunc, (unsigned int) lParam);
90 while (TRUE) {
91 if (lpTask->te.hTask == hTask) break;
92 if (lpTask == NULL) {
93 dprintf_task(stddeb,"EnumTaskWindows // hTask=%04X not found !\n", hTask);
94 return FALSE;
96 lpTask = lpTask->lpNextTask;
98 dprintf_task(stddeb,"EnumTaskWindows // found hTask=%04X !\n", hTask);
99 wptr = lpTask->lpWndList;
100 if (wptr == NULL) return FALSE;
101 if (lpEnumFunc == NULL) return FALSE;
102 while ((hWnd = *(wptr++)) != 0) {
103 if (++count >= MAXWIN_PER_TASK) return FALSE;
104 dprintf_task(stddeb,"EnumTaskWindows // hWnd=%04X count=%d !\n", hWnd, count);
105 #ifdef WINELIB
106 bRet = (*lpEnumFunc)(hWnd, lParam);
107 #else
108 bRet = CallBack16(lpEnumFunc, 2, 0, (int)hWnd, 2, (int)lParam);
109 #endif
110 if (bRet == 0) break;
112 return TRUE;
116 /**********************************************************************
117 * CreateNewTask [internal]
119 HANDLE CreateNewTask(HINSTANCE hInst, HTASK hTaskParent)
121 HANDLE hTask;
122 LPWINETASKENTRY lpTask = lpTaskList;
123 LPWINETASKENTRY lpNewTask;
124 MODULEENTRY module;
126 module.dwSize = sizeof(module);
127 ModuleFindHandle(&module, hInst);
129 if (lpTask != NULL) {
130 while (TRUE) {
131 if (lpTask->lpNextTask == NULL) break;
132 lpTask = lpTask->lpNextTask;
135 hTask = GlobalAlloc(GMEM_MOVEABLE, sizeof(WINETASKENTRY));
136 lpNewTask = (LPWINETASKENTRY) GlobalLock(hTask);
137 dprintf_task(stddeb,"CreateNewTask entry allocated %p\n", lpNewTask);
138 if (lpNewTask == NULL) return 0;
139 if (lpTaskList == NULL) {
140 lpTaskList = lpNewTask;
141 lpNewTask->lpPrevTask = NULL;
143 else {
144 lpTask->lpNextTask = lpNewTask;
145 lpTask->te.hNext = lpNewTask->te.hTask;
146 lpNewTask->lpPrevTask = lpTask;
148 lpNewTask->lpNextTask = NULL;
149 lpNewTask->hIcon = 0;
150 lpNewTask->te.dwSize = sizeof(TASKENTRY);
151 lpNewTask->te.hModule = 0;
152 lpNewTask->te.hInst = hInst;
153 lpNewTask->te.hTask = hTask;
154 lpNewTask->te.hTaskParent = hTaskParent;
155 lpNewTask->te.wSS = 0;
156 lpNewTask->te.wSP = 0;
157 lpNewTask->te.wStackTop = 0;
158 lpNewTask->te.wStackMinimum = 0;
159 lpNewTask->te.wStackBottom = 0;
160 lpNewTask->te.wcEvents = 0;
161 lpNewTask->te.hQueue = 0;
162 strcpy(lpNewTask->te.szModule, module.szModule);
163 lpNewTask->te.wPSPOffset = 0;
164 lpNewTask->unix_pid = getpid();
165 lpNewTask->lpWndList = (HWND *) malloc(MAXWIN_PER_TASK * sizeof(HWND));
166 if (lpNewTask->lpWndList != NULL)
167 memset((LPSTR)lpNewTask->lpWndList, 0, MAXWIN_PER_TASK * sizeof(HWND));
168 dprintf_task(stddeb,"CreateNewTask // unix_pid=%08X return hTask=%04X\n",
169 lpNewTask->unix_pid, hTask);
170 GlobalUnlock(hTask);
171 nTaskCount++;
172 return hTask;
176 /**********************************************************************
177 * AddWindowToTask [internal]
179 BOOL AddWindowToTask(HTASK hTask, HWND hWnd)
181 HWND *wptr;
182 int count = 0;
183 LPWINETASKENTRY lpTask = lpTaskList;
184 dprintf_task(stddeb,"AddWindowToTask(%04X, %04X); !\n", hTask, hWnd);
185 while (TRUE) {
186 if (lpTask->te.hTask == hTask) break;
187 if (lpTask == NULL) {
188 fprintf(stderr,"AddWindowToTask // hTask=%04X not found !\n", hTask);
189 return FALSE;
191 lpTask = lpTask->lpNextTask;
193 wptr = lpTask->lpWndList;
194 if (wptr == NULL) return FALSE;
195 while (*(wptr) != 0) {
196 if (++count >= MAXWIN_PER_TASK) return FALSE;
197 wptr++;
199 *wptr = hWnd;
200 dprintf_task(stddeb,"AddWindowToTask // window added, count=%d !\n", count);
201 return TRUE;
205 /**********************************************************************
206 * RemoveWindowFromTask [internal]
208 BOOL RemoveWindowFromTask(HTASK hTask, HWND hWnd)
210 HWND *wptr;
211 int count = 0;
212 LPWINETASKENTRY lpTask = lpTaskList;
213 dprintf_task(stddeb,"RemoveWindowFromTask (%04X, %04X); !\n", hTask, hWnd);
214 while (TRUE) {
215 if (lpTask->te.hTask == hTask) break;
216 if (lpTask == NULL) {
217 fprintf(stderr,"RemoveWindowFromTask // hTask=%04X not found !\n", hTask);
218 return FALSE;
220 lpTask = lpTask->lpNextTask;
222 wptr = lpTask->lpWndList;
223 if (wptr == NULL) return FALSE;
224 while (*(wptr) != hWnd) {
225 if (++count >= MAXWIN_PER_TASK) return FALSE;
226 wptr++;
228 while (*(wptr) != 0) {
229 *(wptr) = *(wptr + 1);
230 if (++count >= MAXWIN_PER_TASK) return FALSE;
231 wptr++;
233 dprintf_task(stddeb,"RemoveWindowFromTask // window removed, count=%d !\n", --count);
234 return TRUE;
237 BOOL TaskFirst(LPTASKENTRY lpTask)
239 dprintf_task(stddeb,"TaskFirst(%8x)\n", (int) lpTask);
240 if (lpTaskList) {
241 memcpy(lpTask, &lpTaskList->te, lpTask->dwSize);
242 return TRUE;
243 } else
244 return FALSE;
247 BOOL TaskNext(LPTASKENTRY lpTask)
249 LPWINETASKENTRY list;
250 dprintf_task(stddeb,"TaskNext(%8x)\n", (int) lpTask);
251 list = lpTaskList;
252 while (list) {
253 if (list->te.hTask == lpTask->hTask) {
254 list = list->lpNextTask;
255 if (list) {
256 memcpy(lpTask, &list->te, lpTask->dwSize);
257 return TRUE;
258 } else
259 return FALSE;
261 list = list->lpNextTask;
263 return FALSE;
266 BOOL TaskFindHandle(LPTASKENTRY lpTask, HTASK hTask)
268 static LPWINETASKENTRY list;
269 dprintf_task(stddeb,"TaskFindHandle(%8x,%4x)\n", (int) lpTask, hTask);
270 list = lpTaskList;
271 while (list) {
272 if (list->te.hTask == hTask) {
273 list = list->lpNextTask;
274 if (list) {
275 memcpy(lpTask, &list->te, lpTask->dwSize);
276 return TRUE;
277 } else
278 return FALSE;
280 list = list->lpNextTask;
282 return FALSE;