1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
* vi:set noexpandtab tabstop=8 shiftwidth=8:
*
* Copyright (C) 2016 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config.h>
#include <fnmatch.h>
#include <gudev/gudev.h>
#include <gnome-software.h>
#include "gs-plugin-modalias.h"
/**
* SECTION:
* Plugin to set a default icon and basic properties for apps which provide
* support for hardware devices which are attached to this system.
*
* This plugin uses udev to detect attached hardware, and matches it to apps
* which claim to provide support for that modalias in their metainfo.
*
* It does simple listing and matching, so runs entirely in the main thread and
* doesn’t require any locking.
*/
struct _GsPluginModalias {
GsPlugin parent;
GUdevClient *client;
GPtrArray *devices;
};
G_DEFINE_TYPE (GsPluginModalias, gs_plugin_modalias, GS_TYPE_PLUGIN)
static void
gs_plugin_modalias_uevent_cb (GUdevClient *client,
const gchar *action,
GUdevDevice *device,
gpointer user_data)
{
GsPluginModalias *self = GS_PLUGIN_MODALIAS (user_data);
if (g_strcmp0 (action, "add") == 0 ||
g_strcmp0 (action, "remove") == 0) {
g_debug ("invalidating devices as '%s' sent action '%s'",
g_udev_device_get_sysfs_path (device),
action);
g_ptr_array_set_size (self->devices, 0);
}
}
static void
gs_plugin_modalias_init (GsPluginModalias *self)
{
GsPlugin *plugin = GS_PLUGIN (self);
gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream");
gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_BEFORE, "icons");
self->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
self->client = g_udev_client_new (NULL);
g_signal_connect (self->client, "uevent",
G_CALLBACK (gs_plugin_modalias_uevent_cb), self);
}
static void
gs_plugin_modalias_dispose (GObject *object)
{
GsPluginModalias *self = GS_PLUGIN_MODALIAS (object);
g_clear_object (&self->client);
g_clear_pointer (&self->devices, g_ptr_array_unref);
G_OBJECT_CLASS (gs_plugin_modalias_parent_class)->dispose (object);
}
static void
gs_plugin_modalias_ensure_devices (GsPluginModalias *self)
{
g_autoptr(GList) list = NULL;
/* already set */
if (self->devices->len > 0)
return;
/* get the devices, and assume ownership of each */
list = g_udev_client_query_by_subsystem (self->client, NULL);
for (GList *l = list; l != NULL; l = l->next) {
GUdevDevice *device = G_UDEV_DEVICE (l->data);
if (g_udev_device_get_sysfs_attr (device, "modalias") == NULL) {
g_object_unref (device);
continue;
}
g_ptr_array_add (self->devices, device);
}
g_debug ("%u devices with modalias", self->devices->len);
}
static gboolean
gs_plugin_modalias_matches (GsPluginModalias *self,
const gchar *modalias)
{
gs_plugin_modalias_ensure_devices (self);
for (guint i = 0; i < self->devices->len; i++) {
GUdevDevice *device = g_ptr_array_index (self->devices, i);
const gchar *modalias_tmp;
/* get the (optional) device modalias */
modalias_tmp = g_udev_device_get_sysfs_attr (device, "modalias");
if (modalias_tmp == NULL)
continue;
if (fnmatch (modalias, modalias_tmp, 0) == 0) {
g_debug ("matched %s against %s", modalias_tmp, modalias);
return TRUE;
}
}
return FALSE;
}
static gboolean
refine_app (GsPluginModalias *self,
GsApp *app,
GsPluginRefineRequireFlags require_flags,
GCancellable *cancellable,
GError **error)
{
GPtrArray *provided;
guint i;
/* not required */
if (gs_app_has_icons (app))
return TRUE;
if (gs_app_get_kind (app) != AS_COMPONENT_KIND_DRIVER)
return TRUE;
/* do any of the modaliases match any installed hardware */
provided = gs_app_get_provided (app);
for (i = 0 ; i < provided->len; i++) {
GPtrArray *items;
AsProvided *prov = g_ptr_array_index (provided, i);
if (as_provided_get_kind (prov) != AS_PROVIDED_KIND_MODALIAS)
continue;
items = as_provided_get_items (prov);
for (guint j = 0; j < items->len; j++) {
if (gs_plugin_modalias_matches (self, (const gchar*) g_ptr_array_index (items, j))) {
g_autoptr(GIcon) ic = NULL;
ic = g_themed_icon_new ("emblem-system-symbolic");
gs_app_add_icon (app, ic);
gs_app_add_quirk (app, GS_APP_QUIRK_NOT_LAUNCHABLE);
break;
}
}
}
return TRUE;
}
static void
gs_plugin_modalias_refine_async (GsPlugin *plugin,
GsAppList *list,
GsPluginRefineFlags job_flags,
GsPluginRefineRequireFlags require_flags,
GsPluginEventCallback event_callback,
void *event_user_data,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GsPluginModalias *self = GS_PLUGIN_MODALIAS (plugin);
g_autoptr(GTask) task = NULL;
g_autoptr(GError) local_error = NULL;
task = g_task_new (plugin, cancellable, callback, user_data);
g_task_set_source_tag (task, gs_plugin_modalias_refine_async);
for (guint i = 0; i < gs_app_list_length (list); i++) {
GsApp *app = gs_app_list_index (list, i);
if (!refine_app (self, app, require_flags, cancellable, &local_error)) {
g_task_return_error (task, g_steal_pointer (&local_error));
return;
}
}
g_task_return_boolean (task, TRUE);
}
static gboolean
gs_plugin_modalias_refine_finish (GsPlugin *plugin,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_boolean (G_TASK (result), error);
}
static void
gs_plugin_modalias_class_init (GsPluginModaliasClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GsPluginClass *plugin_class = GS_PLUGIN_CLASS (klass);
object_class->dispose = gs_plugin_modalias_dispose;
plugin_class->refine_async = gs_plugin_modalias_refine_async;
plugin_class->refine_finish = gs_plugin_modalias_refine_finish;
}
GType
gs_plugin_query_type (void)
{
return GS_TYPE_PLUGIN_MODALIAS;
}
|