Skip to content

Commit

Permalink
Merge pull request #2 from kareltucek/main
Browse files Browse the repository at this point in the history
Prevent outline overflow.
  • Loading branch information
swillner authored Jul 27, 2021
2 parents f73e854 + 18c3953 commit 32bf4c6
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions highlight-pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ static void hide_cursor() {

static void show_highlight() {
int x, y;
int total_radius = options.radius + options.outline;
get_pointer_position(&x, &y);
XMoveWindow(dpy, win, x - options.radius - 1, y - options.radius - 1);
XMoveWindow(dpy, win, x - total_radius - 1, y - total_radius - 1);
XMapWindow(dpy, win);
redraw();
highlight_visible = 1;
Expand Down Expand Up @@ -153,17 +154,18 @@ static int get_pointer_position(int* x, int* y) {

static void set_window_mask() {
XGCValues gc_values;
Pixmap mask = XCreatePixmap(dpy, win, 2 * options.radius + 2, 2 * options.radius + 2, 1);
int total_radius = options.radius + options.outline;
Pixmap mask = XCreatePixmap(dpy, win, 2 * total_radius + 2, 2 * total_radius + 2, 1);
GC mask_gc = XCreateGC(dpy, mask, 0, &gc_values);
XSetForeground(dpy, mask_gc, 0);
XFillRectangle(dpy, mask, mask_gc, 0, 0, 2 * options.radius + 2, 2 * options.radius + 2);
XFillRectangle(dpy, mask, mask_gc, options.outline, options.outline, 2 * total_radius + 2, 2 * total_radius + 2);

XSetForeground(dpy, mask_gc, 1);
if (options.outline) {
XSetLineAttributes(dpy, mask_gc, options.outline, LineSolid, CapButt, JoinBevel);
XDrawArc(dpy, mask, mask_gc, 0, 0, 2 * options.radius + 1, 2 * options.radius + 1, 0, 360 * 64);
XDrawArc(dpy, mask, mask_gc, options.outline, options.outline, 2 * options.radius + 1, 2 * options.radius + 1, 0, 360 * 64);
} else {
XFillArc(dpy, mask, mask_gc, 0, 0, 2 * options.radius + 1, 2 * options.radius + 1, 0, 360 * 64);
XFillArc(dpy, mask, mask_gc, options.outline, options.outline, 2 * options.radius + 1, 2 * options.radius + 1, 0, 360 * 64);
}

XShapeCombineMask(dpy, win, ShapeBounding, 0, 0, mask, ShapeSet);
Expand All @@ -173,11 +175,12 @@ static void set_window_mask() {
}

static int init_window() {
int total_radius = options.radius + options.outline;
XSetWindowAttributes win_attributes;
win_attributes.event_mask = ExposureMask | VisibilityChangeMask;
win_attributes.override_redirect = True;

win = XCreateWindow(dpy, root, 0, 0, 2 * options.radius + 2, 2 * options.radius + 2, 0, DefaultDepth(dpy, screen), InputOutput, DefaultVisual(dpy, screen),
win = XCreateWindow(dpy, root, options.outline, options.outline, 2 * total_radius + 2, 2 * total_radius + 2, 0, DefaultDepth(dpy, screen), InputOutput, DefaultVisual(dpy, screen),
CWEventMask | CWOverrideRedirect, &win_attributes);
if (!win) {
fprintf(stderr, "Can't create highlight window\n");
Expand Down Expand Up @@ -243,9 +246,9 @@ static void redraw() {
XSetForeground(dpy, gc, button_pressed ? pressed_color.pixel : released_color.pixel);
if (options.outline) {
XSetLineAttributes(dpy, gc, options.outline, LineSolid, CapButt, JoinBevel);
XDrawArc(dpy, win, gc, 0, 0, 2 * options.radius + 1, 2 * options.radius + 1, 0, 360 * 64);
XDrawArc(dpy, win, gc, options.outline, options.outline, 2 * options.radius + 1, 2 * options.radius + 1, 0, 360 * 64);
} else {
XFillArc(dpy, win, gc, 0, 0, 2 * options.radius + 1, 2 * options.radius + 1, 0, 360 * 64);
XFillArc(dpy, win, gc, options.outline, options.outline, 2 * options.radius + 1, 2 * options.radius + 1, 0, 360 * 64);
}
}

Expand Down Expand Up @@ -298,6 +301,7 @@ static void main_loop() {
int fd = ConnectionNumber(dpy);
struct timeval timeout;
int x, y, n;
int total_radius = options.radius + options.outline;
XGenericEventCookie* cookie;
#if TARGET_FPS > 0
Time lasttime = 0;
Expand Down Expand Up @@ -348,7 +352,7 @@ static void main_loop() {
show_highlight();
} else if (highlight_visible) {
get_pointer_position(&x, &y);
XMoveWindow(dpy, win, x - options.radius - 1, y - options.radius - 1);
XMoveWindow(dpy, win, x - total_radius - 1, y - total_radius - 1);
/* unfortunately, this causes increase of the X server's cpu usage */
}
continue;
Expand Down

0 comments on commit 32bf4c6

Please sign in to comment.