Get cursor position on the screen in Gtk4 using gjs

I wonder if it is possible to get co-ordinates of the cursor position outside the ApplicationWindow in Gtk4 with gjs? It seems it is possible in Gtk3 but after grabbing the device like in this example. To the best of my knowledge that API has been removed in Gtk4. How do I create something similar in Gtk4.

I can get co-ordinates of the cursor but relative to the ApplicationWindow.

const eventControllerMotion = new Gtk.EventControllerMotion();
activeWindow.add_controller(eventControllerMotion);

eventControllerMotion.connect("motion", (_, x, y) => {
  console.log(`(${x}, ${y})`);
});

I want the co-ordinates of the cursor on the screen relative to the monitor.

@andyholmes Could you be having some ideas on how to approach this?

I don’t believe that’s possible. You may be able to get the monitor or display geometry from GDK, but it’s generally considered “not an application’s business” what happens outside of itself. I think the same principle applies to applications that want to move their own windows.

What is your end-goal for finding the pointer location on the screen?

I want to do what this color picker app is doing. I think it gets cursor coordinates on the screen, captures a screenshot of the screen at that particular coordinates and picks the RGB color values from the screenshot. I think the most common name used for such apps is eye dropper.

It is similar to the eye dropper browser addon. Except that the eye dropper addon is limited to the browser only. I was thinking of something similar but for the desktop. The linked desktop app picks color from anywhere including the browser. I could use the existing app but I want additional functionality like mapping the picked color to CSS library class out of the box.

It looks like that application is using GTK3, so there’s not much help there for a GTK4 application.

It sounds like you might want to use something like Xdp.Portal.pick_color().

Thanks. Looks like Xdp is part of the libportal library but I get an error when I try to import it like most of the APIs I have encountered in the gjs guide.

import  Xdp  from "gi://Xdp?version=1.0";

The above import gives me the error below.

JS ERROR: Error: Requiring Xdp, version 1.0: Typelib file for namespace ‘Xdp’, version ‘1.0’ not found

Going by the error above, I guess I need to perhaps install libportal. However, the libportal docs doesn’t exactly describe how. So how do I start using Xdp?

Well that depends on your distribution or development environment, so it’s not something the API docs can cover

On Fedora you would dnf install libportal, for example. In a Flatpak manifest you would add it as a module, being sure to set -Dintrospection=true.

FYI, we have a pending merge request for Workbench that covers the Color Picker portal API.

I tried for Flatpak but still getting the same error.

{
  "name" : "libportal",
  "buildsystem": "meson",
  "config-opts": [
    "-Dintrospection=true"
  ],
 "builddir" : true,
 "sources" : [
     {
         "type" : "archive",
         "url" : "https://github.com/flatpak/libportal/releases/download/0.6/libportal-0.6.tar.xz",
         "sha256":"88a12c3ba71bc31acff7238c280de697d609cebc50830c3766776ec35abc6566"
     }
 ]
}

Thanks for your help. Let me just try out electron. It is just straight forward. Hopefully, I will have better luck.

1 Like

See Workbench/re.sonny.Workbench.Devel.json at main · sonnyp/Workbench · GitHub

import Xdp from "gi://Xdp"; works fine

1 Like

I don’t know whether there is something I am supposed to do besides adding the libportal module to the manifest. I keep getting the same error even if I copied the linked code.

JS ERROR: Error: Requiring Xdp, version none: Typelib file for namespace ‘Xdp’ (any version) not found
require@resource:///org/gnome/gjs/modules/esm/gi.js:16:28
@gi://Xdp:3:25

Take a look at GJS app that use libportal and do exactly what they do until you figure out what’s the significant difference. Also, try “Rebuild” in GNOME Builder

Unless you share your code repo, I can’t help you further.

Apparently the specified dependencies are not being downloaded if I go by the terminal logs. But when I clone the Workbench repo, it installs and runs fine. Though, I still see some errors while building Workbench, they’re not related to libportal.
image

I have created a GitHub repo if you want to take a look. Not really sure where the issue is.
Libportal demo app

If anyone comes across this thread, apparently, I was getting that error because the libportal package is not being installed. Essentially when adding modules to the manifest, all modules must be added before the application module. In my case, I added the libportal module after the application module.

"modules": [
    {
      "name": "libportal_demo",
      "builddir": true,
      "buildsystem": "meson",
      "sources": [
        {
          "type": "git",
          "url": "file:///home/mawa/Documents/Projects"
        }
      ]
    },
    {
      "name": "libportal",
      "builddir": true,
      "buildsystem": "meson",
      "config-opts": [
        "-Ddocs=false",
        "-Dvapi=false",
        "-Dbackends=gtk4",
        "-Dintrospection=true"
      ],
      "sources": [
        {
          "type": "archive",
          "url": "https://github.com/flatpak/libportal/releases/download/0.6/libportal-0.6.tar.xz",
          "sha256": "88a12c3ba71bc31acff7238c280de697d609cebc50830c3766776ec35abc6566"
        }
      ]
    }
  ]

It seems the modules in the manifest are installed in order of appearance from the first. The installation stops when the application module is installed hence ignoring modules that come after. The Flatpak docs seems to miss that very important piece of information.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.