Skip to content

Commit

Permalink
Moved handling hotkeys to the keyboard
Browse files Browse the repository at this point in the history
Rework how messages are handled

Found typo in Makefile for release builds

Fixed format string to handle values properly.

Reworking the config since there is a memory leak

Config works now works as expected.

Removed unused resource header include in the main application code.

GetMessage with a -1 as second param get all thread messages not window.

Cleanup comments
  • Loading branch information
DemiRom committed Jan 31, 2024
1 parent 7ed35af commit b54bad3
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 150 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CC = cl
LD = link
RC = rc
CFLAGS = /EHsc
CFLAGS =

EXE_SRCS = wm.c tiling.c error.c config.c keyboard.c
DLL_SRCS = error.c wm_dll.c
DLL_SRCS = wm_dll.c error.c
EXE_NAME = lightwm.exe
DLL_NAME = lightwm_dll.dll
EXE_RC = wm_resources.obj
Expand Down Expand Up @@ -52,7 +52,7 @@ $(DBGDIR)/%.obj: %.rc
#
release: prep $(RELEXE) $(RELDLL)

$(RELEXE): $(REL_EXE_OBJS) $(RELDIR)/$(EXE_RESS)
$(RELEXE): $(REL_EXE_OBJS) $(RELDIR)/$(EXE_RC)
$(CC) $(RELCFLAGS) /Fe:$@ $^ /link user32.lib shell32.lib ole32.lib shlwapi.lib

$(RELDLL): $(REL_DLL_OBJS)
Expand Down
115 changes: 63 additions & 52 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@

#define BUFF_SIZE 65536

PWSTR szConfigFilePath[MAX_PATH];
PWSTR szConfigFilePath;

char* defaultConfigData = NULL;

//Should probably create a meta structure that holds the total count for now just another global variable
ConfigItems configItems;
ConfigItems* configItems;

BOOL createDefaultConfigFile(HINSTANCE);
BOOL loadDefaultConfigResourceData(HINSTANCE);
BOOL writeDefaultConfigDataToFile();
void trim(char* str);
void removeControlChars(char* str);
size_t getLineCount(FILE* file);


void freeConfigItems(ConfigItems* items);

