Getting visual feedback of workspace switch in xfce

16,185

Solution 1

I wrote a small code that shows a notification on workspace switch action. This code requires libnotify and libwnck

To compile it use the command:

gcc -O2 -o wsnd `pkg-config  --cflags --libs libnotify --libs libwnck` wsn.c

If you found compiling errors with above command, try this one:

gcc -O2 -DWNCK_I_KNOW_THIS_IS_UNSTABLE -o wsnd `pkg-config  --cflags --libs libnotify --libs libwnck-1.0` wsn.c

To test it from a terminal: ./wsnd

Include it as XFCE startup application: In XFCE you need to add it as startup applicaion on settings-manager->session and startup -> Application Autostart

The code:

//////////////////////////////////////////////////////////////////////////////////////
// Workspace Switch Notifier                                                        //
// Shows a OSD with workspace name on workspace switching action                    //
//                                                                                  //
// wsn.c -                                                                          //
//                                                                                  //
// Authors:                                                                         //
//    Isaac Maia Pessoa                                                             //
//                                                                                  //
// This program is free software: you can redistribute it and/or modify it          //
// under the terms of the GNU General Public License version 3, as published        //
// by the Free Software Foundation.                                                 //
//                                                                                  //
// This program is distributed in the hope that it will be useful, but              //
// WITHOUT ANY WARRANTY; without even the implied warranties of                     //
// MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR               //
// PURPOSE.  See the GNU General Public License for more details.                   //
//                                                                                  //
// You should have received a copy of the GNU General Public License along          //
// with this program.  If not, see <http://www.gnu.org/licenses/>.                  //
//////////////////////////////////////////////////////////////////////////////////////

#include <libnotify/notify.h>
#include <libwnck/libwnck.h>

#define N_SUMMARY "Workspace Changed"
#define N_ICON    "dialog-information"
#define N_APPNAME "workspace switch notifier"
#define N_TIMEOUT 2000 /*2000ms = 2s */

static NotifyNotification * m_notification = NULL;

static void
on_active_workspace_changed (WnckScreen    *screen,
                             WnckWorkspace *space,
                             gpointer      data)
{

  WnckWorkspace * active_workspace = wnck_screen_get_active_workspace(screen);
  const char * w_name = wnck_workspace_get_name (active_workspace);

  notify_notification_update(m_notification, N_SUMMARY, w_name, N_ICON);
  notify_notification_show(m_notification, NULL);
}

int main(int argc, char ** argv)
{

   GMainLoop *loop;   
   WnckScreen *screen;

   if (notify_init(N_APPNAME))
       m_notification = notify_notification_new(N_SUMMARY, "" , N_ICON);
   else
       fprintf(stderr, "Failed to init notifications\n");
   notify_notification_set_timeout(m_notification, N_TIMEOUT);

   gdk_init (&argc, &argv);

   loop = g_main_loop_new (NULL, FALSE);
   screen = wnck_screen_get_default();

   g_signal_connect (screen, "active-workspace-changed",
                    G_CALLBACK (on_active_workspace_changed), NULL);

   g_main_loop_run (loop);

   g_main_loop_unref (loop);    

   return 0;
}

Solution 2

I've modified Isaac Pessoa's answer for my own preferences. Instead of showing a notification on workspace changed, this briefly shows a hidden Xfce panel with the workspace switcher widget.

Here's what it looks like while switching workspaces: example

Save this file as wschanged.c:

#include <libwnck/libwnck.h>
#include <stdlib.h>

static void
on_active_workspace_changed (WnckScreen    *screen,
                             WnckWorkspace *space,
                             gpointer      data)
{
    // Executes a script on workspace change
    system ("~/.workspace-changed");
}

int main(int argc, char ** argv)
{

   GMainLoop *loop;   
   WnckScreen *screen;

   gdk_init (&argc, &argv);

   loop = g_main_loop_new (NULL, FALSE);
   screen = wnck_screen_get_default();

   g_signal_connect (screen, "active-workspace-changed",
                    G_CALLBACK (on_active_workspace_changed), NULL);

   g_main_loop_run (loop);
   g_main_loop_unref (loop);    

   return 0;
}

