GNOME 2.24 Session Save & Restore

The latest version of the GNOME Desktop version 2.24.0 contains pretty much a complete rewrite of it's session handling module gnome-session.

One of the features missing from this release is the ability to Save and Restore your session, now bear in mind the session saving and restoring was not completely functional in the first place, applications such as StarOffice, Firefox and Thunderbird all ignore the session management and thus could not be restored via gnome-session.

So how can one get around this, is there a valid workaround ?

Actually there is and it's not that difficult :)

The new gnome-session implements an autostart mechanism for determining what applications are launched for a given session. The autostart mechanism in turn employs .desktop files to define the applications and paramaters etc. You can use gnome-session-propertites capplet maintain the list of applications to be launched for your session.

The default set of autostart files are located under /usr/share/gnome/autostart, for user specific applications .desktop files are located under $HOME/.config/autostart.

You can add specific applications via the preferences capplet gnome-session-properties, which will create the custom .desktop files for your in $HOME/.config/autostart, or you can manually create then .desktop files yourself.

So specifying what applications should be Launched/Restored for your session is quite easy.

The only issue here is that all your apps will appear on the the same workspace (first one), and as for geometry, you would have to rely on the specific application having some CLI for setting X/Y and width/height.

For me this wasn't enough, so I thought how difficult could be be to write a simple shell script that would given some config data, do all this positioning/sizing for me. Turned out to be relatively simple.

So I hacked together two scripts, save-session and restore-session.

Looking at restore-session firstly, it simply reads config data from $HOME/.config/restore-session.conf. This new config file contains a list of name identified applications with location and sizing information. Each line contains TAB separated fields of the following format :

    NAME WORKSPACE X Y WIDTH HEIGHT

e.g.
    #NAME WORKSPACE X Y WIDTH HEIGHT
    GnomeTerminal1 0 397 266 593 719
    GnomeTerminal2 0 1001 266 593 719
    GnomeTerminal3 1 30 30 726 719


Line 1 of the config file is ignored as it just defines column headers, for readability. The NAME column is a single word identification for the specific application. In this example I specify three gnome terminals, two will be located on first workspace (bear in mind workspaces tend to be numbered from 0), and the third terminal located in second workspace.

The restore-session script uses the command line utility wnckprop to get and set information for application windows. This is delivered as part of the SUNWgnome-panel package.

So expanding on the example config file above, for my session I want to restore three terminals with the location and size specified. I now need three .desktop files to start my three terminals on login.

Here's an example for one of them :

    [Desktop Entry]
    Type=Application
    Name=Terminal 1
    Exec=gnome-terminal --title="GnomeTerminal1"
    Icon=utilities-terminal
    Comment=Run a command line shell

Notice how you can specify the title for your terminal via the --title CLI option for gnome-terminal. Just create three of these desktop files and set the titles to be GnomeTerminal1, GnomeTerminal2 and GnomeTerminal3. and place them in $HOME /,config/autostart. The next time you login three terminals will be launched.

Then you ask but this dosen't restore the locations/sizes, there are two methods of doing this part. One would be to create another .desktop file and call the restore-session script directly from it, this however is not reliable as there's no way of guaranteeing the launching order of applications via the autostart method, thus the restore-session could get run before your terminals, and thus will not be able to set their window properties.

The 2nd method is to simply place a launcher on your panel, which runs this script. Again not ideal but still a very simple method to set the properties, you just need to press one launcher each time you login in and presto all your windows get relocated and resized to where you want them.

The 2nd script save-session simply get's the name, size and location properties of all your current application windows and generates a $HOME/.config/restore-session.conf file. This generates config file requires hand editing to set the correct names, but it's a good starting point for generating a $HOME/.config/restore-session.conf file.

Both scripts were written over a few hours one evening so are not perfect, but they do highlight a means of working around the fact that your session is not restored anymore. I'm sure they can be improved. To access the source for the scripts and the example config file just click on any of the links.

Comments:

you just saved me 2 hours.
1 thing? where do i get wnckprop?

i may have a few tools for you.

Posted by will wharton on October 23, 2008 at 12:46 AM IST #

Will,

wnckprop utility is delivered to OpenSolaris as part of the SUNWgnome-panel package. You can also download the latest source from the GNOME 2.24 release website :
http://ftp.gnome.org/pub/GNOME/desktop/2.24/2.24.1/

And it should build quite easily regardless of \*nix you are using.

As I use OpenSolaris, here's some useful links :

Desktop GNOME Community on OpenSolaris:
http://www.opensolaris.org/os/community/desktop/communities/gnome/

