7

I am trying to get the pointer position on screen in Gdk and found gdk_display_get_pointer(), which works fine, but it's marked as deprecated and refers to gdk_device_get_position() now.

But how do I use this function? I cannot get a GdkDevice, since there is no factory, nor is there a constructor.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Kugel
  • 808
  • 8
  • 26

2 Answers2

9

use Gdk.DeviceManager.

.....
.....
GdkDisplay *display = gdk_display_get_default ();
GdkDeviceManager *device_manager = gdk_display_get_device_manager (display);
GdkDevice *device = gdk_device_manager_get_client_pointer (device_manager);

// do whatever with Gdk.Device, i.e:
int x, y;
gdk_device_get_position (device, NULL, &x, &y);
printf ("x= %d, y=%d", x,y);
luciomrx
  • 1,165
  • 1
  • 7
  • 7
  • Thought I might add for anyone interested in getting the cursor position in the window, you can use `gdk_window_get_device_position (gtk_widget_get_window(gtk_window), device, &x, &y, NULL); ` – jackw11111 May 07 '19 at 04:22
  • 1
    Should I worry about the speed of calling these three functions? In the end I need to call `gdk_window_get_device_position`, as jackw11111 remarked, but I only have a GtkDrawingArea (widget). It seems a bit strange to be forced to call `gdk_window_get_device_position(gtk_widget_get_window(my_widget), gdk_device_manager_get_client_pointer(gdk_display_get_device_manager(gdk_display_get_default()), &x, &y, NULL);` just to get the x and y in my drawing area :/ – Carlo Wood Jul 17 '20 at 23:05
6

Since GTK+ 3.20+, luciomrx's answer becomes:

GdkDisplay* display = gdk_display_get_default();
GdkSeat* seat = gdk_display_get_default_seat(display);
GdkDevice* pointer = gdk_seat_get_pointer(seat);
Carlo Wood
  • 5,648
  • 2
  • 35
  • 47