Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ubuntu drivers backend #132

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public sealed class SettingsDaemon.Application : Gtk.Application {
base.dbus_register (connection, object_path);

connection.register_object (object_path, new Backends.SystemUpdate ());
connection.register_object (object_path, new Backends.UbuntuDrivers ());

return true;
}
Expand Down
99 changes: 4 additions & 95 deletions src/Backends/SystemUpdate.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,6 @@

[DBus (name="io.elementary.settings_daemon.SystemUpdate")]
public class SettingsDaemon.Backends.SystemUpdate : Object {
public enum State {
UP_TO_DATE,
CHECKING,
AVAILABLE,
DOWNLOADING,
RESTART_REQUIRED,
ERROR
}

public struct CurrentState {
State state;
string status;
}

public struct UpdateDetails {
string[] packages;
int size;
Expand All @@ -33,7 +19,7 @@ public class SettingsDaemon.Backends.SystemUpdate : Object {

private static Settings settings = new GLib.Settings ("io.elementary.settings-daemon.system-update");

private CurrentState current_state;
private PkUtils.CurrentState current_state;
private UpdateDetails update_details;

private Pk.Task task;
Expand Down Expand Up @@ -187,7 +173,7 @@ public class SettingsDaemon.Backends.SystemUpdate : Object {
}

private void progress_callback (Pk.Progress progress, Pk.ProgressType progress_type) {
update_state (current_state.state, status_to_title (progress.status));
update_state (current_state.state, PkUtils.status_to_title (progress.status));
}

private void send_error (string message) {
Expand All @@ -201,7 +187,7 @@ public class SettingsDaemon.Backends.SystemUpdate : Object {
update_state (ERROR, message);
}

private void update_state (State state, string message = "") {
private void update_state (PkUtils.State state, string message = "") {
current_state = {
state,
message
Expand All @@ -210,84 +196,7 @@ public class SettingsDaemon.Backends.SystemUpdate : Object {
state_changed ();
}

private unowned string status_to_title (Pk.Status status) {
// From https://github.com/elementary/appcenter/blob/master/src/Core/ChangeInformation.vala#L51
switch (status) {
case Pk.Status.SETUP:
return _("Starting");
case Pk.Status.WAIT:
return _("Waiting");
case Pk.Status.RUNNING:
return _("Running");
case Pk.Status.QUERY:
return _("Querying");
case Pk.Status.INFO:
return _("Getting information");
case Pk.Status.REMOVE:
return _("Removing packages");
case Pk.Status.DOWNLOAD:
return _("Downloading");
case Pk.Status.REFRESH_CACHE:
return _("Refreshing software list");
case Pk.Status.UPDATE:
return _("Installing updates");
case Pk.Status.CLEANUP:
return _("Cleaning up packages");
case Pk.Status.OBSOLETE:
return _("Obsoleting packages");
case Pk.Status.DEP_RESOLVE:
return _("Resolving dependencies");
case Pk.Status.SIG_CHECK:
return _("Checking signatures");
case Pk.Status.TEST_COMMIT:
return _("Testing changes");
case Pk.Status.COMMIT:
return _("Committing changes");
case Pk.Status.REQUEST:
return _("Requesting data");
case Pk.Status.FINISHED:
return _("Finished");
case Pk.Status.CANCEL:
return _("Cancelling");
case Pk.Status.DOWNLOAD_REPOSITORY:
return _("Downloading repository information");
case Pk.Status.DOWNLOAD_PACKAGELIST:
return _("Downloading list of packages");
case Pk.Status.DOWNLOAD_FILELIST:
return _("Downloading file lists");
case Pk.Status.DOWNLOAD_CHANGELOG:
return _("Downloading lists of changes");
case Pk.Status.DOWNLOAD_GROUP:
return _("Downloading groups");
case Pk.Status.DOWNLOAD_UPDATEINFO:
return _("Downloading update information");
case Pk.Status.REPACKAGING:
return _("Repackaging files");
case Pk.Status.LOADING_CACHE:
return _("Loading cache");
case Pk.Status.SCAN_APPLICATIONS:
return _("Scanning applications");
case Pk.Status.GENERATE_PACKAGE_LIST:
return _("Generating package lists");
case Pk.Status.WAITING_FOR_LOCK:
return _("Waiting for package manager lock");
case Pk.Status.WAITING_FOR_AUTH:
return _("Waiting for authentication");
case Pk.Status.SCAN_PROCESS_LIST:
return _("Updating running applications");
case Pk.Status.CHECK_EXECUTABLE_FILES:
return _("Checking applications in use");
case Pk.Status.CHECK_LIBRARIES:
return _("Checking libraries in use");
case Pk.Status.COPY_FILES:
return _("Copying files");
case Pk.Status.INSTALL:
default:
return _("Installing");
}
}

public async CurrentState get_current_state () throws DBusError, IOError {
public async PkUtils.CurrentState get_current_state () throws DBusError, IOError {
return current_state;
}

Expand Down
229 changes: 229 additions & 0 deletions src/Backends/UbuntuDrivers.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* Copyright 2023 elementary, Inc. (https://elementary.io)
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Authored by: Leonhard Kargl <[email protected]>
*/

[DBus (name="io.elementary.settings_daemon.Drivers")]
public class SettingsDaemon.Backends.UbuntuDrivers : Object {
private const string NOTIFICATION_ID = "drivers";

public signal void state_changed ();

private PkUtils.CurrentState current_state;
private HashTable<string, GenericArray<string>> available_drivers;
private HashTable<string, bool> available_drivers_with_installed;

private Pk.Task task;
private GLib.Cancellable cancellable;

construct {
current_state = {
UP_TO_DATE,
""
};

available_drivers = new HashTable<string, GenericArray<string>> (str_hash, str_equal);
available_drivers_with_installed = new HashTable<string, bool> (str_hash, str_equal);

task = new Pk.Task ();

cancellable = new GLib.Cancellable ();

check_for_drivers.begin (true);
}

private async bool get_drivers_output (Cancellable? cancellable = null, out string? output = null) {
output = null;
string? drivers_exec_path = Environment.find_program_in_path ("ubuntu-drivers");
if (drivers_exec_path == null) {
return false;
}

Subprocess command;
try {
command = new Subprocess (SubprocessFlags.STDOUT_PIPE, drivers_exec_path, "list");
yield command.communicate_utf8_async (null, cancellable, out output, null);
} catch (Error e) {
critical ("Failed to launch ubuntu-drivers: %s", e.message);
return false;
}

return command.get_exit_status () == 0;
}

public async void check_for_drivers (bool notify) throws DBusError, IOError {
update_state (CHECKING);
available_drivers.remove_all ();

string? command_output;
var result = yield get_drivers_output (cancellable, out command_output);
if (!result || command_output == null) {
update_state (UP_TO_DATE);
critical ("Failed to get ubuntu-drivers output");
return;
}

string[] tokens = command_output.split ("\n");
foreach (unowned string package_name in tokens) {
if (package_name.strip () == "") {
continue;
}

// Filter out the nvidia server drivers
if (package_name.contains ("nvidia") && package_name.contains ("-server")) {
continue;
}

// ubuntu-drivers returns lines like the following for dkms packages:
// backport-iwlwifi-dkms, (kernel modules provided by backport-iwlwifi-dkms)
// nvidia-driver-470, (kernel modules provided by linux-modules-nvidia-470-generic-hwe-20.04)
// we want to install both packages if they're different

string[] parts = package_name.split (",");
// Get the driver part (before the comma)
string[] package_names = {};
package_names += parts[0];

if (parts.length > 1) {
if (parts[1].contains ("kernel modules provided by")) {
string[] kernel_module_parts = parts[1].split (" ");
// Get the remainder of the string after the last space
var last_part = kernel_module_parts[kernel_module_parts.length - 1];
// Strip off the trailing bracket
last_part = last_part.replace (")", "");

if (!(last_part in package_names)) {
package_names += last_part;
}
} else {
warning ("Unrecognised line from ubuntu-drivers, needs checking: %s", package_name);
}
}

available_drivers[parts[0]] = yield update_installed (parts[0], package_names);
}

if (available_drivers.length == 0) {
update_state (UP_TO_DATE);
return;
}

update_state (AVAILABLE);

if (notify) {
var notification = new Notification (_("Drivers available"));
notification.set_default_action (Application.ACTION_PREFIX + Application.SHOW_UPDATES_ACTION);
notification.set_body (_("For your system are drivers available"));
notification.set_icon (new ThemedIcon ("software-update-available"));

GLib.Application.get_default ().send_notification (NOTIFICATION_ID, notification);
}
}

private async GenericArray<string> update_installed (string driver, string[] package_names) {
var array = new GenericArray<string> ();
try {
var result = yield task.resolve_async (Pk.Filter.NONE, package_names, null, () => {});

var packages = result.get_package_array ();

bool all_installed = true;
foreach (var package in packages) {
array.add (package.package_id);

if (all_installed && (Pk.Info.INSTALLED == package.info)) {
available_drivers_with_installed[driver] = true;
} else {
all_installed = false;
available_drivers_with_installed[driver] = false;
}
}
} catch (Error e) {
critical ("Failed to get package details, treating as not installed: %s", e.message);
}

return array;
}

public async void install (string pkg_name) throws DBusError, IOError {
if (!(pkg_name in available_drivers)) {
critical ("Driver not found");
return;
}

if (current_state.state != AVAILABLE) {
warning ("No drivers available, or already downloading a driver.");
return;
}

cancellable.reset ();

update_state (DOWNLOADING);

try {
string[] pkgs = available_drivers[pkg_name].data; // It seems arrays are imediately freed with async methods so this prevents a seg fault
var results = yield task.install_packages_async (pkgs, cancellable, progress_callback);

if (results.get_exit_code () == CANCELLED) {
debug ("Installation was cancelled");
update_state (AVAILABLE);
return;
}

foreach (var driver in available_drivers.get_keys ()) {
string[] driver_pkgs = available_drivers[driver].data;
yield update_installed (driver, driver_pkgs);
}

var notification = new Notification (_("Restart required"));
notification.set_body (_("Please restart your system to finalize driver installation"));
notification.set_icon (new ThemedIcon ("system-reboot"));
notification.set_default_action (Application.ACTION_PREFIX + Application.SHOW_UPDATES_ACTION);

GLib.Application.get_default ().send_notification (NOTIFICATION_ID, notification);

update_state (AVAILABLE);
} catch (Error e) {
critical ("Failed to install driver: %s", e.message);
send_error (e.message);
}
}

public void cancel () throws DBusError, IOError {
cancellable.cancel ();
}

private void progress_callback (Pk.Progress progress, Pk.ProgressType progress_type) {
update_state (current_state.state, PkUtils.status_to_title (progress.status));
}

private void send_error (string message) {
var notification = new Notification (_("A driver couldn't be installed"));
notification.set_body (_("An error occurred while trying to install a driver"));
notification.set_icon (new ThemedIcon ("dialog-error"));
notification.set_default_action (Application.ACTION_PREFIX + Application.SHOW_UPDATES_ACTION);

GLib.Application.get_default ().send_notification (NOTIFICATION_ID, notification);

update_state (ERROR, message);
}

private void update_state (PkUtils.State state, string message = "") {
current_state = {
state,
message
};

state_changed ();
}

public async PkUtils.CurrentState get_current_state () throws DBusError, IOError {
return current_state;
}

public async HashTable<string, bool> get_available_drivers () throws DBusError, IOError {
return available_drivers_with_installed;
}
}
Loading