From 6ba49443d75fa047467de8a3953a18582ef6b993 Mon Sep 17 00:00:00 2001 From: papplampe Date: Thu, 28 May 2015 19:11:50 +0200 Subject: [PATCH] improved hotkey toggling; attempt to fix non exiting process problem --- Makefile | 2 +- virgo.c | 49 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 996c372..f6cab49 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ SRCS=virgo.c OBJS=$(SRCS:.c=.o) -CFLAGS=-O3 -nostdlib -fno-asynchronous-unwind-tables -fno-builtin -fno-ident -ffunction-sections -fdata-sections -Wall -DRELEASE=1 +CFLAGS=-O3 -nostdlib -fno-asynchronous-unwind-tables -fno-builtin -fno-ident -ffunction-sections -fdata-sections -Wall LIBS=-lgdi32 -lmsvcrt -luser32 -lshell32 -lkernel32 LDFLAGS=-static -nostdlib -fno-builtin -s -Wl,-e,__main,--gc-sections,-subsystem,windows $(LIBS) ARCH=32 diff --git a/virgo.c b/virgo.c index 4556132..47e0b28 100644 --- a/virgo.c +++ b/virgo.c @@ -14,6 +14,10 @@ #define stb__sbmaybegrow(a,n) (stb__sbneedgrow(a,(n)) ? stb__sbgrow(a,n) : 0) #define stb__sbgrow(a,n) ((a) = stb__sbgrowf((a), (n), sizeof(*(a)))) +#ifndef MOD_NOREPEAT +#define MOD_NOREPEAT 0x4000 +#endif + #define NUM_DESKTOPS 4 typedef struct { @@ -49,7 +53,7 @@ static void *stb__sbgrowf(void *arr, int increment, int itemsize) p[0] = m; return p+2; } else { - exit(1); + ExitProcess(1); return (void *)(2*sizeof(int)); } } @@ -115,6 +119,7 @@ static void trayicon_deinit(Trayicon *t) DeleteObject(t->hBitmap); ReleaseDC(t->dummyHWND, t->hdc); Shell_NotifyIcon(NIM_DELETE, &t->nid); + DestroyWindow(t->dummyHWND); } static void windows_mod(Windows *wins, int state) @@ -178,7 +183,7 @@ static void register_hotkey(int id, int mod, int vk) if(!RegisterHotKey(NULL, id, mod, vk)) { MessageBox(NULL, "could not register hotkey", "error", MB_ICONEXCLAMATION); - exit(1); + ExitProcess(1); } } @@ -220,9 +225,25 @@ static void virgo_update(Virgo *v) EnumWindows((WNDENUMPROC)&enum_func, (LPARAM)v); } +static void virgo_toggle_hotkeys(Virgo *v) +{ + int i; + v->handle_hotkeys = !v->handle_hotkeys; + if(v->handle_hotkeys) { + for(i=0; icurrent = 0; v->handle_hotkeys = 1; @@ -233,7 +254,7 @@ static void virgo_init(Virgo *v) register_hotkey(i*2+1, MOD_CONTROL|MOD_NOREPEAT, i+1+0x30); } register_hotkey(i*2, MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_NOREPEAT, 'Q'); - register_hotkey((i*2)+1, MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_NOREPEAT, 'S'); + register_hotkey(i*2+1, MOD_ALT|MOD_CONTROL|MOD_SHIFT|MOD_NOREPEAT, 'S'); trayicon_init(&v->trayicon); } @@ -275,12 +296,8 @@ static void virgo_go_to_desk(Virgo *v, int desk) trayicon_set(&v->trayicon, v->current+1); } -#ifdef RELEASE -extern int __main(void) asm("__main"); -int __main(void) -#else -int main(int argc, char **argv) -#endif +extern void __main(void) asm("__main"); +void __main(void) { Virgo v; MSG msg; @@ -289,12 +306,12 @@ int main(int argc, char **argv) if(msg.message != WM_HOTKEY) { continue; } - if(msg.wParam == (NUM_DESKTOPS*2)+1) { - v.handle_hotkeys = !v.handle_hotkeys; + if(msg.wParam == NUM_DESKTOPS*2) { + break; + } + if(msg.wParam == NUM_DESKTOPS*2+1) { + virgo_toggle_hotkeys(&v); } else if(v.handle_hotkeys) { - if(msg.wParam == NUM_DESKTOPS*2) { - break; - } if(msg.wParam%2 == 0) { virgo_go_to_desk(&v, msg.wParam/2); } else { @@ -303,5 +320,5 @@ int main(int argc, char **argv) } } virgo_deinit(&v); - return EXIT_SUCCESS; + ExitProcess(0); } \ No newline at end of file