Skip to content

Commit

Permalink
Modified the Makefile to allow for debug builds
Browse files Browse the repository at this point in the history
The debug header is also modified to only print when in debug build.

Moved resources to main exe and moved config handling to main exe

Created a custom callback to handle hotkey presses.

Fixed a bug where I was not passing the correct value to RegisterHotkey

RegisterHotkey requires a virtual key scan code not a char as I initially believed.
  • Loading branch information
DemiRom committed Jan 30, 2024
1 parent 875b0b9 commit 216fe62
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 284 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
*.obj
*.exp
*.exe
*.pdb
*.ilk

tags

.idea/
.vs/
wm.ilk

*.pdb

*.ilk
debug/
release/
94 changes: 72 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,84 @@
CC = cl
LD = link
RC = rc
CC = cl
LD = link
RC = rc
CFLAGS = /EHsc

EXEC = lightwm.exe
DLL = lightwm_dll.dll
RESOURCES=wm_dll_resources.obj
EXE_SRCS = wm.c tiling.c error.c config.c keyboard.c
DLL_SRCS = error.c wm_dll.c
EXE_NAME = lightwm.exe
DLL_NAME = lightwm_dll.dll
EXE_RC = wm_resources.obj
DLL_RC =

# Boiler plate
CFLAGS = /EHsc /Zi
LDFLAGS =
DBGDIR = debug
DBGEXE = $(DBGDIR)/$(EXE_NAME)
DBG_EXE_OBJS = $(addprefix $(DBGDIR)/, $(EXE_SRCS:.c=.obj))
DBG_DLL_OBJS = $(addprefix $(DBGDIR)/, $(DLL_SRCS:.c=.obj))
DBGDLL = $(DBGDIR)/$(DLL_NAME)
DBGCFLAGS = $(CFLAGS) /DDEBUG /Zi

EXE_SRCS = wm.c tiling.c error.c
DLL_SRCS = error.c config.c keyboard.c wm_dll.c
RELDIR = release
RELEXE = $(RELDIR)/$(EXE_NAME)
REL_EXE_OBJS = $(addprefix $(RELDIR)/, $(EXE_SRCS:.c=.obj))
REL_DLL_OBJS = $(addprefix $(RELDIR)/, $(DLL_SRCS:.c=.obj))
RELDLL = $(RELDIR)/$(DLL_NAME)
RELCFLAGS = $(CFLAGS) /Ox


EXE_OBJS = $(EXE_SRCS:.c=.obj)
DLL_OBJS = $(DLL_SRCS:.c=.obj)
.PHONY: all clean debug prep release

all: $(DLL) $(EXEC)
# Default build
all: clean prep release

$(EXEC): $(EXE_OBJS)
$(CC) $(CFLAGS) /Fe:$@ $^ /link user32.lib
#
# Debug rules
#
debug: clean prep $(DBGEXE) $(DBGDLL)

$(DLL): $(DLL_OBJS) $(RESOURCES)
$(CC) $(CFLAGS) /LD /Fe:$@ $^ /link user32.lib shell32.lib ole32.lib shlwapi.lib /DEF:wm_dll.def
$(DBGEXE): $(DBG_EXE_OBJS) $(DBGDIR)/$(EXE_RC)
$(CC) $(DBGCFLAGS) /Fe:$@ $^ /link user32.lib shell32.lib ole32.lib shlwapi.lib

%.obj: %.c
$(CC) $(CFLAGS) /c /Fo:$@ $<
$(DBGDLL): $(DBG_DLL_OBJS)
$(CC) $(DBGCFLAGS) /Fe:$@ $^ /LD /link user32.lib /DEF:wm_dll.def

%.obj: %.rc
$(DBGDIR)/%.obj: %.c
$(CC) $(DBGCFLAGS) /c /Fo:$@ $<

$(DBGDIR)/%.obj: %.rc
$(RC) /fo $@ $<

#
# Release rules
#
release: prep $(RELEXE) $(RELDLL)

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

$(RELDLL): $(REL_DLL_OBJS)
$(CC) $(RELCFLAGS) /Fe:$@ $^ /LD /link user32.lib /DEF:wm_dll.def

$(RELDIR)/%.obj: %.c
$(CC) $(RELCFLAGS) /c /Fo:$@ $<

$(RELDIR)/%.obj: %.rc
$(RC) /fo $@ $<

#
# Prep and clean rules
#
prep:
@echo off > temp.bat && \
echo IF NOT EXIST $(DBGDIR) mkdir $(DBGDIR) >> temp.bat && \
echo IF NOT EXIST $(RELDIR) mkdir $(RELDIR) >> temp.bat && \
temp.bat && \
del temp.bat

clean:
del *.obj *.exe *.dll *.lib *.exp
@echo off > temp.bat && \
echo IF EXIST $(DBGDIR) del /S /Q $(DBGDIR) >> temp.bat && \
echo IF EXIST $(RELDIR) del /S /Q $(RELDIR) >> temp.bat && \
echo IF EXIST $(DBGDIR) rd /S /Q $(DBGDIR) >> temp.bat && \
echo IF EXIST $(RELDIR) rd /S /Q $(RELDIR) >> temp.bat && \
temp.bat && \
del temp.bat
7 changes: 0 additions & 7 deletions actions.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/**
* Actions
*
* Demetry Romanowski
* [email protected]
**/

#pragma once

//Just add the action to this preprocessor macro to generate the ENUM and string array
Expand Down
60 changes: 34 additions & 26 deletions config.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
/**
* Config Reader and Parser
* This program will read the config for lightwm in the appdata directory
*
* Demetry Romanowski
* [email protected]
**/
#include "config.h"

#include <Windows.h>
Expand All @@ -22,18 +15,12 @@

#define BUFF_SIZE 65536

/**
* Config reader global vars
**/
PWSTR szConfigFilePath[MAX_PATH];
char* defaultConfigData = NULL;

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

/**
* Private prototypes here
**/
BOOL CreateDefaultConfigFile(HINSTANCE);
BOOL LoadDefaultConfigResourceData(HINSTANCE);
BOOL WriteDefaultConfigDataToFile();
Expand All @@ -48,7 +35,6 @@ DWORD ReadConfigFile()

if(configFileHandle == NULL)
{
SetLastError(ERROR_INVALID_HANDLE);
reportWin32Error(L"Config file could not be opened");
CleanupConfigReader();
return ERROR_INVALID_HANDLE;
Expand All @@ -61,14 +47,17 @@ DWORD ReadConfigFile()

if(configItems.configItem == NULL)
{
reportWin32Error(L"Allocation ConfigItem struct");
reportWin32Error(L"Allocation ConfigItem memory");
CleanupConfigReader();
return ERROR_NOT_ENOUGH_MEMORY;
}

for(size_t lineCount = 0; fgets(line, sizeof(line), configFileHandle); lineCount++) {
for(size_t lineCount = 0; fgets(line, sizeof(line), configFileHandle); lineCount++)
{
if(strlen(line) == 0)
{
continue;
}

//Get the first half of the line
char* token = strtok(line, " ");
Expand All @@ -81,7 +70,7 @@ DWORD ReadConfigFile()
configItems.configItem[lineCount].value = (char*)malloc(strlen(token) + 1);
strncpy(configItems.configItem[lineCount].value, token, strlen(token) + 1);

DEBUG_PRINT("DEBUG config.c: Name: %s Value: %s Name LEN: %lu Value LEN: %lu Count: %lu\n",
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),
Expand All @@ -101,7 +90,6 @@ void GetConfigFilePath()
HRESULT getAppDataPathResult = SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, NULL, szConfigFilePath);

if(!SUCCEEDED(getAppDataPathResult)) {
SetLastError(ERROR_PATH_NOT_FOUND); //SHGetKnownFolderPath does not set an error on fail so we set it manually
reportWin32Error(L"Could not get the users appdata directory");
CoTaskMemFree(szConfigFilePath);
exit(ERROR_PATH_NOT_FOUND);
Expand Down Expand Up @@ -132,7 +120,6 @@ uint8_t LoadConfigFile(HINSTANCE resourceModuleHandle)
{
if(!CreateDefaultConfigFile(resourceModuleHandle))
{
SetLastError(ERROR_RESOURCE_NOT_AVAILABLE);
reportWin32Error(L"Create a default config file");
return ERROR_RESOURCE_NOT_AVAILABLE; //TODO Maybe find a better error code here
}
Expand Down Expand Up @@ -179,10 +166,14 @@ ConfigItems* GetConfigItems()
BOOL CreateDefaultConfigFile(HINSTANCE resourceModuleHandle)
{
if(!LoadDefaultConfigResourceData(resourceModuleHandle))
{
return FALSE;
}

if(!WriteDefaultConfigDataToFile())
{
return FALSE;
}

return TRUE;
}
Expand Down Expand Up @@ -248,18 +239,27 @@ void strip(char* str)
char *start = str;
char *end = str + len - 1;

while (*start == ' ') start++;
while (*end == ' ') end--;
while (*start == ' ')
{
start++;
}

while (*end == ' ')
{
end--;
}

memmove(str, start, end - start + 1);
str[end - start + 1] = '\0';
}

void removeControlChars(char* str) {
void removeControlChars(char* str)
{
int i, j = 0;
char temp[1024]; // Assuming the maximum length of the string is 1024

for (i = 0; str[i] != '\0'; ++i) {
for (i = 0; str[i] != '\0'; ++i)
{
if ((unsigned char)str[i] < 32) continue; // Skip control characters
temp[j] = str[i];
++j;
Expand All @@ -277,15 +277,23 @@ size_t GetLineCount(FILE* file)
{
size_t res = fread(buf, 1, BUFF_SIZE, file);
if (ferror(file))
return -1;
{
return -1;
}

int i;
for(i = 0; i < res; i++)
if (buf[i] == '\n')
{
if (buf[i] == '\n')
{
counter++;
}
}

if (feof(file))
break;
{
break;
}
}

fseek(file, 0, SEEK_SET);
Expand Down
8 changes: 0 additions & 8 deletions config.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/**
* Config Reader and Parser
* This program will read the config for lightwm in the appdata directory
*
* Demetry Romanowski
* [email protected]
**/

#pragma once

#include <stdint.h>
Expand Down
6 changes: 2 additions & 4 deletions debug.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#define _DEBUG

#ifdef _DEBUG
#define DEBUG_PRINT(format, ...) printf("DEBUG: Line %d: " format "\n", __LINE__, ##__VA_ARGS__)
#ifdef DEBUG
#define DEBUG_PRINT(format, ...) printf("DEBUG: %s Line %d: " format "\n", __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define DEBUG_PRINT(format, ...)
#endif
Loading

0 comments on commit 216fe62

Please sign in to comment.