首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >高亮当前鼠标位置

高亮当前鼠标位置
EN

Unix & Linux用户
提问于 2015-02-09 22:00:43
回答 1查看 10.4K关注 0票数 25

我正在运行一个双屏幕设置,并且我的触控板大部分时间被禁用(包括隐藏鼠标指针)。当我重新启用触控板(并再次显示鼠标指针)时,我已经失去了指针之前的位置。

我正在寻找一个工具来突出当前鼠标的位置(例如,由一个圆圈)。理想情况下,这将是一个单一的命令闪烁的循环很短的时间。

我知道xdotool可以找到当前的位置,但是没有突出显示;而且,key-mon不提供此功能。我也读过cairo composition manager提供了这样的功能,但是我想知道是否有一个更小的工具来实现这一点。

如果没有这样的工具:使用xdotool getmouselocation提供的数据,在光标周围显示这样一个圆圈的最简单方法是什么?

如果这与此相关:我不使用桌面环境,只使用xmonad窗口管理器。

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2015-09-09 22:06:15

虽然我喜欢Mikeserv对聪明的回答,但它的缺点是,它会创建一个窗口,“窃取”焦点,并不得不点击。我还发现它只是稍微花了太长的时间才开始:大约0.2到0.3秒,对于一个“平稳”的体验来说,这有点太慢了。

我终于开始钻研XLib,并编写了一个基本的C程序来完成这个任务。视觉效果大致类似于Windows (XP) (来自内存)。它不是很漂亮,但它能工作;-)它不会“窃取”焦点,开始时几乎瞬间,你可以点击“通过”它。

您可以用cc find-cursor.c -o find-cursor -lX11 -lXext -lXfixes编译它。在顶部有一些变量可以调整以改变大小、速度等。

我在https://github.com/arp242/find-cursor发布了这个程序。我建议您使用此版本,因为它有以下脚本没有的一些改进(例如命令行参数和单击“通过”窗口的能力)。由于它的简单性,我留下了下面的部分。

代码语言:javascript
运行
复制
/*
 * https://github.com/arp242/find-cursor
 * Copyright © 2015 Martin Tournoij <martin@arp242.net>
 * See below for full copyright
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>


// Some variables you can play with :-)
int size = 220;
int step = 40;
int speed = 400;
int line_width = 2;
char color_name[] = "black";


int main(int argc, char* argv[]) {
    // 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);
        exit(1);
    }

    Display *display = XOpenDisplay(display_name);
    int screen = DefaultScreen(display);

    // Get the mouse cursor position
    int win_x, win_y, root_x, root_y = 0;
    unsigned int mask = 0;
    Window child_win, root_win;
    XQueryPointer(display, XRootWindow(display, screen),
        &child_win, &root_win,
        &root_x, &root_y, &win_x, &win_y, &mask);

    // Create a window at the mouse position
    XSetWindowAttributes window_attr;
    window_attr.override_redirect = 1;
    Window window = XCreateWindow(display, XRootWindow(display, screen),
        root_x - size/2, root_y - size/2,   // x, y position
        size, size,                         // width, height
        0,                                  // border width
        DefaultDepth(display, screen),      // depth
        CopyFromParent,                     // class
        DefaultVisual(display, screen),     // visual
        CWOverrideRedirect,                 // valuemask
        &window_attr                        // attributes
    );
    XMapWindow(display, window);
    XStoreName(display, window, "find-cursor");

    XClassHint *class = XAllocClassHint();
    class->res_name = "find-cursor";
    class->res_class = "find-cursor";
    XSetClassHint(display, window, class);
    XFree(class);

    // Keep the window on top
    XEvent e;
    memset(&e, 0, sizeof(e));
    e.xclient.type = ClientMessage;
    e.xclient.message_type = XInternAtom(display, "_NET_WM_STATE", False);
    e.xclient.display = display;
    e.xclient.window = window;
    e.xclient.format = 32;
    e.xclient.data.l[0] = 1;
    e.xclient.data.l[1] = XInternAtom(display, "_NET_WM_STATE_STAYS_ON_TOP", False);
    XSendEvent(display, XRootWindow(display, screen), False, SubstructureRedirectMask, &e);

    XRaiseWindow(display, window);
    XFlush(display);

    // Prepare to draw on this window
    XGCValues values;
    values.graphics_exposures = False;

    unsigned long valuemask = 0;
    GC gc = XCreateGC(display, window, valuemask, &values);

    Colormap colormap = DefaultColormap(display, screen);
    XColor color;
    XAllocNamedColor(display, colormap, color_name, &color, &color);
    XSetForeground(display, gc, color.pixel);
    XSetLineAttributes(display, gc, line_width, LineSolid, CapButt, JoinBevel);

    // Draw the circles
    for (int i=1; i<=size; i+=step) { 
        XDrawArc(display, window, gc,
            size/2 - i/2, size/2 - i/2,   // x, y position
            i, i,                         // Size
            0, 360 * 64);                 // Make it a full circle

        XSync(display, False);
        usleep(speed * 100);
    }
    XFreeGC(display, gc);
    XCloseDisplay(display);
}


/*
 *  The MIT License (MIT)
 * 
 *  Copyright © 2015 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
 *  deal in the Software without restriction, including without limitation the
 *  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 *  sell copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 * 
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 * 
 *  The software is provided "as is", without warranty of any kind, express or
 *  implied, including but not limited to the warranties of merchantability,
 *  fitness for a particular purpose and noninfringement. In no event shall the
 *  authors or copyright holders be liable for any claim, damages or other
 *  liability, whether in an action of contract, tort or otherwise, arising
 *  from, out of or in connection with the software or the use or other dealings
 *  in the software.
 */
票数 30
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/183910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档