1c2c66affSColin Finck /*
2da7dcec6SHermès Bélusca-Maïto  * PROJECT:     ReactOS Task Manager
3c706222fSHermès Bélusca-Maïto  * LICENSE:     LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4da7dcec6SHermès Bélusca-Maïto  * PURPOSE:     Processor Affinity.
5da7dcec6SHermès Bélusca-Maïto  * COPYRIGHT:   Copyright 1999-2001 Brian Palmer <brianp@reactos.org>
6da7dcec6SHermès Bélusca-Maïto  *              Copyright 2005 Klemens Friedl <frik85@reactos.at>
7c2c66affSColin Finck  */
8c2c66affSColin Finck 
9c2c66affSColin Finck #include "precomp.h"
10c2c66affSColin Finck 
11c2c66affSColin Finck HANDLE        hProcessAffinityHandle;
12c2c66affSColin Finck 
13c2c66affSColin Finck static const DWORD dwCpuTable[] = {
14c2c66affSColin Finck     IDC_CPU0,   IDC_CPU1,   IDC_CPU2,   IDC_CPU3,
15c2c66affSColin Finck     IDC_CPU4,   IDC_CPU5,   IDC_CPU6,   IDC_CPU7,
16c2c66affSColin Finck     IDC_CPU8,   IDC_CPU9,   IDC_CPU10,  IDC_CPU11,
17c2c66affSColin Finck     IDC_CPU12,  IDC_CPU13,  IDC_CPU14,  IDC_CPU15,
18c2c66affSColin Finck     IDC_CPU16,  IDC_CPU17,  IDC_CPU18,  IDC_CPU19,
19c2c66affSColin Finck     IDC_CPU20,  IDC_CPU21,  IDC_CPU22,  IDC_CPU23,
20c2c66affSColin Finck     IDC_CPU24,  IDC_CPU25,  IDC_CPU26,  IDC_CPU27,
21c2c66affSColin Finck     IDC_CPU28,  IDC_CPU29,  IDC_CPU30,  IDC_CPU31,
22c2c66affSColin Finck };
23c2c66affSColin Finck 
24c2c66affSColin Finck static INT_PTR CALLBACK AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
25c2c66affSColin Finck 
ProcessPage_OnSetAffinity(void)26c2c66affSColin Finck void ProcessPage_OnSetAffinity(void)
27c2c66affSColin Finck {
28c2c66affSColin Finck     DWORD    dwProcessId;
29c2c66affSColin Finck     WCHAR    strErrorText[260];
30c2c66affSColin Finck     WCHAR    szTitle[256];
31c2c66affSColin Finck 
32c2c66affSColin Finck     dwProcessId = GetSelectedProcessId();
33c2c66affSColin Finck 
34c2c66affSColin Finck     if (dwProcessId == 0)
35c2c66affSColin Finck         return;
36c2c66affSColin Finck 
37c2c66affSColin Finck     hProcessAffinityHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION, FALSE, dwProcessId);
38c2c66affSColin Finck     if (!hProcessAffinityHandle) {
3936aea8e5SKatayama Hirofumi MZ         GetLastErrorText(strErrorText, _countof(strErrorText));
4036aea8e5SKatayama Hirofumi MZ         LoadStringW(hInst, IDS_MSG_ACCESSPROCESSAFF, szTitle, _countof(szTitle));
41c2c66affSColin Finck         MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
42c2c66affSColin Finck         return;
43c2c66affSColin Finck     }
44c2c66affSColin Finck     DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_AFFINITY_DIALOG), hMainWnd, AffinityDialogWndProc);
45c2c66affSColin Finck     if (hProcessAffinityHandle)    {
46c2c66affSColin Finck         CloseHandle(hProcessAffinityHandle);
47c2c66affSColin Finck         hProcessAffinityHandle = NULL;
48c2c66affSColin Finck     }
49c2c66affSColin Finck }
50c2c66affSColin Finck 
51c2c66affSColin Finck INT_PTR CALLBACK
AffinityDialogWndProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)52c2c66affSColin Finck AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
53c2c66affSColin Finck {
54c2c66affSColin Finck     DWORD_PTR  dwProcessAffinityMask = 0;
55c2c66affSColin Finck     DWORD_PTR  dwSystemAffinityMask = 0;
56c2c66affSColin Finck     WCHAR  strErrorText[260];
57c2c66affSColin Finck     WCHAR  szTitle[256];
58c2c66affSColin Finck     BYTE   nCpu;
59c2c66affSColin Finck 
60c2c66affSColin Finck     switch (message) {
61c2c66affSColin Finck     case WM_INITDIALOG:
62c2c66affSColin Finck 
63c2c66affSColin Finck         /*
64c2c66affSColin Finck          * Get the current affinity mask for the process and
65c2c66affSColin Finck          * the number of CPUs present in the system
66c2c66affSColin Finck          */
67c2c66affSColin Finck         if (!GetProcessAffinityMask(hProcessAffinityHandle, &dwProcessAffinityMask, &dwSystemAffinityMask))    {
6836aea8e5SKatayama Hirofumi MZ             GetLastErrorText(strErrorText, _countof(strErrorText));
69c2c66affSColin Finck             EndDialog(hDlg, 0);
7036aea8e5SKatayama Hirofumi MZ             LoadStringW(hInst, IDS_MSG_ACCESSPROCESSAFF, szTitle, _countof(szTitle));
71c2c66affSColin Finck             MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
72c2c66affSColin Finck         }
73c2c66affSColin Finck 
7436aea8e5SKatayama Hirofumi MZ         for (nCpu = 0; nCpu < _countof(dwCpuTable); nCpu++) {
75c2c66affSColin Finck             /*
76c2c66affSColin Finck              * Enable a checkbox for each processor present in the system
77c2c66affSColin Finck              */
78*a64a6749STimo Kreuzer             if (dwSystemAffinityMask & ((ULONG_PTR)1 << nCpu))
79c2c66affSColin Finck                 EnableWindow(GetDlgItem(hDlg, dwCpuTable[nCpu]), TRUE);
80c2c66affSColin Finck             /*
81c2c66affSColin Finck              * Check each checkbox that the current process
82c2c66affSColin Finck              * has affinity with
83c2c66affSColin Finck              */
84*a64a6749STimo Kreuzer             if (dwProcessAffinityMask & ((ULONG_PTR)1 << nCpu))
85c2c66affSColin Finck                 CheckDlgButton(hDlg, dwCpuTable[nCpu], BST_CHECKED);
86c2c66affSColin Finck         }
87c2c66affSColin Finck 
88c2c66affSColin Finck         return TRUE;
89c2c66affSColin Finck 
90c2c66affSColin Finck     case WM_COMMAND:
91c2c66affSColin Finck 
92c2c66affSColin Finck         /*
93c2c66affSColin Finck          * If the user has cancelled the dialog box
94c2c66affSColin Finck          * then just close it
95c2c66affSColin Finck          */
96c2c66affSColin Finck         if (LOWORD(wParam) == IDCANCEL) {
97c2c66affSColin Finck             EndDialog(hDlg, LOWORD(wParam));
98c2c66affSColin Finck             return TRUE;
99c2c66affSColin Finck         }
100c2c66affSColin Finck 
101c2c66affSColin Finck         /*
102c2c66affSColin Finck          * The user has clicked OK -- so now we have
103c2c66affSColin Finck          * to adjust the process affinity mask
104c2c66affSColin Finck          */
105c2c66affSColin Finck         if (LOWORD(wParam) == IDOK) {
10636aea8e5SKatayama Hirofumi MZ             for (nCpu = 0; nCpu < _countof(dwCpuTable); nCpu++) {
107c2c66affSColin Finck                 /*
108c2c66affSColin Finck                  * First we have to create a mask out of each
109c2c66affSColin Finck                  * checkbox that the user checked.
110c2c66affSColin Finck                  */
111c2c66affSColin Finck                 if (IsDlgButtonChecked(hDlg, dwCpuTable[nCpu]))
112*a64a6749STimo Kreuzer                     dwProcessAffinityMask |= ((ULONG_PTR)1 << nCpu);
113c2c66affSColin Finck             }
114c2c66affSColin Finck 
115c2c66affSColin Finck             /*
116c2c66affSColin Finck              * Make sure they are giving the process affinity
117c2c66affSColin Finck              * with at least one processor. I'd hate to see a
118c2c66affSColin Finck              * process that is not in a wait state get deprived
119c2c66affSColin Finck              * of it's cpu time.
120c2c66affSColin Finck              */
121c2c66affSColin Finck             if (!dwProcessAffinityMask) {
12236aea8e5SKatayama Hirofumi MZ                 LoadStringW(hInst, IDS_MSG_PROCESSONEPRO, strErrorText, _countof(strErrorText));
12336aea8e5SKatayama Hirofumi MZ                 LoadStringW(hInst, IDS_MSG_INVALIDOPTION, szTitle, _countof(szTitle));
124c2c66affSColin Finck                 MessageBoxW(hDlg, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
125c2c66affSColin Finck                 return TRUE;
126c2c66affSColin Finck             }
127c2c66affSColin Finck 
128c2c66affSColin Finck             /*
129c2c66affSColin Finck              * Try to set the process affinity
130c2c66affSColin Finck              */
131c2c66affSColin Finck             if (!SetProcessAffinityMask(hProcessAffinityHandle, dwProcessAffinityMask)) {
13236aea8e5SKatayama Hirofumi MZ                 GetLastErrorText(strErrorText, _countof(strErrorText));
133c2c66affSColin Finck                 EndDialog(hDlg, LOWORD(wParam));
13436aea8e5SKatayama Hirofumi MZ                 LoadStringW(hInst, IDS_MSG_ACCESSPROCESSAFF, szTitle, _countof(szTitle));
135c2c66affSColin Finck                 MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
136c2c66affSColin Finck             }
137c2c66affSColin Finck 
138c2c66affSColin Finck             EndDialog(hDlg, LOWORD(wParam));
139c2c66affSColin Finck             return TRUE;
140c2c66affSColin Finck         }
141c2c66affSColin Finck 
142c2c66affSColin Finck         break;
143c2c66affSColin Finck     }
144c2c66affSColin Finck 
145c2c66affSColin Finck     return 0;
146c2c66affSColin Finck }
147