我正试图找出如何使用GdkDeviceManager,所以我编写了以下程序,该程序将打印所有物理输入设备:

#include <stdio.h>
#include <gdk/gdk.h>

int main(int argc, char **argv) {
    GList *devices, *it;
    GdkDeviceManager mgr;

    devices = gdk_device_manager_list_devices(&mgr, GDK_DEVICE_TYPE_SLAVE);

    for (it = devices; it != NULL; it = it->next) {
        GdkDevice *dev = it->data;
        printf("Current device: %s\n", gdk_device_get_name(dev));
    }

    g_list_free(devices);
    return 0;
}

但是当我试图通过
gcc mousetest.c $(pkg-config --libs --cflags gtk+-3.0 gdk-3.0) -Wall

我明白了
mousetest.c: In function ‘main’:
mousetest.c:6:22: error: storage size of ‘mgr’ isn’t known
     GdkDeviceManager mgr;
                      ^
mousetest.c:6:22: warning: unused variable ‘mgr’ [-Wunused-variable]

最佳答案

目前无法实例化GdkDeviceManager,您需要通过显示器从中获取指针。要获得显示,需要初始化Gtk+。
下面的工作代码,

#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>

int main(int argc, char **argv) {

    gtk_init(&argc, &argv);

    GList *devices, *it;
    GdkDisplay *display = gdk_display_get_default();
    GdkDeviceManager *mgr = gdk_display_get_device_manager(display);

    devices = gdk_device_manager_list_devices(mgr, GDK_DEVICE_TYPE_SLAVE);

    for (it = devices; it != NULL; it = it->next) {
        GdkDevice *dev = it->data;
        printf("Current device: %s\n", gdk_device_get_name(dev));
    }

    g_list_free(devices);
    return 0;
}

08-16 21:35