1 // Profile.cpp: implementation of the CProfile class
\r
3 //////////////////////////////////////////////////////////////////////
\r
7 #include "xkeymacs.h"
\r
8 #include "dotxkeymacs.h"
\r
10 #include "../xkeymacsdll/xkeymacsdll.h"
\r
11 #include "../xkeymacsdll/AppName.h"
\r
12 #include "../xkeymacsdll/Utils.h"
\r
13 #include <TlHelp32.h>
\r
17 static char THIS_FILE[]=__FILE__;
\r
18 #define new DEBUG_NEW
\r
21 Config CProfile::m_Config;
\r
22 KeyString CProfile::m_KeyString(CProfile::Is106Keyboard() != FALSE);
\r
23 TCHAR CProfile::m_AppTitle[MAX_APP][WINDOW_TEXT_LENGTH];
\r
24 TASK_LIST CProfile::m_TaskList[MAX_TASKS];
\r
25 DWORD CProfile::m_dwTasks;
\r
27 void CProfile::LoadData()
\r
29 CDotXkeymacs::Load();
\r
34 void CProfile::SaveData()
\r
36 DeleteAllRegistryData();
\r
41 void CProfile::InitDllData()
\r
47 void CProfile::DeleteAllRegistryData()
\r
50 if (RegOpenKeyEx(HKEY_CURRENT_USER, CString(MAKEINTRESOURCE(IDS_REGSUBKEY_DATA)), 0, KEY_ALL_ACCESS, &hkey) == ERROR_SUCCESS) {
\r
51 // I am sure that I have to do only one time, but...
\r
52 for (int nAppID = 0; nAppID < MAX_APP; ++nAppID) {
\r
54 TCHAR szName[SUB_KEY_NAME_LENGTH] = {'\0'};
\r
55 DWORD dwName = sizeof(szName);
\r
58 while (RegEnumKeyEx(hkey, dwIndex++, szName, &dwName, NULL, NULL, NULL, &filetime) == ERROR_SUCCESS) {
\r
59 // RegDeleteKey(hkey, szName);
\r
60 SHDeleteKey(hkey, szName);
\r
61 ZeroMemory(szName, sizeof(szName));
\r
62 dwName = sizeof(szName);
\r
69 void CProfile::LevelUp()
\r
71 int nCurrentLevel = AfxGetApp()->GetProfileInt(_T(""), _T("Level"), 0);
\r
72 for (int nAppID = 0; nAppID < MAX_APP; ++nAppID) {
\r
74 entry.Format(IDS_REG_ENTRY_APPLICATION, nAppID);
\r
75 const CString appName = AfxGetApp()->GetProfileString(CString(MAKEINTRESOURCE(IDS_REG_SECTION_APPLICATION)), entry);
\r
76 if (appName.IsEmpty())
\r
78 switch (nCurrentLevel) {
\r
80 AddKeyBind2C_(appName, VK_LCONTROL);
\r
81 AddKeyBind2C_(appName, VK_RCONTROL);
\r
84 // Set kill-ring-max 1 if it is 0.
\r
85 if (!AfxGetApp()->GetProfileInt(appName, CString(MAKEINTRESOURCE(IDS_REG_ENTRY_KILL_RING_MAX)), 0))
\r
86 AfxGetApp()->WriteProfileInt(appName, CString(MAKEINTRESOURCE(IDS_REG_ENTRY_KILL_RING_MAX)), 1);
\r
90 // Chaged a label from Enter to newline.
\r
91 const CString subKey = CString(MAKEINTRESOURCE(IDS_REGSUBKEY_DATA)) + _T("\\") + appName;
\r
92 const CString srcKey = subKey + _T("\\") + _T("Enter");
\r
93 const CString dstKey = subKey + _T("\\") + _T("newline");
\r
94 HKEY hDstKey = NULL;
\r
95 if (RegCreateKeyEx(HKEY_CURRENT_USER, dstKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hDstKey, NULL) == ERROR_SUCCESS) {
\r
96 SHCopyKey(HKEY_CURRENT_USER, srcKey, hDstKey, NULL);
\r
97 SHDeleteKey(HKEY_CURRENT_USER, srcKey);
\r
98 RegCloseKey(hDstKey);
\r
103 // rename original function to remove IDS_REG_ORIGINAL_PREFIX
\r
104 for (int nFuncID = 0; nFuncID < CDotXkeymacs::GetFunctionNumber(); ++nFuncID) {
\r
106 const CString subKey = CString(MAKEINTRESOURCE(IDS_REGSUBKEY_DATA)) + _T("\\") + appName + _T("\\") + CString(MAKEINTRESOURCE(IDS_REG_ORIGINAL_PREFIX)) + CDotXkeymacs::GetFunctionSymbol(nFuncID);
\r
107 if (RegOpenKeyEx(HKEY_CURRENT_USER, subKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
\r
108 // Use registry data
\r
109 TCHAR szKeyBind[128];
\r
110 DWORD dwKeyBind = sizeof(szKeyBind);
\r
111 for (DWORD dwIndex = 0; RegEnumKeyEx(hKey, dwIndex, szKeyBind, &dwKeyBind, NULL, NULL, NULL, NULL) == ERROR_SUCCESS; ++dwIndex) {
\r
113 StringToKey(szKeyBind, nType, nKey);
\r
114 SaveKeyBind(appName, CDotXkeymacs::GetFunctionSymbol(nFuncID), nType, nKey);
\r
115 dwKeyBind = sizeof(szKeyBind);
\r
122 AfxGetApp()->WriteProfileInt(_T(""), _T("Level"), 4);
\r
125 void CProfile::AddKeyBind2C_(LPCSTR appName, BYTE bVk)
\r
128 for (nComID = 0; nComID < MAX_COMMAND; ++nComID)
\r
129 if (Commands[nComID].fCommand == CCommands::C_)
\r
131 SaveKeyBind(appName, nComID, NONE, bVk);
\r
134 void CProfile::LoadRegistry()
\r
136 bool bDialog = false;
\r
137 const CString section(MAKEINTRESOURCE(IDS_REG_SECTION_APPLICATION));
\r
138 for (int nAppID = 0; nAppID < MAX_APP; ++nAppID) {
\r
140 entry.Format(IDS_REG_ENTRY_APPLICATION, nAppID);
\r
141 CString appName = AfxGetApp()->GetProfileString(section, entry);
\r
142 if (appName.IsEmpty()) {
\r
146 appName.LoadString(IDS_DIALOG);
\r
149 appName.LoadString(IDS_DEFAULT);
\r
150 } else if (appName == CString(MAKEINTRESOURCE(IDS_DIALOG)))
\r
152 _tcsncpy_s(m_Config.szSpecialApp[nAppID], appName, _TRUNCATE);
\r
153 entry.LoadString(IDS_REG_ENTRY_APPLICATOIN_TITLE);
\r
154 _tcsncpy_s(m_AppTitle[nAppID], AfxGetApp()->GetProfileString(appName, entry), _TRUNCATE);
\r
155 entry.LoadString(IDS_REG_ENTRY_WINDOW_TEXT);
\r
156 _tcsncpy_s(m_Config.szWindowText[nAppID], AfxGetApp()->GetProfileString(appName, entry, _T("*")), _TRUNCATE);
\r
158 const CString regApp = CString(MAKEINTRESOURCE(IDS_REGSUBKEY_DATA)) + _T("\\") + appName;
\r
159 for (BYTE nComID = 1; nComID < MAX_COMMAND; ++nComID) {
\r
160 entry = CCommands::GetCommandName(nComID);
\r
162 const CString regKey = regApp + _T("\\") + entry;
\r
163 if (RegOpenKeyEx(HKEY_CURRENT_USER, regKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
\r
164 TCHAR szKeyBind[128];
\r
165 DWORD dwKeyBind = _countof(szKeyBind);
\r
166 for (DWORD dwIndex = 0; RegEnumKeyEx(hKey, dwIndex, szKeyBind, &dwKeyBind, NULL, NULL, NULL, NULL) == ERROR_SUCCESS; ++dwIndex) {
\r
168 StringToKey(szKeyBind, nType, nKey);
\r
169 m_Config.nCommandID[nAppID][nType][nKey] = nComID;
\r
170 dwKeyBind = _countof(szKeyBind);
\r
174 // Set the default assignment
\r
175 for (int i = 0; int nKey = CCommands::GetDefaultCommandKey(nComID, i); ++i) {
\r
176 if (CCommands::GetDefaultControlID(nComID, i) == IDC_CO2)
\r
178 int nType = CCommands::GetDefaultCommandType(nComID, i);
\r
179 m_Config.nCommandID[nAppID][nType][nKey] = nComID;
\r
183 for (int nFuncID = 0; nFuncID < CDotXkeymacs::GetFunctionNumber(); ++nFuncID) {
\r
185 const CString regKey = regApp + _T("\\") + CDotXkeymacs::GetFunctionSymbol(nFuncID);
\r
186 if (RegOpenKeyEx(HKEY_CURRENT_USER, regKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
\r
187 CDotXkeymacs::ClearKey(nFuncID, nAppID);
\r
188 TCHAR szKeyBind[128];
\r
189 DWORD dwKeyBind = _countof(szKeyBind);
\r
190 for (DWORD dwIndex = 0; RegEnumKeyEx(hKey, dwIndex, szKeyBind, &dwKeyBind, NULL, NULL, NULL, NULL) == ERROR_SUCCESS; ++dwIndex) {
\r
192 StringToKey(szKeyBind, nType, nKey);
\r
193 CDotXkeymacs::SetKey(nFuncID, nAppID, nType, nKey);
\r
194 dwKeyBind = _countof(szKeyBind);
\r
200 entry.LoadString(IDS_REG_ENTRY_KILL_RING_MAX);
\r
201 int n = AfxGetApp()->GetProfileInt(appName, entry, 1);
\r
202 m_Config.nKillRingMax[nAppID] = static_cast<BYTE>(n > 255 ? 255 : n);
\r
203 entry.LoadString(IDS_REG_ENTRY_USE_DIALOG_SETTING);
\r
204 m_Config.bUseDialogSetting[nAppID] = static_cast<BYTE>(AfxGetApp()->GetProfileInt(appName, entry, 1));
\r
205 entry.LoadString(IDS_REG_ENTRY_DISABLE_XKEYMACS);
\r
206 m_Config.nSettingStyle[nAppID] = static_cast<BYTE>(AfxGetApp()->GetProfileInt(appName, entry, 0) ? SETTING_DISABLE : SETTING_SPECIFIC);
\r
207 entry.LoadString(IDC_REG_ENTRY_IGNORE_META_CTRL);
\r
208 m_Config.bIgnoreUndefinedMetaCtrl[nAppID] = static_cast<BYTE>(AfxGetApp()->GetProfileInt(appName, entry, 0));
\r
209 entry.LoadString(IDC_REG_ENTRY_IGNORE_C_X);
\r
210 m_Config.bIgnoreUndefinedC_x[nAppID] = static_cast<BYTE>(AfxGetApp()->GetProfileInt(appName, entry, 0));
\r
211 entry.LoadString(IDC_REG_ENTRY_ENABLE_CUA);
\r
212 m_Config.bEnableCUA[nAppID] = static_cast<BYTE>(AfxGetApp()->GetProfileInt(appName, entry, 0));
\r
213 entry.LoadString(IDS_REG_ENTRY_326_COMPATIBLE);
\r
214 m_Config.b326Compatible[nAppID] = static_cast<BYTE>(AfxGetApp()->GetProfileInt(appName, entry, 0));
\r
218 void CProfile::SaveRegistry()
\r
220 const CString section(MAKEINTRESOURCE(IDS_REG_SECTION_APPLICATION));
\r
221 for (int nAppID = 0; nAppID < MAX_APP; ++nAppID) {
\r
222 LPCTSTR appName = m_Config.szSpecialApp[nAppID];
\r
224 entry.Format(IDS_REG_ENTRY_APPLICATION, nAppID);
\r
226 if (!AfxGetApp()->GetProfileString(section, entry).IsEmpty())
\r
227 AfxGetApp()->WriteProfileString(section, entry, _T(""));
\r
230 AfxGetApp()->WriteProfileString(section, entry, appName);
\r
232 entry.LoadString(IDS_REG_ENTRY_APPLICATOIN_TITLE);
\r
233 CString appTitle = m_AppTitle[nAppID];
\r
234 appTitle.TrimLeft(_T(' '));
\r
235 AfxGetApp()->WriteProfileString(appName, entry, appTitle);
\r
236 entry.LoadString(IDS_REG_ENTRY_WINDOW_TEXT);
\r
237 AfxGetApp()->WriteProfileString(appName, entry, m_Config.szWindowText[nAppID]);
\r
239 // Create all commands
\r
240 for (int nComID = 1; nComID < MAX_COMMAND; ++nComID)
\r
241 SaveKeyBind(appName, nComID, 0, 0);
\r
242 for (int nType = 0; nType < MAX_COMMAND_TYPE; ++nType)
\r
243 for (int nKey = 0; nKey < MAX_KEY; ++nKey)
\r
244 SaveKeyBind(appName, m_Config.nCommandID[nAppID][nType][nKey], nType, nKey);
\r
245 for (int nFuncID = 0; nFuncID < CDotXkeymacs::GetFunctionNumber(); ++nFuncID)
\r
246 for (int nKeyID = 0; nKeyID < CDotXkeymacs::GetKeyNumber(nFuncID, nAppID); ++nKeyID) {
\r
248 CDotXkeymacs::GetKey(nFuncID, nAppID, nKeyID, &nType, &nKey);
\r
249 SaveKeyBind(appName, CDotXkeymacs::GetFunctionSymbol(nFuncID), nType, nKey);
\r
252 entry.LoadString(IDS_REG_ENTRY_KILL_RING_MAX);
\r
253 AfxGetApp()->WriteProfileInt(appName, entry, m_Config.nKillRingMax[nAppID]);
\r
254 entry.LoadString(IDS_REG_ENTRY_USE_DIALOG_SETTING);
\r
255 AfxGetApp()->WriteProfileInt(appName, entry, m_Config.bUseDialogSetting[nAppID]);
\r
256 entry.LoadString(IDS_REG_ENTRY_DISABLE_XKEYMACS);
\r
257 AfxGetApp()->WriteProfileInt(appName, entry, m_Config.nSettingStyle[nAppID] == SETTING_DISABLE);
\r
258 entry.LoadString(IDC_REG_ENTRY_IGNORE_META_CTRL);
\r
259 AfxGetApp()->WriteProfileInt(appName, entry, m_Config.bIgnoreUndefinedMetaCtrl[nAppID]);
\r
260 entry.LoadString(IDC_REG_ENTRY_IGNORE_C_X);
\r
261 AfxGetApp()->WriteProfileInt(appName, entry, m_Config.bIgnoreUndefinedC_x[nAppID]);
\r
262 entry.LoadString(IDC_REG_ENTRY_ENABLE_CUA);
\r
263 AfxGetApp()->WriteProfileInt(appName, entry, m_Config.bEnableCUA[nAppID]);
\r
264 entry.LoadString(IDS_REG_ENTRY_326_COMPATIBLE);
\r
265 AfxGetApp()->WriteProfileInt(appName, entry, m_Config.b326Compatible[nAppID]);
\r
269 void CProfile::SetDllData()
\r
271 memset(m_Config.nFunctionID, -1, sizeof(m_Config.nFunctionID));
\r
272 for (int nFuncID = 0; nFuncID < CDotXkeymacs::GetFunctionNumber(); ++nFuncID)
\r
273 _tcscpy_s(m_Config.szFunctionDefinition[nFuncID], CDotXkeymacs::GetFunctionDefinition(nFuncID));
\r
275 for (int nAppID = 0; nAppID < MAX_APP; ++nAppID) {
\r
276 m_Config.nCommandID[nAppID][CONTROL]['X'] = 0; // C-x is unassigned.
\r
277 for (int nType = 0; nType < MAX_COMMAND_TYPE; ++nType)
\r
278 for (int nKey = 0; nKey < MAX_KEY; ++nKey)
\r
279 if ((nType & CONTROLX) && m_Config.nCommandID[nAppID][nType][nKey])
\r
280 m_Config.nCommandID[nAppID][CONTROL]['X'] = 1; // C-x is available.
\r
281 for (BYTE nFuncID = 0; nFuncID < CDotXkeymacs::GetFunctionNumber(); ++nFuncID)
\r
282 for (int nKeyID = 0; nKeyID < CDotXkeymacs::GetKeyNumber(nFuncID, nAppID); ++nKeyID) {
\r
284 CDotXkeymacs::GetKey(nFuncID, nAppID, nKeyID, &nType, &nKey);
\r
285 m_Config.nFunctionID[nAppID][nType][nKey] = nFuncID;
\r
286 if (nType & CONTROLX)
\r
287 m_Config.nCommandID[nAppID][CONTROL]['X'] = 1; // C-x is available.
\r
290 m_Config.b106Keyboard = static_cast<BYTE>(Is106Keyboard());
\r
291 CXkeymacsDll::SetConfig(m_Config);
\r
292 CXkeymacsApp *pApp = static_cast<CXkeymacsApp *>(AfxGetApp());
\r
293 if (!pApp->IsWow64())
\r
295 if (!CXkeymacsDll::SaveConfig())
\r
297 pApp->SendIPCMessage(XKEYMACS_RELOAD);
\r
300 void CProfile::SaveKeyBind(LPCTSTR appName, int comID, int type, int key)
\r
304 LPCTSTR comName = CCommands::GetCommandName(comID);
\r
307 SaveKeyBind(appName, comName, type, key);
\r
310 void CProfile::SaveKeyBind(LPCTSTR appName, LPCTSTR comName, int type, int key)
\r
312 CString subKey = CString(MAKEINTRESOURCE(IDS_REGSUBKEY_DATA)) + _T("\\") + appName + _T("\\") + comName;
\r
313 CString s = KeyToString(type, key);
\r
315 subKey = subKey + _T("\\") + s;
\r
317 if (RegCreateKeyEx(HKEY_CURRENT_USER, subKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL) == ERROR_SUCCESS)
\r
321 void CProfile::StringToKey(LPCTSTR str, int& type, int& key)
\r
323 m_KeyString.ToKey(str, type, key);
\r
326 CString CProfile::KeyToString(int type, int key)
\r
328 return m_KeyString.ToString(type, key);
\r
331 void CProfile::InitAppList(CProperties& cProperties)
\r
335 EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&cProperties));
\r
337 for (int i = 0; i < MAX_APP; ++i) {
\r
338 LPCTSTR appName = m_Config.szSpecialApp[i];
\r
339 LPCTSTR appTitle = m_AppTitle[i];
\r
340 if (!appName[0] || !_tcscmp(appName, _T("IME")))
\r
342 if (CString(MAKEINTRESOURCE(IDS_DEFAULT)) == appName ||
\r
343 CString(MAKEINTRESOURCE(IDS_DIALOG)) == appName)
\r
345 cProperties.AddItem(appTitle, appName);
\r
347 AddIMEInfo(cProperties);
\r
350 void CProfile::GetTaskList()
\r
352 ZeroMemory(m_TaskList, sizeof(m_TaskList));
\r
354 HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
\r
355 if (hProcessSnap == (HANDLE)-1)
\r
359 PROCESSENTRY32 processEntry32 = {sizeof(PROCESSENTRY32)};
\r
360 if (Process32First(hProcessSnap, &processEntry32)) {
\r
362 lstrcpy(m_TaskList[m_dwTasks].ProcessName, processEntry32.szExeFile);
\r
363 m_TaskList[m_dwTasks++].dwProcessId = processEntry32.th32ProcessID;
\r
364 } while (m_dwTasks < MAX_TASKS && Process32Next(hProcessSnap, &processEntry32));
\r
367 CloseHandle(hProcessSnap);
\r
370 BOOL CALLBACK CProfile::EnumWindowsProc(HWND hWnd, LPARAM lParam)
\r
372 CProperties *pProperties = reinterpret_cast<CProperties*>(lParam);
\r
373 PTASK_LIST pTask = CProfile::m_TaskList;
\r
375 TCHAR szWindowName[WINDOW_TEXT_LENGTH];
\r
376 TCHAR szClassName[CLASS_NAME_LENGTH];
\r
377 WINDOWPLACEMENT wpl;
\r
379 wpl.length = sizeof(WINDOWPLACEMENT);
\r
380 ::GetWindowText(hWnd, szWindowName, sizeof(szWindowName));
\r
381 GetClassName(hWnd, szClassName, sizeof(szClassName));
\r
384 // Get Process Name
\r
385 DWORD dwProcessId = 0;
\r
386 GetWindowThreadProcessId(hWnd, &dwProcessId);
\r
388 for (i = 0; i < CProfile::m_dwTasks; ++i) {
\r
389 if (pTask[i].dwProcessId == dwProcessId) {
\r
391 // Get Application Name
\r
392 if (szWindowName[0] == '\0') {
\r
395 if (!_tcsnicmp(pTask[i].ProcessName, CString(MAKEINTRESOURCE(IDS_B2)), sizeof(pTask[i].ProcessName))) {
\r
396 appTitle.LoadString(IDS_BECKY);
\r
397 } else if (!_tcsnicmp(pTask[i].ProcessName, CString(MAKEINTRESOURCE(IDS_EXPLORER)), sizeof(pTask[i].ProcessName))) {
\r
398 appTitle.LoadString(IDS_PROGRAM_MANAGER);
\r
399 } else if (!_tcsnicmp(pTask[i].ProcessName, CString(MAKEINTRESOURCE(IDS_MSIMN)), sizeof(pTask[i].ProcessName))) {
\r
400 appTitle.LoadString(IDS_OUTLOOK_EXPRESS);
\r
401 } else if (!_tcsnicmp(pTask[i].ProcessName, CString(MAKEINTRESOURCE(IDS_PROJECT)), sizeof(pTask[i].ProcessName))
\r
402 || !_tcsnicmp(pTask[i].ProcessName, CString(MAKEINTRESOURCE(IDS_EXCEL)), sizeof(pTask[i].ProcessName))
\r
403 || !_tcsnicmp(pTask[i].ProcessName, _T("psp.exe"), sizeof(pTask[i].ProcessName))) {
\r
404 GetAppTitle(appTitle, szWindowName, 1);
\r
405 } else if (!_tcsnicmp(pTask[i].ProcessName, _T("sakura.exe"), sizeof(pTask[i].ProcessName))) {
\r
406 GetAppTitle(appTitle, szWindowName, 2); // '.' is included, so...
\r
407 } else if (!_tcsnicmp(pTask[i].ProcessName, CString(MAKEINTRESOURCE(IDS_MSDN)), sizeof(pTask[i].ProcessName))) {
\r
408 appTitle = szWindowName;
\r
409 } else if (!_tcsnicmp(pTask[i].ProcessName, _T("devenv.exe"), sizeof(pTask[i].ProcessName))) {
\r
410 appTitle.Format(_T("Microsoft Visual Studio .NET"));
\r
411 } else if (!_tcsnicmp(pTask[i].ProcessName, _T("vb6.exe"), sizeof(pTask[i].ProcessName))) {
\r
412 appTitle.Format(_T("Microsoft Visual Basic"));
\r
413 } else if (!_tcsnicmp(pTask[i].ProcessName, _T("ssexp.exe"), sizeof(pTask[i].ProcessName))) {
\r
414 appTitle.LoadString(IDS_VISUAL_SOURCESAFE_EXPLORER);
\r
415 } else if (!_tcsnicmp(pTask[i].ProcessName, _T("sh.exe"), sizeof(pTask[i].ProcessName))) {
\r
416 appTitle.Format(_T("MKS Korn Shell"));
\r
417 } else if (!_tcsnicmp(pTask[i].ProcessName, _T("csh.exe"), sizeof(pTask[i].ProcessName))) {
\r
418 appTitle.Format(_T("C Shell"));
\r
419 } else if (!_tcsnicmp(pTask[i].ProcessName, _T("vim.exe"), sizeof(pTask[i].ProcessName))) {
\r
420 appTitle.Format(_T("VIM"));
\r
422 AppName::CorrectAppName(szWindowName, pTask[i].ProcessName);
\r
423 GetAppTitle(appTitle, szWindowName);
\r
429 if (IsWindowVisible(hWnd) && // Is visible?
\r
430 GetWindow(hWnd, GW_OWNER) == NULL && // Is top level window?
\r
431 lstrlen(szWindowName) > 0) { // Have caption?
\r
432 pProperties->AddItem(appTitle, pTask[i].ProcessName);
\r
437 // This function returns the nth string in a window name separated by " - ".
\r
438 // If there aren't a sufficient number of strings, it returns the last string
\r
439 // appropriate for the title.
\r
440 bool CProfile::GetAppTitle(CString& appTitle, const CString& windowName, int nth)
\r
442 const CString sep(MAKEINTRESOURCE(IDS_SEPARATE_WINDOWTITLE));
\r
443 int nSep = windowName.Find(sep);
\r
445 appTitle = windowName;
\r
448 if (GetAppTitle(appTitle, windowName.Right(windowName.GetLength() - nSep - sep.GetLength()), --nth) ||
\r
449 !nth || nth > 0 && appTitle.GetAt(0) != _T('[') && appTitle.FindOneOf(_T(".]")) == -1)
\r
451 appTitle = windowName.Left(nSep);
\r
455 void CProfile::AddIMEInfo(CProperties& cProperties)
\r
458 for (IMEListIterator p = imeList.begin(); p != imeList.end(); ++p)
\r
459 cProperties.AddItem(p->szDescription, p->szFileName);
\r
462 void CProfile::ClearData(const CString szCurrentApplication)
\r
464 for (int nAppID = 0; nAppID < MAX_APP; ++nAppID)
\r
465 if (szCurrentApplication == m_Config.szSpecialApp[nAppID]) {
\r
466 ZeroMemory(m_Config.nCommandID[nAppID], sizeof(m_Config.nCommandID[nAppID]));
\r
467 ZeroMemory(m_Config.szSpecialApp[nAppID], CLASS_NAME_LENGTH);
\r
472 void CProfile::CopyData(const CString szDstApp, const CString szSrcApp)
\r
474 int nDstApp = AssignAppID(szDstApp);
\r
475 int nSrcApp = GetAppID(szSrcApp);
\r
476 if (nDstApp == MAX_APP || nSrcApp == MAX_APP)
\r
478 SetSettingStyle(nDstApp, SETTING_SPECIFIC);
\r
480 #define CopyMember(member) CopyMemory(&m_Config. ## member ## [nDstApp], &m_Config. ## member ## [nSrcApp], sizeof(m_Config. ## member ## [nSrcApp]))
\r
481 CopyMember(b326Compatible);
\r
482 CopyMember(nFunctionID);
\r
483 CopyMember(bEnableCUA);
\r
484 CopyMember(bUseDialogSetting);
\r
485 CopyMember(bIgnoreUndefinedC_x);
\r
486 CopyMember(bIgnoreUndefinedMetaCtrl);
\r
487 CopyMember(nKillRingMax);
\r
488 CopyMember(nCommandID);
\r
492 int CProfile::AssignAppID(LPCSTR appName)
\r
494 int nAppID = GetAppID(appName);
\r
495 if (nAppID != MAX_APP)
\r
497 for (nAppID = 0; nAppID < MAX_APP; ++nAppID)
\r
498 if (!m_Config.szSpecialApp[nAppID][0]) {
\r
499 _tcsncpy_s(m_Config.szSpecialApp[nAppID], appName, _TRUNCATE);
\r
505 int CProfile::DefaultAppID()
\r
507 const CString name(MAKEINTRESOURCE(IDS_DEFAULT));
\r
508 for(int nAppID = 0; nAppID < MAX_APP; ++nAppID)
\r
509 if (name == m_Config.szSpecialApp[nAppID])
\r
514 int CProfile::GetAppID(LPCSTR appName)
\r
517 for (nAppID = 0; nAppID < MAX_APP; ++nAppID)
\r
518 if (!_tcscmp(appName, m_Config.szSpecialApp[nAppID]))
\r
523 int CProfile::GetSettingStyle(int nAppID)
\r
525 if (nAppID == MAX_APP)
\r
526 return SETTING_DEFAULT;
\r
527 return m_Config.nSettingStyle[nAppID];
\r
530 void CProfile::SetSettingStyle(int nAppID, int nSettingStyle)
\r
532 if (nAppID == MAX_APP)
\r
534 m_Config.nSettingStyle[nAppID] = static_cast<BYTE>(nSettingStyle);
\r
537 void CProfile::SetAppTitle(int nAppID, const CString& appTitle)
\r
539 _tcsncpy_s(m_AppTitle[nAppID], appTitle, _TRUNCATE);
\r
542 int CProfile::GetCommandID(int nAppID, int nType, int nKey)
\r
544 int nComID = m_Config.nCommandID[nAppID][nType][nKey];
\r
545 if (nKey == 0xf0 && Commands[nComID].fCommand == CCommands::C_Eisu)
\r
546 // Change CommandID C_
\r
547 for (nComID = 1; nComID < MAX_COMMAND; nComID++)
\r
548 if (Commands[nComID].fCommand == CCommands::C_)
\r
553 void CProfile::SetCommandID(int nAppID, int nType, int nKey, int nComID)
\r
555 if (nKey == 0xf0 && Commands[nComID].fCommand == CCommands::C_)
\r
556 // Change CommandID C_Eisu
\r
557 for (nComID = 1; nComID < MAX_COMMAND; ++nComID)
\r
558 if (Commands[nComID].fCommand == CCommands::C_Eisu)
\r
560 m_Config.nCommandID[nAppID][nType][nKey] = static_cast<BYTE>(nComID);
\r
563 BOOL CProfile::GetUseDialogSetting(int nAppID)
\r
565 return m_Config.bUseDialogSetting[nAppID];
\r
568 void CProfile::SetUseDialogSetting(int nAppID, BOOL bUseDialogSetting)
\r
570 m_Config.bUseDialogSetting[nAppID] = static_cast<BYTE>(bUseDialogSetting);
\r
573 BOOL CProfile::GetEnableCUA(int nAppID)
\r
575 return m_Config.bEnableCUA[nAppID];
\r
578 void CProfile::SetEnableCUA(int nAppID, BOOL bEnableCUA)
\r
580 m_Config.bEnableCUA[nAppID] = static_cast<BYTE>(bEnableCUA);
\r
583 int CProfile::GetKillRingMax(int nAppID)
\r
585 return m_Config.nKillRingMax[nAppID];
\r
588 void CProfile::SetKillRingMax(int nAppID, int nKillRingMax)
\r
590 m_Config.nKillRingMax[nAppID] = static_cast<BYTE>(nKillRingMax > 255 ? 255 : nKillRingMax);
\r
593 CString CProfile::GetWindowText(int nAppID)
\r
595 return m_Config.szWindowText[nAppID];
\r
598 void CProfile::SetWindowText(int nAppID, const CString szWindowText)
\r
600 if (CUtils::GetWindowTextType(szWindowText) == IDS_WINDOW_TEXT_IGNORE)
\r
601 _tcscpy_s(m_Config.szWindowText[nAppID], _T("*"));
\r
603 _tcsncpy_s(m_Config.szWindowText[nAppID], szWindowText, _TRUNCATE);
\r
606 BOOL CProfile::Is106Keyboard()
\r
608 static KEYBOARD_TYPE keyboard = UNKNOWN_KEYBOARD;
\r
610 if (keyboard == UNKNOWN_KEYBOARD) {
\r
611 OSVERSIONINFO verInfo = {0};
\r
612 verInfo.dwOSVersionInfoSize = sizeof (verInfo);
\r
613 GetVersionEx(&verInfo);
\r
616 DWORD cbData = sizeof(subtype);
\r
618 if (verInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
\r
620 const CString szSubKey(_T("SYSTEM\\CurrentControlSet\\Services\\i8042prt\\Parameters"));
\r
621 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
\r
622 static const CString szValueName(_T("OverrideKeyboardSubtype"));
\r
623 if (RegQueryValueEx(hKey, szValueName, NULL, NULL, (LPBYTE)&subtype, &cbData) != ERROR_SUCCESS) {
\r
628 } else if (verInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
\r
629 subtype = GetPrivateProfileInt(_T("keyboard"), _T("subtype"), 0, _T("system.ini"));
\r
632 keyboard = (subtype & 0x02) ? JAPANESE_KEYBOARD : ENGLISH_KEYBOARD;
\r
635 return keyboard == JAPANESE_KEYBOARD;
\r
638 BOOL CProfile::IsVistaOrLater()
\r
640 OSVERSIONINFO info = {sizeof(OSVERSIONINFO)};
\r
641 GetVersionEx(&info);
\r
643 if (6 <= info.dwMajorVersion) {
\r
649 void CProfile::RestartComputer()
\r
651 if (!AdjustTokenPrivileges(SE_SHUTDOWN_NAME)) {
\r
655 ExitWindowsEx(EWX_REBOOT, 0);
\r
658 void CProfile::ImportProperties()
\r
660 if (!AdjustTokenPrivileges(SE_RESTORE_NAME)) {
\r
664 CFileDialog oFileOpenDialog(TRUE, _T("reg"), _T("xkeymacs"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, CString(MAKEINTRESOURCE(IDS_REGISTRATION_FILTER)));
\r
665 if (oFileOpenDialog.DoModal() == IDOK) {
\r
666 CString szCommandLine;
\r
667 szCommandLine.Format(_T("regedit \"%s\""), oFileOpenDialog.GetPathName());
\r
668 CUtils::Run(szCommandLine, TRUE); // regedit "x:\xkeymacs.reg"
\r
671 DiableTokenPrivileges();
\r
675 void CProfile::ExportProperties()
\r
677 if (!AdjustTokenPrivileges(SE_BACKUP_NAME)) {
\r
681 CFileDialog oFileOpenDialog(FALSE, _T("reg"), _T("xkeymacs"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, CString(MAKEINTRESOURCE(IDS_REGISTRATION_FILTER)));
\r
682 if (oFileOpenDialog.DoModal() == IDOK) {
\r
683 CString szCommandLine;
\r
684 szCommandLine.Format(_T("regedit /e \"%s\" HKEY_CURRENT_USER\\%s"), oFileOpenDialog.GetPathName(), CString(MAKEINTRESOURCE(IDS_REGSUBKEY_DATA)));
\r
685 CUtils::Run(szCommandLine, TRUE); // regedit /e "x:\xkeymacs.reg" HKEY_CURRENT_USER\Software\Oishi\XKeymacs2
\r
688 DiableTokenPrivileges();
\r
692 BOOL CProfile::AdjustTokenPrivileges(LPCTSTR lpName)
\r
697 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
\r
699 if (LookupPrivilegeValue(NULL, lpName, &luid)) {
\r
700 TOKEN_PRIVILEGES tp;
\r
701 tp.PrivilegeCount = 1;
\r
702 tp.Privileges[0].Luid = luid;
\r
703 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
\r
705 if (!::AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
\r
711 CloseHandle(hToken);
\r
719 BOOL CProfile::DiableTokenPrivileges()
\r
724 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
\r
725 if (!::AdjustTokenPrivileges(hToken, TRUE, NULL, NULL, NULL, NULL)) {
\r
728 CloseHandle(hToken);
\r
736 int CProfile::GetKeyboardSpeed()
\r
738 int nKeyboardSpeed = 31; // default value of Windows
\r
739 CString szSubKey(MAKEINTRESOURCE(IDS_REGSUBKEY_KEYBOARD));
\r
740 CString szValueName(MAKEINTRESOURCE(IDS_KEYBOARD_SPEED));
\r
743 if (RegOpenKeyEx(HKEY_CURRENT_USER, szSubKey, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {
\r
745 DWORD dwType = REG_SZ;
\r
746 BYTE data[4] = {0};
\r
747 DWORD dwcbData = sizeof(data)/sizeof(data[0]);
\r
748 RegQueryValueEx(hKey, szValueName, NULL, &dwType, (LPBYTE)&data, &dwcbData);
\r
751 for (DWORD i = 0; i < dwcbData; ++i) {
\r
754 nKeyboardSpeed = nKeyboardSpeed * 10 + data[i] - _T('0');
\r
756 nKeyboardSpeed = data[i] - _T('0');
\r
764 return nKeyboardSpeed;
\r