JDS Project on OpenSolaris :
http://www.opensolaris.org/os/project/jds/

Building JDS on OpenSolaris :
http://www.opensolaris.org/os/project/jds/contributing/building/

Hope this helps.

Posted by Matt Keenan on October 23, 2008 at 04:07 AM IST #

wnckprop isn't available on ubuntu 8.10, but there's libwnck22 with this files:
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libwnck22
/usr/share/doc/libwnck22/copyright
/usr/lib
/usr/lib/libwnck-1.so.22.3.13
/usr/share/doc/libwnck22/README
/usr/share/doc/libwnck22/AUTHORS
/usr/share/doc/libwnck22/NEWS.gz
/usr/share/doc/libwnck22/changelog.Debian.gz
/usr/lib/libwnck-1.so.22

There's some way to use this library?

Regards...

Posted by parq on November 02, 2008 at 10:15 AM GMT #

Some workaround:
As i'm a little lazy and don't want to install 58 packages in ubuntu 8.10 to get wnckprop, i did the following:
- Get the file libwnck-dev_2.24.1-0ubuntu1_i386.deb NOT from synaptic. Instead of this, i've fonud the file on an ubuntu mirror like this: http://mirror.switch.ch/ftp/mirror/ubuntu/pool/main/libw/libwnck/libwnck-dev_2.24.1-0ubuntu1_i386.deb
- Open the file with file roller, do NOT open it with gdebi.
- Open data.tar.gz
- Find the file in ./usr/bin/wnckprop
- Extract it to the filesystem in /usr/bin/wnckprop
- Correct permission to 755 root:root
It's ready to use!

Regards...

Posted by parq on November 02, 2008 at 10:45 AM GMT #

I've found that wnckprop is language dependant and the scripts doesn't work in spanish.
I've fixed save-session script, but the 'name' field is the name of the file opened by an application, so restore-session can't open the correct app.
This is an output of wnckprop in spanish:

$ wnckprop --window=44040529
Nombre: GNOME 2.24 Session Save & Restore : Mattman's Blog - Mozilla Firefox
Nombre del icono: GNOME 2.24 Session Save & Restore : Mattman's Blog - Mozilla Firefox
Iconos: establecer
En el área de trabajo 3 («Escritorio 4»)
En la pantalla: 0 (Gestor de ventanas: Metacity)
Tipo de ventana: ventana normal
Geometría (x, y, anchura, altura): 0, 25, 1024, 718
Grupo de clase: Firefox
XID: 44040529
PID: 1232
ID de sesión: 1069b0d0de9f2047ea122565576718383200000320110041
Líder del grupo: 44040193
Estado: normal
Acciones posibles: mover, redimensionar, enrollar, desenrollar, maximizar horizontalmente, desmaximizar horizontalmente, maximizar verticalmente, desmaximizar verticalmente, cambiar área de trabajo, pinchar, despinchar, minimizar, desminimizar, maximizar, desmaximizar, cambiar modo a pantalla completa, cerrar, poner encima, no poner encima, poner debajo, no poner debajo
$

Please tell me wich line you are getting as $win_id

Tx!

Posted by parq on November 02, 2008 at 01:57 PM GMT #

Great to see you hacking the scripts, bear in mind I knocked these up pretty
quickly.

Now you ask where I got $win_id from, firstly I'm presuming you are talking
about restore-session script. If so the line :

Line 64 : win_id=`grep $win_title /tmp/$$.wnckprop | head -1 | awk 'BEGIN {FS=": ";}{print $1;}'`

Is what you are looking for. Very simply, this line searches for a specific
string $win_title, in a list windows that was generated by wnckprop --list
and it extracts the first field from the line and thats the Window Id or XID
for that window.

From above line, the file /tmp/$$.wnckprop was produced by
"wnckprop --list"

$win_title is the first column of a line from a restore-session.conf file.
e.g.
GnomeTerminal1 0 397 266 593 719

win_title would be "GnomeTerminal1"

hope this makes sense.

Posted by Matthew Keenan on November 03, 2008 at 04:06 AM GMT #

Matthew,

I mean wich line of wnckprop --window=xxxxxxxx did you toke as $win_id.
I've post an output of a window (wnckprop --window=44040529) so you can tell me wich line (number, #) is the one for $win_id. 1st line? 8th line?
1st:: Nombre: GNOME 2.24 Session Save & Restore : Mattman's Blog - Mozilla Firefox
8th:: Grupo de clase: Firefox
This approuch is to make the script language independent.

I've looking for an option, and I've found wmctrl, wich is simpler to install in Ubuntu 8.10

In both cases (wnckprop and wmctrl) they saw only the apps in windows and not the apps in the tray. Any idea about that?

See you...

Posted by parq on November 03, 2008 at 04:58 AM GMT #

$win_id = XID, which is the 9th line :

9th:: XID: 44040529

This should (and AFAIK is) language independent, as XID is the X server window
id.

$win_title however is dependent on localisation, it's taken from the 1st line
Nombre (Spanish), Name (English).

Bear in mind I only use one word from this line, and try and make it unique to
that window, this way I name my gnome-terminals in the example I give.

This is a limitation and it would be great to have a better workaround. Like
including the entire string of the window title...

cheers

Matt

Posted by Matthew Keenan on November 03, 2008 at 05:52 AM GMT #

Parg Wrote :
> I think that is easier to work with wmctrl, that provides this info in a single line and it's > simpler to install.

I had a quick look at wmctrl and whilst it comes in it's own package tarball makes it simple
to grab and install, it's usage is quite similar to wnckprop. However wnckprop is installed by
default on OpenSolaris, so you don't need to go hunting for it at all :)

> By the way, i was thinking about other ideas:
> - Use only 1 script, with options like save or restore ('script save' or 'script restore').

Easily done. Work away :)

> - Include in gnome session the command to auto restore the windows on session open ('script
> restore).

This was an actual feature of gnome-session prior to GNOME 2.24, and is a feature that is planned to make it back into gnome-session, hopefully in the GNOME 2.26 timeframe.

> - Add an cron task to save the active windows every 1min or 2min (or whatever) so you don't
> have to make an 'script save' before exit the session.

Like autosave of a document, to be honest I would not see much use for this, most users set up
their session and then save it. They like to know exactly what their session is going to look
like when logging in. Again gnome-session used to have a working option of automatically saving
your session when logging out, thus when you logged back in again you continued from where you left off. A autosaving cron job would only be useful really if you had a session crash.

> - Replace the name 'script' for something better. Like 'gnome-session-autorestore' or
> something.

Not sure what you mean here... but you can rename these scripts to what you want, I only provide
them as an example of what can be done to attempt working around the loss of this feature in
GNOME 2.24.

> I was working with that 'n found some things:
> - win_id
> In restore-session you get win_id from $win_title (line 64) which is the
> #1 field in the restore-session.conf (line 57)
> In save-session you save the #1 field as $win_title (line 105) which is
> set up from 'Name' field of wnckprop output (line 81).
> So, I can't see where u work with XID.

Wnckprop requires XID in order to move it's location and resize it. So I use "Name" field to
locate a specific window, get it's XID and then use the XID when relocating/resizing the actual
window. The XID cannot be stored in a the restore-session.conf file as these ID's are assigned
dynamically by the X-Server when starting up.

> - I was able to use wnckprop to modify existing windows but no for open
> a new one.
> Can the script do that?

Nope, wnckprop or my scripts are not written to start applications, placing a .desktop file in
$HOME/.config/autostart is what tells the session what applications to start.

cheers

Matt

Posted by Matthew Keenan on November 05, 2008 at 05:56 AM GMT #

For those that prefer to use the built-in package management in Ubuntu/Debian, this will get you wnckprop (and much more that you don't need, BTW):

sudo apt-get install libwnck-dev

Bill

Posted by Bill Korb on November 05, 2008 at 10:11 AM GMT #

Bill,

That's why i'm proposing wmctrl, because is cleaner to install than wmckprop. (libwnck-dev install more than 50 packages in ubuntu)

Regards....

Posted by parq on November 06, 2008 at 02:20 AM GMT #

Matt,

Just a question, since this script only moves the apps to their saved geometry and workspace, how do you start the apps?

In my case, the apps don't even start. So, when session-restore script is ran, nothing happens, because the apps are missing.

What happens in your case?

Posted by parq on November 06, 2008 at 03:12 PM GMT #

parq,

As mentioned before, the scripts do not start applications :).

To launch applications at login :
- You need to be running GNOME 2.24.
- Create app.desktop file's and place them in $HOME/.config/autostart
One desktop file for each app you want to start.
- When you log in, gnome-session, reads this autostart directory and for each
desktop file present it launches the corresponding application.

After you login, you can then run restore-session.sh, and this places all the
already launched apps in their save locations.

Posted by Matt Keenan on November 06, 2008 at 03:41 PM GMT #

One tips to avoid localization problems in each script (save/restore-session),
just after the line containing:

SAVED_SESSION=$HOME/.config/restore-session.conf

add these two lines (save and set LANG as expected by the script):

OLD_LANG=`env | grep LANG | awk 'BEGIN {FS=": ";}{print $1;}'`
LANG=en_US.UTF8

And in the last line of each script (save/restore-session) add this line
(restore LANG with its initial value eg before modifications):

LANG=${OLD_LANG}

This is seems to work correctly in french.

Posted by nvieville on December 17, 2008 at 09:55 AM GMT #

To complete my previous comment, here is the restore-session script modified to deals with windows title containing spaces or specials characters and parsing .config/restore-session.conf file containing TAB separated fields:

### Beginning of script - do not copy in the bash script
#!/bin/bash
# name : restore-session
# author : Matt Keenan (matt.keenan@sun.com)
# Date : Oct 2008
#
# restore-session is a quick means of restoring your apps locations
# and geometry. It can be used in conjunction with Gnome 2.24 gnome-sessiojn
# autostart feature, as the save session feature for gnome-session in Gnome
# 2.24 does not exist.
#
# It uses wnckprop to position and resize known application windows as
# specified and identified in $HOME/.config/restore-session.conf
#
# The format of restore-session.conf is :
#
# <App Name> <Workspace (starting at 0)> <X-CoOrd> <Y-CoOrd>
#
# It has been written to work with both metacity and compiz, with metacity
# wnckprop --set-workspace is used, but as with compiz there is only one
# workspace and 4 viewports, the moving to another viewport is achieved
# by increasing the X CoOrdinate by screen widths.

# Saved Session Data file
SAVED_SESSION=$HOME/.config/restore-session.conf
OLD_LANG=`env | grep LANG | awk 'BEGIN {FS=": ";}{print $1;}'`
LANG=en_US.UTF8

# Check if Compiz or metacity running, hacky would be nice to have
# something better
wnckprop --window=`wnckprop --list | head -1 | awk 'BEGIN {FS=": ";}{print $1;}'` | grep compiz > /dev/null
if [ $? -eq 0 ]; then
compiz=1
else
compiz=0
fi

# Get Screen width (only needed when using compiz)
if [ $compiz -eq 1 ]; then
screen_width=`xdpyinfo | grep " dimensions:" | awk ' { \\
dimstr=substr($0, 18, 10); \\
if (index(dimstr, "x") == 5) { \\
print substr(dimstr, 1, 4); \\
} else { \\
print substr(dimstr, 1, 3); \\
} \\
}'`
fi

# Get List of current windows
wnckprop --list > /tmp/$$.wnckprop 2>&1

lineno=0
while read LINE
do
if [ $lineno -eq 0 ]; then
# Skip first line, just column headers
let lineno=$lineno+1
else
win_title=`echo -n "${LINE}" | awk 'BEGIN {FS="\\t";}{print $1;}'`
win_workspace=`echo -n "${LINE}" | awk 'BEGIN {FS="\\t";}{print $2;}'`
win_x=`echo -n "${LINE}" | awk 'BEGIN {FS="\\t";}{print $3;}'`
win_y=`echo -n "${LINE}" | awk 'BEGIN {FS="\\t";}{print $4;}'`
win_width=`echo -n "${LINE}" | awk 'BEGIN {FS="\\t";}{print $5;}'`
win_height=`echo -n "${LINE}" | awk 'BEGIN {FS="\\t";}{print $6;}'`

win_id=`grep -e "${win_title}" /tmp/$$.wnckprop | head -1 | awk 'BEGIN {FS=": ";}{print $1;}'`

echo "$win_title $win_workspace $win_x $win_y $win_width $win_height"

if [ "$win_id" != "" ]; then

if [ $win_x -gt -1 ]; then
x_opt="--set-x=$win_x"
else
x_opt="--set-x=$win_x"
fi

if [ $win_y -gt -1 ]; then
y_opt="--set-y=$win_y"
else
y_opt="--set-y=$win_y"
fi

if [ $win_width -gt 0 ]; then
width_opt="--set-width=$win_width"
else
width_opt=""
fi

if [ $win_height -gt 0 ]; then
height_opt="--set-height=$win_height"
else
height_opt=""
fi

if [ $compiz -eq 1 ]; then
# if x > current screen width then just use it. As it and
# ignore workspace column otherwise use workspace column
# to calculate new x coord to position in correct viewport
if [ $win_x -gt $screen_width ]; then
x_opt="--set-x=$win_x"
else
let new_x=$screen_width\*$win_workspace
let new_x=$new_x+$win_x
x_opt="--set-x=$new_x"
fi

