server: Don't round up the header size for image mappings.
[wine] / programs / taskmgr / affinity.c
1 /*
2  *  ReactOS Task Manager
3  *
4  *  affinity.c
5  *
6  *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22     
23 #define WIN32_LEAN_AND_MEAN    /* Exclude rarely-used stuff from Windows headers */
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <stdlib.h>
27 #include <malloc.h>
28 #include <memory.h>
29 #include <tchar.h>
30 #include <winnt.h>
31 #include <stdio.h>
32     
33 #include "taskmgr.h"
34 #include "perfdata.h"
35
36 HANDLE        hProcessAffinityHandle;
37
38 INT_PTR CALLBACK AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
39
40 void ProcessPage_OnSetAffinity(void)
41 {
42     LV_ITEM            lvitem;
43     ULONG            Index;
44     DWORD            dwProcessId;
45     TCHAR            strErrorText[260];
46
47     for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) {
48         memset(&lvitem, 0, sizeof(LV_ITEM));
49         lvitem.mask = LVIF_STATE;
50         lvitem.stateMask = LVIS_SELECTED;
51         lvitem.iItem = Index;
52         ListView_GetItem(hProcessPageListCtrl, &lvitem);
53         if (lvitem.state & LVIS_SELECTED)
54             break;
55     }
56     dwProcessId = PerfDataGetProcessId(Index);
57     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
58         return;
59     hProcessAffinityHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION, FALSE, dwProcessId);
60     if (!hProcessAffinityHandle) {
61         GetLastErrorText(strErrorText, 260);
62         MessageBox(hMainWnd, strErrorText, _T("Unable to Access or Set Process Affinity"), MB_OK|MB_ICONSTOP);
63         return;
64     }
65     DialogBox(hInst, MAKEINTRESOURCE(IDD_AFFINITY_DIALOG), hMainWnd, AffinityDialogWndProc);
66     if (hProcessAffinityHandle)    {
67         CloseHandle(hProcessAffinityHandle);
68         hProcessAffinityHandle = NULL;
69     }
70 }
71
72 INT_PTR CALLBACK
73 AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
74 {
75     DWORD    dwProcessAffinityMask = 0;
76     DWORD    dwSystemAffinityMask = 0;
77     TCHAR    strErrorText[260];
78
79     switch (message) {
80     case WM_INITDIALOG:
81
82         /*
83          * Get the current affinity mask for the process and
84          * the number of CPUs present in the system
85          */
86         if (!GetProcessAffinityMask(hProcessAffinityHandle, &dwProcessAffinityMask, &dwSystemAffinityMask))    {
87             GetLastErrorText(strErrorText, 260);
88             EndDialog(hDlg, 0);
89             MessageBox(hMainWnd, strErrorText, _T("Unable to Access or Set Process Affinity"), MB_OK|MB_ICONSTOP);
90         }
91
92         /*
93          * Enable a checkbox for each processor present in the system
94          */
95         if (dwSystemAffinityMask & 0x00000001)
96             EnableWindow(GetDlgItem(hDlg, IDC_CPU0), TRUE);
97         if (dwSystemAffinityMask & 0x00000002)
98             EnableWindow(GetDlgItem(hDlg, IDC_CPU1), TRUE);
99         if (dwSystemAffinityMask & 0x00000004)
100             EnableWindow(GetDlgItem(hDlg, IDC_CPU2), TRUE);
101         if (dwSystemAffinityMask & 0x00000008)
102             EnableWindow(GetDlgItem(hDlg, IDC_CPU3), TRUE);
103         if (dwSystemAffinityMask & 0x00000010)
104             EnableWindow(GetDlgItem(hDlg, IDC_CPU4), TRUE);
105         if (dwSystemAffinityMask & 0x00000020)
106             EnableWindow(GetDlgItem(hDlg, IDC_CPU5), TRUE);
107         if (dwSystemAffinityMask & 0x00000040)
108             EnableWindow(GetDlgItem(hDlg, IDC_CPU6), TRUE);
109         if (dwSystemAffinityMask & 0x00000080)
110             EnableWindow(GetDlgItem(hDlg, IDC_CPU7), TRUE);
111         if (dwSystemAffinityMask & 0x00000100)
112             EnableWindow(GetDlgItem(hDlg, IDC_CPU8), TRUE);
113         if (dwSystemAffinityMask & 0x00000200)
114             EnableWindow(GetDlgItem(hDlg, IDC_CPU9), TRUE);
115         if (dwSystemAffinityMask & 0x00000400)
116             EnableWindow(GetDlgItem(hDlg, IDC_CPU10), TRUE);
117         if (dwSystemAffinityMask & 0x00000800)
118             EnableWindow(GetDlgItem(hDlg, IDC_CPU11), TRUE);
119         if (dwSystemAffinityMask & 0x00001000)
120             EnableWindow(GetDlgItem(hDlg, IDC_CPU12), TRUE);
121         if (dwSystemAffinityMask & 0x00002000)
122             EnableWindow(GetDlgItem(hDlg, IDC_CPU13), TRUE);
123         if (dwSystemAffinityMask & 0x00004000)
124             EnableWindow(GetDlgItem(hDlg, IDC_CPU14), TRUE);
125         if (dwSystemAffinityMask & 0x00008000)
126             EnableWindow(GetDlgItem(hDlg, IDC_CPU15), TRUE);
127         if (dwSystemAffinityMask & 0x00010000)
128             EnableWindow(GetDlgItem(hDlg, IDC_CPU16), TRUE);
129         if (dwSystemAffinityMask & 0x00020000)
130             EnableWindow(GetDlgItem(hDlg, IDC_CPU17), TRUE);
131         if (dwSystemAffinityMask & 0x00040000)
132             EnableWindow(GetDlgItem(hDlg, IDC_CPU18), TRUE);
133         if (dwSystemAffinityMask & 0x00080000)
134             EnableWindow(GetDlgItem(hDlg, IDC_CPU19), TRUE);
135         if (dwSystemAffinityMask & 0x00100000)
136             EnableWindow(GetDlgItem(hDlg, IDC_CPU20), TRUE);
137         if (dwSystemAffinityMask & 0x00200000)
138             EnableWindow(GetDlgItem(hDlg, IDC_CPU21), TRUE);
139         if (dwSystemAffinityMask & 0x00400000)
140             EnableWindow(GetDlgItem(hDlg, IDC_CPU22), TRUE);
141         if (dwSystemAffinityMask & 0x00800000)
142             EnableWindow(GetDlgItem(hDlg, IDC_CPU23), TRUE);
143         if (dwSystemAffinityMask & 0x01000000)
144             EnableWindow(GetDlgItem(hDlg, IDC_CPU24), TRUE);
145         if (dwSystemAffinityMask & 0x02000000)
146             EnableWindow(GetDlgItem(hDlg, IDC_CPU25), TRUE);
147         if (dwSystemAffinityMask & 0x04000000)
148             EnableWindow(GetDlgItem(hDlg, IDC_CPU26), TRUE);
149         if (dwSystemAffinityMask & 0x08000000)
150             EnableWindow(GetDlgItem(hDlg, IDC_CPU27), TRUE);
151         if (dwSystemAffinityMask & 0x10000000)
152             EnableWindow(GetDlgItem(hDlg, IDC_CPU28), TRUE);
153         if (dwSystemAffinityMask & 0x20000000)
154             EnableWindow(GetDlgItem(hDlg, IDC_CPU29), TRUE);
155         if (dwSystemAffinityMask & 0x40000000)
156             EnableWindow(GetDlgItem(hDlg, IDC_CPU30), TRUE);
157         if (dwSystemAffinityMask & 0x80000000)
158             EnableWindow(GetDlgItem(hDlg, IDC_CPU31), TRUE);
159
160
161         /*
162          * Check each checkbox that the current process
163          * has affinity with
164          */
165         if (dwProcessAffinityMask & 0x00000001)
166             SendMessage(GetDlgItem(hDlg, IDC_CPU0), BM_SETCHECK, BST_CHECKED, 0);
167         if (dwProcessAffinityMask & 0x00000002)
168             SendMessage(GetDlgItem(hDlg, IDC_CPU1), BM_SETCHECK, BST_CHECKED, 0);
169         if (dwProcessAffinityMask & 0x00000004)
170             SendMessage(GetDlgItem(hDlg, IDC_CPU2), BM_SETCHECK, BST_CHECKED, 0);
171         if (dwProcessAffinityMask & 0x00000008)
172             SendMessage(GetDlgItem(hDlg, IDC_CPU3), BM_SETCHECK, BST_CHECKED, 0);
173         if (dwProcessAffinityMask & 0x00000010)
174             SendMessage(GetDlgItem(hDlg, IDC_CPU4), BM_SETCHECK, BST_CHECKED, 0);
175         if (dwProcessAffinityMask & 0x00000020)
176             SendMessage(GetDlgItem(hDlg, IDC_CPU5), BM_SETCHECK, BST_CHECKED, 0);
177         if (dwProcessAffinityMask & 0x00000040)
178             SendMessage(GetDlgItem(hDlg, IDC_CPU6), BM_SETCHECK, BST_CHECKED, 0);
179         if (dwProcessAffinityMask & 0x00000080)
180             SendMessage(GetDlgItem(hDlg, IDC_CPU7), BM_SETCHECK, BST_CHECKED, 0);
181         if (dwProcessAffinityMask & 0x00000100)
182             SendMessage(GetDlgItem(hDlg, IDC_CPU8), BM_SETCHECK, BST_CHECKED, 0);
183         if (dwProcessAffinityMask & 0x00000200)
184             SendMessage(GetDlgItem(hDlg, IDC_CPU9), BM_SETCHECK, BST_CHECKED, 0);
185         if (dwProcessAffinityMask & 0x00000400)
186             SendMessage(GetDlgItem(hDlg, IDC_CPU10), BM_SETCHECK, BST_CHECKED, 0);
187         if (dwProcessAffinityMask & 0x00000800)
188             SendMessage(GetDlgItem(hDlg, IDC_CPU11), BM_SETCHECK, BST_CHECKED, 0);
189         if (dwProcessAffinityMask & 0x00001000)
190             SendMessage(GetDlgItem(hDlg, IDC_CPU12), BM_SETCHECK, BST_CHECKED, 0);
191         if (dwProcessAffinityMask & 0x00002000)
192             SendMessage(GetDlgItem(hDlg, IDC_CPU13), BM_SETCHECK, BST_CHECKED, 0);
193         if (dwProcessAffinityMask & 0x00004000)
194             SendMessage(GetDlgItem(hDlg, IDC_CPU14), BM_SETCHECK, BST_CHECKED, 0);
195         if (dwProcessAffinityMask & 0x00008000)
196             SendMessage(GetDlgItem(hDlg, IDC_CPU15), BM_SETCHECK, BST_CHECKED, 0);
197         if (dwProcessAffinityMask & 0x00010000)
198             SendMessage(GetDlgItem(hDlg, IDC_CPU16), BM_SETCHECK, BST_CHECKED, 0);
199         if (dwProcessAffinityMask & 0x00020000)
200             SendMessage(GetDlgItem(hDlg, IDC_CPU17), BM_SETCHECK, BST_CHECKED, 0);
201         if (dwProcessAffinityMask & 0x00040000)
202             SendMessage(GetDlgItem(hDlg, IDC_CPU18), BM_SETCHECK, BST_CHECKED, 0);
203         if (dwProcessAffinityMask & 0x00080000)
204             SendMessage(GetDlgItem(hDlg, IDC_CPU19), BM_SETCHECK, BST_CHECKED, 0);
205         if (dwProcessAffinityMask & 0x00100000)
206             SendMessage(GetDlgItem(hDlg, IDC_CPU20), BM_SETCHECK, BST_CHECKED, 0);
207         if (dwProcessAffinityMask & 0x00200000)
208             SendMessage(GetDlgItem(hDlg, IDC_CPU21), BM_SETCHECK, BST_CHECKED, 0);
209         if (dwProcessAffinityMask & 0x00400000)
210             SendMessage(GetDlgItem(hDlg, IDC_CPU22), BM_SETCHECK, BST_CHECKED, 0);
211         if (dwProcessAffinityMask & 0x00800000)
212             SendMessage(GetDlgItem(hDlg, IDC_CPU23), BM_SETCHECK, BST_CHECKED, 0);
213         if (dwProcessAffinityMask & 0x01000000)
214             SendMessage(GetDlgItem(hDlg, IDC_CPU24), BM_SETCHECK, BST_CHECKED, 0);
215         if (dwProcessAffinityMask & 0x02000000)
216             SendMessage(GetDlgItem(hDlg, IDC_CPU25), BM_SETCHECK, BST_CHECKED, 0);
217         if (dwProcessAffinityMask & 0x04000000)
218             SendMessage(GetDlgItem(hDlg, IDC_CPU26), BM_SETCHECK, BST_CHECKED, 0);
219         if (dwProcessAffinityMask & 0x08000000)
220             SendMessage(GetDlgItem(hDlg, IDC_CPU27), BM_SETCHECK, BST_CHECKED, 0);
221         if (dwProcessAffinityMask & 0x10000000)
222             SendMessage(GetDlgItem(hDlg, IDC_CPU28), BM_SETCHECK, BST_CHECKED, 0);
223         if (dwProcessAffinityMask & 0x20000000)
224             SendMessage(GetDlgItem(hDlg, IDC_CPU29), BM_SETCHECK, BST_CHECKED, 0);
225         if (dwProcessAffinityMask & 0x40000000)
226             SendMessage(GetDlgItem(hDlg, IDC_CPU30), BM_SETCHECK, BST_CHECKED, 0);
227         if (dwProcessAffinityMask & 0x80000000)
228             SendMessage(GetDlgItem(hDlg, IDC_CPU31), BM_SETCHECK, BST_CHECKED, 0);
229
230         return TRUE;
231
232     case WM_COMMAND:
233
234         /*
235          * If the user has cancelled the dialog box
236          * then just close it
237          */
238         if (LOWORD(wParam) == IDCANCEL) {
239             EndDialog(hDlg, LOWORD(wParam));
240             return TRUE;
241         }
242
243         /*
244          * The user has clicked OK -- so now we have
245          * to adjust the process affinity mask
246          */
247         if (LOWORD(wParam) == IDOK) {
248             /*
249              * First we have to create a mask out of each
250              * checkbox that the user checked.
251              */
252             if (SendMessage(GetDlgItem(hDlg, IDC_CPU0), BM_GETCHECK, 0, 0))
253                 dwProcessAffinityMask |= 0x00000001;
254             if (SendMessage(GetDlgItem(hDlg, IDC_CPU1), BM_GETCHECK, 0, 0))
255                 dwProcessAffinityMask |= 0x00000002;
256             if (SendMessage(GetDlgItem(hDlg, IDC_CPU2), BM_GETCHECK, 0, 0))
257                 dwProcessAffinityMask |= 0x00000004;
258             if (SendMessage(GetDlgItem(hDlg, IDC_CPU3), BM_GETCHECK, 0, 0))
259                 dwProcessAffinityMask |= 0x00000008;
260             if (SendMessage(GetDlgItem(hDlg, IDC_CPU4), BM_GETCHECK, 0, 0))
261                 dwProcessAffinityMask |= 0x00000010;
262             if (SendMessage(GetDlgItem(hDlg, IDC_CPU5), BM_GETCHECK, 0, 0))
263                 dwProcessAffinityMask |= 0x00000020;
264             if (SendMessage(GetDlgItem(hDlg, IDC_CPU6), BM_GETCHECK, 0, 0))
265                 dwProcessAffinityMask |= 0x00000040;
266             if (SendMessage(GetDlgItem(hDlg, IDC_CPU7), BM_GETCHECK, 0, 0))
267                 dwProcessAffinityMask |= 0x00000080;
268             if (SendMessage(GetDlgItem(hDlg, IDC_CPU8), BM_GETCHECK, 0, 0))
269                 dwProcessAffinityMask |= 0x00000100;
270             if (SendMessage(GetDlgItem(hDlg, IDC_CPU9), BM_GETCHECK, 0, 0))
271                 dwProcessAffinityMask |= 0x00000200;
272             if (SendMessage(GetDlgItem(hDlg, IDC_CPU10), BM_GETCHECK, 0, 0))
273                 dwProcessAffinityMask |= 0x00000400;
274             if (SendMessage(GetDlgItem(hDlg, IDC_CPU11), BM_GETCHECK, 0, 0))
275                 dwProcessAffinityMask |= 0x00000800;
276             if (SendMessage(GetDlgItem(hDlg, IDC_CPU12), BM_GETCHECK, 0, 0))
277                 dwProcessAffinityMask |= 0x00001000;
278             if (SendMessage(GetDlgItem(hDlg, IDC_CPU13), BM_GETCHECK, 0, 0))
279                 dwProcessAffinityMask |= 0x00002000;
280             if (SendMessage(GetDlgItem(hDlg, IDC_CPU14), BM_GETCHECK, 0, 0))
281                 dwProcessAffinityMask |= 0x00004000;
282             if (SendMessage(GetDlgItem(hDlg, IDC_CPU15), BM_GETCHECK, 0, 0))
283                 dwProcessAffinityMask |= 0x00008000;
284             if (SendMessage(GetDlgItem(hDlg, IDC_CPU16), BM_GETCHECK, 0, 0))
285                 dwProcessAffinityMask |= 0x00010000;
286             if (SendMessage(GetDlgItem(hDlg, IDC_CPU17), BM_GETCHECK, 0, 0))
287                 dwProcessAffinityMask |= 0x00020000;
288             if (SendMessage(GetDlgItem(hDlg, IDC_CPU18), BM_GETCHECK, 0, 0))
289                 dwProcessAffinityMask |= 0x00040000;
290             if (SendMessage(GetDlgItem(hDlg, IDC_CPU19), BM_GETCHECK, 0, 0))
291                 dwProcessAffinityMask |= 0x00080000;
292             if (SendMessage(GetDlgItem(hDlg, IDC_CPU20), BM_GETCHECK, 0, 0))
293                 dwProcessAffinityMask |= 0x00100000;
294             if (SendMessage(GetDlgItem(hDlg, IDC_CPU21), BM_GETCHECK, 0, 0))
295                 dwProcessAffinityMask |= 0x00200000;
296             if (SendMessage(GetDlgItem(hDlg, IDC_CPU22), BM_GETCHECK, 0, 0))
297                 dwProcessAffinityMask |= 0x00400000;
298             if (SendMessage(GetDlgItem(hDlg, IDC_CPU23), BM_GETCHECK, 0, 0))
299                 dwProcessAffinityMask |= 0x00800000;
300             if (SendMessage(GetDlgItem(hDlg, IDC_CPU24), BM_GETCHECK, 0, 0))
301                 dwProcessAffinityMask |= 0x01000000;
302             if (SendMessage(GetDlgItem(hDlg, IDC_CPU25), BM_GETCHECK, 0, 0))
303                 dwProcessAffinityMask |= 0x02000000;
304             if (SendMessage(GetDlgItem(hDlg, IDC_CPU26), BM_GETCHECK, 0, 0))
305                 dwProcessAffinityMask |= 0x04000000;
306             if (SendMessage(GetDlgItem(hDlg, IDC_CPU27), BM_GETCHECK, 0, 0))
307                 dwProcessAffinityMask |= 0x08000000;
308             if (SendMessage(GetDlgItem(hDlg, IDC_CPU28), BM_GETCHECK, 0, 0))
309                 dwProcessAffinityMask |= 0x10000000;
310             if (SendMessage(GetDlgItem(hDlg, IDC_CPU29), BM_GETCHECK, 0, 0))
311                 dwProcessAffinityMask |= 0x20000000;
312             if (SendMessage(GetDlgItem(hDlg, IDC_CPU30), BM_GETCHECK, 0, 0))
313                 dwProcessAffinityMask |= 0x40000000;
314             if (SendMessage(GetDlgItem(hDlg, IDC_CPU31), BM_GETCHECK, 0, 0))
315                 dwProcessAffinityMask |= 0x80000000;
316
317             /*
318              * Make sure they are giving the process affinity
319              * with at least one processor. I'd hate to see a
320              * process that is not in a wait state get deprived
321              * of it's cpu time.
322              */
323             if (!dwProcessAffinityMask) {
324                 MessageBox(hDlg, _T("The process must have affinity with at least one processor."), _T("Invalid Option"), MB_OK|MB_ICONSTOP);
325                 return TRUE;
326             }
327
328             /*
329              * Try to set the process affinity
330              */
331             if (!SetProcessAffinityMask(hProcessAffinityHandle, dwProcessAffinityMask)) {
332                 GetLastErrorText(strErrorText, 260);
333                 EndDialog(hDlg, LOWORD(wParam));
334                 MessageBox(hMainWnd, strErrorText, _T("Unable to Access or Set Process Affinity"), MB_OK|MB_ICONSTOP);
335             }
336
337             EndDialog(hDlg, LOWORD(wParam));
338             return TRUE;
339         }
340
341         break;
342     }
343
344     return 0;
345 }