how to query the precise top-left absolute x,y coordinates of the gnome-terminal text window area under ubuntu?

58 Views Asked by At

For fun and learning, I'm writing an ncurses app in C under ubuntu which calls mvaddch to place an 'o' character as close as it can to the mouse position as I drag the mouse pointer around. The app needs to run under gnome-terminal. I have it mostly working, but I've had to use a brittle hack which I'd like some help to get rid of.

Can someone show me some C code that I could use to precisely calculate the top left x,y coordinates of the text area my gnome-terminal window? Any pointers on how to figure this out would be appreciated.

I'm calling XQueryPointer to get the mouse coordinates and XTranslateCoordinates to map the mouse coordinates into my gnome-terminal window. The translated x,y coordinates I get back when I'm manually pointing to the top left pixel of my gnome-terminal window come back as ballpark 20,20.

When I call XGetWindowAttributes or XGetGeometry, the x,y coordinates returned for my gnome-terminal window appear to be a little to the left and above the visible part of the window on my desktop. The decorations appear to be 0x0. I've manually eyeballed the offsets into the top left of the text area of the gnome-terminal window to be roughly x-21, y-69.

I've tried listing all the windows I can see via XQueryTree on the theory that there might be some overlapping window representing the text area, but that does not seem to yield any other windows with top left x,y close to what I've eyeballed.

I'm using the X functions instead of mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL); and getmouse(&mevent); because I'd like to be able to track the mouse position even outside the gnome-terminal. That way, if I drag the mouse while outside the window, I can still move the 'o' along the edges of the gnome-terminal window.

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.6 LTS
Release:    20.04
Codename:   focal

gnome-terminal --version
# GNOME Terminal 3.36.2 using VTE 0.60.3 +BIDI +GNUTLS +ICU +SYSTEMD

xwininfo: Window id: 0x24e0848 "...mywindowname..."

  Absolute upper-left X:  1491
  Absolute upper-left Y:  98
  Relative upper-left X:  1491
  Relative upper-left Y:  98
  Width: 2166
  Height: 1245
  Depth: 32
  Visual: 0x74c
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x2400009 (not installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +1491+98  -183+98  -183-817  +1491-817
  -geometry 175x44+1491+98

The full program appears to be too long to paste in here, but here is some of the code:


      while (running) {
        Window ncurses_win;
        ncurses_win = XRootWindow(display, screen);
        
        XWindowAttributes attr;
        Status stat = XGetWindowAttributes(display, ncurses_win, &attr);
        fprintf(stderr, "%d %d %d %d\n", attr.x, attr.y, attr.height, attr.width);
        
        // Initialize Xlib variables for window coordinates
        int ncwin_x = 1525, ncwin_y = 127, rat_x, rat_y;
        int ncwin_xx = 3480, ncwin_yy = 1050;
        
        Window root, parent, *children;
        unsigned int nChildren;
        bool found = false;
        if (0 != XQueryTree(display, ncurses_win, &root, &parent, &children, &nChildren)) {
          for (unsigned int i = 0; i  ncwin_x && mouse_x  ncwin_y && mouse_y  max_x) cursor_x = max_x-1;
        if (cursor_y  max_y) cursor_y = max_y-1;
    
        if (cursor_x >= 0 && cursor_x = 0 && cursor_y 
0

There are 0 best solutions below