This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[SOLVED] Run shell script at unlock

Tags: None
(comma "," separated)
WindPower
Registered Member
Posts
27
Karma
0
OS

[SOLVED] Run shell script at unlock

Sat May 30, 2009 3:05 am
Hello,
I've been trying to run a script right when KDE is locked and succeeded, now I'm trying to run another script (that basically "undoes" what the first script does) each time the environment is unlocked.

First, the "lock" script. I'm using a really ugly method, so I'd like to know if there's a more elegant way to do it. If not, that's fine, I'm mentioning it here just in case: I moved /usr/lib/kde4/libexec/krunner_lock to /usr/lib/kde4/libexec/krunner_lock_bin, then created a shell script at /usr/lib/kde4/libexec/krunner_lock and put the script I wanted to execute, followed by a call to /usr/lib/kde4/libexec/krunner_lock_bin. It's ugly but it works :undecided:

I'd like to execute another script whenever the workspace is unlocked. I don't mind if it's hackish (can't get more hackish than the method above). I tried using the Autostart thing in the System settings, but you can only set scripts to start on system/session startup, not session unlock.

(Should I have posted this in the "Users" forum rather than the "Developers" one? :-O)

Edit: While you could have guessed looking at the path I mentioned (/usr/lib/kde4/...), I'm using KDE 4. I guess it's important to mention that. Kubuntu 9.04 64-bit to be more precise.

Last edited by WindPower on Mon Jun 01, 2009 4:44 pm, edited 1 time in total.
User avatar
anda_skoa
KDE Developer
Posts
783
Karma
4
OS

[SOLVED] Run shell script at unlock

Sat May 30, 2009 10:22 am
Can't you put the second script after you call krunner_lock_bin?

Cheers,
_


anda_skoa, proud to be a member of KDE forums since 2008-Oct.
WindPower
Registered Member
Posts
27
Karma
0
OS

[SOLVED] Run shell script at unlock

Sat May 30, 2009 3:36 pm
anda_skoa wrote:Can't you put the second script after you call krunner_lock_bin?

Cheers,
_
No, because that gets executed whenever the mouse is moved while the screensaver is running, not when the session is unlocked.

I found a way to do it: http://live.gnome.org/GnomeScreensaver/ ... 8bb9b33043 But... It's for Gnome :-(

Edit: Also found this, which I could adapt to my needs: http://trac.gajim.org/wiki/KDesktopLock But... KDE 3 :-( Could it be made to work with KDE 4?

Last edited by WindPower on Mon Jun 01, 2009 4:43 pm, edited 1 time in total.
WindPower
Registered Member
Posts
27
Karma
0
OS
Solved! Basically, krunner_lock is called with the argument --forcelock when the session gets locked. If this argument is not present, the session unlocks without password whenever the mouse is moved (which is why krunner_lock_bin exited right when the mouse moved). My bash script, which replaced krunner_lock, didn't pass its own arguments to krunner_lock_bin, but now it does and the whole thing works.
For those of you who want to do something similar, here's the code:
Code: Select all
#!/bin/sh

# Do stuff before locking...
/usr/lib/kde4/libexec/krunner_lock_bin $@
# Do stuff after unlocking...
And don't forget to chmod +x /usr/lib/kde4/libexec/krunner_lock ;-)

Last edited by WindPower on Sat May 30, 2009 5:11 pm, edited 1 time in total.
KIAaze
Registered Member
Posts
17
Karma
0
Hi,

I'm running Kubuntu 9.10 and can't find /usr/lib/kde4/libexec/krunner_lock or any krunner_lock.
Have you managed to adapt the script to the new Kubuntu/KDE version?
WindPower
Registered Member
Posts
27
Karma
0
OS
KIAaze wrote:Hi,