Now create a new Xfce panel anywhere on the edge of your screen and set it to auto-hide. Add the workspace switcher widget. Make a note of the panel number as displayed in the drop-down menu of the panel preferences window.

Make a .workspace-changed file in your home directory, replacing < P_NUMBER > with the panel number that you just created. Make sure this file is executable! (chmod +x .workspace-changed)

#!/bin/sh

# Show the auto-hidden workspace panel for a moment, then set it to autohide again
xfconf-query -c xfce4-panel -p /panels/panel-<P_NUMBER>/autohide -t bool -s false
xfconf-query -c xfce4-panel -p /panels/panel-<P_NUMBER>/autohide -t bool -s true

Now compile with:

gcc -O2 -DWNCK_I_KNOW_THIS_IS_UNSTABLE -o wschanged `pkg-config  --cflags --libs libwnck-1.0` wschanged.c

Test it in your terminal (./wschanged) to make sure everything looks right. When satisfied, add the program as an Xfce startup application: Settings -> Session and Startup -> Application Autostart.

Solution 3

Most of the answers are 5 years old and as of xubuntu 18.04 don't work anymore. I've put together an updated version of zspotter's answer that runs on 18.04. For simplicity I'll assume that all files are located in the home directory, you can easily adapt the scripts to a different folder.

First create a file called wschanged.c with this contents:

#include <libwnck/libwnck.h>
#include <stdlib.h>

static void
on_active_workspace_changed (WnckScreen    *screen,
                             WnckWorkspace *space,
                             gpointer      data)
{
    // Executes a script on workspace change
    system ("~/.workspace-changed");
}

int main(int argc, char ** argv)
{

   GMainLoop *loop;   
   WnckScreen *screen;

   gdk_init (&argc, &argv);

   loop = g_main_loop_new (NULL, FALSE);
   screen = wnck_screen_get_default();

   g_signal_connect (screen, "active-workspace-changed",
                    G_CALLBACK (on_active_workspace_changed), NULL);

   g_main_loop_run (loop);
   g_main_loop_unref (loop);    

   return 0;
}

Second, create a new Xfce panel anywhere on the edge of your screen and set it to auto-hide. Add the workspace switcher widget. Make a note of the panel number as displayed in the drop-down menu of the panel preferences window, in our example the panel number is 2, adjust accordingly.

Third, here things start to get different, create another script in the same folder called wschanged.sh with this contents (remember to set it as executable with rightclick -> properties-> set as executable, or chmod +x .wschanged.sh in a terminal):

#!/bin/sh

# Show the auto-hidden workspace panel for a moment, then set it to autohide again
xfconf-query -c xfce4-panel -p /panels/panel-2/autohide-behavior -s 1
xfconf-query -c xfce4-panel -p /panels/panel-2/autohide-behavior -s 2

The panels no longer have an autohide property but an autohide-behavior property and thus the adjustments in the script.

Fourth, we have to compile the .c file, the commands in previous answers don't work anymore, as suggested here use:

gcc -O2 -DWNCK_I_KNOW_THIS_IS_UNSTABLE -o wschanged `pkg-config --cflags libwnck-3.0` wschanged.c `pkg-config --libs libwnck-3.0`

If you get an error install libwnck-3-dev with sudo apt install libwnck-3-dev.

You can now test the script with ./wschanged (place yourself in the same directory as the executable). You can also now set to run the command at startup.

Solution 4

You could install conky

save this to a file...

# Static and Variable Values used by Habitual/John Jones

# Static Values
own_window yes
own_window_transparent yes
own_window_type desktop
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
double_buffer yes
use_xft yes
draw_outline no
draw_borders no
uppercase no
draw_shades no
draw_shades yes
double_buffer yes
border_width 0
text_buffer_size 2048
default_color white
update_interval 1.0

# Variable Values # These values are the ONLY thing that change from widget to widget
alignment bm
gap_x 5
gap_y 44
minimum_size 50 1
maximum_width 100

TEXT
${desktop_number}

run it in Xfce. (terminal) -> with conky -c /path/to/file &

icon/launcher conky -c /path/to/file

Everytime you switch desktops, the number of the desktop will be on the screen.

