Skip to content

Commit

Permalink
Updated the debug header to allow to print to dbgview
Browse files Browse the repository at this point in the history
Fixed format string for DWORD

Added ability to OutputDebugString to windows with formatting

Changed format to correctly read variable.

Allow the DLL to read shared memory and some cleanup.

Cleanup some TODO's
  • Loading branch information
DemiRom committed Feb 2, 2024
1 parent 70e4cba commit e34e089
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 50 deletions.
24 changes: 18 additions & 6 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DWORD readConfigFile()
return ERROR_INVALID_HANDLE;
}

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

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

Expand Down Expand Up @@ -95,7 +95,17 @@ void getConfigFilePath()
//TODO: We don't check other results possible results, i.e E_FAIL
HRESULT getAppDataPathResult = SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, &szConfigFilePath);

if(!SUCCEEDED(getAppDataPathResult)) {
if(!SUCCEEDED(getAppDataPathResult)) {
switch(getAppDataPathResult) {
case E_FAIL:
SetLastError(ERROR_DIRECTORY_NOT_SUPPORTED);
break;
case E_INVALIDARG:
SetLastError(ERROR_TIERING_INVALID_FILE_ID);
break;
default:
SetLastError(ERROR_GEN_FAILURE);
}
reportWin32Error(L"Could not get the users appdata directory");
cleanupConfigReader();
exit(ERROR_PATH_NOT_FOUND);
Expand All @@ -112,7 +122,9 @@ void getConfigFilePath()
break;
case STRSAFE_E_INSUFFICIENT_BUFFER:
SetLastError(ERROR_INSUFFICIENT_BUFFER);
break;
break;
default:
SetLastError(ERROR_GEN_FAILURE);
}
reportWin32Error(L"Could not append file name to appdata path");
cleanupConfigReader();
Expand Down Expand Up @@ -197,7 +209,7 @@ BOOL loadDefaultConfigResourceData(HINSTANCE resourceModuleHandle)
if(hRes == NULL)
{
puts("Could not get HRSRC Handle");
DEBUG_PRINT("FindResource Error: %i", GetLastError());
DEBUG_PRINT("FindResource Error: %lu", GetLastError());
return FALSE;
}

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

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

Expand Down
28 changes: 26 additions & 2 deletions debug.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
#pragma once

#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__)

#include <stdio.h>
#include <stdarg.h>

inline void DebugPrintHelper(const char* format, ...) {
char buffer[1024];
va_list args;
va_start(args, format);
vsprintf_s(buffer, 1024, format, args);
va_end(args);
OutputDebugStringA(buffer);
}

inline void DebugWPrintHelper(const wchar_t* format, ...) {
wchar_t buffer[1024];
va_list args;
va_start(args, format);
vswprintf_s(buffer, 1024, format, args);
va_end(args);
OutputDebugStringW(buffer);
}

#define DEBUG_PRINT(format, ...) printf("DEBUG: %s Line %d: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
DebugPrintHelper(format, ##__VA_ARGS__)
#define DEBUG_WPRINT(format, ...) wprintf(L"DEBUG: %ls Line %d: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
DebugWPrintHelper(format, ##__VA_ARGS__)
#else
#define DEBUG_PRINT(format, ...)
#define DEBUG_WPRINT(format, ...)
Expand Down
14 changes: 6 additions & 8 deletions keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
#include "debug.h"

UINT getModifier(char* value);
UINT getKeyCode(char* value);
UINT getKeyCode(const char* value);
void addKeyboardKeybind(enum Action action, UINT modifier, UINT keyCode);

BOOL initializeKeyboardConfig(ConfigItems* configItems)
BOOL initializeKeyboardConfig(const ConfigItems* configItems)
{
assert(configItems != NULL);
assert(configItems->configItem != NULL);
assert(configItems->configItemsCount != 0);

for(size_t i = 0; i < configItems->configItemsCount; i++) {
//TODO Modify the macro to use generate all these automatically.
for(size_t i = 0; i < configItems->configItemsCount; i++) {
ADD_KEYBOARD_KEYBIND(i, WORKSPACE_1);
ADD_KEYBOARD_KEYBIND(i, WORKSPACE_2);
ADD_KEYBOARD_KEYBIND(i, WORKSPACE_3);
Expand Down Expand Up @@ -55,7 +54,7 @@ UINT getModifier(char* value)
return MOD_ALT;
}

UINT getKeyCode(char* value)
UINT getKeyCode(const char* value)
{
DEBUG_PRINT("GetKeyCode char value '%c'", value[strlen(value) - 1]);
return VkKeyScanEx(value[strlen(value) - 1], GetKeyboardLayout(0));
Expand All @@ -82,9 +81,8 @@ 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
{
// 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");
Expand Down
77 changes: 55 additions & 22 deletions wm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#include "error.h"
#include "tiling.h"
#include "keyboard.h"
#include "config.h"
#include "messages.h"

#include "debug.h"
#include "debug.h"
#include "tiling.h"

HMODULE wmDll;
HHOOK hookShellProcHandle;

typedef BOOL (*SetMessageThreadIdType)(DWORD);

void cleanupObjects() {
cleanupKeyboard();

Expand All @@ -39,9 +36,54 @@ void ctrlc(int sig) {
exit(ERROR_SUCCESS);
}

LPVOID createAddressSharedMemory() {
// Create a shared memory region
HANDLE hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
sizeof(DWORD),
"lightwmthreadid"
);

if (hMapFile == NULL) {
DEBUG_PRINT("Could not create file mapping object (%lu).", GetLastError());
return NULL;
}

LPVOID lpMapAddress = MapViewOfFile(
hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
sizeof(DWORD)
);

if (lpMapAddress == NULL) {
DEBUG_PRINT("Could not map view of file (%lu).", GetLastError());
CloseHandle(hMapFile);
return NULL;
}

return lpMapAddress;
}

int main() {
SetProcessDPIAware();

//Create shared memory
LPVOID sharedMemoryAddress = createAddressSharedMemory();

if(sharedMemoryAddress == NULL) {
reportWin32Error(L"Create Shared Memory");
goto cleanup;
}

DWORD dwThreadId = GetCurrentThreadId();
DEBUG_PRINT("Lightwm.exe thread id: %lu", dwThreadId);
CopyMemory((PVOID)sharedMemoryAddress, &dwThreadId, sizeof(DWORD));

wmDll = LoadLibraryW(L"lightwm_dll");

if (wmDll == NULL) {
Expand All @@ -62,18 +104,9 @@ int main() {
reportWin32Error(L"SetWindowsHookExW for shell hook");
goto cleanup;
}

SetMessageThreadIdType setMessageThreadId = (SetMessageThreadIdType)GetProcAddress(wmDll, "SetMessageThreadId");

if (setMessageThreadId == NULL) {
reportWin32Error(L"GetProcAddress for setMessageThreadId");
goto cleanup;
}

setMessageThreadId(GetCurrentThreadId());

signal(SIGINT, ctrlc);

//Load the configuration
if(loadConfigFile(NULL) != ERROR_SUCCESS)
{
Expand All @@ -86,21 +119,21 @@ int main() {
reportWin32Error(L"Setup keyboard config");
goto cleanup;
}

// Handle a message loop
//tileWindows();
MSG msg;
while (GetMessage(&msg, -1, 0, 0) != 0) {
tileWindows();
MSG msg;
while (GetMessage(&msg, (HWND)-1, 0, 0) != 0) {
switch(msg.message)
{
case WM_HOTKEY:
LRESULT ret = handleHotkey(msg.wParam, msg.lParam);
case WM_HOTKEY:
const LRESULT ret = handleHotkey(msg.wParam, msg.lParam);
if(ret != ERROR_SUCCESS) {
DEBUG_PRINT("HotKey was unhandled! Ret: %lli", ret);
}
break;
case LWM_WINDOW_EVENT:
//tileWindows();
tileWindows();
DEBUG_PRINT("LWM_WINDOW_EVENT Message handled");
break;
}
Expand Down
49 changes: 38 additions & 11 deletions wm_dll.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,62 @@

#include "debug.h"

DWORD messageThreadId = 0;
DWORD lightwmMainThreadId = 0;

void readAddress() {
// Open the shared memory region
HANDLE hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
"lightwmthreadid" // name of mapping object
);

if (hMapFile == NULL) {
DEBUG_PRINT("Could not open file mapping object (%lu).", GetLastError());
return;
}

LPVOID lpMapAddress = MapViewOfFile(
hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
sizeof(DWORD)
);

if (lpMapAddress == NULL) {
DEBUG_PRINT("Could not map view of file (%lu).", GetLastError());
CloseHandle(hMapFile);
return;
}

CopyMemory(&lightwmMainThreadId, (PVOID)lpMapAddress, sizeof(DWORD));

UnmapViewOfFile(lpMapAddress);
CloseHandle(hMapFile);
}

__declspec(dllexport) LRESULT CALLBACK ShellProc(int code, WPARAM wparam, LPARAM lparam) {
DEBUG_PRINT("ShellProc hook called with a thread id of %i", messageThreadId); //TODO Cleanup this debug
if (code == HSHELL_WINDOWCREATED || code == HSHELL_WINDOWDESTROYED) {
PostThreadMessageW(messageThreadId, LWM_WINDOW_EVENT, 0, 0);
PostThreadMessageW(lightwmMainThreadId, LWM_WINDOW_EVENT, 0, 0);
}

return CallNextHookEx(NULL, code, wparam, lparam);
}

__declspec(dllexport) void SetMessageThreadId(DWORD threadId)
{
DEBUG_PRINT("Set thread ID to %i", threadId); //TODO Cleanup this debug string
messageThreadId = threadId;
}

BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ulReasonForCall, LPVOID lpReserved)
{
switch(ulReasonForCall) {
case DLL_PROCESS_ATTACH:
DEBUG_PRINT("DLL Loaded");
DEBUG_PRINT("DLL Loaded");
readAddress();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
DEBUG_PRINT("DLL Freed");
DEBUG_PRINT("DLL Freed");
break;
default:
break;
Expand Down
1 change: 0 additions & 1 deletion wm_dll.def
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
LIBRARY lightwm_dll
EXPORTS
SetMessageThreadId
ShellProc

0 comments on commit e34e089

Please sign in to comment.