gnomegnome-terminal

What is the GNOME_TERMINAL_SCREEN environment variable?


I've recently installed Ubuntu 20.4 LTS on one of my computers. This release of Ubuntu uses the gnome desktop manager (3.36.3) with gnome-terminal (3.36.2) by default.

In each terminal window that I open, the GNOME_TERMINAL_SCREEN environment variable is defined to "/org/gnome/Terminal/screen/some-guid", where each terminal window's GUID is unique.

Does anyone know what this variable is supposed to be used for? Is there some way of using the GUID in Xlib or XCB to identify the terminal's X window?


Solution

  • The $GNOME_TERMINAL_SCREEN environment variable contains an object path for D-Bus.
    It is used to address a tab in the Gnome Terminal when starting a process in it, and to be signalled of its termination.

    You can see the relevant part of its D-Bus interface by running this command:

    dbus-send --session --type=method_call --print-reply \
            --dest=org.gnome.Terminal "$GNOME_TERMINAL_SCREEN" \
            org.freedesktop.DBus.Introspectable.Introspect
    

    The output (snipped for relevance):

    [...]
    <!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
        "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
    <!-- GDBus 2.64.6 -->
    <node>
      [...]
      <interface name="org.gnome.Terminal.Terminal0">
        <method name="Exec">
          <arg type="a{sv}" name="options" direction="in"/>
          <arg type="aay" name="arguments" direction="in"/>
        </method>
        <signal name="ChildExited">
          <arg type="i" name="exit_code"/>
        </signal>
      </interface>
    </node>
    

    If you run dbus-monitor and open and close a Gnome Terminal tab, you can see the D-Bus communication in action.

    The X Window System is not aware of what goes on in the D-Bus realm, and as far as I know Gnome Terminal does not expose any X specific information through D-Bus.

    I have found one way to tie a process to the X window of a Gnome Terminal in which it is running, but it is less than ideal. Nonetheless, it may suffice for your purposes.
    The idea as that when opening a Gnome Terminal window, we will generate an identifying value, and store it both in an X property of the Gnome Terminal window, and in an environment variable. We can then later get the environment variable from the environment of the process (via /proc/<pid>/environ if needed), and scan the windows for the one which has our value in the X property.

    As the window does not exist yet when opening a new Gnome Terminal, we can not set a property ourselves, but the gnome-terminal command accepts an option --role, and stores its value in the WM_WINDOW_ROLE X property of the Gnome Terminal window.
    The purpose of the WM_WINDOW_ROLE X property is to uniquely identify windows belonging to the same client. Without --role, Gnome Terminal assigns it a unique value, but you can do this yourself.

    So here is a start-gnome-terminal wrapper, which you could invoke from the key binding which would normally start gnome-terminal:

    #!/bin/sh
    FINDWIN_ROLE=findwin-role-$(xxd -p -l 16 < /dev/urandom)
    export FINDWIN_ROLE
    exec gnome-terminal --role "$FINDWIN_ROLE" "$@"
    

    To look through the windows for the property later, you could use wmctrl -l and xprop.