diff options
author | Robert Tari <robert@tari.in> | 2024-07-11 04:29:11 +0200 |
---|---|---|
committer | Robert Tari <robert@tari.in> | 2024-07-11 04:29:11 +0200 |
commit | 907c32c00d9bbfa6b50b1a7635104ea7ccba9208 (patch) | |
tree | 381817f527ee1fac9c8cb733630fd87c9025b225 /src/keyboard-lomiri.c | |
parent | b5eecfc6ae3b09897efb24ef4067373d98668b07 (diff) | |
download | ayatana-indicator-keyboard-907c32c00d9bbfa6b50b1a7635104ea7ccba9208.tar.gz ayatana-indicator-keyboard-907c32c00d9bbfa6b50b1a7635104ea7ccba9208.tar.bz2 ayatana-indicator-keyboard-907c32c00d9bbfa6b50b1a7635104ea7ccba9208.zip |
Support multiple system layouts
fixes https://github.com/AyatanaIndicators/ayatana-indicator-keyboard/issues/68
Diffstat (limited to 'src/keyboard-lomiri.c')
-rw-r--r-- | src/keyboard-lomiri.c | 192 |
1 files changed, 118 insertions, 74 deletions
diff --git a/src/keyboard-lomiri.c b/src/keyboard-lomiri.c index 637bb9f4..6666c7b8 100644 --- a/src/keyboard-lomiri.c +++ b/src/keyboard-lomiri.c @@ -445,111 +445,155 @@ static void keyboard_init(Keyboard *self) } else { - // Get layouts from /etc/default/keyboard - gchar *sFile; - g_file_get_contents ("/etc/default/keyboard", &sFile, NULL, NULL); - gchar **lLines = g_strsplit(sFile, "\n", -1); - guint nLines = g_strv_length(lLines); - gchar **lLayouts = NULL; - gchar **lVariants = NULL; - - for (guint nLine = 0; nLine < nLines; nLine++) + // Get system layouts + gboolean bDefaultLocation = g_file_test ("/etc/default/keyboard", G_FILE_TEST_EXISTS); + gchar *sLocation = NULL; + + if (bDefaultLocation) + { + 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) { - gboolean bIsLayout = g_str_has_prefix(lLines[nLine], "XKBLAYOUT"); + GRegex *pRegex = NULL; - if (bIsLayout == TRUE) + if (bDefaultLocation) { - gboolean bQuoted = g_strrstr(lLines[nLine], "\"") != NULL; - gchar *sLayout = NULL; + #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 (bQuoted == TRUE) + 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) { - sLayout = (lLines[nLine] + 11); - guint nLength = strlen(sLayout); - sLayout[nLength - 1] = '\0'; + gchar *sLayouts = g_match_info_fetch (pMatchInfo, 1); + lLayouts = g_strsplit (sLayouts, ",", -1); + g_free (sLayouts); } else { - sLayout = (lLines[nLine] + 10); + g_error ("PANIC: No system XkbLayout found"); } - lLayouts = g_strsplit(sLayout, ",", -1); - - continue; + g_match_info_free (pMatchInfo); + g_regex_unref (pRegex); + } + else + { + g_error ("PANIC: Failed to compile regex: %s", pError->message); + g_error_free (pError); } - gboolean bIsVariant = g_str_has_prefix(lLines[nLine], "XKBVARIANT"); + 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 (bIsVariant == TRUE) + if (!pError) { - gboolean bQuoted = g_strrstr(lLines[nLine], "\"") != NULL; - gchar *sVariant = NULL; + GMatchInfo *pMatchInfo = NULL; - if (bQuoted == TRUE) - { - sVariant = (lLines[nLine] + 12); - guint nLength = strlen(sVariant); - sVariant[nLength - 1] = '\0'; - } - else + #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) { - sVariant = (lLines[nLine] + 11); + gchar *sVariants = g_match_info_fetch (pMatchInfo, 1); + lVariants = g_strsplit (sVariants, ",", -1); + g_free (sVariants); } - lVariants = g_strsplit(sVariant, ",", -1); - - continue; + g_match_info_free (pMatchInfo); + g_regex_unref (pRegex); + } + else + { + g_error ("PANIC: Failed to compile regex: %s", pError->message); + g_error_free (pError); } - } - - guint nVariants = 0; - if (lVariants != NULL) + g_free(sFile); + } + else { - g_strv_length(lVariants); + g_error ("PANIC: Failed to get %s contents: %s", sLocation, pError->message); + g_error_free (pError); } - if (lLayouts != NULL) + guint nLayouts = g_strv_length (lLayouts); + + for (guint nLayout = 0; nLayout < nLayouts; nLayout++) { - guint nLayouts = g_strv_length(lLayouts); + gchar *sId = NULL; + guint nVariantLength = 0; - for (guint nLayout = 0; nLayout < nLayouts; nLayout++) + if (lVariants && lVariants[nLayout]) { - gchar *sId = NULL; - - if (nVariants > nLayout) - { - guint nVariant = strlen(lVariants[nLayout]); - - if (nVariants == nLayouts && nVariant > 0) - { - sId = g_strconcat(lLayouts[nLayout], "+", lVariants[nLayout], NULL); - } - else - { - sId = g_strdup(lLayouts[nLayout]); - } - } - else - { - sId = g_strdup(lLayouts[nLayout]); - } - - self->pPrivate->lLayoutRec = g_slist_append(self->pPrivate->lLayoutRec, sId); + nVariantLength = strlen (lVariants[nLayout]); } - self->pPrivate->nLayout = 0; - - g_strfreev(lLayouts); - } + if (nVariantLength) + { + sId = g_strconcat (lLayouts[nLayout], "+", lVariants[nLayout], NULL); + } + else + { + sId = g_strdup (lLayouts[nLayout]); + } - if (lVariants != NULL) - { - g_strfreev(lVariants); + self->pPrivate->lLayoutRec = g_slist_append (self->pPrivate->lLayoutRec, sId); } - g_strfreev(lLines); - g_free(sFile); + g_strfreev (lLayouts); + g_strfreev (lVariants); + self->pPrivate->nLayout = 0; + //~ Get system layouts ActUserManager *pManager = act_user_manager_get_default(); gboolean bIsLoaded; |