Skip to content

Commit

Permalink
refactor: simplify unused compartment code
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Dec 24, 2024
1 parent 18c108f commit fe62bac
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 234 deletions.
33 changes: 0 additions & 33 deletions ChewingPreferences/ChewingPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "UiPropertyPage.h"
#include "KeyboardPropertyPage.h"
#include "SymbolsPropertyPage.h"
#include "Dialog.h"
#include "PropertyDialog.h"
#include "AboutDialog.h"
#include "resource.h"
Expand All @@ -33,10 +32,6 @@

namespace Chewing {

// {F4D1E543-FB2C-48D7-B78D-20394F355381} // global compartment GUID for config change notification
static const GUID g_configChangedGuid =
{ 0xf4d1e543, 0xfb2c, 0x48d7, { 0xb7, 0x8d, 0x20, 0x39, 0x4f, 0x35, 0x53, 0x81 } };

static void initControls() {
INITCOMMONCONTROLSEX ic;
ic.dwSize = sizeof(ic);
Expand Down Expand Up @@ -65,35 +60,7 @@ static void configDialog(HINSTANCE hInstance) {
dlg.addPage(symbolsPage);
INT_PTR ret = dlg.showModal(hInstance, (LPCTSTR)IDS_CONFIG_TITLE, 0, HWND_DESKTOP);
if(ret) { // the user clicks OK button
// get current time stamp and set the value to global compartment to notify all
// text service instances to reload their config.
// TextService::onCompartmentChanged() of all other instances will be triggered.
config.save();

DWORD stamp = ::GetTickCount();
if(stamp == Config::INVALID_TIMESTAMP) // this is almost impossible
stamp = 0;
// set global compartment value to notify existing ChewingTextService instances
::CoInitialize(NULL); // initialize COM
winrt::com_ptr<ITfThreadMgr> threadMgr;
if(CoCreateInstance(CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, IID_ITfThreadMgr, threadMgr.put_void()) == S_OK) {
TfClientId clientId = 0;
if(threadMgr->Activate(&clientId) == S_OK) {
winrt::com_ptr<ITfCompartmentMgr> compartmentMgr;
if(threadMgr->GetGlobalCompartment(compartmentMgr.put()) == S_OK) {
winrt::com_ptr<ITfCompartment> compartment;
if(compartmentMgr->GetCompartment(g_configChangedGuid, compartment.put()) == S_OK) {
VARIANT var;
VariantInit(&var);
var.vt = VT_I4;
var.lVal = ::GetTickCount(); // set current timestamp
compartment->SetValue(clientId, &var);
}
}
threadMgr->Deactivate();
}
}
::CoUninitialize();
}
}

Expand Down
56 changes: 25 additions & 31 deletions chewing_tip/ChewingConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ Config::Config():
easySymbolsWithShift = 1;
easySymbolsWithCtrl = 0;
upperCaseWithShift = 0;

stamp = INVALID_TIMESTAMP;
}

Config::~Config(void) {
Expand Down Expand Up @@ -163,33 +161,35 @@ void Config::save() {
BOOL isWow64 = FALSE;
::IsWow64Process(process, &isWow64);
DWORD regFlags = isWow64 ? KEY_WOW64_64KEY : 0;
DWORD timestamp = GetTickCount();

HKEY hk = NULL;
LSTATUS ret = ::RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\ChewingTextService", 0,
NULL, 0, regFlags|KEY_READ|KEY_WRITE , NULL, &hk, NULL);
if(ERROR_SUCCESS == ret) {
::RegSetValueEx(hk, L"KeyboardLayout", 0, REG_DWORD, (LPBYTE)&keyboardLayout, sizeof(DWORD));
::RegSetValueEx(hk, L"CandPerRow", 0, REG_DWORD, (LPBYTE)&candPerRow, sizeof(DWORD));
::RegSetValueEx(hk, L"DefaultEnglish", 0, REG_DWORD, (LPBYTE)&defaultEnglish, sizeof(DWORD));
::RegSetValueEx(hk, L"DefaultFullSpace", 0, REG_DWORD, (LPBYTE)&defaultFullSpace, sizeof(DWORD));
::RegSetValueEx(hk, L"ShowCandWithSpaceKey", 0, REG_DWORD, (LPBYTE)&showCandWithSpaceKey, sizeof(DWORD));
::RegSetValueEx(hk, L"SwitchLangWithShift", 0, REG_DWORD, (LPBYTE)&switchLangWithShift, sizeof(DWORD));
::RegSetValueEx(hk, L"OutputSimpChinese", 0, REG_DWORD, (LPBYTE)&outputSimpChinese, sizeof(DWORD));
::RegSetValueEx(hk, L"AddPhraseForward", 0, REG_DWORD, (LPBYTE)&addPhraseForward, sizeof(DWORD));
::RegSetValueEx(hk, L"ColorCandWnd", 0, REG_DWORD, (LPBYTE)&colorCandWnd, sizeof(DWORD));
::RegSetValueEx(hk, L"AdvanceAfterSelection", 0, REG_DWORD, (LPBYTE)&advanceAfterSelection, sizeof(DWORD));
::RegSetValueEx(hk, L"DefFontSize", 0, REG_DWORD, (LPBYTE)&fontSize, sizeof(DWORD));
::RegSetValueEx(hk, L"SelKeyType", 0, REG_DWORD, (LPBYTE)&selKeyType, sizeof(DWORD));
::RegSetValueEx(hk, L"ConvEngine", 0, REG_DWORD, (LPBYTE)&convEngine, sizeof(DWORD));
::RegSetValueEx(hk, L"SelAreaLen", 0, REG_DWORD, (LPBYTE)&candPerPage, sizeof(DWORD));
::RegSetValueEx(hk, L"CursorCandList", 0, REG_DWORD, (LPBYTE)&cursorCandList, sizeof(DWORD));
::RegSetValueEx(hk, L"EnableCapsLock", 0, REG_DWORD, (LPBYTE)&enableCapsLock, sizeof(DWORD));
::RegSetValueEx(hk, L"FullShapeSymbols", 0, REG_DWORD, (LPBYTE)&fullShapeSymbols, sizeof(DWORD));
::RegSetValueEx(hk, L"PhraseMark", 0, REG_DWORD, (LPBYTE)&phraseMark, sizeof(DWORD));
::RegSetValueEx(hk, L"EscCleanAllBuf", 0, REG_DWORD, (LPBYTE)&escCleanAllBuf, sizeof(DWORD));
::RegSetValueEx(hk, L"EasySymbolsWithShift", 0, REG_DWORD, (LPBYTE)&easySymbolsWithShift, sizeof(DWORD));
::RegSetValueEx(hk, L"EasySymbolsWithCtrl", 0, REG_DWORD, (LPBYTE)&easySymbolsWithCtrl, sizeof(DWORD));
::RegSetValueEx(hk, L"UpperCaseWithShift", 0, REG_DWORD, (LPBYTE)&upperCaseWithShift, sizeof(DWORD));
::RegSetValueExW(hk, L"KeyboardLayout", 0, REG_DWORD, (LPBYTE)&keyboardLayout, sizeof(DWORD));
::RegSetValueExW(hk, L"CandPerRow", 0, REG_DWORD, (LPBYTE)&candPerRow, sizeof(DWORD));
::RegSetValueExW(hk, L"DefaultEnglish", 0, REG_DWORD, (LPBYTE)&defaultEnglish, sizeof(DWORD));
::RegSetValueExW(hk, L"DefaultFullSpace", 0, REG_DWORD, (LPBYTE)&defaultFullSpace, sizeof(DWORD));
::RegSetValueExW(hk, L"ShowCandWithSpaceKey", 0, REG_DWORD, (LPBYTE)&showCandWithSpaceKey, sizeof(DWORD));
::RegSetValueExW(hk, L"SwitchLangWithShift", 0, REG_DWORD, (LPBYTE)&switchLangWithShift, sizeof(DWORD));
::RegSetValueExW(hk, L"OutputSimpChinese", 0, REG_DWORD, (LPBYTE)&outputSimpChinese, sizeof(DWORD));
::RegSetValueExW(hk, L"AddPhraseForward", 0, REG_DWORD, (LPBYTE)&addPhraseForward, sizeof(DWORD));
::RegSetValueExW(hk, L"ColorCandWnd", 0, REG_DWORD, (LPBYTE)&colorCandWnd, sizeof(DWORD));
::RegSetValueExW(hk, L"AdvanceAfterSelection", 0, REG_DWORD, (LPBYTE)&advanceAfterSelection, sizeof(DWORD));
::RegSetValueExW(hk, L"DefFontSize", 0, REG_DWORD, (LPBYTE)&fontSize, sizeof(DWORD));
::RegSetValueExW(hk, L"SelKeyType", 0, REG_DWORD, (LPBYTE)&selKeyType, sizeof(DWORD));
::RegSetValueExW(hk, L"ConvEngine", 0, REG_DWORD, (LPBYTE)&convEngine, sizeof(DWORD));
::RegSetValueExW(hk, L"SelAreaLen", 0, REG_DWORD, (LPBYTE)&candPerPage, sizeof(DWORD));
::RegSetValueExW(hk, L"CursorCandList", 0, REG_DWORD, (LPBYTE)&cursorCandList, sizeof(DWORD));
::RegSetValueExW(hk, L"EnableCapsLock", 0, REG_DWORD, (LPBYTE)&enableCapsLock, sizeof(DWORD));
::RegSetValueExW(hk, L"FullShapeSymbols", 0, REG_DWORD, (LPBYTE)&fullShapeSymbols, sizeof(DWORD));
::RegSetValueExW(hk, L"PhraseMark", 0, REG_DWORD, (LPBYTE)&phraseMark, sizeof(DWORD));
::RegSetValueExW(hk, L"EscCleanAllBuf", 0, REG_DWORD, (LPBYTE)&escCleanAllBuf, sizeof(DWORD));
::RegSetValueExW(hk, L"EasySymbolsWithShift", 0, REG_DWORD, (LPBYTE)&easySymbolsWithShift, sizeof(DWORD));
::RegSetValueExW(hk, L"EasySymbolsWithCtrl", 0, REG_DWORD, (LPBYTE)&easySymbolsWithCtrl, sizeof(DWORD));
::RegSetValueExW(hk, L"UpperCaseWithShift", 0, REG_DWORD, (LPBYTE)&upperCaseWithShift, sizeof(DWORD));
::RegSetValueExW(hk, L"ModifiedTimestamp", 0, REG_DWORD, (LPBYTE)&timestamp, sizeof(DWORD));

::RegCloseKey(hk);

Expand All @@ -199,17 +199,11 @@ void Config::save() {
}
}

void Config::reloadIfNeeded(DWORD timestamp) {
if(stamp != timestamp) {
load();
stamp = timestamp;
}
}

bool Config::reloadIfNeeded() {
if (hChangeEvent) {
DWORD result = WaitForSingleObject(hChangeEvent, 0);
if (WAIT_OBJECT_0 == result) {
OutputDebugStringW(L"[chewing] config change detected, reload.\n");
load();
watchChanges();
return true;
Expand Down
7 changes: 0 additions & 7 deletions chewing_tip/ChewingConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class Config {
void load();
void save();

// reload configurations if changes are detected (if timestamp is different from this->stamp)
void reloadIfNeeded(DWORD timestamp);
// Returns true if config was reloaded
bool reloadIfNeeded();
void watchChanges();
Expand All @@ -46,10 +44,6 @@ class Config {
static bool createSecurityDesc(SECURITY_DESCRIPTOR& sd);

public:
enum {
INVALID_TIMESTAMP = (DWORD)-1
};

// Configuration
DWORD keyboardLayout; // keyboard type
DWORD candPerRow; // candidate string per row (not supported yet)
Expand Down Expand Up @@ -78,7 +72,6 @@ class Config {
static const wchar_t* convEngines[];

private:
DWORD stamp; // timestamp used to check if the config values are up to date
HANDLE hChangeEvent;
HKEY monitorHkey;
};
Expand Down
61 changes: 20 additions & 41 deletions chewing_tip/ChewingTextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ static const GUID g_settingsButtonGuid = // settings button/menu
static const GUID g_shiftSpaceGuid = // shift + space
{ 0xc77a44f5, 0xdb21, 0x474e, { 0xa2, 0xa2, 0xa1, 0x72, 0x42, 0x21, 0x7a, 0xb3 } };

// {F4D1E543-FB2C-48D7-B78D-20394F355381} // global compartment GUID for config change notification
static const GUID g_configChangedGuid =
{ 0xf4d1e543, 0xfb2c, 0x48d7, { 0xb7, 0x8d, 0x20, 0x39, 0x4f, 0x35, 0x53, 0x81 } };

// this is the GUID of the IME mode icon in Windows 8
// the value is not available in older SDK versions, so let's define it ourselves.
static const GUID _GUID_LBI_INPUTMODE =
Expand All @@ -90,6 +86,7 @@ TextService::TextService():
symbolsFileTime_(0),
chewingContext_(NULL) {

OutputDebugStringW(L"[chewing] Load config and start watching changes\n");
config_.load();
config_.watchChanges();

Expand Down Expand Up @@ -127,9 +124,6 @@ TextService::TextService():
}
addButton(imeModeIcon_);
}

// global compartment stuff
addCompartmentMonitor(g_configChangedGuid, true);
}

TextService::~TextService(void) {
Expand All @@ -155,8 +149,7 @@ CLSID TextService::clsid() {

// virtual
void TextService::onActivate() {
DWORD configStamp = globalCompartmentValue(g_configChangedGuid);
config().reloadIfNeeded(configStamp);
config().reloadIfNeeded();
initChewingContext();
updateLangButtons();
if(imeModeIcon_) // windows 8 IME mode icon
Expand Down Expand Up @@ -194,7 +187,24 @@ void TextService::onKillFocus() {
bool TextService::filterKeyDown(Ime::KeyEvent& keyEvent) {
Config& cfg = config();
if (cfg.reloadIfNeeded()) {
applyConfig();
// check if chewing context needs to be reloaded
bool chewingNeedsReload = false;
// check if symbols.dat file is changed
// get last mtime of symbols.dat file
std::wstring file = userDir() + L"\\symbols.dat";
struct _stat64 stbuf;
if(_wstat64(file.c_str(), &stbuf) == 0 && symbolsFileTime_ != stbuf.st_mtime) {
symbolsFileTime_ = stbuf.st_mtime;
chewingNeedsReload = true;
}
// re-create a new chewing context if needed
if(chewingNeedsReload && chewingContext_) {
freeChewingContext();
initChewingContext();
}
else {
applyConfig(); // apply the latest config
}
}
lastKeyDownCode_ = keyEvent.keyCode();
// return false if we don't need this key
Expand Down Expand Up @@ -634,37 +644,6 @@ bool TextService::onCommand(UINT id, CommandType type) {
return true;
}

// virtual
void TextService::onCompartmentChanged(const GUID& key) {
if(::IsEqualGUID(key, g_configChangedGuid)) {
// changes of configuration are detected
DWORD stamp = globalCompartmentValue(g_configChangedGuid);
config().reloadIfNeeded(stamp);

// check if chewing context needs to be reloaded
bool chewingNeedsReload = false;
// check if symbols.dat file is changed
// get last mtime of symbols.dat file
std::wstring file = userDir() + L"\\symbols.dat";
struct _stat64 stbuf;
if(_wstat64(file.c_str(), &stbuf) == 0 && symbolsFileTime_ != stbuf.st_mtime) {
symbolsFileTime_ = stbuf.st_mtime;
chewingNeedsReload = true;
}

// re-create a new chewing context if needed
if(chewingNeedsReload && chewingContext_) {
freeChewingContext();
initChewingContext();
}
else {
applyConfig(); // apply the latest config
}
return;
}
Ime::TextService::onCompartmentChanged(key);
}

// called when the keyboard is opened or closed
// virtual
void TextService::onKeyboardStatusChanged(bool opened) {
Expand Down
3 changes: 0 additions & 3 deletions chewing_tip/ChewingTextService.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ class TextService: public Ime::TextService {

virtual bool onCommand(UINT id, CommandType type);

// called when a compartment value is changed
virtual void onCompartmentChanged(const GUID& key);

// called when the keyboard is opened or closed
virtual void onKeyboardStatusChanged(bool opened);

Expand Down
Loading

0 comments on commit fe62bac

Please sign in to comment.