Add -f and -t options

For --follow and --transparent. Enabling this may not always work, so
make them optional.
pull/6/head
Martin Tournoij 7 years ago
parent ddc70eb040
commit 5a27101bf1
No known key found for this signature in database
GPG Key ID: A6258419189EE585

@ -20,7 +20,10 @@
void usage(char *name); void usage(char *name);
int parse_num(int ch, char *opt, char *name); int parse_num(int ch, char *opt, char *name);
void draw(char *name, int size, int distance, int wait, int line_width, char *color_name); void draw(
char *name,
int size, int distance, int wait, int line_width, char *color_name,
int follow, int transparent);
static struct option longopts[] = { static struct option longopts[] = {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
@ -29,22 +32,32 @@ static struct option longopts[] = {
{"wait", required_argument, NULL, 'w'}, {"wait", required_argument, NULL, 'w'},
{"line-width", required_argument, NULL, 'l'}, {"line-width", required_argument, NULL, 'l'},
{"color", required_argument, NULL, 'c'}, {"color", required_argument, NULL, 'c'},
{"follow", no_argument, NULL, 'f'},
{"transparent", no_argument, NULL, 't'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
void usage(char *name) { void usage(char *name) {
printf("Usage: %s [-stplc]\n\n", name); printf("Usage: %s [-stplc]\n\n", name);
printf(" -h, --help Show this help.\n"); printf(" -h, --help Show this help.\n");
printf("\n");
printf("Shape options:\n");
printf(" -s, --size Maximum size the circle will grow to in pixels.\n"); printf(" -s, --size Maximum size the circle will grow to in pixels.\n");
printf(" -d, --distance Distance between the circles in pixels.\n"); printf(" -d, --distance Distance between the circles in pixels.\n");
printf(" -l, --line-width Width of the lines in pixels.\n"); printf(" -l, --line-width Width of the lines in pixels.\n");
printf(" -w, --wait Time to wait before drawing the next circle in microseconds.\n"); printf(" -w, --wait Time to wait before drawing the next circle in microseconds.\n");
printf(" -c, --color Color; can either be an X11 color name or RGB as hex (i.e. #ff0055).\n"); printf(" -c, --color Color; can either be an X11 color name or RGB as hex (i.e. #ff0055).\n");
printf("\n"); printf("\n");
printf("The defaults:\n"); printf("Extra options:\n");
printf(" %s --size 220 --distance 40 --wait 400 --line-width 2 --color black\n", name); printf(" -f, --follow Follow the cursor position as the cursor is moving.\n");
printf("Draw a circle\n"); printf(" -t, --transparent Make the window truly 'transparent'. This helps with\n");
printf(" %s --size 100 --distance 1 --wait 20 --line-width 1 --color black\n", name); printf(" some display issues when following the cursor position,\n");
printf(" but it doesn't work well with all WMs, which is why\n");
printf(" it's disabled by default.\n");
printf("\n");
printf("Examples:\n");
printf(" Defaults: %s --size 220 --distance 40 --wait 400 --line-width 2 --color black\n", name);
printf(" Circle: %s --size 100 --distance 1 --wait 20 --line-width 1 --color black\n", name);
printf("\n"); printf("\n");
} }
@ -68,9 +81,11 @@ int main(int argc, char* argv[]) {
int wait = 400; int wait = 400;
int line_width = 2; int line_width = 2;
char color_name[64] = "black"; char color_name[64] = "black";
int follow = 0;
int transparent = 0;
int ch; int ch;
while ((ch = getopt_long(argc, argv, "hs:d:w:l:c:r:", longopts, NULL)) != -1) while ((ch = getopt_long(argc, argv, "hs:d:w:l:c:r:ft", longopts, NULL)) != -1)
switch (ch) { switch (ch) {
case 's': case 's':
size = parse_num(ch, optarg, argv[0]); size = parse_num(ch, optarg, argv[0]);
@ -90,6 +105,12 @@ int main(int argc, char* argv[]) {
case 'h': case 'h':
usage(argv[0]); usage(argv[0]);
exit(0); exit(0);
case 'f':
follow = 1;
break;
case 't':
transparent = 1;
break;
default: default:
usage(argv[0]); usage(argv[0]);
exit(1); exit(1);
@ -97,10 +118,16 @@ int main(int argc, char* argv[]) {
argc -= optind; argc -= optind;
argv += optind; argv += optind;
draw(argv[0], size, distance, wait, line_width, color_name); draw(argv[0],
size, distance, wait, line_width, color_name,
follow, transparent);
} }
void draw(char *name, int size, int distance, int wait, int line_width, char *color_name) { void draw(
char *name,
int size, int distance, int wait, int line_width, char *color_name,
int follow, int transparent
) {
// Setup display and such // Setup display and such
char *display_name = getenv("DISPLAY"); char *display_name = getenv("DISPLAY");
if (!display_name) { if (!display_name) {
@ -125,24 +152,39 @@ void draw(char *name, int size, int distance, int wait, int line_width, char *co
&child_win, &root_win, &child_win, &root_win,
&root_x, &root_y, &win_x, &win_y, &mask); &root_x, &root_y, &win_x, &win_y, &mask);
XVisualInfo vinfo;
XMatchVisualInfo(display, DefaultScreen(display), 32, TrueColor, &vinfo);
// Create a window at the mouse position // Create a window at the mouse position
Window window;
XSetWindowAttributes window_attr; XSetWindowAttributes window_attr;
window_attr.override_redirect = 1; window_attr.override_redirect = 1;
window_attr.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
window_attr.background_pixel = 0; if (transparent) {
Window window = XCreateWindow(display, XRootWindow(display, screen), XVisualInfo vinfo;
root_x - size/2, root_y - size/2, // x, y position XMatchVisualInfo(display, DefaultScreen(display), 32, TrueColor, &vinfo);
size, size, // width, height window_attr.colormap = XCreateColormap(display, DefaultRootWindow(display), vinfo.visual, AllocNone);
4, // border width window_attr.background_pixel = 0;
vinfo.depth, // depth window = XCreateWindow(display, XRootWindow(display, screen),
CopyFromParent, // class root_x - size/2, root_y - size/2, // x, y position
vinfo.visual, // visual size, size, // width, height
CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect, // valuemask 4, // border width
&window_attr // attributes vinfo.depth, // depth
); CopyFromParent, // class
vinfo.visual, // visual
CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect, // valuemask
&window_attr // attributes
);
}
else {
window = XCreateWindow(display, XRootWindow(display, screen),
root_x - size/2, root_y - size/2, // x, y position
size, size, // width, height
4, // border width
DefaultDepth(display, screen), // depth
CopyFromParent, // class
DefaultVisual(display, screen), // visual
CWOverrideRedirect, // valuemask
&window_attr // attributes
);
}
// Make round shaped window. // Make round shaped window.
XGCValues xgcv; XGCValues xgcv;
@ -213,16 +255,21 @@ void draw(char *name, int size, int distance, int wait, int line_width, char *co
// Draw the circles // Draw the circles
int i = 1; int i = 1;
for (i=1; i<=size; i+=distance) { for (i=1; i<=size; i+=distance) {
XClearWindow(display, window); if (follow) {
XClearWindow(display, window);
}
XDrawArc(display, window, gc, XDrawArc(display, window, gc,
size/2 - i/2, size/2 - i/2, // x, y position size/2 - i/2, size/2 - i/2, // x, y position
i, i, // Size i, i, // Size
0, 360 * 64); // Make it a full circle 0, 360 * 64); // Make it a full circle
XQueryPointer(display, XRootWindow(display, screen), if (follow) {
&child_win, &root_win, XQueryPointer(display, XRootWindow(display, screen),
&root_x, &root_y, &win_x, &win_y, &mask); &child_win, &root_win,
XMoveWindow(display, window, root_x - size/2, root_y - size/2); &root_x, &root_y, &win_x, &win_y, &mask);
XMoveWindow(display, window, root_x - size/2, root_y - size/2);
}
XSync(display, False); XSync(display, False);
usleep(wait * 100); usleep(wait * 100);

Loading…
Cancel
Save