DWORD readConfigFile()
{
assert(szConfigFilePath != NULL);

//Try to open the config file based on the path
FILE* configFileHandle = _wfopen(*szConfigFilePath, L"r");
FILE* configFileHandle = _wfopen(szConfigFilePath, L"r");

if(configFileHandle == NULL)
{
Expand All @@ -41,11 +45,13 @@ DWORD readConfigFile()
}

char line[BUFF_SIZE]; //TODO must have a more clever way of getting a line length

configItems.configItem = (ConfigItem*)malloc(sizeof(ConfigItem) * getLineCount(configFileHandle) + 1);
configItems.configItemsCount = getLineCount(configFileHandle) + 1;

configItems = (ConfigItems*)malloc(sizeof(ConfigItems));

configItems->configItem = (ConfigItem*)malloc(sizeof(ConfigItem) * getLineCount(configFileHandle) + 1);
configItems->configItemsCount = getLineCount(configFileHandle) + 1;

if(configItems.configItem == NULL)
if(configItems->configItem == NULL)
{
reportWin32Error(L"Allocation ConfigItem memory");
cleanupConfigReader();
Expand All @@ -61,20 +67,20 @@ DWORD readConfigFile()

//Get the first half of the line
char* token = strtok(line, " ");
configItems.configItem[lineCount].name = (char*)malloc(strlen(token) + 1);
strncpy(configItems.configItem[lineCount].name, token, strlen(token) + 1);
configItems->configItem[lineCount].name = (char*)malloc(strlen(token) + 1);
strncpy(configItems->configItem[lineCount].name, token, strlen(token) + 1);

//Get the second half of the line
token = strtok(NULL, " ");
removeControlChars(token);
configItems.configItem[lineCount].value = (char*)malloc(strlen(token) + 1);
strncpy(configItems.configItem[lineCount].value, token, strlen(token) + 1);
configItems->configItem[lineCount].value = (char*)malloc(strlen(token) + 1);
strncpy(configItems->configItem[lineCount].value, token, strlen(token) + 1);

DEBUG_PRINT("Name: %s Value: %s Name LEN: %lu Value LEN: %lu Count: %lu",
configItems.configItem[lineCount].name,
configItems.configItem[lineCount].value,
strlen(configItems.configItem[lineCount].name),
strlen(configItems.configItem[lineCount].value),
DEBUG_PRINT("Name: %s Value: %s Name LEN: %zu Value LEN: %zu Count: %zu",
configItems->configItem[lineCount].name,
configItems->configItem[lineCount].value,
strlen(configItems->configItem[lineCount].name),
strlen(configItems->configItem[lineCount].value),
lineCount);
}

Expand All @@ -85,17 +91,19 @@ DWORD readConfigFile()
}

void getConfigFilePath()
{
{
//TODO: We don't check other results possible results, i.e E_FAIL
HRESULT getAppDataPathResult = SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, szConfigFilePath);

HRESULT getAppDataPathResult = SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &szConfigFilePath);
if(!SUCCEEDED(getAppDataPathResult)) {
reportWin32Error(L"Could not get the users appdata directory");
CoTaskMemFree(szConfigFilePath);
cleanupConfigReader();
exit(ERROR_PATH_NOT_FOUND);
}

HRESULT concatStringResult = StringCchCatW(*szConfigFilePath, MAX_PATH, L"\\lightwm.config");
assert(szConfigFilePath != NULL);

HRESULT concatStringResult = StringCchCatW(szConfigFilePath, MAX_PATH, L"\\lightwm.config");

if(!SUCCEEDED(concatStringResult)) {
switch(concatStringResult){
Expand All @@ -107,16 +115,18 @@ void getConfigFilePath()
break;
}
reportWin32Error(L"Could not append file name to appdata path");
CoTaskMemFree(szConfigFilePath);
cleanupConfigReader();
exit(GetLastError());
}
}
}

