Skip to content

Commit

Permalink
Create SensorManager, detect before accel search result (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Aug 14, 2024
1 parent 25bdb7e commit ecbc33f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 46 deletions.
8 changes: 6 additions & 2 deletions src/DisplayPlug.vala
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,22 @@ public class Display.Plug : Switchboard.Plug {
public override async Gee.TreeMap<string, string> search (string search) {
var search_results = new Gee.TreeMap<string, string> ((GLib.CompareDataFunc<string>)strcmp, (Gee.EqualDataFunc<string>)str_equal);
search_results.set ("%s%s".printf (display_name, _("Screen Resolution")), "displays");
search_results.set ("%s%s".printf (display_name, _("Screen Rotation")), "displays");
search_results.set ("%s%s".printf (display_name, _("Primary display")), "displays");
search_results.set ("%s%s".printf (display_name, _("Screen mirroring")), "displays");
search_results.set ("%s%s".printf (display_name, _("Screen Rotation")), "displays");
search_results.set ("%s%s".printf (display_name, _("Scaling factor")), "displays");
search_results.set ("%s%s".printf (display_name, _("Rotation lock")), "displays");
search_results.set ("%s%s".printf (display_name, _("Night Light")), "night-light");
search_results.set ("%s%s%s".printf (display_name, _("Night Light"), _("Schedule")), "night-light");
search_results.set ("%s%s%s".printf (display_name, _("Night Light"), _("Color temperature")), "night-light");
search_results.set ("%s%s%s".printf (display_name, _("Filters"), _("Color Blindness")), "filters");
search_results.set ("%s%s%s".printf (display_name, _("Filters"), _("Color Vision Deficiency")), "filters");
search_results.set ("%s%s%s".printf (display_name, _("Filters"), _("Grayscale")), "filters");
search_results.set ("%s%s%s".printf (display_name, _("Filters"), _("Monochrome")), "filters");

if (SensorManager.get_default ().has_accelerometer) {
search_results.set ("%s%s".printf (display_name, _("Rotation lock")), "displays");
}

return search_results;
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/Interfaces/SensorManager.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* SPDX-License-Identifier: LGPL-2.0-or-later
* SPDX-FileCopyrightText: 2024 elementary, Inc. (https://elementary.io)
*/

[DBus (name = "net.hadess.SensorProxy")]
public interface Display.SensorProxy : GLib.DBusProxy {
public abstract bool has_accelerometer { get; }
}

public class Display.SensorManager : Object {
public bool has_accelerometer { get; private set; }

private static GLib.Once<SensorManager> instance;
public static unowned SensorManager get_default () {
return instance.once (() => new SensorManager ());
}

private class SensorManager () { }

construct {
try {
// Synchronous otherwise search might be false negative
SensorProxy sensor_proxy = Bus.get_proxy_sync (BusType.SYSTEM, "net.hadess.SensorProxy", "/net/hadess/SensorProxy");
has_accelerometer = sensor_proxy.has_accelerometer;
} catch (Error e) {
info ("Unable to connect to SensorProxy bus, probably means no accelerometer supported: %s", e.message);
}
}
}
71 changes: 27 additions & 44 deletions src/Views/DisplaysView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,33 @@ public class Display.DisplaysView : Gtk.Box {

action_bar.pack_start (mirror_box);

var schema_source = GLib.SettingsSchemaSource.get_default ();
var rotation_lock_schema = schema_source.lookup (TOUCHSCREEN_SETTINGS_PATH, true);
if (rotation_lock_schema != null) {
rotation_lock_box = new Gtk.Box (HORIZONTAL, 6) {
margin_top = 6,
margin_end = 6,
margin_bottom = 6,
margin_start = 6,
valign = CENTER
};

action_bar.pack_start (rotation_lock_box);

detect_accelerometer.begin ();
} else {
info ("Schema \"org.gnome.settings-daemon.peripherals.touchscreen\" is not installed on your system.");
if (SensorManager.get_default ().has_accelerometer) {
var schema_source = GLib.SettingsSchemaSource.get_default ();
var rotation_lock_schema = schema_source.lookup (TOUCHSCREEN_SETTINGS_PATH, true);
if (rotation_lock_schema != null) {
var rotation_lock_switch = new Gtk.Switch ();

var rotation_lock_label = new Gtk.Label (_("Rotation Lock:")) {
mnemonic_widget = rotation_lock_switch
};

rotation_lock_box = new Gtk.Box (HORIZONTAL, 6) {
margin_top = 6,
margin_end = 6,
margin_bottom = 6,
margin_start = 6,
valign = CENTER
};
rotation_lock_box.append (rotation_lock_label);
rotation_lock_box.append (rotation_lock_switch);

action_bar.pack_start (rotation_lock_box);

var touchscreen_settings = new GLib.Settings (TOUCHSCREEN_SETTINGS_PATH);
touchscreen_settings.bind ("orientation-lock", rotation_lock_switch, "active", DEFAULT);
} else {
info ("Schema \"org.gnome.settings-daemon.peripherals.touchscreen\" is not installed on your system.");
}
}

action_bar.pack_end (button_box);
Expand Down Expand Up @@ -151,32 +162,4 @@ public class Display.DisplaysView : Gtk.Box {
error_dialog.response.connect (error_dialog.destroy);
error_dialog.present ();
}

private async void detect_accelerometer () {
bool has_accelerometer = false;

try {
SensorProxy sensors = yield GLib.Bus.get_proxy (BusType.SYSTEM, "net.hadess.SensorProxy", "/net/hadess/SensorProxy");
has_accelerometer = sensors.has_accelerometer;
} catch (Error e) {
info ("Unable to connect to SensorProxy bus, probably means no accelerometer supported: %s", e.message);
}

if (has_accelerometer) {
var touchscreen_settings = new GLib.Settings (TOUCHSCREEN_SETTINGS_PATH);

var rotation_lock_label = new Gtk.Label (_("Rotation Lock:"));
var rotation_lock_switch = new Gtk.Switch ();

rotation_lock_box.append (rotation_lock_label);
rotation_lock_box.append (rotation_lock_switch);

touchscreen_settings.bind ("orientation-lock", rotation_lock_switch, "state", DEFAULT);
}
}

[DBus (name = "net.hadess.SensorProxy")]
private interface SensorProxy : GLib.DBusProxy {
public abstract bool has_accelerometer { get; }
}
}
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plug_files = files(
'Interfaces/GalaDBus.vala',
'Interfaces/NightLightManager.vala',
'Interfaces/MutterDisplayConfig.vala',
'Interfaces/SensorManager.vala',
'Objects/VirtualMonitor.vala',
'Objects/MonitorMode.vala',
'Objects/MonitorManager.vala',
Expand Down

0 comments on commit ecbc33f

Please sign in to comment.