LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 10-22-2014, 02:07 PM   #1
atelszewski
Member
 
Registered: Aug 2007
Distribution: Slackware
Posts: 948

Rep: Reputation: Disabled
Session, DE, logout and unsaved documents


Hi,

I've been looking into the topic for some time now and cannot find anything relevant or I'm asking the wrong question on the search engines.

This question is not targeted at any particular Desktop Environment, although I'm particularly interested in XFCE and KDE.

The problem and the question:
Let's say that I'm in the middle of editing some kind of document (Kate, Writer, GIMP, etc.) and I have unsaved changes (that is the document is in modified state). And now I Log Out from the session and the process happily happens and I'm logged out... and my unsaved changes to the documents are lost. Is there a way to make the Desktop Environment (session manager) aware of the opened and unsaved documents and to prevent the session termination if such documents exist?

OS & DE:
Slackware 14.1
XFCE 4.10 & KDE 4.13.3

--
Best regards,
Andrzej Telszewski
 
Old 10-23-2014, 08:30 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,321
Blog Entries: 28

Rep: Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141Reputation: 6141
So far as I know, there isn't. The open program is aware that you have made changes to an open file; so far as I know, the desktop environment is not.
 
Old 10-24-2014, 10:46 AM   #3
atelszewski
Member
 
Registered: Aug 2007
Distribution: Slackware
Posts: 948

Original Poster
Rep: Reputation: Disabled
Hi,

To make the picture even bigger, the problem also applies to applications like Thunderbird, Osmo or Figaro's Password Manager. I start them all, together with the DE using the autostart and I would like them to be properly closed when I log out without having to close them separately. What I mean by not proper closure when logging out, is that these applications will lose their last selected views, sometimes adjusted colums widths.

I believe the problem is that when I log out, all the apps get simply killed.
Is there a way to tell the applications to close gracefully when logging out?

I once came across a solution that was using a script called when logging out and that was sending signals to specified applications as to tell them to close gracefully. But it was rather an awkward hack, using mix of waiting for PID and sending the signal more than once, because the application wouldn't react only for a single signal. Also not all the apps would close properly when sending the signal, so there was a hack to detect opened X apps to close them using some window manager message passing system - and it wasn't always working properly.

Is there a solution that does not need that much messing around?

--
Best regards,
Andrzej Telszewski
 
Old 10-24-2014, 12:58 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
i subscribed to this thread yesterday, but without commenting - because all i had to say was what frankbell said, except with "i might be wrong" added.

and i have been wondering this myself - it seems such a logical and uncomplicated feature to have.

maybe the "big ones" like kde, unity, gnome have it?
 
Old 06-10-2015, 07:00 PM   #5
atelszewski
Member
 
Registered: Aug 2007
Distribution: Slackware
Posts: 948

Original Poster
Rep: Reputation: Disabled
Hi,

I've been experimenting for some time now and finally I put myself together to tell what I have found out.

I would call the situation an ordered mess. Mess, because we all know how the situation looks like, ordered, because it's not completely bad and could improve in the future.

The biggest problem is that some (or many) applications can not be closed externally. Basically there are two categories:
1. Applications that do not support external closure,
2. Applications that support external closure.

Category 1. is a bad thing (tm), because you need a lot of trickery to close it cleanly and it might sometimes fail. Thunderbird is an example of the application that falls into this category. It cannot be cleanly closed neither by a signal, nor by dbus, not even by command line. To deal with the Thunderbird clean close I use xdotool sending keystrokes, but it does not always work (well, works most of the time). Another examples of the applications that I use and they don't support external closure are: FPM2 (Figaro's Password Manager 2) and Osmo (personal organizer). For those two applications I have modified their source code to catch SIGTERM and exit cleanly.

Category 2. is a much nicer one. Basically I discovered 2 possibilities (a, b) and I can think of a third one (c):
a) application supports dbus and registers with the session manager,
b) application supports catching signals and clean closure,
c) application supports command line closure of a running instance(s).

Possibilities b) and c) are the most cumbersome here. You have to track the PID of the application and send it a signal when you want to close it (I'm not entirely sure if the signal is automatically sent when the X server is closed) and the application has no possibility of preventing the X server (session) shutdown, which is the whole point here. The example of application I use is Liferea (feed reader).

Possibility a) is the most promising one (actually it works as expected). When this kind of application starts, it registers itself with the session manager (over dbus) and then it is able to receive a signal when a user requests Log out. Then, the application can reply to the session manager that it has unsaved document and prevent the session from being shutdown if the user chooses so. Disclaimer: I'm not entirely sure about this mechanism, but I guess it is something like that. Example of an application that falls here is Kate (KDE's text editor).

