diff options
author | Robert Tari <robert@tari.in> | 2023-12-03 23:10:15 +0100 |
---|---|---|
committer | Robert Tari <robert@tari.in> | 2023-12-04 08:33:10 +0100 |
commit | 92416feefc2ff70e30f0378c263fcebc9d9a127d (patch) | |
tree | ae0dcde312b16816039bed7d17b7cc254f4c50c7 | |
parent | aef2ed6874d249a35ee666b433daff070a7b7765 (diff) | |
download | ayatana-indicator-display-92416feefc2ff70e30f0378c263fcebc9d9a127d.tar.gz ayatana-indicator-display-92416feefc2ff70e30f0378c263fcebc9d9a127d.tar.bz2 ayatana-indicator-display-92416feefc2ff70e30f0378c263fcebc9d9a127d.zip |
src/service.cpp: Add AccountsService support
-rw-r--r-- | src/service.cpp | 227 |
1 files changed, 212 insertions, 15 deletions
diff --git a/src/service.cpp b/src/service.cpp index 03872d2..f3fd33b 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -31,26 +31,31 @@ extern "C" #include <ayatana/common/utils.h> #ifdef COLOR_TEMP_ENABLED - #include "solar.h" + #include <act/act.h> + #include <pwd.h> + #include "solar.h" #endif } #ifdef COLOR_TEMP_ENABLED -typedef struct -{ - guint nTempLow; - guint nTempHigh; - const gchar *sName; -} TempProfile; + #define GREETER_BUS_NAME "org.ayatana.greeter" + #define GREETER_BUS_PATH "/org/ayatana/greeter" -TempProfile m_lTempProfiles[] = -{ - {0, 0, N_("Manual")}, - {4500, 6500, N_("Adaptive (Colder)")}, - {3627, 4913, N_("Adaptive")}, - {3058, 4913, N_("Adaptive (Warmer)")}, - {0, 0, NULL} -}; + typedef struct + { + guint nTempLow; + guint nTempHigh; + const gchar *sName; + } TempProfile; + + TempProfile m_lTempProfiles[] = + { + {0, 0, N_("Manual")}, + {4500, 6500, N_("Adaptive (Colder)")}, + {3627, 4913, N_("Adaptive")}, + {3058, 4913, N_("Adaptive (Warmer)")}, + {0, 0, NULL} + }; #endif class DisplayIndicator::Impl @@ -99,6 +104,7 @@ public: } #ifdef COLOR_TEMP_ENABLED + this->pAccountsServiceConnection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL); if (!this->bGreeter) { @@ -194,10 +200,23 @@ public: g_error("No %s schema could be found", sSchema); } } + else + { + this->pConnection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); + this->nGreeterSubscription = g_dbus_connection_signal_subscribe (this->pConnection, NULL, GREETER_BUS_NAME, "UserChanged", GREETER_BUS_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE, onUserChanged, this, NULL); + loadManager (this); + } #endif } } +#ifdef COLOR_TEMP_ENABLED + if (!this->bGreeter) + { + gint nUid = geteuid (); + getAccountsService (this, nUid); + } +#endif m_action_group = create_action_group(); // build the icon @@ -241,6 +260,21 @@ public: ~Impl() { #ifdef COLOR_TEMP_ENABLED + if (this->nGreeterSubscription) + { + g_dbus_connection_signal_unsubscribe (this->pConnection, this->nGreeterSubscription); + } + + if (this->lUsers) + { + g_slist_free (this->lUsers); + } + + if (this->pConnection) + { + g_object_unref (this->pConnection); + } + if (nCallback) { g_source_remove (nCallback); @@ -270,6 +304,11 @@ public: { g_clear_object (&pColorSchemeSettings); } + + if (this->pAccountsServiceConnection) + { + g_object_unref (this->pAccountsServiceConnection); + } #endif g_signal_handlers_disconnect_by_data(m_settings, this); @@ -293,9 +332,130 @@ public: private: #ifdef COLOR_TEMP_ENABLED + static void onUserChanged (GDBusConnection *pConnection, const gchar *sSender, const gchar *sPath, const gchar *sInterface, const gchar *sSignal, GVariant *pParameters, gpointer pUserData) + { + DisplayIndicator::Impl *pImpl = (DisplayIndicator::Impl*) pUserData; + g_variant_get (pParameters, "(s)", &pImpl->sUser); + loadManager (pImpl); + } + + static void getAccountsService (DisplayIndicator::Impl *pImpl, gint nUid) + { + pImpl->bReadingAccountsService = TRUE; + gchar *sPath = g_strdup_printf ("/org/freedesktop/Accounts/User%i", nUid); + GDBusProxy *pProxy = g_dbus_proxy_new_sync (pImpl->pAccountsServiceConnection, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.Accounts", sPath, "org.freedesktop.DBus.Properties", NULL, NULL); + g_free (sPath); + + if (pProxy) + { + const gchar *lProperties[] = {"brightness", "color-temp", "color-temp-profile"}; + + for (gint nIndex = 0; nIndex < 3; nIndex++) + { + GVariant *pParams = g_variant_new ("(ss)", "org.ayatana.indicator.display.AccountsService", lProperties[nIndex]); + GVariant *pValue = g_dbus_proxy_call_sync (pProxy, "Get", pParams, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL); + + if (pValue) + { + GVariant *pChild0 = g_variant_get_child_value (pValue, 0); + g_variant_unref (pValue); + GVariant *pChild1 = g_variant_get_child_value (pChild0, 0); + g_variant_unref (pChild0); + g_settings_set_value (pImpl->m_settings, lProperties[nIndex], pChild1); + g_variant_unref (pChild1); + } + } + } + + pImpl->bReadingAccountsService = FALSE; + } + + static void onUserLoaded (DisplayIndicator::Impl *pImpl, ActUser *pUser) + { + g_signal_handlers_disconnect_by_func (G_OBJECT (pUser), (gpointer) G_CALLBACK (onUserLoaded), pImpl); + + if (!pImpl->sUser) + { + GError *pError = NULL; + GVariant *pGreeterUser = g_dbus_connection_call_sync (pImpl->pConnection, GREETER_BUS_NAME, GREETER_BUS_PATH, GREETER_BUS_NAME, "GetUser", NULL, G_VARIANT_TYPE ("(s)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &pError); + + if (pError) + { + g_debug ("Failed calling GetUser, the greeter may not be ready yet: %s", pError->message); + g_error_free (pError); + + return; + } + + g_variant_get (pGreeterUser, "(s)", &pImpl->sUser); + } + + gboolean bPrefix = g_str_has_prefix (pImpl->sUser, "*"); + + if (!bPrefix) + { + const gchar *sUserName = act_user_get_user_name (pUser); + gboolean bSame = g_str_equal (pImpl->sUser, sUserName); + + if (bSame) + { + gint nUid = act_user_get_uid (pUser); + getAccountsService (pImpl, nUid); + updateColor (pImpl); + } + } + } + + static void onManagerLoaded (DisplayIndicator::Impl *pImpl) + { + ActUserManager *pManager = act_user_manager_get_default (); + + if (!pImpl->lUsers) + { + pImpl->lUsers = act_user_manager_list_users (pManager); + } + + for (GSList *lUser = pImpl->lUsers; lUser; lUser = lUser->next) + { + ActUser *pUser = static_cast<ActUser*>(lUser->data); + gboolean bLoaded = act_user_is_loaded (pUser); + + if (bLoaded) + { + onUserLoaded (pImpl, pUser); + } + else + { + g_signal_connect_swapped (pUser, "notify::is-loaded", G_CALLBACK (onUserLoaded), pImpl); + } + } + } + + static void loadManager (DisplayIndicator::Impl *pImpl) + { + ActUserManager *pManager = act_user_manager_get_default (); + gboolean bLoaded = FALSE; + g_object_get (pManager, "is-loaded", &bLoaded, NULL); + + if (bLoaded) + { + onManagerLoaded (pImpl); + } + else + { + g_signal_connect_swapped (pManager, "notify::is-loaded", G_CALLBACK (onManagerLoaded), pImpl); + } + } + static gboolean updateColor (gpointer pData) { DisplayIndicator::Impl *pImpl = (DisplayIndicator::Impl*) pData; + + if (pImpl->bReadingAccountsService) + { + return G_SOURCE_CONTINUE; + } + guint nProfile = 0; g_settings_get (pImpl->m_settings, "color-temp-profile", "q", &nProfile); gdouble fBrightness = g_settings_get_double (pImpl->m_settings, "brightness"); @@ -390,6 +550,37 @@ private: pImpl->fLastBrightness = fBrightness; pImpl->nLasColorTemp = nTemperature; g_free (sCommand); + gint nUid = 0; + + if (!pImpl->bGreeter) + { + nUid = geteuid (); + } + else if (pImpl->sUser) + { + const struct passwd *pPasswd = getpwnam (pImpl->sUser); + + if (pPasswd) + { + nUid = pPasswd->pw_uid; + } + } + + if (nUid) + { + gchar *sPath = g_strdup_printf ("/org/freedesktop/Accounts/User%i", nUid); + GDBusProxy *pProxy = g_dbus_proxy_new_sync (pImpl->pAccountsServiceConnection, G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.Accounts", sPath, "org.freedesktop.DBus.Properties", NULL, NULL); + g_free (sPath); + GVariant *pBrightnessValue = g_variant_new ("d", pImpl->fLastBrightness); + GVariant *pBrightnessParams = g_variant_new ("(ssv)", "org.ayatana.indicator.display.AccountsService", "brightness", pBrightnessValue); + g_dbus_proxy_call (pProxy, "Set", pBrightnessParams, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); + GVariant *pColorTempValue = g_variant_new ("q", pImpl->nLasColorTemp); + GVariant *pColorTempParams = g_variant_new ("(ssv)", "org.ayatana.indicator.display.AccountsService", "color-temp", pColorTempValue); + g_dbus_proxy_call (pProxy, "Set", pColorTempParams, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); + GVariant *pProfileValue = g_variant_new ("q", nProfile); + GVariant *pProfileParams = g_variant_new ("(ssv)", "org.ayatana.indicator.display.AccountsService", "color-temp-profile", pProfileValue); + g_dbus_proxy_call (pProxy, "Set", pProfileParams, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL); + } } if (!pImpl->bGreeter) @@ -950,6 +1141,12 @@ private: GSettings *pMetacitySettings = NULL; GSettings *pColorSchemeSettings = NULL; gboolean bTest; + guint nGreeterSubscription; + GDBusConnection *pConnection; + gchar *sUser = NULL; + GSList *lUsers = NULL; + gboolean bReadingAccountsService = FALSE; + GDBusConnection *pAccountsServiceConnection = NULL; #endif }; |