Change GTK display on the fly
Using some shared library injection it's possible to change the display of a GTK window while it's running.
// Include the headers we will need
#include <stdio.h>
#include <gtk/gtk.h>
// Now write the function we will be executing.
// This one takes a string containing the display to connect to
void change_display(char *display_str)
{
GdkDisplay *display = gdk_display_get_default();
GtkWidget *toplevel = NULL;
GdkWindow *window = NULL;
GtkWidget *widget = NULL;
// Not 100% necessary but gives you time before the line below this one runs
sleep(5);
// This line gets the GDK window below the cursor
window = gdk_display_get_window_at_pointer(display, NULL, NULL);
if (window) {
gdk_window_get_user_data(window, (gpointer)&widget);
toplevel = gtk_widget_get_toplevel(widget);
GdkDisplay *new_display = gdk_display_open(display_str);
if (new_display)
gtk_window_set_screen(GTK_WINDOW(toplevel), gdk_display_get_screen(new_display, 0));
else
fprintf(stderr, "Could not connect to display \"%s\"\n", display_str);
} else {
fprintf(stderr, "No window found\n");
}
// By this point the window should have moved itself to the display you passed in
}
gcc -shared -fPIC -o lib lib.c `pkg-config --libs --cflags gtk+-2.0`
gdb attach `pidof your-gtk-application-here`
(gdb) print (void*)dlopen("/path/to/your/current/directory/lib", 2)
(gdb) print change_display("hostname:0")