diff options
author | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2025-02-23 18:07:22 +0100 |
---|---|---|
committer | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2025-02-23 18:07:22 +0100 |
commit | 95e94abd02c6abb0be8f72f887d01b7bea94c469 (patch) | |
tree | ad344d44e3a25e063d49af788ba40a94ba064408 | |
parent | 270798fbb77775a01fa339622a16c365acb57d67 (diff) | |
parent | 95eb5965e3a266c774cb6997c63213ad51bd5475 (diff) | |
download | ayatana-indicator-keyboard-95e94abd02c6abb0be8f72f887d01b7bea94c469.tar.gz ayatana-indicator-keyboard-95e94abd02c6abb0be8f72f887d01b7bea94c469.tar.bz2 ayatana-indicator-keyboard-95e94abd02c6abb0be8f72f887d01b7bea94c469.zip |
Merge branch 'tari01-pr/system-layouts'
Attributes GH PR #72: https://github.com/AyatanaIndicators/ayatana-indicator-keyboard/pull/72
-rw-r--r-- | src/keyboard-lomiri.c | 145 | ||||
-rw-r--r-- | src/keyboard-x11.c | 151 | ||||
-rw-r--r-- | src/system-layouts.h | 186 |
3 files changed, 203 insertions, 279 deletions
diff --git a/src/keyboard-lomiri.c b/src/keyboard-lomiri.c index 6666c7b8..c00bc55a 100644 --- a/src/keyboard-lomiri.c +++ b/src/keyboard-lomiri.c @@ -19,6 +19,7 @@ #include <glib-object.h> #include "languages.h" #include "keyboard.h" +#include "system-layouts.h" enum { @@ -373,6 +374,7 @@ static void onSourcesChanged (GSettings *pSettings, const gchar *sKey, gpointer static void keyboard_init(Keyboard *self) { self->pPrivate = keyboard_get_instance_private(self); + self->pPrivate->lLayoutRec = NULL; self->pPrivate->lLayouts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, freeLayout); // Read all available layouts @@ -446,152 +448,19 @@ static void keyboard_init(Keyboard *self) else { // Get system layouts - gboolean bDefaultLocation = g_file_test ("/etc/default/keyboard", G_FILE_TEST_EXISTS); - gchar *sLocation = NULL; + getSystemLayouts ("/etc/default/keyboard", &self->pPrivate->lLayoutRec, NULL, FALSE); - if (bDefaultLocation) + if (!self->pPrivate->lLayoutRec) { - sLocation = "/etc/default/keyboard"; - } - else - { - sLocation = "/etc/X11/xorg.conf.d/00-keyboard.conf"; - } - - gchar *sFile = NULL; - GError *pError = NULL; - g_file_get_contents (sLocation, &sFile, NULL, &pError); - GStrv lLayouts = NULL; - GStrv lVariants = NULL; - - if (!pError) - { - GRegex *pRegex = NULL; - - if (bDefaultLocation) - { - #if GLIB_CHECK_VERSION(2, 73, 0) - pRegex = g_regex_new (" *XKBLAYOUT *= *\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); - #else - pRegex = g_regex_new (" *XKBLAYOUT *= *\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); - #endif - } - else - { - #if GLIB_CHECK_VERSION(2, 73, 0) - pRegex = g_regex_new (" *Option +\"*XkbLayout\"* +\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); - #else - pRegex = g_regex_new (" *Option +\"*XkbLayout\"* +\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); - #endif - } - - if (!pError) - { - GMatchInfo *pMatchInfo = NULL; - - #if GLIB_CHECK_VERSION(2, 73, 0) - gboolean bMatch = g_regex_match (pRegex, sFile, G_REGEX_MATCH_DEFAULT, &pMatchInfo); - #else - gboolean bMatch = g_regex_match (pRegex, sFile, (GRegexMatchFlags) 0, &pMatchInfo); - #endif - - if (bMatch) - { - gchar *sLayouts = g_match_info_fetch (pMatchInfo, 1); - lLayouts = g_strsplit (sLayouts, ",", -1); - g_free (sLayouts); - } - else - { - g_error ("PANIC: No system XkbLayout found"); - } - - g_match_info_free (pMatchInfo); - g_regex_unref (pRegex); - } - else - { - g_error ("PANIC: Failed to compile regex: %s", pError->message); - g_error_free (pError); - } - - if (bDefaultLocation) - { - #if GLIB_CHECK_VERSION(2, 73, 0) - pRegex = g_regex_new (" *XKBVARIANT *= *\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); - #else - pRegex = g_regex_new (" *XKBVARIANT *= *\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); - #endif - } - else - { - #if GLIB_CHECK_VERSION(2, 73, 0) - pRegex = g_regex_new (" *Option +\"*XkbVariant\"* +\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); - #else - pRegex = g_regex_new (" *Option +\"*XkbVariant\"* +\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); - #endif - } - - if (!pError) - { - GMatchInfo *pMatchInfo = NULL; - - #if GLIB_CHECK_VERSION(2, 73, 0) - gboolean bMatch = g_regex_match (pRegex, sFile, G_REGEX_MATCH_DEFAULT, &pMatchInfo); - #else - gboolean bMatch = g_regex_match (pRegex, sFile, (GRegexMatchFlags) 0, &pMatchInfo); - #endif - - if (bMatch) - { - gchar *sVariants = g_match_info_fetch (pMatchInfo, 1); - lVariants = g_strsplit (sVariants, ",", -1); - g_free (sVariants); - } - - g_match_info_free (pMatchInfo); - g_regex_unref (pRegex); - } - else - { - g_error ("PANIC: Failed to compile regex: %s", pError->message); - g_error_free (pError); - } - - g_free(sFile); - } - else - { - g_error ("PANIC: Failed to get %s contents: %s", sLocation, pError->message); - g_error_free (pError); + getSystemLayouts ("/etc/X11/xorg.conf.d/00-keyboard.conf", &self->pPrivate->lLayoutRec, NULL, FALSE); } - guint nLayouts = g_strv_length (lLayouts); - - for (guint nLayout = 0; nLayout < nLayouts; nLayout++) + if (!self->pPrivate->lLayoutRec) { - gchar *sId = NULL; - guint nVariantLength = 0; - - if (lVariants && lVariants[nLayout]) - { - nVariantLength = strlen (lVariants[nLayout]); - } - - if (nVariantLength) - { - sId = g_strconcat (lLayouts[nLayout], "+", lVariants[nLayout], NULL); - } - else - { - sId = g_strdup (lLayouts[nLayout]); - } - + gchar *sId = g_strdup ("us"); self->pPrivate->lLayoutRec = g_slist_append (self->pPrivate->lLayoutRec, sId); } - g_strfreev (lLayouts); - g_strfreev (lVariants); self->pPrivate->nLayout = 0; //~ Get system layouts diff --git a/src/keyboard-x11.c b/src/keyboard-x11.c index 83811521..d7d482f5 100644 --- a/src/keyboard-x11.c +++ b/src/keyboard-x11.c @@ -20,6 +20,7 @@ #include <libxklavier/xklavier.h> #include "languages.h" #include "keyboard.h" +#include "system-layouts.h" #define GREETER_BUS_NAME "org.ayatana.greeter" #define GREETER_BUS_PATH "/org/ayatana/greeter" @@ -567,6 +568,7 @@ static void onUserChanged (GDBusConnection *pConnection, const gchar *sSender, c static void keyboard_init(Keyboard *self) { self->pPrivate = keyboard_get_instance_private(self); + self->pPrivate->lLayoutRec = NULL; self->pPrivate->lLayouts = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, freeLayout); // Read all available layouts @@ -657,156 +659,23 @@ static void keyboard_init(Keyboard *self) self->pPrivate->nSubscription = g_dbus_connection_signal_subscribe (self->pPrivate->pConnection, NULL, GREETER_BUS_NAME, "UserChanged", GREETER_BUS_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE, onUserChanged, self, NULL); // Get system layouts - gboolean bDefaultLocation = g_file_test ("/etc/default/keyboard", G_FILE_TEST_EXISTS); - gchar *sLocation = NULL; + getSystemLayouts ("/etc/default/keyboard", &self->pPrivate->lLayoutRec, &self->pPrivate->lSystemLayouts, TRUE); - if (bDefaultLocation) + if (!self->pPrivate->lLayoutRec) { - sLocation = "/etc/default/keyboard"; + getSystemLayouts ("/etc/X11/xorg.conf.d/00-keyboard.conf", &self->pPrivate->lLayoutRec, &self->pPrivate->lSystemLayouts, TRUE); } - else - { - sLocation = "/etc/X11/xorg.conf.d/00-keyboard.conf"; - } - - gchar *sFile = NULL; - GError *pError = NULL; - g_file_get_contents (sLocation, &sFile, NULL, &pError); - GStrv lLayouts = NULL; - GStrv lVariants = NULL; - - if (!pError) - { - GRegex *pRegex = NULL; - - if (bDefaultLocation) - { - #if GLIB_CHECK_VERSION(2, 73, 0) - pRegex = g_regex_new (" *XKBLAYOUT *= *\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); - #else - pRegex = g_regex_new (" *XKBLAYOUT *= *\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); - #endif - } - else - { - #if GLIB_CHECK_VERSION(2, 73, 0) - pRegex = g_regex_new (" *Option +\"*XkbLayout\"* +\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); - #else - pRegex = g_regex_new (" *Option +\"*XkbLayout\"* +\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); - #endif - } - - if (!pError) - { - GMatchInfo *pMatchInfo = NULL; - - #if GLIB_CHECK_VERSION(2, 73, 0) - gboolean bMatch = g_regex_match (pRegex, sFile, G_REGEX_MATCH_DEFAULT, &pMatchInfo); - #else - gboolean bMatch = g_regex_match (pRegex, sFile, (GRegexMatchFlags) 0, &pMatchInfo); - #endif - - if (bMatch) - { - gchar *sLayouts = g_match_info_fetch (pMatchInfo, 1); - lLayouts = g_strsplit (sLayouts, ",", -1); - g_free (sLayouts); - } - else - { - g_error ("PANIC: No system XkbLayout found"); - } - - g_match_info_free (pMatchInfo); - g_regex_unref (pRegex); - } - else - { - g_error ("PANIC: Failed to compile regex: %s", pError->message); - g_error_free (pError); - } - - if (bDefaultLocation) - { - #if GLIB_CHECK_VERSION(2, 73, 0) - pRegex = g_regex_new (" *XKBVARIANT *= *\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); - #else - pRegex = g_regex_new (" *XKBVARIANT *= *\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); - #endif - } - else - { - #if GLIB_CHECK_VERSION(2, 73, 0) - pRegex = g_regex_new (" *Option +\"*XkbVariant\"* +\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); - #else - pRegex = g_regex_new (" *Option +\"*XkbVariant\"* +\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); - #endif - } - - if (!pError) - { - GMatchInfo *pMatchInfo = NULL; - - #if GLIB_CHECK_VERSION(2, 73, 0) - gboolean bMatch = g_regex_match (pRegex, sFile, G_REGEX_MATCH_DEFAULT, &pMatchInfo); - #else - gboolean bMatch = g_regex_match (pRegex, sFile, (GRegexMatchFlags) 0, &pMatchInfo); - #endif - - if (bMatch) - { - gchar *sVariants = g_match_info_fetch (pMatchInfo, 1); - lVariants = g_strsplit (sVariants, ",", -1); - g_free (sVariants); - } - g_match_info_free (pMatchInfo); - g_regex_unref (pRegex); - } - else - { - g_error ("PANIC: Failed to compile regex: %s", pError->message); - g_error_free (pError); - } - - g_free(sFile); - } - else + if (!self->pPrivate->lLayoutRec) { - g_error ("PANIC: Failed to get %s contents: %s", sLocation, pError->message); - g_error_free (pError); - } - - guint nLayouts = g_strv_length (lLayouts); - GStrvBuilder *pBuilder = g_strv_builder_new (); - - for (guint nLayout = 0; nLayout < nLayouts; nLayout++) - { - gchar *sId = NULL; - guint nVariantLength = 0; - - if (lVariants && lVariants[nLayout]) - { - nVariantLength = strlen (lVariants[nLayout]); - } - - if (nVariantLength) - { - sId = g_strconcat (lLayouts[nLayout], "+", lVariants[nLayout], NULL); - } - else - { - sId = g_strdup (lLayouts[nLayout]); - } - + GStrvBuilder *pBuilder = g_strv_builder_new (); + gchar *sId = g_strdup ("us"); self->pPrivate->lLayoutRec = g_slist_append (self->pPrivate->lLayoutRec, sId); g_strv_builder_add (pBuilder, sId); + self->pPrivate->lSystemLayouts = g_strv_builder_end (pBuilder); + g_strv_builder_unref (pBuilder); } - self->pPrivate->lSystemLayouts = g_strv_builder_end (pBuilder); - g_strv_builder_unref (pBuilder); - g_strfreev (lLayouts); - g_strfreev (lVariants); self->pPrivate->nLayout = 0; //~ Get system layouts diff --git a/src/system-layouts.h b/src/system-layouts.h new file mode 100644 index 00000000..af9b2b5c --- /dev/null +++ b/src/system-layouts.h @@ -0,0 +1,186 @@ +/* + * Copyright 2024 Robert Tari <robert@tari.in> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __SYSTEM_LAYOUTS__ +#define __SYSTEM_LAYOUTS__ + +G_BEGIN_DECLS + +static void getSystemLayouts (gchar *sLocation, GSList **lLayoutRec, GStrv *lSystemLayouts, gboolean bSystemLayouts) +{ + gboolean bExists = g_file_test (sLocation, G_FILE_TEST_EXISTS); + + if (!bExists) + { + return; + } + + gchar *sFile = NULL; + GError *pError = NULL; + g_file_get_contents (sLocation, &sFile, NULL, &pError); + + if (pError) + { + g_error_free (pError); + + return; + } + + GRegex *pRegex = NULL; + gboolean bDefaultLocation = g_str_equal (sLocation, "/etc/default/keyboard"); + + if (bDefaultLocation) + { + #if GLIB_CHECK_VERSION(2, 73, 0) + pRegex = g_regex_new (" *XKBLAYOUT *= *\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); + #else + pRegex = g_regex_new (" *XKBLAYOUT *= *\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); + #endif + } + else + { + #if GLIB_CHECK_VERSION(2, 73, 0) + pRegex = g_regex_new (" *Option +\"*XkbLayout\"* +\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); + #else + pRegex = g_regex_new (" *Option +\"*XkbLayout\"* +\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); + #endif + } + + if (pError) + { + g_warning ("Panic: Failed to compile regex: %s", pError->message); + g_error_free (pError); + g_free(sFile); + + return; + } + + GMatchInfo *pMatchInfo = NULL; + + #if GLIB_CHECK_VERSION(2, 73, 0) + gboolean bMatch = g_regex_match (pRegex, sFile, G_REGEX_MATCH_DEFAULT, &pMatchInfo); + #else + gboolean bMatch = g_regex_match (pRegex, sFile, (GRegexMatchFlags) 0, &pMatchInfo); + #endif + + if (!bMatch) + { + g_regex_unref (pRegex); + g_free(sFile); + + return; + } + + gchar *sLayouts = g_match_info_fetch (pMatchInfo, 1); + GStrv lLayouts = g_strsplit (sLayouts, ",", -1); + g_free (sLayouts); + g_match_info_free (pMatchInfo); + g_regex_unref (pRegex); + + if (bDefaultLocation) + { + #if GLIB_CHECK_VERSION(2, 73, 0) + pRegex = g_regex_new (" *XKBVARIANT *= *\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); + #else + pRegex = g_regex_new (" *XKBVARIANT *= *\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); + #endif + } + else + { + #if GLIB_CHECK_VERSION(2, 73, 0) + pRegex = g_regex_new (" *Option +\"*XkbVariant\"* +\"*([,a-zA-Z0-9]*)\"*", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, &pError); + #else + pRegex = g_regex_new (" *Option +\"*XkbVariant\"* +\"*([,a-zA-Z0-9]*)\"*", (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &pError); + #endif + } + + if (pError) + { + g_warning ("Panic: Failed to compile regex: %s", pError->message); + g_error_free (pError); + g_free(sFile); + + return; + } + + #if GLIB_CHECK_VERSION(2, 73, 0) + bMatch = g_regex_match (pRegex, sFile, G_REGEX_MATCH_DEFAULT, &pMatchInfo); + #else + bMatch = g_regex_match (pRegex, sFile, (GRegexMatchFlags) 0, &pMatchInfo); + #endif + + if (!bMatch) + { + g_regex_unref (pRegex); + g_free(sFile); + + return; + } + + gchar *sVariants = g_match_info_fetch (pMatchInfo, 1); + GStrv lVariants = g_strsplit (sVariants, ",", -1); + g_free (sVariants); + g_match_info_free (pMatchInfo); + g_regex_unref (pRegex); + g_free(sFile); + guint nLayouts = g_strv_length (lLayouts); + GStrvBuilder *pBuilder = NULL; + + if (bSystemLayouts) + { + pBuilder = g_strv_builder_new (); + } + + for (guint nLayout = 0; nLayout < nLayouts; nLayout++) + { + gchar *sId = NULL; + guint nVariantLength = 0; + + if (lVariants && lVariants[nLayout]) + { + nVariantLength = strlen (lVariants[nLayout]); + } + + if (nVariantLength) + { + sId = g_strconcat (lLayouts[nLayout], "+", lVariants[nLayout], NULL); + } + else + { + sId = g_strdup (lLayouts[nLayout]); + } + + *lLayoutRec = g_slist_append (*lLayoutRec, sId); + + if (bSystemLayouts) + { + g_strv_builder_add (pBuilder, sId); + } + } + + if (bSystemLayouts) + { + *lSystemLayouts = g_strv_builder_end (pBuilder); + g_strv_builder_unref (pBuilder); + } + + g_strfreev (lLayouts); + g_strfreev (lVariants); +} + +G_END_DECLS + +#endif |