I'm running Kubuntu 9.10 and can't find /usr/lib/kde4/libexec/krunner_lock or any krunner_lock.
Have you managed to adapt the script to the new Kubuntu/KDE version?
Yes, it's here now:
/usr/lib/kde4/libexec/kscreenlocker
KIAaze
Registered Member
Posts
17
Karma
0
Thanks. I guess I didn't search hard enough. :)
KIAaze
Registered Member
Posts
17
Karma
0
While I'm at it, would you know how to do the following things through the script?:
-mark xchat as away/back
-mark skype as away/back
-pause/resume or change speeds in ktorrent (or other torrent client)
-pause/resume amarok

I'm also interested in any other interesting ideas you may have. :)

My current script:
Code: Select all
#!/bin/sh

#store compositing setting
COMPOSITING=`qdbus org.kde.kwin /KWin org.kde.KWin.compositingActive`

# Do stuff before locking...
if [ "$COMPOSITING" = "true" ]
then
 kwriteconfig --file kwinrc --group Compositing --key Enabled false
 kwin --replace &
fi

# lock screen
/usr/lib/kde4/libexec/kscreenlocker.bin $@

# Do stuff after unlocking...
if [ "$COMPOSITING" = "true" ]
then
  # turn on
  kwriteconfig --file kwinrc --group Compositing --key Enabled true
  # Then restart kwin (in Alt+F2) with:
  kwin --replace &
fi

;)
WindPower
Registered Member
Posts
27
Karma
0
OS
I wouldn't know for XChat as I don't use it. Most of these programs should have a D-Bus interface, so it should be possible.
For Pidgin, you can use the purple-remote utility (it's a wrapper around its D-BUS interface)
For Skype, you can either use Pidgin with the skype4pidgin plugin and set your status from Pidgin, or you can use the Skype API which is apparently available via D-BUS on the Linux version of Skype: https://developer.skype.com/Docs/ApiDoc ... I_on_Linux
For XChat: http://blogs.gnome.org/ovitters/2005/11 ... and-xchat/
For KTorrent: http://www.kvirc.de/docu/fnc_system.dbus.html has some example for stopping KTorrent's downloads, or you can use KTorrent's scripting interface: http://ktorrent.org/wiki/index.php/Writing_Scripts
For Amarok, I'm sure it has a D-BUS interface too but you can use amarok with command-line arguments to get what you want: http://docs.kde.org/development/en/extr ... tions.html
WindPower
Registered Member
Posts
27
Karma
0
OS
It's possible to go a long way with D-Bus... Here's some of the stuff I have in my lock script:

Code: Select all
qdbus org.kde.kmix /Mixer0 mute Master:0 > org.kde.kmix.Mixer0.mute # Saves whether the volume is currently muted or not
purple-remote 'setstatus?status=away&message=Away' # Sets away status on all accounts (including Skype)
qdbus org.kde.konversation /irc setAway Away. # Set away message on IRC (using Konversation)
qdbus org.kde.kmix /Mixer0 setMute Master:0 true # Mute the speakers
qdbus org.kde.amarok /Player Pause # Pause Amarok


And unlock:
Code: Select all
purple-remote 'setstatus?status=available&message=' # Unset away status on all IM accounts
qdbus org.kde.konversation /irc setBack # Unset away on IRC
qdbus org.kde.kmix /Mixer0 setMute Master:0 `cat org.kde.kmix.Mixer0.mute` # Reset the speakers' mute status to what it was before locking
qdbus org.kde.amarok /Player Play # Resume music


Note: I am saving the mute status in a file rather than a script variable because my locking and unlocking scripts are two separate files; if you do all the scripting in /usr/lib/kde4/libexec/kscreenlocker, you can use variables just fine.
User avatar
bk322
Registered Member
Posts
7
Karma
0
OS
Now it's June-2012 and on Kubuntu 12.04 I'm using the following script:

Code: Select all
#!/bin/bash
/usr/lib/kde4/libexec/kscreenlocker --forcelock
echo "it works!"


run it from the command line - if You are not sure what it is doing.


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft