Skip to content

Commit

Permalink
Menu Bar: make shortcut key configurable
Browse files Browse the repository at this point in the history
This allows other letters than the first one of the (lower case)
key name to be used as shortcut, avoiding clashes among keys
with the same name (e.g. 'scan' and 'search', #69).
  • Loading branch information
Gerrit Renker committed Nov 7, 2020
1 parent 1a0055f commit 3ed6c16
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 46 deletions.
1 change: 0 additions & 1 deletion iw_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ void iw_get_interface_list(char** if_list, size_t max_entries)
* iw_dyn_info_get - populate dynamic information
* @info: information to populate
* @ifname: interface name
* @if: range information to use (number of encryption keys)
*/
void dyn_info_get(struct iw_dyn_info *info, const char *ifname)
{
Expand Down
4 changes: 2 additions & 2 deletions wavemon.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH wavemon 1 "October 2020" Linux "User Manuals"
.TH wavemon 1 "November 2020" Linux "User Manuals"
.SH NAME
wavemon \- a wireless network monitor
.SH SYNOPSIS
Expand All @@ -18,7 +18,7 @@ represents the same levels as a moving histogram.
On startup, you'll see (depending on configuration) one of the different monitor
screens. At the bottom, you'll find a \fImenu-bar\fR listing the screens and
their activating keys. Each screen is activated by either the corresponding
function key (F1..10), its numeric shortcut (1..0), or the underlined initial
function key (F1..10), its numeric shortcut (1..0), or the underlined shortcut
letter of the screen name. The following \fIscreens\fR can be selected:
.TP
.B Info (F1 or 'i')
Expand Down
75 changes: 32 additions & 43 deletions wavemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,35 @@
/**
* screen switching table
* @key_name: name under which the screen appears in the menu bar
* @shortcut: 1-character shortcut key for @key_name
* @init: screen initialisation function pointer
* @loop: screen update function pointer (connected to menu)
* @fini: screen cleanup function pointer
*/
static const struct {
const char *const key_name;
const char shortcut;
void (*init)(void);
int (*loop)(WINDOW *);
void (*fini)(void);
} screens[] = {
[SCR_INFO] = {
.key_name = "info",
.shortcut = 'i',
.init = scr_info_init,
.loop = scr_info_loop,
.fini = scr_info_fini
},
[SCR_LHIST] = {
.key_name = "lhist",
.shortcut = 'l',
.init = scr_lhist_init,
.loop = scr_lhist_loop,
.fini = scr_lhist_fini
},
[SCR_SCAN] = {
.key_name = "scan",
.shortcut = 's',
.init = scr_aplst_init,
.loop = scr_aplst_loop,
.fini = scr_aplst_fini
Expand All @@ -65,24 +70,28 @@ static const struct {
},
[SCR_PREFS] = {
.key_name = "prefs",
.shortcut = 'p',
.init = scr_conf_init,
.loop = scr_conf_loop,
.fini = scr_conf_fini
},
[SCR_HELP] = {
.key_name = "help",
.shortcut = 'h',
.init = scr_help_init,
.loop = scr_help_loop,
.fini = scr_help_fini
},
[SCR_ABOUT] = {
.key_name = "about",
.shortcut = 'a',
.init = scr_about_init,
.loop = scr_about_loop,
.fini = scr_about_fini
},
[SCR_QUIT] = {
.key_name = "quit",
.shortcut = 'q',
}
};

Expand All @@ -103,24 +112,31 @@ static void sig_winch(int signo)
static WINDOW *init_menubar(const enum wavemon_screen active)
{
WINDOW *menu = newwin(1, WAV_WIDTH, WAV_HEIGHT, 0);
enum wavemon_screen cur;

nodelay(menu, TRUE);
keypad(menu, TRUE);
wmove(menu, 0, 0);
for (cur = SCR_INFO; cur <= SCR_QUIT; cur++) {
if (*screens[cur].key_name) {
for (enum wavemon_screen cur = SCR_INFO; cur <= SCR_QUIT; cur++) {
const char *p = screens[cur].key_name;

if (*p) {
wattrset(menu, A_REVERSE | A_BOLD);
wprintw(menu, "F%d", cur + 1);

wattrset(menu, cur != active ? COLOR_PAIR(CP_INACTIVE)
: COLOR_PAIR(CP_ACTIVE) | A_BOLD);

wattron(menu, A_UNDERLINE);
waddch(menu, screens[cur].key_name[0]);
wattroff(menu, A_UNDERLINE);

wprintw(menu, "%-6s", screens[cur].key_name + 1);
for (int i = 0; i < MAX_MENU_KEY; i++) {
if (*p == screens[cur].shortcut) {
wattron(menu, A_UNDERLINE);
waddch(menu, *p++);
wattroff(menu, A_UNDERLINE);
} else if (*p) {
waddch(menu, *p++);
} else {
waddch(menu, ' ');
}
}
}
}
wrefresh(menu);
Expand Down Expand Up @@ -234,41 +250,14 @@ int main(int argc, char *argv[])
}

/* Main menu */
switch (key) {
case KEY_F(1):
case '1':
case 'i':
next = SCR_INFO;
break;
case KEY_F(2):
case '2':
case 'l':
next = SCR_LHIST;
break;
case KEY_F(3):
case 's':
case '3':
next = SCR_SCAN;
break;
case KEY_F(7):
case 'p':
case '7':
next = SCR_PREFS;
break;
case KEY_F(8):
case 'h':
case '8':
next = SCR_HELP;
break;
case KEY_F(9):
case 'a':
case '9':
next = SCR_ABOUT;
break;
case KEY_F(10):
case 'q':
case '0':
next = SCR_QUIT;
for (enum wavemon_screen s = SCR_INFO; s <= SCR_QUIT; s++) {
if (*screens[s].key_name && (
key == (s == SCR_QUIT ? '0' : '1' + s) ||
key == KEY_F(s + 1) ||
key == screens[s].shortcut)) {
next = s;
break;
}
}
} while (next == cur);
}
Expand Down
4 changes: 4 additions & 0 deletions wavemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ enum info_screen_geometry {
*/
#define WAV_WIDTH (COLS)
#define WAV_HEIGHT (LINES-1)
/*
* Maximum length of a menubar key entry
*/
#define MAX_MENU_KEY (MIN_SCREEN_COLS/10)
/*
* Maximum lengths/coordinates inside the bordered screen.
*
Expand Down

0 comments on commit 3ed6c16

Please sign in to comment.