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