4

The Yoga 900 has an accelerometer which should allow auto-rotation behavior.

On installing https://github.com/hadess/iio-sensor-proxy (Linux*, LightDM) it is possible to run monitor-sensor.

➜  ~ monitor-sensor
    Waiting for iio-sensor-proxy to appear
+++ iio-sensor-proxy appeared
=== Has accelerometer (orientation: undefined)
=== Has ambient light sensor (value: 0.000000, unit: lux)
    Accelerometer orientation changed: normal
    Light changed: 49.999999 (lux)
    Light changed: 79.999998 (lux)
    Accelerometer orientation changed: left-up
    Accelerometer orientation changed: normal
    Accelerometer orientation changed: left-up
    Accelerometer orientation changed: bottom-up

With iio-sensor-proxy the data is made available on dbus. It has the form:

signal time=1479631365.562013 sender=:1.15 -> destination=(null destination) serial=449861 path=/com/ubuntu/Upstart; interface=com.ubuntu.Upstart0_6; member=EventEmitted
   string "dbus"
   array [
      string "SIGNAL=PropertiesChanged"
      string "BUS=system"
      string "INTERFACE=org.freedesktop.DBus.Properties"
      string "OBJPATH=/net/hadess/SensorProxy"
      string "SENDER=:1.4"
      string "ARG0=net.hadess.SensorProxy"
   ]
  1. Is it possible to run a script that only wakes up on certain dbus events? Preferably I re-use an event loop in a daemon that already exists rather than building my own Python script or C program. Something like adding a file to /etc/dbus.d/handlers/net/hadess/SensorProxy would be really cool.

  2. If I don't run monitor-sensor I do not seem messages appearing on dbus, even though iio-sensor-proxy is actually run. Are these messages only sent if someone is listening for them?

[*] Linux V 4.8.1-040801-generic #201610071031 SMP Fri Oct 7 14:34:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

PS: According to powertop when using monitor-sensor:

14.7 mW      1.5 ms/s       8.8        Process        dbus-monitor

The answer should describe the canonical way to handle this on Linux and be the most friendly solution from a battery usage perspective.

harrymc
  • 498,455

1 Answers1

1

I'm not a Linux guru, but it seems like dbus-monitor is indeed the tool to use.

An answer to the post How to create a daemon which would be listening to dbus and fire script on message says :

Based on https://askubuntu.com/questions/150790/how-do-i-run-a-script-on-a-dbus-signal

#!/bin/bash

interface=org.gnome.ScreenSaver
member=ActiveChanged

dbus-monitor --profile "interface='$interface',member='$member'" |
while read -r line; do
    echo $line | grep ActiveChanged && your_script_goes_here
done

Just stick that in /etc/init.d/monitor-for-unlock, make it executable, and then make a soft link in rc2.d

chmod +x /etc/init.d/monitor-for-unlock
cd /etc/rc2.d
ln -s /etc/init.d/monitor-for-unlock .

The article Monitoring D-Bus adds :

Probably the most powerful feature of dbus-monitor is the fact that you are not limited to using just one watch expression at a time. The following example simultaneously monitors all 3 Tomboy signals and uses awk to parse the output from dbus-monitor and display a meaningful message.

#!/bin/bash

OJECT="'org.gnome.Tomboy'"
IFACE="'org.gnome.Tomboy.RemoteControl'"
DPATH="'/org/gnome/Tomboy/RemoteControl'"

WATCH1="type='signal', sender=${OJECT}, interface=${IFACE}, path=${DPATH}, member='NoteAdded'"
WATCH2="type='signal', sender=${OJECT}, interface=${IFACE}, path=${DPATH}, member='NoteSaved'"
WATCH3="type='signal', sender=${OJECT}, interface=${IFACE}, path=${DPATH}, member='NoteDeleted'"

dbus-monitor "${WATCH1}" "${WATCH2}" "${WATCH3}" | \
awk '
/member=NoteAdded/ { getline; print "Created note " substr($2,7) }
/member=NoteSaved/ { getline; print "Added note " substr($2,7) }
/member=NoteDeleted/ { getline; print "Deleted note " substr($2,7) }
'

Here is the output generated when I clicked on the Tomboy icon to create a new note, waited for the automatic save and then selected the delete option to delete the note.

$ ./test
Created note //tomboy/3da026dc-f6ee-4637-8a94-bec6e2844824"
Added note //tomboy/3da026dc-f6ee-4637-8a94-bec6e2844824"
Deleted note //tomboy/3da026dc-f6ee-4637-8a94-bec6e2844824"
harrymc
  • 498,455