commit 1c3e1b095f05efec354e797b151613cb33cc8a1d
parent c60cd269ba0a7b2df30c88459e0ef1dcaa9a874f
Author: theSoberSobber <akshatshrivastava0010@gmail.com>
Date: Fri, 1 Dec 2023 23:44:12 +0530
parent c60cd269ba0a7b2df30c88459e0ef1dcaa9a874f
Author: theSoberSobber <akshatshrivastava0010@gmail.com>
Date: Fri, 1 Dec 2023 23:44:12 +0530
add stuff
15 files changed, 233 insertions(+), 147 deletions(-)
diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +a.exe +main.o +sxhkd-win32.exe
diff --git a/Makefile b/Makefile @@ -0,0 +1,22 @@ +DEBUG = +CFLAGS = -w -Wall -Os ${DEBUG} +LDFLAGS = -s -mwindows +CC = gcc + +SRC = main.c +OBJ = ${SRC:.c=.o} + +all: options sxhkd-win32 + +options: + @echo sxhkd-win32 build options: + @echo "CFLAGS = ${CFLAGS}" + @echo "LDFLAGS = ${LDFLAGS}" + @echo "CC = ${CC}" + +sxhkd-win32: ${OBJ} + @echo "${CC} -o $@" + @${CC} -o $@ ${OBJ} ${LDFLAGS} + +clean: + rm -f ${OBJ} sxhkd-win32
diff --git a/README.md b/README.md @@ -1,6 +1,36 @@ # sxhkd-win32 -sxhkd impl for windows, cus ahk sucks. +sxhkd impl for windows, cus why not. + +# Instructions +- Edit config.h and actions.h according to what you want +- Build using `make all` or `make` +- Copy the produced `sxhkd-win32.exe` to your location of choice +- Run `make clean` in the working directory to clean it + +# Sample config.h + +```c +#define MODKEY (MOD_ALT) +// options: MOD_WIN | MOD_CTRL | MOD_SHIFT | MOD_ALT +// can be combined by binary OR operator + +// Use MODKEY | MOD_SHIFT for mod + shift +// VK_RETURN == RETURN KEY on Keyboard + +// command, argument +static const char *termcmd[] = { "cmd.exe", NULL }; +static const char *vscode[] = {"code", "D:/code"}; + +static Key keys[] = { + /* modifier key function argument */ + { MODKEY, 'B', test, {0} }, + { MODKEY, 'P', spawn, {.v = vscode} }, + { MODKEY, VK_RETURN, spawn, {.v = termcmd } }, + { MODKEY, 'E', toggleexplorer, {0} }, + { MODKEY | MOD_SHIFT, 'Q', quit, {0} } +}; +``` # TODO -- structure correctly -- decide on config and see how similar can it be to sxhkd +- Installer (or start menu + program files) +- Scheduler
diff --git a/actions.cpp b/actions.cpp @@ -1,9 +0,0 @@ -#ifdef DEBUG -#include "debug.h" -#else -#define dbg -#endif - -void test() { - dbg("Test"); -}- \ No newline at end of file
diff --git a/actions.h b/actions.h @@ -1 +1,4 @@ -static void test();- \ No newline at end of file +static void test(const Arg* arg); +static void quit(const Arg* arg); +static void spawn(const Arg* arg); +static void toggleexplorer(const Arg* arg);+ \ No newline at end of file
diff --git a/compile.bat b/compile.bat @@ -1 +0,0 @@ -gcc ./hotKeyTry.c -DDEBUG -mwindows
diff --git a/config.h b/config.h @@ -0,0 +1,19 @@ +#define MODKEY (MOD_ALT) +// options: MOD_WIN | MOD_CTRL | MOD_SHIFT | MOD_ALT +// can be combined by binary OR operator + +// Use MODKEY | MOD_SHIFT for mod + shift +// VK_RETURN == RETURN KEY on Keyboard + +// command, argument +static const char *termcmd[] = { "cmd.exe", NULL }; +static const char *vscode[] = {"code", "D:/code"}; + +static Key keys[] = { + /* modifier key function argument */ + { MODKEY, 'B', test, {0} }, + { MODKEY, 'P', spawn, {.v = vscode} }, + { MODKEY, VK_RETURN, spawn, {.v = termcmd } }, + { MODKEY, 'E', toggleexplorer, {0} }, + { MODKEY | MOD_SHIFT, 'Q', quit, {0} } +};
diff --git a/debug.h b/debug.h @@ -1,7 +1,11 @@ +#ifdef DEBUG void dbg(const char *errstr, ...) { va_list ap; va_start(ap, errstr); vfprintf(stdout, errstr, ap); fflush(stdout); va_end(ap); -}- \ No newline at end of file +} +#else +#define dbg +#endif+ \ No newline at end of file
diff --git a/hotKeyTry.c b/hotKeyTry.c @@ -1,128 +0,0 @@ -#include <windows.h> -#include <winuser.h> -#include <stdlib.h> -#include <stdio.h> -#include <shellapi.h> -#include <stdbool.h> - -#define NAME "sxhkd-win32" /* Used for window name/class */ - -// options: MOD_CTRL | MOD_SHIFT | MOD_ALT -#define MODKEY MOD_WIN -#define HOTKEY_ID 1 - -void eprint(const char *errstr, ...) { - va_list ap; - va_start(ap, errstr); - vfprintf(stdout, errstr, ap); - fflush(stdout); - va_end(ap); -} - -HWND dwmhwnd; - -#ifdef DEBUG -#define dbg eprint -#else -#define dbg -#endif - -void quit(const Arg *arg) { - PostMessage(dwmhwnd, WM_CLOSE, 0, 0); -} - -void test() { - dbg("Toggling Explorer!"); -} - -void cleanup() { - UnregisterHotKey(dwmhwnd, HOTKEY_ID); - DestroyWindow(dwmhwnd); -} - -void die(const char *errstr, ...) { - va_list ap; - va_start(ap, errstr); - vfprintf(stdout, errstr, ap); - fflush(stdout); - va_end(ap); - cleanup(); - exit(EXIT_FAILURE); -} - -LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { - switch (msg) { - case WM_CREATE: - break; - case WM_CLOSE: - cleanup(); - break; - case WM_DESTROY: - PostQuitMessage(0); - break; - case WM_HOTKEY: - if (wParam == HOTKEY_ID) { - dbg("\nHotkey was pressed!\n"); - test(); - } - break; - default: - return DefWindowProc(hwnd, msg, wParam, lParam); - } - return 0; -} - - -void setup(HINSTANCE hInstance) { - WNDCLASSEX winClass; - - winClass.cbSize = sizeof(WNDCLASSEX); - winClass.style = 0; - winClass.lpfnWndProc = WndProc; - winClass.cbClsExtra = 0; - winClass.cbWndExtra = 0; - winClass.hInstance = hInstance; - winClass.hIcon = NULL; - winClass.hIconSm = NULL; - winClass.hCursor = NULL; - winClass.hbrBackground = NULL; - winClass.lpszMenuName = NULL; - winClass.lpszClassName = NAME; - - if (!RegisterClassEx(&winClass)) die("Error registering window class\n"); - dwmhwnd = CreateWindowEx(0, NAME, NAME, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL); - if (!dwmhwnd) die("Error creating window\n"); - - RegisterHotKey(dwmhwnd, HOTKEY_ID, MODKEY, (unsigned int)'4'); -} - - -int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { - -#ifdef DEBUG - extern char ** __argv; - extern int __argc; - - // This attaches a console to the parent process if it has a console - if (AttachConsole(ATTACH_PARENT_PROCESS)) { - // reopen stout handle as console window output - freopen("CONOUT$", "wb", stdout); - // reopen stderr handle as console window output - freopen("CONOUT$", "wb", stderr); - } -#endif - - dbg("Hello!! :))\n"); - - MSG msg; - - setup(hInstance); - - while (GetMessage(&msg, NULL, 0, 0) > 0) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - cleanup(); - - return msg.wParam; -}
diff --git a/main.c b/main.c @@ -0,0 +1,140 @@ +#include <windows.h> +#include <winuser.h> +#include <stdlib.h> +#include <stdio.h> +#include <shellapi.h> +#include <stdbool.h> + +#define NAME "sxhkd-win32" +#define LENGTH(x) (sizeof x / sizeof x[0]) + +typedef union { + int i; + unsigned int ui; + float f; + void *v; +} Arg; + +typedef struct { + unsigned int mod; + unsigned int key; + void (*func)(const Arg *); + const Arg arg; +} Key; + +#include "debug.h" +#include "actions.h" +#include "config.h" + +HWND dwmhwnd; + +/* Actions Implementation */ + +void quit(const Arg* arg) { + PostMessage(dwmhwnd, WM_CLOSE, 0, 0); +} + +void test(const Arg* arg) { + dbg("Hello from handler!\n"); +} + +void spawn(const Arg *arg) { + ShellExecute(NULL, NULL, ((char **)arg->v)[0], ((char **)arg->v)[1], NULL, SW_SHOWDEFAULT); +} + +void toggleexplorer(const Arg *arg) { +#define setvisibility(x, y) SetWindowPos(x, 0, 0, 0, 0, 0, (y ? SWP_SHOWWINDOW : SWP_HIDEWINDOW) | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER); + HWND hwnd = FindWindow("Progman", "Program Manager"); + if (hwnd) setvisibility(hwnd, !IsWindowVisible(hwnd)); + + hwnd = FindWindow("Shell_TrayWnd", NULL); + if (hwnd) setvisibility(hwnd, !IsWindowVisible(hwnd)); +} + +/* Actions End here */ + +void cleanup() { + for (int i = 0; i < LENGTH(keys); i++) UnregisterHotKey(dwmhwnd, i + 1); + DestroyWindow(dwmhwnd); +} + +void die(const char *errstr, ...) { + va_list ap; + va_start(ap, errstr); + dbg(errstr, ap); + va_end(ap); + cleanup(); + exit(EXIT_FAILURE); +} + +LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { + switch (msg) { + case WM_CREATE: + break; + case WM_CLOSE: + cleanup(); + break; + case WM_DESTROY: + PostQuitMessage(0); + break; + case WM_HOTKEY: + if (wParam > 0 && wParam <= LENGTH(keys)) { + dbg("\nHotkey was pressed!\n"); + keys[wParam - 1].func(&(keys[wParam - 1].arg)); + } + break; + default: + return DefWindowProc(hwnd, msg, wParam, lParam); + } + return 0; +} + + +void setup(HINSTANCE hInstance) { + WNDCLASSEX winClass; + + winClass.cbSize = sizeof(WNDCLASSEX); + winClass.style = 0; + winClass.lpfnWndProc = WndProc; + winClass.cbClsExtra = 0; + winClass.cbWndExtra = 0; + winClass.hInstance = hInstance; + winClass.hIcon = NULL; + winClass.hIconSm = NULL; + winClass.hCursor = NULL; + winClass.hbrBackground = NULL; + winClass.lpszMenuName = NULL; + winClass.lpszClassName = NAME; + + if (!RegisterClassEx(&winClass)) die("Error registering window class\n"); + dwmhwnd = CreateWindowEx(0, NAME, NAME, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL); + if (!dwmhwnd) die("Error creating window\n"); + for (int i = 0; i < LENGTH(keys); i++) RegisterHotKey(dwmhwnd, i + 1, keys[i].mod, keys[i].key); +} + + +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { + +#ifdef DEBUG + extern char ** __argv; + extern int __argc; + + if (AttachConsole(ATTACH_PARENT_PROCESS)) { + freopen("CONOUT$", "wb", stdout); + freopen("CONOUT$", "wb", stderr); + } +#endif + + dbg("Hello!! : ))\n"); + + MSG msg; + setup(hInstance); + + while (GetMessage(&msg, NULL, 0, 0) > 0) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + cleanup(); + + return msg.wParam; +}+ \ No newline at end of file
diff --git a/old-scripts/compile.bat b/old-scripts/compile.bat @@ -0,0 +1,2 @@ +@echo off +gcc ./main.c -DDEBUG -mwindows
diff --git a/old-scripts/run.bat b/old-scripts/run.bat @@ -0,0 +1,2 @@ +@echo off +start /w a.exe
diff --git a/run.bat b/run.bat @@ -1 +0,0 @@ -start /w a.exe
: nply/dwhkd