uint8_t loadConfigFile(HINSTANCE resourceModuleHandle)
{
szConfigFilePath = (PWSTR)malloc(sizeof(WCHAR) * MAX_PATH);
getConfigFilePath();

if(!PathFileExistsW(*szConfigFilePath))
assert(szConfigFilePath != NULL);

if(!PathFileExistsW(szConfigFilePath))
{
if(!createDefaultConfigFile(resourceModuleHandle))
{
Expand All @@ -130,34 +140,35 @@ uint8_t loadConfigFile(HINSTANCE resourceModuleHandle)
return ERROR_SUCCESS;
}

void cleanupConfigReader()
{
CoTaskMemFree(szConfigFilePath);

if(configItems.configItem) //TODO Cleanup the name and value pointers for the strings
{
for(size_t i = 0; i < configItems.configItemsCount; i++)
{
if(&configItems.configItem[i] != NULL)
{
if(configItems.configItem[i].name)
{
free(configItems.configItem[i].name);
}

if(configItems.configItem[i].value)
{
free(configItems.configItem[i].value);
}
void freeConfigItems(ConfigItems* items) {
if(items != NULL) {
for (size_t i = 0; i < items->configItemsCount; i++) {
if(&items->configItem[i] != NULL)
{
free(items->configItem[i].name);
free(items->configItem[i].value);
}
}
free(configItems.configItem);
if(items->configItem != NULL)
{
free(items->configItem);
}
free(items);
}
}

void cleanupConfigReader()
{
freeConfigItems(configItems);
DEBUG_PRINT("Cleaned up config items");

CoTaskMemFree(szConfigFilePath);
DEBUG_PRINT("Cleaned up filePath ptr");
}

ConfigItems* getConfigItems()
{
return &configItems;
return configItems;
}

/**
Expand Down Expand Up @@ -186,7 +197,7 @@ BOOL loadDefaultConfigResourceData(HINSTANCE resourceModuleHandle)
if(hRes == NULL)
{
puts("Could not get HRSRC Handle");
printf("%s %i\n", "FindResource Error: ", GetLastError());
DEBUG_PRINT("FindResource Error: %i", GetLastError());
return FALSE;
}

Expand All @@ -195,7 +206,7 @@ BOOL loadDefaultConfigResourceData(HINSTANCE resourceModuleHandle)
if(hData == NULL)
{
puts("Could not load resource");
printf("%s %i\n", "LoadResource Error: ", GetLastError());
DEBUG_PRINT("LoadResource Error: %i", GetLastError());
return FALSE;
}

Expand All @@ -204,7 +215,7 @@ BOOL loadDefaultConfigResourceData(HINSTANCE resourceModuleHandle)
if(defaultConfigResourceData == NULL)
{
puts("Could not read resource");
printf("%s %i\n", "LockResource Error: ", GetLastError());
DEBUG_PRINT("LockResource Error: ", GetLastError());
return FALSE;
}

Expand All @@ -217,7 +228,7 @@ BOOL loadDefaultConfigResourceData(HINSTANCE resourceModuleHandle)

BOOL writeDefaultConfigDataToFile()
{
FILE* configFileHandle = _wfopen(*szConfigFilePath, L"w");
FILE* configFileHandle = _wfopen(szConfigFilePath, L"w");

if(configFileHandle == NULL)
{
Expand Down
2 changes: 2 additions & 0 deletions debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#ifdef DEBUG
#define DEBUG_PRINT(format, ...) printf("DEBUG: %s Line %d: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#define DEBUG_WPRINT(format, ...) wprintf(L"DEBUG: %ls Line %d: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define DEBUG_PRINT(format, ...)
#define DEBUG_WPRINT(format, ...)
#endif
42 changes: 42 additions & 0 deletions keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void cleanupKeyboard()
UnregisterHotKey(NULL, WINDOW_DOWN);
UnregisterHotKey(NULL, WINDOW_LEFT);
UnregisterHotKey(NULL, WINDOW_RIGHT);
DEBUG_PRINT("Unregistered all hotkeys");
}

UINT getModifier(char* value)
Expand Down Expand Up @@ -74,4 +75,45 @@ void addKeyboardKeybind(enum Action action, UINT modifier, UINT keyCode)
}

DEBUG_PRINT("Registered %s hotkey", ACTION_STRINGS[action]);
}

LRESULT handleHotkey(WPARAM wparam, LPARAM lparam)
{
DEBUG_PRINT("handleHotkey called - %lli %lli", wparam, lparam);

switch(wparam)
{
//TODO Can either trigger an event like the ShellProc callback, or handle directly.
// one method to handle virtual desktops is using the IVirtualDesktopManager in ShObjIdl but
// that is only available for Window 10 1809 or later.
case WORKSPACE_1:
puts("Switch to workspace 1");
break;
case WORKSPACE_2:
puts("Switch to workspace 2");
break;
case WORKSPACE_3:
puts("Switch to workspace 3");
break;
case WORKSPACE_4:
puts("Switch to workspace 4");
break;
case WINDOW_UP:
puts("Highlight window above");
break;
case WINDOW_DOWN:
puts("Highlight window below");
break;
case WINDOW_LEFT:
puts("Highlight window left");
break;
case WINDOW_RIGHT:
puts("Highlight window right");
break;
default:
DEBUG_PRINT("Unhandled hotkey message! Hotkey ID: %lli", wparam);
break;
}

return ERROR_SUCCESS;
}
1 change: 1 addition & 0 deletions keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ if(strcmp(configItems->configItem[i].name, ACTION_STRINGS[action]) == 0) \

BOOL initializeKeyboardConfig(ConfigItems* configItems);
void cleanupKeyboard();
LRESULT handleHotkey(WPARAM lparam, LPARAM wparam);
3 changes: 3 additions & 0 deletions messages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//System reserves messages from range of 0x0000 through 0x03FF

#define LWM_WINDOW_EVENT 0x0400
Loading

0 comments on commit b54bad3

Please sign in to comment.