Use xlib apis with gtk widget

Hello I have a question can we use xlib apis directly with gtk widget? I have written a code but xlib apis I used doesn’t work on the gtk widget here is my code

#include <gdk/x11/gdkx.h>
#include <gtk/gtk.h>
#include <X11/Xlib.h>

static void
activate (GtkApplication* app,
          gpointer        user_data)
  GtkWidget *widget;
  GdkDisplay*   display;
  GdkSurface*   surface;
  GtkNative*  native;
  Window    window;
  Display*  xdisplay;

widget = gtk_application_window_new(app);
native = gtk_widget_get_native(widget);
surface = gtk_native_get_surface(native);

display = gdk_display_open(NULL);

xdisplay = gdk_x11_display_get_xdisplay(display);

surface = gdk_surface_new_toplevel (display);

window = gdk_x11_surface_get_xid(surface);


XStoreName(xdisplay, window, "HERE");

XMoveWindow(xdisplay, window, 600, 200);

main (int    argc,
      char **argv)
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;

I suspect that the reason is that the window isn’t still realized. I think that it must go through the main loop to be really created and shown.

Try to connect to the window’s “realize” signal and there get the xdisplay, the surface, the Xid and everything else.

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