Add command-line arguments

import/heads/master
Martin Tournoij 8 years ago
parent ca79a272a8
commit 9e721c0916

@ -1,8 +1,27 @@
Simple XLib program to highlight the cursor position. This is similar to the
feature found in Windows XP (and possibly later?)
You can compile it by typing `make`. At the top of the file there are some
variables you can tweak if you wish.
You can map it in your WM or with `xbindkeys`.
![screenshot.gif](https://bytebucket.org/Carpetsmoker/find-cursor/raw/tip/screenshot.gif)
Using it
========
Compile it by typing `make`. Run it with `-h` for some options controlling the
appearance.
- The author runs it with [`xcape`][xcape]:
xcape -e 'Control_L=Escape;Shift_L=KP_Add'
When Left shift is tapped, a Keypad Add is sent I don't have a numpad on my
keyboard which we can then use to launch the program.
- I configured my window manager (PekWM) to pick up Numpad Add and launch this:
KeyPress = "KP_Add" { Actions = "Exec find-cursor" }
I'm not going to include instructions for every window manager out there.
I'm sure you can figure out how to use it with your WM ;-) You can also use
[`xbindkeys`](xbindkeys), which should work with `$any` window manager.
[xcape]: https://github.com/alols/xcape
[xbindkeys]: http://www.nongnu.org/xbindkeys/xbindkeys.html

@ -1,31 +1,119 @@
/*
* http://code.arp242.net/find-cursor
* Copyright © 2015 Martin Tournoij <martin@arp242.net>
* Copyright © 2015-2016 Martin Tournoij <martin@arp242.net>
* See below for full copyright
*/
#include <stdlib.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdamage.h>
#include <X11/extensions/Xrender.h>
#include <X11/extensions/shape.h>
void usage(char *name);
int get_num(int ch, char *opt, char *name);
void draw(char *name, int size, int step, int speed, int line_width, char *color_name);
static struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"size", required_argument, NULL, 's'},
{"step", required_argument, NULL, 't'},
{"speed", required_argument, NULL, 'p'},
{"line-width", required_argument, NULL, 'l'},
{"color", required_argument, NULL, 'c'},
{NULL, 0, NULL, 0}
};
void usage(char *name) {
printf("Usage: %s [-stplc]\n\n", name);
printf(" -h, --help Show this help.\n");
printf(" -s, --size Size in pixels.\n");
printf(" -t, --step ???.\n");
printf(" -p, --speed Animation speed.\n");
printf(" -l, --line-width Width of the lines in pixels.\n");
printf(" -c, --color Color; can either be an X11 color name or RGB as hex (i.e. #ff0055).\n");
printf("\n");
printf("The defaults:\n");
printf(" %s --size 220 --step 40 --speed 400 --line-width 2 --color black\n", name);
printf("Draw a circle\n");
printf(" %s --size 100 --step 1 --speed 20 --line-width 1 --color black\n", name);
printf("\n");
}
// Some variables you can play with :-)
int size = 220;
int step = 40;
int speed = 400;
int line_width = 2;
char color_name[] = "black";
int get_num(int ch, char *opt, char *name) {
char *end;
long result = strtol(optarg, &end, 10);
if (*end) {
fprintf(stderr, "%s: %d must be a number\n", name, ch);
usage(name);
exit(1);
}
return result;
}
int main(int argc, char* argv[]) {
// Parse options
int size = 220;
int step = 40;
int speed = 400;
int line_width = 2;
char color_name[64] = "black";
int ch;
while ((ch = getopt_long(argc, argv, "hs:t:p:l:c:r:", longopts, NULL)) != -1)
switch (ch) {
case 's':
size = get_num(ch, optarg, argv[0]);
break;
case 't':
step = get_num(ch, optarg, argv[0]);
break;
case 'p':
speed = get_num(ch, optarg, argv[0]);
break;
case 'l':
line_width = get_num(ch, optarg, argv[0]);
break;
case 'c':
strncpy(color_name, optarg, sizeof(color_name));
printf("c: %s\n", color_name);
break;
case 'h':
usage(argv[0]);
exit(0);
default:
fprintf(stderr, "%s: Unknown option: %c\n", argv[0], (char) ch);
usage(argv[0]);
exit(1);
}
argc -= optind;
argv += optind;
draw(argv[0], size, step, speed, line_width, color_name);
}
void draw(char *name, int size, int step, int speed, int line_width, char *color_name) {
// Setup display and such
char *display_name = getenv("DISPLAY");
if (!display_name) {
fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name);
fprintf(stderr, "%s: cannot connect to X server '%s'\n", name, display_name);
exit(1);
}
@ -74,6 +162,15 @@ int main(int argc, char* argv[]) {
e.xclient.data.l[1] = XInternAtom(display, "_NET_WM_STATE_STAYS_ON_TOP", False);
XSendEvent(display, XRootWindow(display, screen), False, SubstructureRedirectMask, &e);
// Setting this makes sure compositing window managers don't apply
// shadows and such
// https://specifications.freedesktop.org/wm-spec/1.4/ar01s05.html
// https://wiki.archlinux.org/index.php/Compton
Atom type_util = XInternAtom(display, "_NET_WM_WINDOW_TYPE", 0);
Atom type = XInternAtom(display, "_NET_WM_WINDOW_TYPE_MENU", 0);
XChangeProperty(display, window, type,
XA_ATOM, 32, PropModeReplace, (unsigned char *)&type_util, 1);
XRaiseWindow(display, window);
XFlush(display);
@ -106,7 +203,7 @@ int main(int argc, char* argv[]) {
/*
* The MIT License (MIT)
*
* Copyright © 2015 Martin Tournoij
* Copyright © 2015-2016 Martin Tournoij
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to

Loading…
Cancel
Save