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