Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[plugins] Split mce state tracking into separate plugin. JB#50501
It was meant to be a separate plugin from the beginning. Dependency
on mce is pretty much the only Sailfish OS specific part of nfcd,
so it better be in a separate repo.
  • Loading branch information
monich committed Aug 1, 2020
1 parent c1562c8 commit 4f1051c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 120 deletions.
8 changes: 8 additions & 0 deletions core/src/nfc_manager.c
Expand Up @@ -49,6 +49,8 @@ struct nfc_manager_priv {
NfcPlugins* plugins;
GHashTable* adapters;
guint next_adapter_index;
gboolean requested_power;
NFC_MODE requested_mode;
};

typedef GObjectClass NfcManagerClass;
Expand Down Expand Up @@ -211,6 +213,8 @@ nfc_manager_add_adapter(

nfc_adapter_set_name(adapter, name);
nfc_adapter_set_enabled(adapter, self->enabled);
nfc_adapter_request_mode(adapter, priv->requested_mode);
nfc_adapter_request_power(adapter, priv->requested_power);
g_hash_table_insert(priv->adapters, name, nfc_adapter_ref(adapter));
g_free(self->adapters);
self->adapters = nfc_manager_adapters(priv);
Expand Down Expand Up @@ -271,8 +275,10 @@ nfc_manager_request_power(
gboolean on)
{
if (G_LIKELY(self)) {
NfcManagerPriv* priv = self->priv;
NfcAdapter** adapters = nfc_manager_ref_adapters(self->priv);

priv->requested_power = on;
if (adapters) {
NfcAdapter** ptr = adapters;

Expand All @@ -290,8 +296,10 @@ nfc_manager_request_mode(
NFC_MODE mode)
{
if (G_LIKELY(self)) {
NfcManagerPriv* priv = self->priv;
NfcAdapter** adapters = nfc_manager_ref_adapters(self->priv);

priv->requested_mode = mode;
if (adapters) {
NfcAdapter** ptr = adapters;

Expand Down
2 changes: 0 additions & 2 deletions plugins/Makefile
Expand Up @@ -146,8 +146,6 @@ $(COVERAGE_DBUS_NEARD_BUILD_DIR):
# Setting plugin
#

PKGS += libmce-glib

SETTINGS_DIR = settings
SETTINGS_PLUGIN_SRC = \
settings_plugin.c
Expand Down
124 changes: 8 additions & 116 deletions plugins/settings/settings_plugin.c
Expand Up @@ -39,9 +39,6 @@

#include <gio/gio.h>

#include <mce_display.h>
#include <mce_log.h>

#include <dbusaccess_policy.h>
#include <dbusaccess_peer.h>

Expand All @@ -63,12 +60,6 @@ enum {
SETTINGS_DBUS_CALL_COUNT
};

enum {
DISPLAY_VALID,
DISPLAY_STATE,
MCE_EVENT_COUNT
};

enum {
NFC_ADAPTER_ADDED,
NFC_EVENT_COUNT
Expand All @@ -78,17 +69,13 @@ typedef NfcPluginClass SettingsPluginClass;
typedef struct settings_plugin {
NfcPlugin parent;
NfcManager* manager;
MceDisplay* display;
OrgSailfishosNfcSettings* iface;
DAPolicy* policy;
char* storage_dir;
char* storage_file;
guint own_name_id;
gulong dbus_call_id[SETTINGS_DBUS_CALL_COUNT];
gulong mce_event_id[MCE_EVENT_COUNT];
gulong nfc_event_id[NFC_EVENT_COUNT];
gboolean nfc_enabled;
gboolean nfc_always_on;
NFC_MODE auto_mode;
} SettingsPlugin;

Expand Down Expand Up @@ -142,9 +129,6 @@ static const DA_ACTION settings_policy_actions[] = {
static const char settings_default_policy[] =
DA_POLICY_VERSION ";group(privileged)=allow";

#define mce_display_on(display) \
((display) && (display)->valid && (display)->state != MCE_DISPLAY_STATE_OFF)

static
GKeyFile*
settings_plugin_load_config(
Expand Down Expand Up @@ -226,7 +210,6 @@ settings_plugin_update_config(
{
GKeyFile* config = settings_plugin_load_config(self);
const gboolean enabled = settings_plugin_nfc_enabled(config);
const gboolean always_on = settings_plugin_nfc_always_on(config);
gboolean save = FALSE;

if (enabled != self->nfc_enabled) {
Expand All @@ -235,12 +218,6 @@ settings_plugin_update_config(
self->nfc_enabled);
}

if (always_on != self->nfc_always_on) {
save = TRUE;
g_key_file_set_boolean(config, SETTINGS_GROUP, SETTINGS_KEY_ALWAYS_ON,
self->nfc_always_on);
}

if (save) {
settings_plugin_save_config(self, config);
}
Expand Down Expand Up @@ -285,66 +262,6 @@ settings_plugin_access_allowed(
return FALSE;
}

static
gboolean
settings_plugin_adapter_enabled(
SettingsPlugin* self)
{
return self->nfc_enabled &&
(self->nfc_always_on || mce_display_on(self->display));
}

static
void
settings_plugin_adapter_added(
NfcManager* manager,
NfcAdapter* adapter,
void* plugin)
{
SettingsPlugin* self = SETTINGS_PLUGIN(plugin);
const gboolean enable = settings_plugin_adapter_enabled(self);

if (enable && self->auto_mode != NFC_MODE_NONE) {
nfc_adapter_request_power(adapter, TRUE);
nfc_adapter_request_mode(adapter, self->auto_mode);
}
}

static
void
settings_plugin_update_nfc_state(
SettingsPlugin* self)
{
const gboolean enable = settings_plugin_adapter_enabled(self);

nfc_manager_set_enabled(self->manager, enable);
if (enable && self->auto_mode != NFC_MODE_NONE) {
nfc_manager_request_power(self->manager, TRUE);
nfc_manager_request_mode(self->manager, self->auto_mode);
}
}

static
void
settings_plugin_display_state_handler(
MceDisplay* display,
gpointer plugin)
{
settings_plugin_update_nfc_state(SETTINGS_PLUGIN(plugin));
}

static
void
settings_plugin_drop_display(
SettingsPlugin* self)
{
if (self->display) {
mce_display_remove_all_handlers(self->display, self->mce_event_id);
mce_display_unref(self->display);
self->display = NULL;
}
}

static
void
settings_plugin_set_nfc_enabled(
Expand All @@ -353,24 +270,9 @@ settings_plugin_set_nfc_enabled(
{
if (self->nfc_enabled != enabled) {
self->nfc_enabled = enabled;
/* We only need to follow display state when NFC is enabled */
if (enabled) {
GINFO("NFC enabled");
if (!self->nfc_always_on) {
self->display = mce_display_new();
self->mce_event_id[DISPLAY_VALID] =
mce_display_add_valid_changed_handler(self->display,
settings_plugin_display_state_handler, self);
self->mce_event_id[DISPLAY_STATE] =
mce_display_add_state_changed_handler(self->display,
settings_plugin_display_state_handler, self);
}
} else {
GINFO("NFC disabled");
settings_plugin_drop_display(self);
}
GINFO("NFC %s", enabled ? "enabled" : "disabled");
org_sailfishos_nfc_settings_emit_enabled_changed(self->iface, enabled);
settings_plugin_update_nfc_state(self);
nfc_manager_set_enabled(self->manager, enabled);
settings_plugin_update_config(self);
}
}
Expand Down Expand Up @@ -498,10 +400,6 @@ settings_plugin_start(

GVERBOSE("Starting");
self->manager = nfc_manager_ref(manager);
self->nfc_event_id[NFC_ADAPTER_ADDED] =
nfc_manager_add_adapter_added_handler(manager,
settings_plugin_adapter_added, self);

self->iface = org_sailfishos_nfc_settings_skeleton_new();
self->dbus_call_id[SETTINGS_DBUS_CALL_GET_ALL] =
g_signal_connect(self->iface, "handle-get-all",
Expand All @@ -522,9 +420,11 @@ settings_plugin_start(
self, NULL);

config = settings_plugin_load_config(self);
self->nfc_always_on = settings_plugin_nfc_always_on(config);
if (settings_plugin_nfc_always_on(config)) {
nfc_manager_request_power(self->manager, TRUE);
}
settings_plugin_set_nfc_enabled(self, settings_plugin_nfc_enabled(config));
settings_plugin_update_nfc_state(self);
nfc_manager_set_enabled(self->manager, self->nfc_enabled);
g_key_file_unref(config);
return TRUE;
}
Expand All @@ -537,7 +437,6 @@ settings_plugin_stop(
SettingsPlugin* self = SETTINGS_PLUGIN(plugin);

GVERBOSE("Stopping");
settings_plugin_drop_display(self);
if (self->own_name_id) {
g_bus_unown_name(self->own_name_id);
self->own_name_id = 0;
Expand All @@ -551,7 +450,6 @@ settings_plugin_stop(
self->iface = NULL;
}
if (self->manager) {
nfc_manager_remove_all_handlers(self->manager, self->nfc_event_id);
nfc_manager_unref(self->manager);
self->manager = NULL;
}
Expand Down Expand Up @@ -603,14 +501,8 @@ settings_plugin_create(
return g_object_new(SETTINGS_TYPE_PLUGIN, NULL);
}

static GLogModule* const settings_plugin_logs[] = {
&GLOG_MODULE_NAME,
&MCE_LOG_MODULE,
NULL
};

NFC_PLUGIN_DEFINE2(settings, "Settings storage and D-Bus interface",
settings_plugin_create, settings_plugin_logs, 0)
NFC_PLUGIN_DEFINE(settings, "Settings storage and D-Bus interface",
settings_plugin_create)

/*
* Local Variables:
Expand Down
1 change: 0 additions & 1 deletion rpm/nfcd.spec
Expand Up @@ -11,7 +11,6 @@ Source: %{name}-%{version}.tar.bz2
%define glib_version 2.32

BuildRequires: pkgconfig(glib-2.0) >= %{glib_version}
BuildRequires: pkgconfig(libmce-glib)
BuildRequires: pkgconfig(libdbusaccess)
BuildRequires: pkgconfig(libglibutil) >= %{libglibutil_version}
BuildRequires: pkgconfig(libdbuslogserver-gio) >= %{libdbuslog_version}
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Expand Up @@ -9,7 +9,7 @@
#

PKGS = gio-unix-2.0 gio-2.0 libglibutil
LIB_PKGS = $(PKGS) libmce-glib libdbusaccess libdbuslogserver-gio
LIB_PKGS = $(PKGS) libdbusaccess libdbuslogserver-gio

#
# Default target
Expand Down

0 comments on commit 4f1051c

Please sign in to comment.