diff --git a/eu.ichibi.Lumos.json b/eu.ichibi.Lumos.json index 44bcea2..22c751e 100644 --- a/eu.ichibi.Lumos.json +++ b/eu.ichibi.Lumos.json @@ -5,11 +5,11 @@ "sdk" : "org.gnome.Sdk", "command" : "lumos", "finish-args" : [ - "--share=network", "--share=ipc", "--socket=fallback-x11", "--device=dri", - "--socket=wayland" + "--socket=wayland", + "--system-talk-name=net.hadess.SensorProxy" ], "cleanup" : [ "/include", diff --git a/src/meson.build b/src/meson.build index cd63817..e101f36 100644 --- a/src/meson.build +++ b/src/meson.build @@ -30,6 +30,7 @@ lumos_sources = [ '__init__.py', 'main.py', 'window.py', + 'sensors_polling_timer.py', 'widgets/widgets_loader.py', 'widgets/time_priority_page.py', 'widgets/aperture_priority_page.py', diff --git a/src/sensors_polling_timer.py b/src/sensors_polling_timer.py new file mode 100644 index 0000000..f0d872c --- /dev/null +++ b/src/sensors_polling_timer.py @@ -0,0 +1,43 @@ +from gi.repository import Gio, GLib +from threading import Timer + +# window.py +# +# Copyright 2024 Daniele Verducci +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# SPDX-License-Identifier: GPL-3.0-or-later + + +# A timer for polling the light sensor via DBus. +# Checks the sensor at a specific interval and runs a callback only when an update is detected. +# +# Usage: +# Instantiate it, then call run(interval_in_seconds, callback_function) +# To stop it, call cancel() +class SensorsPollingTimer(Timer): + def run(self): + # Setup + bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None) + self.proxy = Gio.DBusProxy.new_sync(bus,Gio.DBusProxyFlags.NONE,None,'net.hadess.SensorProxy','/net/hadess/SensorProxy','org.freedesktop.DBus.Properties', None) + self.oldValue = None + + # Loop + while not self.finished.wait(self.interval): + value = self.proxy.Get('(ss)', 'net.hadess.SensorProxy', 'LightLevel') + if (self.oldValue != value): + self.oldValue = value + self.function(value) # Invoke callback + diff --git a/src/window.py b/src/window.py index 5ddf41b..7d3577b 100644 --- a/src/window.py +++ b/src/window.py @@ -19,6 +19,7 @@ from gi.repository import Adw from gi.repository import Gtk +from .sensors_polling_timer import SensorsPollingTimer @Gtk.Template(resource_path='/eu/ichibi/Lumos/window.ui') class LumosWindow(Adw.ApplicationWindow): @@ -28,3 +29,11 @@ class LumosWindow(Adw.ApplicationWindow): def __init__(self, **kwargs): super().__init__(**kwargs) + + # Start polling sensors + self.sensorsPollingTimer = SensorsPollingTimer(1, self.onSensorRead) + self.sensorsPollingTimer.run() + + def onSensorRead(self, value): + # Called when the light value changed + print("Read {} lux".format(value))