From 7e9b1815549082f20fe1dab7d33060b6369741d5 Mon Sep 17 00:00:00 2001 From: brs-brs Date: Thu, 1 Aug 2024 13:28:16 +0200 Subject: [PATCH] Add "-f" option: print fancy name of current layout group --- CMakeLists.txt | 2 +- README.md | 1 + man/xkb-switch.1 | 5 ++++- src/XKbSwitch.cpp | 13 ++++++++++++- src/XKeyboard.cpp | 31 +++++++++++++++++++++++++++++++ src/XKeyboard.hpp | 3 +++ 6 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cf63bba..41018c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.10) PROJECT( XKB-SWITCH ) SET(MAJOR_VERSION 2) -SET(MINOR_VERSION 0) +SET(MINOR_VERSION 1) SET(RELEASE_VERSION 1) SET(XKBSWITCH_VERSION ${MAJOR_VERSION}.${MINOR_VERSION}.${RELEASE_VERSION}) ADD_DEFINITIONS(-DXKBSWITCH_VERSION="${XKBSWITCH_VERSION}") diff --git a/README.md b/README.md index 8d76f18..ccd1211 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ Usage: xkb-switch -s ARG Sets current layout group to ARG xkb-switch -W Infinitely waits for group change xkb-switch -n|--next Switch to the next layout group xkb-switch [-p] Displays current layout group + xkb-switch -f|--fancy Displays fancy name of current layout group ``` *A note on `xkb-switch -x`* diff --git a/man/xkb-switch.1 b/man/xkb-switch.1 index f55e2bf..d97aec6 100644 --- a/man/xkb-switch.1 +++ b/man/xkb-switch.1 @@ -1,4 +1,4 @@ -.TH "xkb-switch" "1" "1.3.1" "J. Bromley, S. Mironov, Alexei Rad'kov" "User Commands" +.TH "xkb-switch" "1" "2.1.1" "J. Bromley, S. Mironov, Alexei Rad'kov" "User Commands" .SH "xkb-switch" .LP .B xkb\-switch @@ -39,6 +39,9 @@ Switch to the next layout group. .BR \-p Display current layout group. .TP +.TP +.BR \-f ", " \-\^\-fancy +Display fancy name of current layout group. .SH "AUTHORS" .LP J. Bromley, S. Mironov, Alexei Rad'kov diff --git a/src/XKbSwitch.cpp b/src/XKbSwitch.cpp index 15b7393..98a428b 100644 --- a/src/XKbSwitch.cpp +++ b/src/XKbSwitch.cpp @@ -47,6 +47,7 @@ void usage() cerr << " xkb-switch -n|--next Switch to the next layout group" << endl; cerr << " xkb-switch -d|--debug Print debug information" << endl; cerr << " xkb-switch [-p] Displays current layout group" << endl; + cerr << " xkb-switch -f|--fancy Displays fancy name of current layout group" << endl; } string print_layouts(const string_vector& sv) @@ -77,6 +78,7 @@ int main( int argc, char* argv[] ) int m_print = 0; int m_next = 0; int m_list = 0; + int m_fancy = 0; int opt; int option_index = 0; string newgrp; @@ -91,9 +93,10 @@ int main( int argc, char* argv[] ) {"next", no_argument, NULL, 'n'}, {"help", no_argument, NULL, 'h'}, {"debug", no_argument, NULL, 'd'}, + {"fancy", no_argument, NULL, 'f'}, {NULL, 0, NULL, 0}, }; - while ((opt = getopt_long(argc, argv, "s:lvwWpnhd", + while ((opt = getopt_long(argc, argv, "s:lvwWpnhdf", long_options, &option_index))!=-1) { switch (opt) { case 's': @@ -131,6 +134,9 @@ int main( int argc, char* argv[] ) case 'd': verbose++; break; + case 'f': + m_fancy++; + break; case '?': THROW_MSG(verbose, "Invalid arguments. Check --help."); break; @@ -191,7 +197,12 @@ int main( int argc, char* argv[] ) } if(m_print) { + if (!m_fancy) { cout << syms.at(xkb.get_group()) << endl; + } + else { + cout << xkb.get_long_group_name() << endl; + } } if(m_list) { diff --git a/src/XKeyboard.cpp b/src/XKeyboard.cpp index a0fc3d9..a6ab0f0 100644 --- a/src/XKeyboard.cpp +++ b/src/XKeyboard.cpp @@ -196,6 +196,37 @@ int XKeyboard::get_group() const return static_cast(xkbState.group); } +std::string XKeyboard::get_long_group_name() const +{ + if (_display == nullptr) { + throw std::runtime_error("Display not opened."); + } + + XkbStateRec xkbState; + + if (XkbGetState(_display, _deviceId, &xkbState) != Success) { + throw std::runtime_error("Failed to get keyboard state."); + } + + XkbDescPtr desc = XkbGetKeyboard(_display, XkbAllComponentsMask, _deviceId); + if (desc == nullptr) { + throw std::runtime_error("Failed to get keyboard description."); + } + + char* groupName = XGetAtomName(_display, desc->names->groups[xkbState.group]); + if (groupName == nullptr) { + XkbFreeKeyboard(desc, 0, True); + throw std::runtime_error("Failed to get group name."); + } + + std::string longGroupName = groupName; + + XFree(groupName); + XkbFreeKeyboard(desc, 0, True); + + return longGroupName; +} + // returns true if symbol is ok bool filter(const string_vector& nonsyms, const std::string& symbol) { diff --git a/src/XKeyboard.hpp b/src/XKeyboard.hpp index 821857b..df1b5e5 100644 --- a/src/XKeyboard.hpp +++ b/src/XKeyboard.hpp @@ -69,6 +69,9 @@ class XKeyboard void build_layout_from(string_vector& vec, const layout_variant_strings& lv); void build_layout(string_vector& vec); + // Returns fancy layout name as a string + std::string get_long_group_name() const; + // Waits for kb event void wait_event(); };