the "alignment bm" value controls desktop placement. bm is bottom-middle.

See http://conky.sourceforge.net/docs.html for DOCs.

Solution 5

I have this little python3 code-blob that reacts to workspace switching.

It's not made to display a notification, but to work around a chromium redraw bug. Anything you want to happen on workspace switch, you can easily throw in to the "fire_the_kludge" function though.

https://github.com/aking1012/chromiumKludge/blob/master/kludge.py

Here's a reworked example that uses libnotify2 to tell you your workspace:

#!/usr/bin/env python3

from gi.repository import Wnck, Gtk, Notify
import signal, time

class Kludge:
    def __init__(self):
        self.first = True
        signal.signal(signal.SIGINT, signal.SIG_DFL)
        self.screen = Wnck.Screen.get_default()
        Notify.init("Workspace Switch Notifier")

    def fire_the_kludge(self, data_a, data_b):
        time.sleep(.1)
        try:
            workspace_num = str(self.screen.get_active_workspace().get_number())
        except:
            workspace_num = "Some error happened"
        popup = Notify.Notification.new("Workspace: " + workspace_num)
        popup.show()
        time.sleep(1)
        popup.close()

    def main(self):
        self.screen.connect("active-workspace-changed", self.fire_the_kludge)
        Gtk.main()

if __name__ == '__main__':
    print("Here comes the kludge")
    kludge = Kludge()
    kludge.main()
Share:
16,185

Related videos on Youtube

intuited
Author by

intuited

// shenanigans

Updated on September 17, 2022

Comments

  • intuited
    intuited almost 2 years

    Not sure if this is an appropriate question for this site, since it's not really specific to Ubuntu. Those who feel it isn't should probably respond to my crosspost on the Unix and Linux stackexchange site. Sorry for any confusion, I'm still negotiating the borderlines between these sites (and superuser.com, where I also crossposted). I guess we all are.

    I make heavy use of workspaces, and have a lot of them (a 6x4 grid). I usually run openbox, but am currently using a machine that doesn't have openbox set up, so I'm using xfce because it's already mostly configured to my liking. I've gotten used to getting visual feedback when I switch workspaces, showing me which one I've just moved to, and am finding myself a bit disoriented in xfce. In openbox this is a big heads-up display, which is pretty much ideal. I'm aware that the workspace switcher panel applet will highlight the active workspace, but this only seems to work for workspaces which have some desktop space showing; since I generally have all my windows maximized, this isn't super helpful.

    Is there a way to enable visual feedback showing the new workspace when switching workspaces in xfce?

  • intuited
    intuited over 13 years
    Hmm. I might be doing something wrong. I'm seeing a number at the bottom of the screen, but a) it's always 24 (the number of desktops I have); b) it only shows up if that part of the desktop is not covered by a window. I'd like it to pop up, nice and big (taking up about 1/4 of the total screen space), on top of whatever's already being displayed, and then fade out after about a second.
  • intuited
    intuited over 13 years
    Hmm.. I'm hoping to avoid using compiz, since I'm running a relatively ancient laptop, but this is good to know.
  • intuited
    intuited almost 12 years
    Cool! I don't use xfce any more, so I don't think I'll have time to test this out. If somebody else can confirm that it works, though (it does look like it should, based on my limited knowledge of gtk code), I'll accept this answer. I think in practice it would take a bit of getting used to, because it's giving the workspace name rather than displaying its position in a grid image. But after a year and a half, it doesn't look like anything more ideal is going to come along :) Maybe the xubuntu maintainers could help you get this into the repository, if you're interested.
  • nkuttler
    nkuttler over 10 years
    Yes this is absolutely functional. On Arch I seem to have libwnck-3.0 so I bumped the version number a little in the second command.
  • Victor
    Victor over 10 years
    Works great! Just changed the icon and the text. Thanks!
  • sbrattla
    sbrattla about 7 years
    Tried compiling this on a fresh install of Xubuntu 16.04 but fails with wsn.c:23:30: fatal error: libnotify/notify.h: No such file or directory
  • fransua
    fransua over 3 years
    @intuited you might use $desktop_name instead conky.sourceforge.net/variables.html