在 GTK3 中不工作 gdk_test_simulate_button 的问题

Problem with not working gdk_test_simulate_button in GTK3

无法 gdk_test_simulate_button 工作。谁能帮帮我,哪里出错了?

  1. 按下鼠标右键。
  2. 计时器开始。
  3. 计时器结束后,将模拟左键按下。
#include <stdio.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

int x_root = 0;
int y_root = 0;

gboolean simulate_left_button(gpointer user_data) {
  GdkWindow* gdkwindow = gtk_widget_get_window(user_data);
  Display* display = GDK_WINDOW_XDISPLAY(gdkwindow);
  Window window = GDK_WINDOW_XID(gdkwindow);
  Window root = DefaultRootWindow(display);
  Window subwindow = None;
  int x = 0, y = 0;

  XTranslateCoordinates(display, root, window, x_root, y_root, &x, &y,
    &subwindow);

  printf("XID: 0x%08lx\n", window);
  printf("x_root = %d, y_root = %d\n", x_root, y_root);
  printf("x = %d, y = %d\n", x, y);
  
  if (!gdk_test_simulate_button(gdkwindow, x, y, 1, GDK_BUTTON1_MASK,
      GDK_BUTTON_PRESS)) {
    fprintf(stderr, "gdk_test_simulate_button()\n");
  }
  return FALSE;
}

gboolean on_button_press (GtkWidget* widget, GdkEventButton* event,
    GdkWindowEdge edge) {
  if (event->type != GDK_BUTTON_PRESS)
    return TRUE;

  if (event->button == 3) {
    printf("Right XID: 0x%08lx\n", GDK_WINDOW_XID(gtk_widget_get_window(widget)));
    printf("Right x = %f, y = %f\n", event->x_root, event->y_root);

    x_root = event->x_root;
    y_root = event->y_root;
    g_timeout_add(1000, simulate_left_button, widget);
  } else if (event->button == 1) {
    printf("Left XID: 0x%08lx\n", GDK_WINDOW_XID(gtk_widget_get_window(widget)));
    printf("Left x = %f, y = %f\n", event->x_root, event->y_root);
  } else {
    printf("Unknown x = %f, y = %f\n", event->x_root, event->y_root);
  }
  return FALSE;
}

int main(int argc, char* argv[]) {
  GtkWidget* window = NULL;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
  gtk_window_set_title(GTK_WINDOW(window), "Simulate Left button");
  gtk_window_set_decorated(GTK_WINDOW (window), FALSE);
  gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);

  g_signal_connect(G_OBJECT(window), "button-press-event",
      G_CALLBACK(on_button_press), NULL);

  g_signal_connect_swapped(G_OBJECT(window), "destroy",
      G_CALLBACK(gtk_main_quit), G_OBJECT(window));

  gtk_widget_show(window);

  gtk_main();

  return 0;
}

// Build command: clang -Wall -g `pkg-config --cflags --libs gtk+-3.0` -lX11 test.c

在靠近左上角 window 边缘按下鼠标右键并经过 1 秒时的实际输出:

Right XID: 0x03c00003
Right x = 817.465820, y = 410.487183
XID: 0x03c00003
x_root = 817, y_root = 410
x = 4, y = 6

1 秒后的预期输出:

Right XID: 0x03c00003
Right x = 817.465820, y = 410.487183
XID: 0x03c00003
x_root = 817, y_root = 410
x = 4, y = 6
Left XID: 0x03c00003   <-- These lines are missing above, "button-press-event" signal not raised.
Left x = 817, y = 410  <--

使用命令构建时在 GTK2 中运行良好

clang -Wall -g `pkg-config --cflags --libs gtk+-2.0` -lX11 test.c

我使用函数 gdk_event_put 而不是 gdk_test_simulate_button 解决了这个问题。

gboolean simulate_left_button(gpointer user_data) {
  GdkWindow* gdkwindow = gtk_widget_get_window(user_data);
  GdkDisplay* gdkdisplay = gdk_display_get_default();
  GdkDeviceManager* device_manager = gdk_display_get_device_manager(gdkdisplay);
  Display *display = GDK_WINDOW_XDISPLAY(gdkwindow);
  Window window = GDK_WINDOW_XID(gdkwindow);
  Window root = DefaultRootWindow(display);
  Window subwindow = None;
  GdkEvent* event = NULL;
  GdkEventButton* event_button = NULL;
  int x = 0, y = 0;

  XTranslateCoordinates(display, root, window, x_root, y_root, &x, &y,
    &subwindow);

  printf("XID: 0x%08lx\n", window);
  printf("x_root = %d, y_root = %d\n", x_root, y_root);
  printf("x = %d, y = %d\n", x, y);

  event = gdk_event_new(GDK_BUTTON_PRESS);
  event_button = &event->button;
  event_button->type = GDK_BUTTON_PRESS;
  event_button->window = gdkwindow;
  event_button->send_event = 1;
  event_button->time = GDK_CURRENT_TIME;
  event_button->x = x;
  event_button->y = y;
  event_button->state = GDK_BUTTON1_MASK;
  event_button->button = 1;
  event_button->device = gdk_device_manager_get_client_pointer(device_manager);
  event_button->x_root = x_root;
  event_button->y_root = y_root;

  gdk_event_put(event);
  gdk_event_free(event);
  return FALSE;
}