You are not logged in.

#1 2010-01-25 15:44:04

anti-destin
Member
Registered: 2009-02-14
Posts: 234

[solved] creating a new pm-utils hook

here's the overall problem. suspend/resume fails when compositing is enabled. i've discovered, however, that it seems to work when compositing is disabled and kwin is restarted. to get it all to work, i need to disable compositing before suspending, and on resume, restart kwin and enable compositing.

i'm trying to follow this guide here: http://wiki.archlinux.org/index.php/Pm- … _own_hooks

in particular, i'm looking to use this particular hook: http://wiki.archlinux.org/index.php/Pm- … re_suspend

i have a few questions.

1. what does all that code mean?
2. how do i add 'kwin --replace' to the code?

thanks.

Last edited by anti-destin (2010-01-27 17:47:33)

Offline

#2 2010-01-25 16:03:27

benob
Member
Registered: 2008-11-11
Posts: 187

Re: [solved] creating a new pm-utils hook

The script is in bash. If you are not familiar with it, you might start looking at, for instance http://www.linuxconfig.org/Bash_scripting_Tutorial. Make sure you understand about case/esac, variable affectation and backquotes.

This is a bash script.

#!/bin/bash

Get the name of the current user (I prefer the second technique mentioned to do that) and put it in the USER variable. Finger gets you the list of users, grep is used to keep lines in that list according to regular expressions, the -o option tells it to keep only the exact match.

USER=`finger|grep '*:0'|grep -o '^\w*'`

Get the identifier for the dbus session of that user. Dbus is a daemon that manages program configurations and other stuff. Sed is used to strip of the DBUS... string from the result.

DBUS_SESSION_BUS_ADDRESS=`grep -o 'DBUS_SESSION_BUS_ADDRESS=.*' /home/$USER/.dbus/session-bus/*|sed s/DBUS_SESSION_BUS_ADDRESS=//`

In bash, case/esac has similar semantics to switch() in c. It substitutes for a series of conditional statements, like "if $1 is hibernate, then... else if $1 is suspend...". By the way, $1 is the first argument of the script, the action to perform.

case $1 in
   hibernate)
       ;;
   suspend)

Then, if the action is suspend, it tells the dbus daemon that if compositing is active, it should toggle it. Sudo changes the user to the current user instead of root (because dbus won't talk to a different user). Qdbus is usedto read/set variables, it reads dbus id from DBUS_SESSION_BUS_ADDRESS which is set before executing the command, on the same line (this is the sh way of doing this).

       if `sudo -u $USER -i DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS qdbus org.kde.kwin /KWin compositingActive`;
       then
               sudo -u $USER -i DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS qdbus org.kde.kwin /KWin toggleCompositing;
               sleep 1
       fi
       ;;
   thaw)
       ;;

Same story on resume.

   resume)
               sudo -u $USER -i DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS qdbus org.kde.kwin /KWin toggleCompositing;
       ;;
   *)  echo "somebody is calling me totally wrong."
       ;;
esac

I don't know why you want to run kwin --replace, but if you need to, you should do it in the resume section...

Offline

#3 2010-01-25 17:23:11

anti-destin
Member
Registered: 2009-02-14
Posts: 234

Re: [solved] creating a new pm-utils hook

thank you, benob. your explanation was extremely helpful.

as for why i want 'kwin --replace', the reason is simple. i've found that restarting kwin before enabling compositing prevents the problem from occurring.

i tried the following script:

#!/bin/bash
USER=`who | grep ':0' | grep -o '^\w*' | head -n1`
. /home/$USER/.dbus/session-bus/*
case $1 in
   hibernate)
       ;;
   suspend)
       if `sudo -u $USER -i DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS qdbus org.kde.kwin /KWin compositingActive`;
       then
               sudo -u $USER -i DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS qdbus org.kde.kwin /KWin toggleCompositing;
               sleep 1
       fi
       ;;
   thaw)
       ;;
   resume)
               kwin --replace
               sudo -u $USER -i DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS qdbus org.kde.kwin /KWin toggleCompositing;
       ;;
   *)
       ;;
esac

but it didn't solve the problem. i checked pm-suspend.log and found this:

/etc/pm/sleep.d/00togglecompositing suspend suspend: Could not connect to D-Bus server: org.freedesktop.DBus.Error.NoServer: Failed to connect to socket /tmp/dbus-x2WQfx6PSQ: Connection refused
success.

and

/etc/pm/sleep.d/00togglecompositing resume suspend: kwin: cannot connect to X server 
Could not connect to D-Bus server: org.freedesktop.DBus.Error.NoServer: Failed to connect to socket /tmp/dbus-x2WQfx6PSQ: Connection refused
Returned exit code 1.

any ideas?

Offline

#4 2010-01-26 12:17:05

benob
Member
Registered: 2008-11-11
Posts: 187

Re: [solved] creating a new pm-utils hook

It's weird. Is dbus still running while suspending?

What happens if you run it from a shell ?

sudo /etc/pm/sleep.d/00togglecompositing suspend

You can probably add "echo $USER; cat /home/$USER/.dbus/session-bus/*" to see exactly how the variables are affected.
Sorry if I can't solve your problem: I'm not a dbus specialist, neither do I use kde.

Offline

#5 2010-01-27 03:31:02

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: [solved] creating a new pm-utils hook

When your resume hooks run you don't have dbus up and running yet (mainly you don't have the correct DBUS address up). Had a similar problem before. You probably have to save $DBUS_SESSION_BUS_ADDRESS on sleep and use it again.

Something like

case $1 in
  hibernate|suspend)
   echo $DBUS_SESSION_BUS_ADDRESS > /home/.dbus_session
    ;;
  thaw|resume)
   DBUS_SESSION_BUS_ADDRESS=`cat /home/.dbus_session` %%put your command here%%
    ;;
esac

Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#6 2010-01-27 17:46:56

anti-destin
Member
Registered: 2009-02-14
Posts: 234

Re: [solved] creating a new pm-utils hook

i got the answer over at the kde forums. two notes:

1. the command 'export DISPLAY=:0' is necessary.
2. the above script is dangerous in the sense that were kwin able to access the display, it would run as root.

a better approach is to create and implement the following three scripts:

~/.local/prepare-suspend.sh

eval 'cat $HOME/.dbus/session-bus/*'
export DISPLAY=:0

if `qdbus org.kde.kwin /KWin compositingActive`; then
        qdbus org.kde.kwin /KWin toggleCompositing
        sleep 2s
fi

~/.local/resume-suspend.sh

eval 'cat $HOME/.dbus/session-bus/*'
export DISPLAY=:0

qdbus org.kde.kwin /KWin toggleCompositing

00togglecompositing

#!/bin/bash
USER=`who | grep ':0' | grep -o '^\w*' | head -n1`
case $1 in
   hibernate)
       ;;
   suspend)
       sudo -u $USER -i /home/$USER/.local/prepare-suspend.sh
       ;;
   thaw)
       ;;
   resume)
       sudo -u $USER -i /home/$USER/.local/resume-suspend.sh
       ;;
   *)
       ;;
esac

simpler, safer, and nicer.

credit goes to bcooksley there.

Offline

Board footer

Powered by FluxBB