# echo "$win_title:$win_workspace : wnckprop --window=$win_id $x_opt $y_opt $width_opt $height_opt"
wnckprop --window=$win_id $x_opt $y_opt $width_opt $height_opt
else
# echo "$win_title:$win_workspace : wnckprop --window=$win_id --set-workspace=$win_workspace $x_opt $y_opt $width_opt $height_opt"
wnckprop --window=$win_id --set-workspace=$win_workspace $x_opt $y_opt $width_opt $height_opt
fi
else
echo "$win_title:$win_workspace - Failed to get win_id"
fi
fi
done < $SAVED_SESSION

rm /tmp/$$.wnckprop
LANG=${OLD_LANG}
### End of script - do not copy in the bash script

Be careful of potentially cropped lines in this HTML page!

Good manual saving/restoring gnome sessions ;)

Posted by nvieville on December 18, 2008 at 02:23 AM GMT #

It baffles me how bad the session management in Gnome is, even when only considering native Gnome applications like Gnome-terminal. It has not worked properly, or not at all, in the last 3 or 4 Fedora releases. Last version did restore my terminals, but they were all piled up at the top left corner of each desktop. The current version does nothing at all. How hard can it be to save a list of open programs and windows in a file and restore them? As you have proven, not too hard.

Posted by DrLex on December 18, 2008 at 03:42 AM GMT #

DrLex, it is quite hard, because it requires the cooperation of lots of differents players, including some which where designed and established decades ago.

Posted by Mariano on December 30, 2008 at 07:05 AM GMT #

Thanks for the scripts.
I wrote these for KDE a long time ago; I save user setting and sessions to tar.bz2 files every time the system transitions from RL3 or RL5 to RL0.

Posted by William Moss on January 17, 2009 at 01:58 PM GMT #

State of session management in GNOME is horrible and seems to have regressed over the years. Thanks for providing this.

Posted by A Sysadmin on August 22, 2009 at 10:25 AM IST #

Great post. Thanks for the scripts, based on them I wrote a little Python script to provide similar functionality: http://thialfihar.org/projects/window_position_session/

It can deal with spaces in the titles, allows regular expressions to match the window titles, and also uses wnckprop's "actions" to restore the state of the window (such as maximized, pinned, etc.).

Hope it'll be useful for some. :)

As mentioned in the post on the site: I'd still like to add some way to restore the tab order in the bottom Panel. Anyone got any ideas for that?

Posted by Thialfihar on October 04, 2009 at 08:47 AM IST #

Thialfihar,

Thanks for writing this, I'll give it a spin when I get a chance. I really like the ability to use regular expressions on the window titles.

As for tab order on the bottom panel, the applet here is the window list applet and or
wnck-applet. The order of tabs in the Window List applet are purely by Launch sequence and as you point out gnome-session cannot guarantee launch order, setting these up in a custom sequence AFAIK cannot be done. Maybe writing some new interface to wnck-applet to allow this might be the way forward, just not sure how many people would want this. And coupled with the fact that gnome-panel and it's applets are likely going to be deprecated in favour of gnome-shell for the next major rev of GNOME e.g. 3.0, this might be work wasted...

Great script though and glad I could give some ideas for it's creation :)

Posted by Matt Keenan on October 09, 2009 at 03:36 AM IST #

l

Posted by guest on December 02, 2009 at 01:15 AM GMT #

Looks like this topic has been left alone for a while. However I actually have this need and am quite surprised that great session saving features aren't already included in Lucid 10.04 (what I'm running). Case in point, Nautilus crashed on me about four times a month and I can restore all of the directories that I was running (of Nautilus) on my several desktops. Any ideas how this script might be tweaks or could you point me to another solution that already exists? Thanks,

Posted by Tim on June 13, 2011 at 02:12 AM IST #

It's exactly what I want!
But, where are those two scripts save-session and restore-session?
Both links seems to be broken. I would like to use them ;)
Great post!

Posted by WillyTell on April 30, 2012 at 04:30 PM IST #

@WillyTell,
Just updated the links, forgot to s/sun.com/oracle.com/ when the blog was transferred., Just right click and save as.

I'm running Gnome 2.30 on Solaris 11 at the moment and the same solution still works for me.

@tim
Apologies in lateness of reply, I'm afraid I don't know of any other solution available.

Posted by Matt Keenan on May 01, 2012 at 03:29 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Install engineer at Oracle who is passionate about Music, Sport and has a soft spot for Solaris

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today