My solution as of now for the applications that do not register with the session manager over dbus is the following one (I'm using Xfce):
1) I have overridden xfce4-session-logout with my own script, which is invoked whenever the user wants to log out or shutdown,
2) the script presents the user with a dialogue of confirmation,
3) if the user confirms the requested action, the script checks if there are some interesting applications running and grabs theirs PIDs,
4) the script tries to close the applications using per-application trickery,
5) if the application is not closed, the script spins over the PID preventing the session from being closed, which forces the user to close the specified application manually.

The script is as follows:
Code:
#!/bin/sh

ACTION=
PID_TERM=
CMD_WCLO=
PID_WCLO=

##
PID_TERM[0]=$(pgrep -d' ' -f -U$UID "^/usr/bin/osmo")
PID_TERM[1]=$(pgrep -d' ' -f -U$UID "^/usr/bin/liferea")
PID_TERM[2]=$(pgrep -d' ' -f -U$UID "^/usr/bin/fpm2")
PID_TERM[3]=$(pgrep -d' ' -f -U$UID "xfce4-notes")

##
CMD_WCLO[0]="thunderbird"
PID_WCLO[0]=$(pgrep -d' ' -U$UID "^${CMD_WCLO[0]}$")

echo "PID TERM: ${PID_TERM[@]}"
echo "PID WCLO: ${PID_WCLO[@]}"

################################################################################
function logout_dialog() {
################################################################################
  local ACTION=

  ACTION=$(kdialog --title "Log out" --default "Log out" --combobox "Choose action:" "Log out" "Restart" "Shutdown")
  [ -z "$ACTION" ] && return

  case "$ACTION" in
    "Log out")  ACTION="--logout" ;;
    "Restart")  ACTION="--reboot" ;;
    "Shutdown") ACTION="--halt"   ;;
    *)          ACTION=           ;;
  esac

  echo $ACTION
}

################################################################################
function close_window() {
################################################################################
  local PID=$1
  local CMD=$2
  local WINDOWID=

  [ -n "$CMD" ] && "$CMD"

  WINDOWID=$(xdotool search --pid $PID)
  for WIN in $WINDOWID; do
    if xprop -id $WIN | grep _NET_WM_ACTION_CLOSE; then
      xdotool windowactivate --sync $WIN
      xdotool key --clearmodifiers --window $WIN ctrl+q
      break
    fi
  done
}

ACTION=$(logout_dialog)
[ -z "$ACTION" ] && exit 0

for PID in ${PID_TERM[@]}; do
  kill -TERM $PID
done

for ((idx = 0; idx < ${#PID_WCLO[@]}; idx++)); do
  [ -n "${PID_WCLO[$idx]}" ] && close_window ${PID_WCLO[$idx]} ${CMD_WCLO[$idx]}
done

for PID in ${PID_TERM[@]} ${PID_WCLO[@]}; do
  while kill -0 $PID &>/dev/null; do
    echo "PID WAIT: $PID"
    sleep 1
  done
done

/usr/bin/xfce4-session-logout -f $ACTION

And finally I'd like to ask for some help: I've been looking for an example on how to an application can register with the session manager over dbus and interact with it in order to prevent the shutdown (like in the case of Kate) but couldn't find anything interesting. I'm sure it must be some generic solution, because from my experience it works the same way both in KDE and Xfce. If anybody knows something, please let me know.

--
Best regards,
Andrzej Telszewski
 
Old 06-13-2015, 04:02 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by atelszewski View Post
I would call the situation an ordered mess.
that's the wonderful world of linux in a nutshell .

it seems you put some effort into this and it looks nice and clean! though i see you still have to manually add applications to the script.

what works for me:
mostly just awareness.
then i realised, if i use suspend instead of logout and hibernate instead of shutdown, the problem goes away.

logging out and rebooting then become administrative tasks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how logout old session sundalo1205 Linux - Newbie 1 05-19-2009 07:25 AM
Save session on logout carlwilson Red Hat 1 07-08-2008 07:00 PM
vnc session is not exiting at logout marozsas Linux - Software 3 07-18-2006 02:53 PM
Unable to logout of kde session arubin Slackware 8 05-12-2006 10:22 PM
Save session on logout in Fluxbox berraf Slackware 1 12-22-2005 03:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration