dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | #--------------------------------------------- |
| 3 | # xdg-screensaver |
| 4 | # |
| 5 | # Utility script to control screensaver. |
| 6 | # |
| 7 | # Refer to the usage() function below for usage. |
| 8 | # |
| 9 | # Copyright 2006, Bryce Harrington <bryce@osdl.org> |
| 10 | # |
| 11 | # LICENSE: |
| 12 | # |
| 13 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 14 | # copy of this software and associated documentation files (the "Software"), |
| 15 | # to deal in the Software without restriction, including without limitation |
| 16 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 17 | # and/or sell copies of the Software, and to permit persons to whom the |
| 18 | # Software is furnished to do so, subject to the following conditions: |
| 19 | # |
| 20 | # The above copyright notice and this permission notice shall be included |
| 21 | # in all copies or substantial portions of the Software. |
| 22 | # |
| 23 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 24 | # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 26 | # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 27 | # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 28 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 29 | # OTHER DEALINGS IN THE SOFTWARE. |
| 30 | # |
| 31 | #--------------------------------------------- |
| 32 | |
| 33 | manualpage() |
| 34 | { |
| 35 | cat << _MANUALPAGE |
| 36 | Name |
| 37 | |
| 38 | xdg-screensaver - command line tool for controlling the screensaver |
| 39 | |
| 40 | Synopsis |
| 41 | |
| 42 | xdg-screensaver suspend WindowID |
| 43 | |
| 44 | xdg-screensaver resume WindowID |
| 45 | |
| 46 | xdg-screensaver { activate | lock | reset | status } |
| 47 | |
| 48 | xdg-screensaver { --help | --manual | --version } |
| 49 | |
| 50 | Description |
| 51 | |
| 52 | xdg-screensaver provides commands to control the screensaver. |
| 53 | |
| 54 | xdg-screensaver is for use inside a desktop session only. It is not recommended |
| 55 | to use xdg-screensaver as root. |
| 56 | |
| 57 | Commands |
| 58 | |
| 59 | suspend WindowID |
| 60 | |
| 61 | Suspends the screensaver and monitor power management. WindowID must be the |
| 62 | X Window ID of an existing window of the calling application. The window |
| 63 | must remain in existance for the duration of the suspension. |
| 64 | |
| 65 | WindowID can be represented as either a decimal number or as a hexadecimal |
| 66 | number consisting of the prefix 0x followed by one or more hexadecimal |
| 67 | digits. |
| 68 | |
| 69 | The screensaver can be suspended in relation to multiple windows at the |
| 70 | same time. In that case screensaver operation is only restored once the |
| 71 | screensaver has been resumed in relation to each of the windows |
| 72 | |
| 73 | resume WindowID |
| 74 | Resume the screensaver and monitor power management after being suspended. |
| 75 | WindowID must be the same X Window ID that was passed to a previous call of |
| 76 | xdg-screensaver suspend |
| 77 | activate |
| 78 | Turns the screensaver on immediately. This may result in the screen getting |
| 79 | locked, depending on existing system policies. |
| 80 | lock |
| 81 | Lock the screen immediately. |
| 82 | reset |
| 83 | Turns the screensaver off immediately. If the screen was locked the user |
| 84 | may be asked to authenticate first. |
| 85 | status |
| 86 | Prints enabled to stdout if the screensaver is enabled to turn on after a |
| 87 | period of inactivity and prints disabled if the screensaver is not enabled. |
| 88 | |
| 89 | Options |
| 90 | |
| 91 | --help |
| 92 | Show command synopsis. |
| 93 | --manual |
| 94 | Show this manualpage. |
| 95 | --version |
| 96 | Show the xdg-utils version information. |
| 97 | |
| 98 | Exit Codes |
| 99 | |
| 100 | An exit code of 0 indicates success while a non-zero exit code indicates |
| 101 | failure. The following failure codes can be returned: |
| 102 | |
| 103 | 1 |
| 104 | Error in command line syntax. |
| 105 | 3 |
| 106 | A required tool could not be found. |
| 107 | 4 |
| 108 | The action failed. |
| 109 | |
| 110 | Examples |
| 111 | |
| 112 | xdg-screensaver suspend 0x1c00007 |
| 113 | |
| 114 | Causes the screensaver to be disabled till xdg-screensaver resume 0x1c00007 is |
| 115 | called. 0x1c00007 must be the X Window ID of an existing window. |
| 116 | |
| 117 | _MANUALPAGE |
| 118 | } |
| 119 | |
| 120 | usage() |
| 121 | { |
| 122 | cat << _USAGE |
| 123 | xdg-screensaver - command line tool for controlling the screensaver |
| 124 | |
| 125 | Synopsis |
| 126 | |
| 127 | xdg-screensaver suspend WindowID |
| 128 | |
| 129 | xdg-screensaver resume WindowID |
| 130 | |
| 131 | xdg-screensaver { activate | lock | reset | status } |
| 132 | |
| 133 | xdg-screensaver { --help | --manual | --version } |
| 134 | |
| 135 | _USAGE |
| 136 | } |
| 137 | |
| 138 | #@xdg-utils-common@ |
| 139 | |
| 140 | #---------------------------------------------------------------------------- |
| 141 | # Common utility functions included in all XDG wrapper scripts |
| 142 | #---------------------------------------------------------------------------- |
| 143 | |
| 144 | DEBUG() |
| 145 | { |
| 146 | [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0; |
| 147 | [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0; |
| 148 | shift |
| 149 | echo "$@" >&2 |
| 150 | } |
| 151 | |
| 152 | #------------------------------------------------------------- |
| 153 | # Exit script on successfully completing the desired operation |
| 154 | |
| 155 | exit_success() |
| 156 | { |
| 157 | if [ $# -gt 0 ]; then |
| 158 | echo "$@" |
| 159 | echo |
| 160 | fi |
| 161 | |
| 162 | exit 0 |
| 163 | } |
| 164 | |
| 165 | |
| 166 | #----------------------------------------- |
| 167 | # Exit script on malformed arguments, not enough arguments |
| 168 | # or missing required option. |
| 169 | # prints usage information |
| 170 | |
| 171 | exit_failure_syntax() |
| 172 | { |
| 173 | if [ $# -gt 0 ]; then |
| 174 | echo "xdg-screensaver: $@" >&2 |
| 175 | echo "Try 'xdg-screensaver --help' for more information." >&2 |
| 176 | else |
| 177 | usage |
| 178 | echo "Use 'man xdg-screensaver' or 'xdg-screensaver --manual' for additional info." |
| 179 | fi |
| 180 | |
| 181 | exit 1 |
| 182 | } |
| 183 | |
| 184 | #------------------------------------------------------------- |
| 185 | # Exit script on missing file specified on command line |
| 186 | |
| 187 | exit_failure_file_missing() |
| 188 | { |
| 189 | if [ $# -gt 0 ]; then |
| 190 | echo "xdg-screensaver: $@" >&2 |
| 191 | fi |
| 192 | |
| 193 | exit 2 |
| 194 | } |
| 195 | |
| 196 | #------------------------------------------------------------- |
| 197 | # Exit script on failure to locate necessary tool applications |
| 198 | |
| 199 | exit_failure_operation_impossible() |
| 200 | { |
| 201 | if [ $# -gt 0 ]; then |
| 202 | echo "xdg-screensaver: $@" >&2 |
| 203 | fi |
| 204 | |
| 205 | exit 3 |
| 206 | } |
| 207 | |
| 208 | #------------------------------------------------------------- |
| 209 | # Exit script on failure returned by a tool application |
| 210 | |
| 211 | exit_failure_operation_failed() |
| 212 | { |
| 213 | if [ $# -gt 0 ]; then |
| 214 | echo "xdg-screensaver: $@" >&2 |
| 215 | fi |
| 216 | |
| 217 | exit 4 |
| 218 | } |
| 219 | |
| 220 | #------------------------------------------------------------ |
| 221 | # Exit script on insufficient permission to read a specified file |
| 222 | |
| 223 | exit_failure_file_permission_read() |
| 224 | { |
| 225 | if [ $# -gt 0 ]; then |
| 226 | echo "xdg-screensaver: $@" >&2 |
| 227 | fi |
| 228 | |
| 229 | exit 5 |
| 230 | } |
| 231 | |
| 232 | #------------------------------------------------------------ |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 233 | # Exit script on insufficient permission to write a specified file |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 234 | |
| 235 | exit_failure_file_permission_write() |
| 236 | { |
| 237 | if [ $# -gt 0 ]; then |
| 238 | echo "xdg-screensaver: $@" >&2 |
| 239 | fi |
| 240 | |
| 241 | exit 6 |
| 242 | } |
| 243 | |
| 244 | check_input_file() |
| 245 | { |
| 246 | if [ ! -e "$1" ]; then |
| 247 | exit_failure_file_missing "file '$1' does not exist" |
| 248 | fi |
| 249 | if [ ! -r "$1" ]; then |
| 250 | exit_failure_file_permission_read "no permission to read file '$1'" |
| 251 | fi |
| 252 | } |
| 253 | |
| 254 | check_vendor_prefix() |
| 255 | { |
| 256 | file_label="$2" |
| 257 | [ -n "$file_label" ] || file_label="filename" |
| 258 | file=`basename "$1"` |
| 259 | case "$file" in |
| 260 | [a-zA-Z]*-*) |
| 261 | return |
| 262 | ;; |
| 263 | esac |
| 264 | |
| 265 | echo "xdg-screensaver: $file_label '$file' does not have a proper vendor prefix" >&2 |
| 266 | echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2 |
| 267 | echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2 |
| 268 | echo "Use --novendor to override or 'xdg-screensaver --manual' for additional info." >&2 |
| 269 | exit 1 |
| 270 | } |
| 271 | |
| 272 | check_output_file() |
| 273 | { |
| 274 | # if the file exists, check if it is writeable |
| 275 | # if it does not exists, check if we are allowed to write on the directory |
| 276 | if [ -e "$1" ]; then |
| 277 | if [ ! -w "$1" ]; then |
| 278 | exit_failure_file_permission_write "no permission to write to file '$1'" |
| 279 | fi |
| 280 | else |
| 281 | DIR=`dirname "$1"` |
| 282 | if [ ! -w "$DIR" -o ! -x "$DIR" ]; then |
| 283 | exit_failure_file_permission_write "no permission to create file '$1'" |
| 284 | fi |
| 285 | fi |
| 286 | } |
| 287 | |
| 288 | #---------------------------------------- |
| 289 | # Checks for shared commands, e.g. --help |
| 290 | |
| 291 | check_common_commands() |
| 292 | { |
| 293 | while [ $# -gt 0 ] ; do |
| 294 | parm="$1" |
| 295 | shift |
| 296 | |
| 297 | case "$parm" in |
| 298 | --help) |
| 299 | usage |
| 300 | echo "Use 'man xdg-screensaver' or 'xdg-screensaver --manual' for additional info." |
| 301 | exit_success |
| 302 | ;; |
| 303 | |
| 304 | --manual) |
| 305 | manualpage |
| 306 | exit_success |
| 307 | ;; |
| 308 | |
| 309 | --version) |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 310 | echo "xdg-screensaver 1.0.2" |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 311 | exit_success |
| 312 | ;; |
| 313 | esac |
| 314 | done |
| 315 | } |
| 316 | |
| 317 | check_common_commands "$@" |
| 318 | |
| 319 | [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL; |
| 320 | if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then |
| 321 | # Be silent |
| 322 | xdg_redirect_output=" > /dev/null 2> /dev/null" |
| 323 | else |
| 324 | # All output to stderr |
| 325 | xdg_redirect_output=" >&2" |
| 326 | fi |
| 327 | |
| 328 | #-------------------------------------- |
| 329 | # Checks for known desktop environments |
| 330 | # set variable DE to the desktop environments name, lowercase |
| 331 | |
| 332 | detectDE() |
| 333 | { |
| 334 | if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde; |
| 335 | elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 336 | elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome; |
| 337 | elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce; |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 338 | fi |
| 339 | } |
| 340 | |
| 341 | #---------------------------------------------------------------------------- |
| 342 | # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4 |
| 343 | # It also always returns 1 in KDE 3.4 and earlier |
| 344 | # Simply return 0 in such case |
| 345 | |
| 346 | kfmclient_fix_exit_code() |
| 347 | { |
| 348 | version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE` |
| 349 | major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'` |
| 350 | minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'` |
| 351 | release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'` |
| 352 | test "$major" -gt 3 && return $1 |
| 353 | test "$minor" -gt 5 && return $1 |
| 354 | test "$release" -gt 4 && return $1 |
| 355 | return 0 |
| 356 | } |
| 357 | |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 358 | # Check if we can use "mv -T" |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 359 | if mv -T ... ... 2>&1 | grep '\.\.\.' > /dev/null ; then |
| 360 | # We can securely move files in /tmp with mv -T |
| 361 | DEBUG 1 "mv -T available" |
| 362 | MV="mv -T" |
| 363 | screensaver_file="/tmp/xdg-screensaver-$USER-"`echo $DISPLAY | sed 's/:/-/g'` |
| 364 | else |
| 365 | # No secure moves available, use home dir |
| 366 | DEBUG 1 "mv -T not available" |
| 367 | MV="mv" |
mdm@chromium.org | ad856b9 | 2009-07-27 21:50:25 +0000 | [diff] [blame] | 368 | screensaver_file="$HOME/.xdg-screensaver-"`echo $(hostname)-$DISPLAY | sed 's/:/-/g'` |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 369 | fi |
| 370 | lockfile_command=`which lockfile 2> /dev/null` |
| 371 | |
| 372 | lockfile() |
| 373 | { |
| 374 | if [ -n "$lockfile_command" ] ; then |
| 375 | $lockfile_command -1 -l 10 -s 3 "$screensaver_file".lock |
| 376 | else |
| 377 | # Poor man's attempt at doing a lockfile |
| 378 | # Be careful not to facilitate a symlink attack |
| 379 | local try |
| 380 | try=0 |
| 381 | while ! ln -s "$screensaver_file".lock "$screensaver_file".lock 2> /dev/null; |
| 382 | do |
| 383 | sleep 1 |
| 384 | try=$(($try+1)) |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 385 | if [ $try -eq 3 ] ; then |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 386 | rm -f "$screensaver_file".lock || return # Can't remove lockfile |
| 387 | try=0 |
| 388 | fi |
| 389 | done |
| 390 | fi |
| 391 | } |
| 392 | |
| 393 | unlockfile() |
| 394 | { |
| 395 | rm -f "$screensaver_file".lock |
| 396 | } |
| 397 | |
| 398 | perform_action() |
| 399 | { |
| 400 | result=1 |
| 401 | |
| 402 | if [ "$1" = "resume" ] ; then |
| 403 | # Restore DPMS state |
| 404 | if [ -f "$screensaver_file.dpms" ]; then |
| 405 | rm "$screensaver_file.dpms" |
| 406 | # Re-enable DPMS |
| 407 | xset +dpms |
| 408 | fi |
| 409 | fi |
| 410 | if [ "$1" = "reset" ] ; then |
| 411 | if xset -q | grep 'DPMS is Enabled' > /dev/null 2> /dev/null; then |
| 412 | xset dpms force on |
| 413 | fi |
| 414 | fi |
| 415 | |
| 416 | case "$DE" in |
| 417 | kde) |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 418 | if [ x"$KDE_SESSION_VERSION" = x"4" ]; then |
| 419 | screensaver_freedesktop "$1" |
| 420 | else |
| 421 | screensaver_kde "$1" |
| 422 | fi |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 423 | ;; |
| 424 | |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 425 | gnome_screensaver) |
| 426 | screensaver_gnome_screensaver "$1" |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 427 | ;; |
| 428 | |
| 429 | xscreensaver) |
| 430 | screensaver_xscreensaver "$1" |
| 431 | ;; |
| 432 | esac |
| 433 | |
| 434 | if [ "$1" = "suspend" ] ; then |
| 435 | # Save DPMS state |
| 436 | if xset -q | grep 'DPMS is Enabled' > /dev/null 2> /dev/null; then |
| 437 | test "${TMPDIR+set}" = set || TMPDIR=/tmp |
| 438 | tmpfile=`mktemp $TMPDIR/tmp.XXXXXXXXXX` |
| 439 | $MV "$tmpfile" "$screensaver_file.dpms" |
| 440 | # Disable DPMS |
| 441 | xset -dpms |
| 442 | fi |
| 443 | fi |
| 444 | |
| 445 | } |
| 446 | |
| 447 | cleanup_suspend() |
| 448 | { |
| 449 | lockfile |
| 450 | test "${TMPDIR+set}" = set || TMPDIR=/tmp |
| 451 | tmpfile=`mktemp $TMPDIR/tmp.XXXXXXXXXX` |
mdm@chromium.org | ad856b9 | 2009-07-27 21:50:25 +0000 | [diff] [blame] | 452 | grep -v "$window_id:$xprop_pid\$" "$screensaver_file" > "$tmpfile" 2> /dev/null |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 453 | $MV "$tmpfile" "$screensaver_file" |
| 454 | if [ ! -s "$screensaver_file" ] ; then |
| 455 | rm "$screensaver_file" |
| 456 | unlockfile |
| 457 | # $screensaver_file is empty, do resume |
| 458 | perform_action resume |
| 459 | else |
| 460 | unlockfile |
| 461 | fi |
| 462 | } |
| 463 | |
| 464 | do_resume() |
| 465 | { |
| 466 | lockfile # Obtain lockfile |
| 467 | # Find the PID of the trackingprocess |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 468 | xprop_pid=`grep "$window_id:" "$screensaver_file" 2> /dev/null | cut -d ':' -f 2` |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 469 | unlockfile # Free lockfile |
| 470 | if [ -n "$xprop_pid" ] && ps -p "$xprop_pid" 2> /dev/null | grep xprop > /dev/null; then |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 471 | # Kill the tracking process |
mdm@chromium.org | ad856b9 | 2009-07-27 21:50:25 +0000 | [diff] [blame] | 472 | kill -s TERM $xprop_pid |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 473 | fi |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 474 | cleanup_suspend |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | XPROP=`which xprop 2> /dev/null` |
| 478 | |
| 479 | check_window_id() |
| 480 | { |
| 481 | if [ -z "$XPROP" ]; then |
| 482 | DEBUG 3 "xprop not found" |
| 483 | return |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 484 | fi |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 485 | DEBUG 2 "Running $XPROP -id $window_id" |
| 486 | if $XPROP -id $window_id > /dev/null 2> /dev/null; then |
| 487 | DEBUG 3 Window $window_id exists |
| 488 | else |
| 489 | DEBUG 3 Window $window_id does not exist |
| 490 | exit_failure_operation_failed "Window $window_id does not exist" |
| 491 | fi |
| 492 | } |
| 493 | |
| 494 | track_window() |
| 495 | { |
| 496 | if [ -z "$XPROP" ]; then |
| 497 | # Don't track window if we don't have xprop |
| 498 | return |
| 499 | fi |
| 500 | lockfile |
| 501 | |
| 502 | test "${TMPDIR+set}" = set || TMPDIR=/tmp |
| 503 | tmpfile=`mktemp $TMPDIR/tmp.XXXXXXXXXX` |
| 504 | # Filter stale entries from the xdg-screensaver status file |
| 505 | # Return if $window_id is being tracked already |
| 506 | ( |
| 507 | already_tracked=1 |
| 508 | IFS_save="$IFS" |
| 509 | IFS=":" |
| 510 | while read wid pid; do |
| 511 | if ps -p "$pid" 2> /dev/null | grep xprop > /dev/null; then |
| 512 | echo "$wid:$pid" |
| 513 | if [ $wid = $window_id ] ; then |
| 514 | already_tracked=0 |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 515 | fi |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 516 | fi |
| 517 | done |
| 518 | IFS="$IFS_save" |
| 519 | exit $already_tracked |
| 520 | ) < $screensaver_file > $tmpfile |
| 521 | already_tracked=$? |
| 522 | |
| 523 | if [ "$already_tracked" -eq "0" ] ; then |
| 524 | $MV "$tmpfile" "$screensaver_file" |
| 525 | # We are already tracking $window_id, don't do anything |
| 526 | unlockfile |
| 527 | return |
| 528 | fi |
| 529 | |
| 530 | # Start tracking $window_id |
| 531 | $XPROP -id $window_id -spy > /dev/null & |
| 532 | xprop_pid=$! |
| 533 | # Add window_id and xprop_pid to the xdg-screensave status file |
| 534 | echo "$window_id:$xprop_pid" >> $tmpfile |
| 535 | $MV "$tmpfile" "$screensaver_file" |
| 536 | unlockfile |
| 537 | # Wait for xprop to edit, it means that the window disappeared |
| 538 | wait $xprop_pid |
| 539 | # Clean up the administration and resume the screensaver |
| 540 | cleanup_suspend |
| 541 | } |
| 542 | |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 543 | screensaver_freedesktop() |
| 544 | { |
| 545 | case "$1" in |
| 546 | suspend) |
| 547 | dbus-send --session \ |
| 548 | --dest=org.freedesktop.ScreenSaver \ |
| 549 | --type=method_call \ |
| 550 | --print-reply \ |
| 551 | --reply-timeout=2000 \ |
| 552 | /ScreenSaver \ |
| 553 | org.freedesktop.ScreenSaver.Inhibit \ |
| 554 | string:$window_id \ |
| 555 | string:xdg-screensaver \ |
| 556 | | grep uint32 | cut -d ' ' -f 5 >| "$screensaver_file.cookie" \ |
| 557 | 2> /dev/null |
| 558 | result=$? |
| 559 | ;; |
| 560 | |
| 561 | resume) |
| 562 | if [ -f "$screensaver_file.cookie" ] ; then |
| 563 | value=`cat "$screensaver_file.cookie"` |
| 564 | dbus-send --session \ |
| 565 | --dest=org.freedesktop.ScreenSaver \ |
| 566 | --type=method_call \ |
| 567 | /ScreenSaver \ |
| 568 | org.freedesktop.ScreenSaver.UnInhibit \ |
| 569 | uint32:$value \ |
| 570 | 2> /dev/null |
| 571 | rm -f "$screensaver_file.cookie" |
| 572 | fi |
| 573 | result=$? |
| 574 | ;; |
| 575 | |
| 576 | activate) |
| 577 | dbus-send --session \ |
| 578 | --dest=org.freedesktop.ScreenSaver \ |
| 579 | --type=method_call \ |
| 580 | /ScreenSaver \ |
| 581 | org.freedesktop.ScreenSaver.SetActive \ |
| 582 | boolean:true \ |
| 583 | 2> /dev/null |
| 584 | result=$? |
| 585 | ;; |
| 586 | |
| 587 | lock) |
| 588 | dbus-send --session \ |
| 589 | --dest=org.freedesktop.ScreenSaver \ |
| 590 | --type=method_call \ |
| 591 | /ScreenSaver \ |
| 592 | org.freedesktop.ScreenSaver.Lock \ |
| 593 | 2> /dev/null |
| 594 | ;; |
| 595 | |
| 596 | reset) |
| 597 | if [ -f "$screensaver_file.cookie" ] ; then |
| 598 | value=`cat "$screensaver_file.cookie"` |
| 599 | dbus-send --session \ |
| 600 | --dest=org.freedesktop.ScreenSaver \ |
| 601 | --type=method_call \ |
| 602 | /ScreenSaver \ |
| 603 | org.freedesktop.ScreenSaver.UnInhibit \ |
| 604 | uint32:$value \ |
| 605 | 2> /dev/null |
| 606 | rm -f "$screensaver_file.cookie" |
| 607 | fi |
| 608 | result=$? |
| 609 | ;; |
| 610 | |
| 611 | status) |
| 612 | status=`dbus-send --session \ |
| 613 | --dest=org.freedesktop.ScreenSaver \ |
| 614 | --type=method_call \ |
| 615 | --print-reply \ |
| 616 | --reply-timeout=2000 \ |
| 617 | /ScreenSaver \ |
| 618 | org.freedesktop.ScreenSaver.GetActive \ |
| 619 | | grep boolean | cut -d ' ' -f 5` |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 620 | result=$? |
| 621 | if [ x"$status" = "xtrue" ]; then |
| 622 | echo "enabled" |
| 623 | elif [ x"$status" = "xfalse" ]; then |
| 624 | echo "disabled" |
| 625 | else |
| 626 | echo "ERROR: dbus org.freedesktop.ScreenSaver.GetActive returned '$status'" >&2 |
| 627 | return 1 |
| 628 | fi |
| 629 | ;; |
| 630 | |
| 631 | *) |
| 632 | echo "ERROR: Unknown command '$1'" >&2 |
| 633 | return 1 |
| 634 | ;; |
| 635 | esac |
| 636 | } |
| 637 | |
| 638 | screensaver_kde() |
| 639 | { |
| 640 | case "$1" in |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 641 | suspend) |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 642 | dcop kdesktop KScreensaverIface enable false > /dev/null |
| 643 | result=$? |
| 644 | ;; |
| 645 | |
| 646 | resume) |
| 647 | dcop kdesktop KScreensaverIface configure > /dev/null |
| 648 | result=$? |
| 649 | ;; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 650 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 651 | activate) |
| 652 | dcop kdesktop KScreensaverIface save > /dev/null |
| 653 | result=$? |
| 654 | ;; |
| 655 | |
| 656 | lock) |
| 657 | dcop kdesktop KScreensaverIface lock > /dev/null |
| 658 | result=$? |
| 659 | ;; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 660 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 661 | reset) |
| 662 | # Turns the screensaver off right now |
| 663 | dcop kdesktop KScreensaverIface quit > /dev/null |
| 664 | result=$? |
| 665 | ;; |
| 666 | |
| 667 | status) |
| 668 | status=`dcop kdesktop KScreensaverIface isEnabled` |
| 669 | result=$? |
| 670 | if [ x"$status" = "xtrue" ]; then |
| 671 | echo "enabled" |
| 672 | elif [ x"$status" = "xfalse" ]; then |
| 673 | echo "disabled" |
| 674 | else |
| 675 | echo "ERROR: kdesktop KScreensaverIface isEnabled returned '$status'" >&2 |
| 676 | return 1 |
| 677 | fi |
| 678 | ;; |
| 679 | |
| 680 | *) |
| 681 | echo "ERROR: Unknown command '$1'" >&2 |
| 682 | return 1 |
| 683 | ;; |
| 684 | esac |
| 685 | } |
| 686 | |
| 687 | screensaver_suspend_loop() |
| 688 | { |
| 689 | lockfile |
| 690 | test "${TMPDIR+set}" = set || TMPDIR=/tmp |
| 691 | tmpfile=`mktemp $TMPDIR/tmp.XXXXXXXXXX` |
| 692 | # Filter stale entries from the xdg-screensaver status file |
| 693 | cat "$screensaver_file" 2> /dev/null | ( |
| 694 | IFS_save="$IFS" |
| 695 | IFS=":" |
| 696 | while read wid pid; do |
| 697 | if ps -p "$pid" 2> /dev/null | grep xprop > /dev/null; then |
| 698 | echo "$wid:$pid" |
| 699 | fi |
| 700 | done |
| 701 | IFS="$IFS_save" |
| 702 | ) > $tmpfile |
| 703 | if [ -s "$tmpfile" ] ; then |
| 704 | # Suspend pending, don't do a thing |
| 705 | $MV "$tmpfile" "$screensaver_file" |
| 706 | unlockfile |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 707 | return |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 708 | fi |
| 709 | $MV "$tmpfile" "$screensaver_file" |
| 710 | unlockfile |
mdm@chromium.org | ad856b9 | 2009-07-27 21:50:25 +0000 | [diff] [blame] | 711 | (while [ -f "$screensaver_file" ]; do $*; sleep 50; done) > /dev/null 2> /dev/null & |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 712 | } |
| 713 | |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 714 | screensaver_gnome_screensaver() |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 715 | { |
| 716 | # TODO |
| 717 | # There seems to be a DBUS interface for gnome-screensaver |
| 718 | # See http://lists.mplayerhq.hu/pipermail/mplayer-dev-eng/2006-April/042579.html and |
| 719 | # http://cvs.gnome.org/viewcvs/gnome-screensaver/src/gs-listener-dbus.c?rev=1.36&view=log |
| 720 | # A problem seems to be that Inhibit is tied to the lifetime of the DBUS appname and |
| 721 | # this can not be used from a script |
| 722 | case "$1" in |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 723 | suspend) |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 724 | screensaver_suspend_loop gnome-screensaver-command --poke |
| 725 | result=0 |
| 726 | ;; |
| 727 | |
| 728 | resume) |
| 729 | # Automatic resume when $screensaver_file disappears |
| 730 | result=0 |
| 731 | ;; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 732 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 733 | activate) |
| 734 | gnome-screensaver-command --activate > /dev/null 2> /dev/null |
| 735 | result=$? |
| 736 | ;; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 737 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 738 | lock) |
| 739 | gnome-screensaver-command --lock > /dev/null 2> /dev/null |
| 740 | result=$? |
| 741 | ;; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 742 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 743 | reset) |
| 744 | # Turns the screensaver off right now |
| 745 | gnome-screensaver-command --deactivate > /dev/null 2> /dev/null |
| 746 | result=$? |
| 747 | ;; |
| 748 | |
| 749 | status) |
| 750 | result=0 |
| 751 | if [ -f "$screensaver_file" ] ; then |
| 752 | echo "disabled" |
| 753 | elif gnome-screensaver-command --query > /dev/null 2> /dev/null; then |
| 754 | echo "enabled" |
| 755 | else |
| 756 | # Something is wrong |
| 757 | echo "disabled" |
| 758 | fi |
| 759 | ;; |
| 760 | |
| 761 | *) |
| 762 | echo "ERROR: Unknown command '$1" >&2 |
| 763 | return 1 |
| 764 | ;; |
| 765 | esac |
| 766 | } |
| 767 | |
| 768 | screensaver_xscreensaver() |
| 769 | { |
| 770 | case "$1" in |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 771 | suspend) |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 772 | screensaver_suspend_loop xscreensaver-command -deactivate |
| 773 | result=0 |
| 774 | ;; |
| 775 | |
| 776 | resume) |
| 777 | # Automatic resume when $screensaver_file disappears |
| 778 | result=0 |
| 779 | ;; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 780 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 781 | activate) |
| 782 | xscreensaver-command -activate > /dev/null 2> /dev/null |
| 783 | result=$? |
| 784 | ;; |
| 785 | |
| 786 | lock) |
| 787 | xscreensaver-command -lock > /dev/null 2> /dev/null |
| 788 | result=$? |
| 789 | ;; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 790 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 791 | reset) |
| 792 | # Turns the screensaver off right now |
| 793 | xscreensaver-command -deactivate > /dev/null 2> /dev/null |
| 794 | result=$? |
| 795 | ;; |
| 796 | |
| 797 | status) |
| 798 | result=0 |
| 799 | if [ -f "$screensaver_file" ] ; then |
| 800 | echo "disabled" |
| 801 | else |
| 802 | echo "enabled" |
| 803 | fi |
| 804 | ;; |
| 805 | |
| 806 | *) |
| 807 | echo "ERROR: Unknown command '$1" >&2 |
| 808 | return 1 |
| 809 | ;; |
| 810 | esac |
| 811 | } |
| 812 | |
| 813 | [ x"$1" != x"" ] || exit_failure_syntax |
| 814 | |
| 815 | action= |
| 816 | window_id= |
| 817 | |
| 818 | case $1 in |
| 819 | suspend) |
| 820 | action="$1" |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 821 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 822 | shift |
| 823 | |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 824 | if [ -z "$1" ] ; then |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 825 | exit_failure_syntax "WindowID argument missing" |
| 826 | fi |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 827 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 828 | window_id="$1" |
| 829 | check_window_id |
| 830 | ;; |
| 831 | |
| 832 | resume) |
| 833 | action="$1" |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 834 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 835 | shift |
| 836 | |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 837 | if [ -z "$1" ] ; then |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 838 | exit_failure_syntax "WindowID argument missing" |
| 839 | fi |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 840 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 841 | window_id="$1" |
| 842 | check_window_id |
| 843 | ;; |
| 844 | |
| 845 | activate) |
| 846 | action="$1" |
| 847 | ;; |
| 848 | |
| 849 | lock) |
| 850 | action="$1" |
| 851 | ;; |
| 852 | |
| 853 | reset) |
| 854 | action="$1" |
| 855 | ;; |
| 856 | |
| 857 | status) |
| 858 | action="$1" |
| 859 | ;; |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 860 | |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 861 | *) |
| 862 | exit_failure_syntax "unknown command '$1'" |
| 863 | ;; |
| 864 | esac |
| 865 | |
| 866 | detectDE |
| 867 | # Consider "xscreensaver" a separate DE |
| 868 | xscreensaver-command -version 2> /dev/null | grep XScreenSaver > /dev/null && DE="xscreensaver" |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 869 | # Consider "gnome-screensaver" a separate DE |
| 870 | gnome-screensaver-command -q > /dev/null 2>&1 && DE="gnome_screensaver" |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 871 | |
| 872 | if [ "$action" = "resume" ] ; then |
| 873 | do_resume |
| 874 | exit_success |
| 875 | fi |
| 876 | |
| 877 | perform_action "$action" |
| 878 | |
| 879 | if [ "$action" = "suspend" ] ; then |
| 880 | # Start tracking $window_id and resume the screensaver once it disappears |
| 881 | ( track_window ) 2> /dev/null > /dev/null & |
mdm@chromium.org | 56df6d3 | 2010-08-31 18:16:49 +0000 | [diff] [blame^] | 882 | fi |
dkegel@google.com | f20da1f | 2009-06-30 19:16:32 +0000 | [diff] [blame] | 883 | |
| 884 | if [ $result -eq 0 ]; then |
| 885 | exit_success |
| 886 | else |
| 887 | exit_failure_operation_failed |
| 888 | fi |