Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'gdk_screen_get_active_window ()' is deprecated since Gtk+ version 3.22

Tags:

gtk3

According to https://developer.gnome.org/gdk3/stable/GdkScreen.html#gdk-screen-get-active-window,

gdk_screen_get_active_window has been deprecated since version 3.22 and should not be used in newly-written code.

But, what should be used instead? (This is one of many deprecated GdkScreen functions.)

To be specific, how would I obtain the location and geometry of the active window?

Edit 12/10/16: After a couple of days looking into this, I've come to the conclusion the answer to this question is outside developer.gnome.org. It may be that separate code needs to be written directly targeting X11, wayland, and mir.

For what it's worth, below is get_window-areas.c that I have written exploring what can be found in Gtk without using deprecated functions. It seems there isn't a way to get window titles or active status; so that, I could not duplicate the functionality of @theGtknerd's answer which uses unstable Wnck libraries.

I am just learning Gtk, so I do appreciate any comments for improving this. I started with the empty window code https://developer.gnome.org/gtk3/stable/gtk-getting-started.html#id-1.2.3.5, added a textview with buffer to it, and then inserted information about the geometry and location of each window into the text buffer.

gcc `pkg-config --cflags gtk+-3.0` -o get_window-areas get_window-areas.c `pkg-config --libs gtk+-3.0`

Compile get_window-areas.c below with the gcc command above.

#include <gtk/gtk.h>

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
  GtkWidget *window = NULL;
  GtkWidget *text_view;
  GtkTextBuffer *buffer;
  int x = 0, y = 0, width = 0, height = 0;
  char char_x[5], char_y[5], char_width[5], char_height[5];
  GdkScreen *screen;
  GdkWindow *dwindow;
  GList *gl_item = NULL, *gl = NULL;

  window = gtk_application_window_new (app);
  screen = gtk_window_get_screen (GTK_WINDOW(window));
  buffer = gtk_text_buffer_new (NULL);
  text_view = gtk_text_view_new_with_buffer (buffer);
  gtk_container_add (GTK_CONTAINER (window), text_view);

  if(screen != NULL)
    { 
      gl = gdk_screen_get_window_stack(screen);
      for (gl_item = g_list_first(gl); gl_item != NULL; gl_item = gl_item->next) 
      { 
        dwindow=gl_item->data;
        gdk_window_get_root_origin(dwindow, &x, &y);
        width = gdk_window_get_width(dwindow);
        height = gdk_window_get_height(dwindow);
        g_object_unref(dwindow);
        snprintf (char_x, 5, "%d", x);
        snprintf (char_y, 5, "%d", y);
        snprintf (char_width, 5, "%d", width);
        snprintf (char_height, 5, "%d", height);
        gtk_text_buffer_insert_at_cursor(buffer,char_width,-1);
        gtk_text_buffer_insert_at_cursor(buffer,"x", -1);
        gtk_text_buffer_insert_at_cursor(buffer,char_height,-1);
        gtk_text_buffer_insert_at_cursor(buffer," at (", -1);
        gtk_text_buffer_insert_at_cursor(buffer,char_x, -1);
        gtk_text_buffer_insert_at_cursor(buffer,",", -1);
        gtk_text_buffer_insert_at_cursor(buffer,char_y,-1);
        gtk_text_buffer_insert_at_cursor(buffer,")\n", -1);
      }; 
      g_list_free (gl);
    } 
  else {gtk_text_buffer_insert_at_cursor(buffer, "Failed to get default screen.\n", -1);}

  gtk_widget_show_all (window);
}

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

  app = gtk_application_new ("com.github.colinkeenan.silentcast", 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;
}
like image 964
Colin Keenan Avatar asked Dec 07 '16 21:12

Colin Keenan


1 Answers

Here is Python code that gets the active window and prints its geometry.

#!/usr/bin/python

import gi
gi.require_version('Wnck', '3.0')
from gi.repository import Wnck
screen = Wnck.Screen.get_default()
screen.force_update()  # recommended per Wnck documentation

# loop all windows
for window in screen.get_windows():
   if window.is_active() == True:
        print (window.get_geometry())
        window_name = window.get_name()
        print (window_name)

# clean up Wnck (saves resources, check documentation)
window = None
screen = None
Wnck.shutdown()

The documentation is https://developer.gnome.org/libwnck/stable/WnckWindow.html.

Edit: I am trying to compile the C I have with:

gcc `pkg-config --cflags --libs libwnck-3.0` -o wnck wnck.c

and I get the error:

/usr/include/libwnck-3.0/libwnck/window.h:30:2: error: #error "libwnck should only be used if you understand that it's subject to frequent change, and is not supported as a fixed API/ABI or as part of the platform"

there is a workaround, but I am not sure Wnck is a good replacement for GdkScreen. I really do not know what to tell you at this point.

like image 131
theGtknerd Avatar answered Sep 29 '22 20:09

theGtknerd