Skip to content

Commit

Permalink
Add "-f" option: print fancy name of current layout group
Browse files Browse the repository at this point in the history
  • Loading branch information
brs-brs committed Aug 1, 2024
1 parent b59966b commit 7e9b181
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`*
Expand Down
5 changes: 4 additions & 1 deletion man/xkb-switch.1
Original file line number Diff line number Diff line change
@@ -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"

This comment has been minimized.

Copy link
@sergei-mironov

sergei-mironov Aug 1, 2024

Owner

Good catch, thanks!

.SH "xkb-switch"
.LP
.B xkb\-switch
Expand Down Expand Up @@ -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
13 changes: 12 additions & 1 deletion src/XKbSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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':
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
31 changes: 31 additions & 0 deletions src/XKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,37 @@ int XKeyboard::get_group() const
return static_cast<int>(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);

This comment has been minimized.

Copy link
@sergei-mironov

sergei-mironov Aug 1, 2024

Owner

OK, but I would prefer some kind of automatic deallocation here..

This comment has been minimized.

Copy link
@brs-brs

brs-brs Aug 1, 2024

Author Contributor

Yes, to be checked and corrected later

XkbFreeKeyboard(desc, 0, True);

return longGroupName;
}

// returns true if symbol is ok
bool filter(const string_vector& nonsyms, const std::string& symbol)
{
Expand Down
3 changes: 3 additions & 0 deletions src/XKeyboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down

1 comment on commit 7e9b181

@sergei-mironov
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I would be glad to accept this PR!

Please sign in to comment.