From 2bd88c14e52a8c1e96b8ef2a7e4f5ba990b59019 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Thu, 1 Feb 2024 05:01:02 -0500 Subject: Further simplify argument procesing, fix -abs negative offsets. --- xdeadzone.c | 106 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 62 insertions(+), 44 deletions(-) (limited to 'xdeadzone.c') diff --git a/xdeadzone.c b/xdeadzone.c index a0398a8..5b89c94 100644 --- a/xdeadzone.c +++ b/xdeadzone.c @@ -27,6 +27,8 @@ # define DBG(x) #endif +enum { M_UNSET, M_ABS, M_NE, M_NW, M_SE, M_SW }; + const char *exe_name; void set_exe_name(const char *p) { @@ -45,7 +47,7 @@ void usage(const int ret) { banner(); printf( "Usage:\n %s " - "[-nw | -ne | -sw | -se | -abs] \n\n" + "<[-b | -w | -i]> [-nw | -ne | -sw | -se | -abs] \n\n" " is WxH for all modes but -abs, or\n WxH[+-]xpos[+-]ypos for -abs\n", exe_name); exit(ret); @@ -68,62 +70,72 @@ int main(int argc, char **argv) { XWindowAttributes attr; XSetWindowAttributes setattr; - int x = -1, y = -1, gflags, visible = 0, black = 0; + int x = -1, y = -1, gflags = -1, visible = 0, black = 0, mode = M_UNSET; unsigned int width, height; set_exe_name(argv[0]); - if(argc == 2) { - if(streq(argv[1], "--help")) { + while(*++argv) { + const char *a = *argv; + DBG(printf("arg: %s\n", a)); + + if(streq(a, "--help")) { usage(0); - } else if(streq(argv[1], "--version")) { + } else if(streq(a, "--version")) { banner(); exit(0); - } - } - - if(argc >= 2) { - if(streq(argv[1], "-b")) { + } else if(streq(a, "-b")) { visible = 1; black = 1; - argv++; - argc--; - } else if(streq(argv[1], "-w")) { + } else if(streq(a, "-w")) { visible = 1; black = 0; - argv++; - argc--; - } else if(streq(argv[1], "-i")) { + } else if(streq(a, "-i")) { visible = 0; - argv++; - argc--; + } else if(streq(a, "-abs")) { + mode = M_ABS; + } else if(streq(a, "-ne")) { + mode = M_NE; + } else if(streq(a, "-nw")) { + mode = M_NW; + } else if(streq(a, "-se")) { + mode = M_SE; + } else if(streq(a, "-sw")) { + mode = M_SW; + } else if(a[0] == '-') { + fprintf(stderr, "%s: invalid option: %s (try --help)\n", exe_name, a); + exit(1); + } else { /* no dash, must be geometry */ + if(gflags == -1) + gflags = XParseGeometry(a, &x, &y, &width, &height); + else + errmsg("multiple geometry arguments given (try --help)"); } } - if(argc != 3) - errmsg("wrong number of arguments (try --help)"); - - gflags = XParseGeometry(argv[2], &x, &y, &width, &height); - if(!(gflags & (WidthValue | HeightValue))) - errmsg("bad geometry (can't parse width and height)"); - - if(streq(argv[1], "-abs")) { - if(!(gflags & (XValue | YValue))) - errmsg("bad geometry: -abs requires X and Y coords"); - } else { - if(gflags & (XValue | YValue)) - errmsg("bad geometry: don't give X and Y coords with -nw/-ne/-sw/-se"); - if(streq(argv[1], "-nw")) { - x = 0; y = 0; - } else if(streq(argv[1], "-ne")) { - x = -width; y = 0; - } else if(streq(argv[1], "-sw")) { - x = 0; y = -height; - } else if(streq(argv[1], "-se")) { - x = -width; y = -height; - } else { - errmsg("bad first argument (not -abs/-nw/-ne/-sw/-se)"); - } + if(gflags == -1) + errmsg("missing required geometry argument (try --help)"); + else + DBG(printf("XParseGeometry got %d %d %d %d\n", x, y, width, height)); + + if(mode != M_ABS && (gflags & (XValue | YValue))) + errmsg("bad geometry: X and Y position not allowed without -abs"); + + switch(mode) { + case M_ABS: + if(!(gflags & (XValue | YValue))) + errmsg("bad geometry: -abs requires X and Y position"); + break; + case M_NW: + x = 0; y = 0; break; + case M_NE: + x = -width; y = 0; break; + case M_SW: + x = 0; y = -height; break; + case M_SE: + x = -width; y = -height; break; + default: + errmsg("no mode given, one of -abs -ne -nw -se -sw is required"); } if(width == 0 || height == 0) @@ -133,8 +145,13 @@ int main(int argc, char **argv) { errmsg("can't open X display"); XGetWindowAttributes(d, DefaultRootWindow(d), &attr); - if(gflags & XNegative) x = (attr.width + x) - width; - if(gflags & YNegative) y = (attr.height + y) - height; + if(mode == M_ABS) { + if(gflags & XNegative) x = (attr.width + x) - width; + if(gflags & YNegative) y = (attr.height + y) - height; + } else { + if(x < 0) x = (attr.width + x); + if(y < 0) y = (attr.height + y); + } DBG(printf("X size %d x %d, x %d, y %d, width %d, height %d\n", attr.width, attr.height, x, y, width, height)); @@ -190,6 +207,7 @@ int main(int argc, char **argv) { ignored the x and y in the XCreateWindow(). */ XMoveWindow(d, w, x, y); + DBG(printf("awaiting EnterNotify events...\n")); while(1) { XNextEvent(d, &ev); if(ev.type == EnterNotify) { -- cgit v1.2.3