alsamixer: small optimizations

textbox.c, mixer_display.c:
  - Replaced manual line drawing code `for (...) mvwaddch(...)` by the
    functions `mvwhline/mvwvline` provided by standard curses

card_select.c, proc_files.c:
  - Moved duplicate code in the create() functions to menu_widget_create()
    in menu_widget.c

card_select.c:
  - Removed redundant calls to sprintf()

device_name.c:
  - Combined multiple calls to form_opts_off()
  - Recognize ASCII DEL (127) as backspace

widget.c:
  - Uncommented code for setting a dummy key handler (all existing
    widgets do provide a key handling function)

Signed-off-by: Benjamin Abendroth <braph93@gmx.de>
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
braph 2019-10-03 20:18:23 +02:00 committed by Jaroslav Kysela
parent 89e746368c
commit 0657b2a559
8 changed files with 58 additions and 115 deletions

View file

@ -72,43 +72,12 @@ static void on_handle_key(int key)
} }
} }
static bool create(void) static void create(void)
{ {
int rows, columns; menu_widget_create(&list_widget, menu, _("Sound Card"));
const char *title;
if (screen_lines < 3 || screen_cols < 10) {
beep();
list_widget.close();
return FALSE;
}
scale_menu(menu, &rows, &columns);
rows += 2;
columns += 2;
if (rows > screen_lines)
rows = screen_lines;
if (columns > screen_cols)
columns = screen_cols;
widget_init(&list_widget, rows, columns, SCREEN_CENTER, SCREEN_CENTER,
attrs.menu, WIDGET_BORDER | WIDGET_SUBWINDOW);
title = _("Sound Card");
mvwprintw(list_widget.window, 0, (columns - 2 - get_mbs_width(title)) / 2, " %s ", title);
set_menu_win(menu, list_widget.window);
set_menu_sub(menu, list_widget.subwindow);
return TRUE;
} }
static void on_window_size_changed(void) void close_card_select_list(void)
{
unpost_menu(menu);
if (!create())
return;
post_menu(menu);
}
static void on_close(void)
{ {
unsigned int i; unsigned int i;
struct card *card, *next_card; struct card *card, *next_card;
@ -128,15 +97,10 @@ static void on_close(void)
widget_free(&list_widget); widget_free(&list_widget);
} }
void close_card_select_list(void)
{
on_close();
}
static struct widget list_widget = { static struct widget list_widget = {
.handle_key = on_handle_key, .handle_key = on_handle_key,
.window_size_changed = on_window_size_changed, .window_size_changed = create,
.close = on_close, .close = close_card_select_list,
}; };
static int get_cards(void) static int get_cards(void)
@ -170,11 +134,9 @@ static int get_cards(void)
if (err < 0) if (err < 0)
continue; continue;
card = ccalloc(1, sizeof *card); card = ccalloc(1, sizeof *card);
sprintf(buf, "%d", number);
card->indexstr = cstrdup(buf);
card->name = cstrdup(snd_ctl_card_info_get_name(info));
sprintf(buf, "hw:%d", number);
card->device_name = cstrdup(buf); card->device_name = cstrdup(buf);
card->indexstr = cstrdup(buf + 3);
card->name = cstrdup(snd_ctl_card_info_get_name(info));
prev_card->next = card; prev_card->next = card;
prev_card = card; prev_card = card;
++count; ++count;
@ -231,8 +193,5 @@ void create_card_select_list(void)
set_menu_spacing(menu, 2, 1, 1); set_menu_spacing(menu, 2, 1, 1);
menu_opts_on(menu, O_SHOWDESC); menu_opts_on(menu, O_SHOWDESC);
if (!create()) create();
return;
post_menu(menu);
} }

View file

@ -76,13 +76,14 @@ static void on_form_key(int key)
{ KEY_RIGHT, REQ_NEXT_CHAR }, { KEY_RIGHT, REQ_NEXT_CHAR },
{ KEY_HOME, REQ_BEG_FIELD }, { KEY_HOME, REQ_BEG_FIELD },
{ KEY_BACKSPACE, REQ_DEL_PREV }, { KEY_BACKSPACE, REQ_DEL_PREV },
{ 127, REQ_DEL_PREV },
{ KEY_DC, REQ_DEL_CHAR }, { KEY_DC, REQ_DEL_CHAR },
{ KEY_BEG, REQ_BEG_FIELD }, { KEY_BEG, REQ_BEG_FIELD },
{ KEY_END, REQ_END_FIELD }, { KEY_END, REQ_END_FIELD },
}; };
unsigned int i; unsigned int i;
if (key >= 32 && key < 256) { if (key >= 32 && key < 256 && key != 127) {
form_driver(form, key); form_driver(form, key);
return; return;
} }
@ -170,8 +171,7 @@ void create_device_name_form(void)
fields[0] = new_field(1, 32, 1, 1, 0, 0); fields[0] = new_field(1, 32, 1, 1, 0, 0);
if (!fields[0]) if (!fields[0])
fatal_error("cannot create field"); fatal_error("cannot create field");
field_opts_off(fields[0], O_ACTIVE); field_opts_off(fields[0], O_ACTIVE|O_EDIT);
field_opts_off(fields[0], O_EDIT);
set_field_fore(fields[0], attrs.textbox); set_field_fore(fields[0], attrs.textbox);
set_field_back(fields[0], attrs.textbox); set_field_back(fields[0], attrs.textbox);
set_field_buffer(fields[0], 0, _("Device name:")); set_field_buffer(fields[0], 0, _("Device name:"));
@ -179,9 +179,7 @@ void create_device_name_form(void)
fields[1] = new_field(1, 32, 2, 1, 0, 0); fields[1] = new_field(1, 32, 2, 1, 0, 0);
if (!fields[1]) if (!fields[1])
fatal_error("cannot create field"); fatal_error("cannot create field");
field_opts_off(fields[1], O_AUTOSKIP); field_opts_off(fields[1], O_AUTOSKIP|O_NULLOK|O_STATIC);
field_opts_off(fields[1], O_NULLOK);
field_opts_off(fields[1], O_STATIC);
set_field_fore(fields[1], attrs.textfield); set_field_fore(fields[1], attrs.textfield);
set_field_back(fields[1], attrs.textfield); set_field_back(fields[1], attrs.textfield);
set_field_buffer(fields[1], 0, mixer_device_name); set_field_buffer(fields[1], 0, mixer_device_name);

View file

@ -61,3 +61,32 @@ int menu_widget_handle_key(MENU *menu, int key)
return -1; return -1;
} }
} }
void menu_widget_create(struct widget *widget, MENU *menu, const char *title)
{
int rows, columns;
if (menu)
unpost_menu(menu);
if (screen_lines < 3 || screen_cols < 10) {
widget->close();
beep();
return;
}
scale_menu(menu, &rows, &columns);
rows += 2;
columns += 2;
if (rows > screen_lines)
rows = screen_lines;
if (columns > screen_cols)
columns = screen_cols;
widget_init(widget, rows, columns, SCREEN_CENTER, SCREEN_CENTER,
attrs.menu, WIDGET_BORDER | WIDGET_SUBWINDOW);
mvwprintw(widget->window, 0, (columns - 2 - get_mbs_width(title)) / 2, " %s ", title);
set_menu_win(menu, widget->window);
set_menu_sub(menu, widget->subwindow);
post_menu(menu);
}

View file

@ -5,5 +5,6 @@
#include <menu.h> #include <menu.h>
int menu_widget_handle_key(MENU *menu, int key); int menu_widget_handle_key(MENU *menu, int key);
void menu_widget_create(struct widget *widget, MENU *menu, const char *title);
#endif #endif

View file

@ -427,10 +427,8 @@ static void display_control(unsigned int control_index)
waddch(mixer_widget.window, ACS_HLINE); waddch(mixer_widget.window, ACS_HLINE);
waddch(mixer_widget.window, ACS_HLINE); waddch(mixer_widget.window, ACS_HLINE);
waddch(mixer_widget.window, ACS_URCORNER); waddch(mixer_widget.window, ACS_URCORNER);
for (i = 0; i < volume_height; ++i) { mvwvline(mixer_widget.window, base_y - volume_height, frame_left, ACS_VLINE, volume_height);
mvwaddch(mixer_widget.window, base_y - i - 1, frame_left, ACS_VLINE); mvwvline(mixer_widget.window, base_y - volume_height, frame_left + 3, ACS_VLINE, volume_height);
mvwaddch(mixer_widget.window, base_y - i - 1, frame_left + 3, ACS_VLINE);
}
mvwaddch(mixer_widget.window, base_y, frame_left, mvwaddch(mixer_widget.window, base_y, frame_left,
control->flags & TYPE_PSWITCH ? ACS_LTEE : ACS_LLCORNER); control->flags & TYPE_PSWITCH ? ACS_LTEE : ACS_LLCORNER);
waddch(mixer_widget.window, ACS_HLINE); waddch(mixer_widget.window, ACS_HLINE);
@ -647,10 +645,8 @@ static void display_scroll_indicators(void)
right = first_visible_control_index + visible_controls < controls_count right = first_visible_control_index + visible_controls < controls_count
? ACS_RARROW : ACS_VLINE; ? ACS_RARROW : ACS_VLINE;
wattrset(mixer_widget.window, attrs.mixer_frame); wattrset(mixer_widget.window, attrs.mixer_frame);
for (y = y0; y <= y1; ++y) { mvwvline(mixer_widget.window, y0, 0, left, y1 - y0 + 1);
mvwaddch(mixer_widget.window, y, 0, left); mvwvline(mixer_widget.window, y0, screen_cols -1, right, y1 - y0 + 1);
mvwaddch(mixer_widget.window, y, screen_cols - 1, right);
}
clickable_set(y0, 0, y1, 0, clickable_set(y0, 0, y1, 0,
CMD_WITH_ARG(CMD_MIXER_PREVIOUS, visible_controls), -1); CMD_WITH_ARG(CMD_MIXER_PREVIOUS, visible_controls), -1);
clickable_set(y0, screen_cols - 1, y1, screen_cols - 1, clickable_set(y0, screen_cols - 1, y1, screen_cols - 1,

View file

@ -51,40 +51,9 @@ static void on_handle_key(int key)
} }
} }
static bool create(void) static void create(void)
{ {
int rows, columns; menu_widget_create(&proc_widget, menu, _("Select File"));
const char *title;
if (screen_lines < 3 || screen_cols < 20) {
proc_widget.close();
beep();
return FALSE;
}
scale_menu(menu, &rows, &columns);
rows += 2;
columns += 2;
if (rows > screen_lines)
rows = screen_lines;
if (columns > screen_cols)
columns = screen_cols;
widget_init(&proc_widget, rows, columns, SCREEN_CENTER, SCREEN_CENTER,
attrs.menu, WIDGET_BORDER | WIDGET_SUBWINDOW);
title = _("Select File");
mvwprintw(proc_widget.window, 0, (columns - 2 - get_mbs_width(title)) / 2, " %s ", title);
set_menu_win(menu, proc_widget.window);
set_menu_sub(menu, proc_widget.subwindow);
return TRUE;
}
static void on_window_size_changed(void)
{
unpost_menu(menu);
if (!create())
return;
post_menu(menu);
} }
static void on_close(void) static void on_close(void)
@ -111,7 +80,7 @@ static void add_item(const char *file_name)
static struct widget proc_widget = { static struct widget proc_widget = {
.handle_key = on_handle_key, .handle_key = on_handle_key,
.window_size_changed = on_window_size_changed, .window_size_changed = create,
.close = on_close, .close = on_close,
}; };
@ -134,8 +103,5 @@ void create_proc_files_list(void)
set_menu_mark(menu, NULL); set_menu_mark(menu, NULL);
menu_opts_off(menu, O_SHOWDESC); menu_opts_off(menu, O_SHOWDESC);
if (!create()) create();
return;
post_menu(menu);
} }

View file

@ -155,8 +155,7 @@ static void update_text_lines(void)
static void update_y_scroll_bar(void) static void update_y_scroll_bar(void)
{ {
int length; int length;
int begin, end; int begin;
int i;
if (max_scroll_y <= 0 || text_lines_count == 0) if (max_scroll_y <= 0 || text_lines_count == 0)
return; return;
@ -164,17 +163,14 @@ static void update_y_scroll_bar(void)
if (length >= text_box_y) if (length >= text_box_y)
return; return;
begin = current_top * (text_box_y - length) / max_scroll_y; begin = current_top * (text_box_y - length) / max_scroll_y;
end = begin + length; mvwvline(text_widget.window, 1, text_box_x + 1, ' ', text_box_y);
for (i = 0; i < text_box_y; ++i) mvwvline(text_widget.window, begin + 1, text_box_x + 1, ACS_BOARD, length);
mvwaddch(text_widget.window, i + 1, text_box_x + 1,
i >= begin && i < end ? ACS_BOARD : ' ');
} }
static void update_x_scroll_bar(void) static void update_x_scroll_bar(void)
{ {
int length; int length;
int begin, end; int begin;
int i;
if (max_scroll_x <= 0 || max_line_width <= 0) if (max_scroll_x <= 0 || max_line_width <= 0)
return; return;
@ -182,10 +178,8 @@ static void update_x_scroll_bar(void)
if (length >= text_box_x) if (length >= text_box_x)
return; return;
begin = current_left * (text_box_x - length) / max_scroll_x; begin = current_left * (text_box_x - length) / max_scroll_x;
end = begin + length; mvwhline(text_widget.window, text_box_y + 1, 1, ' ', text_box_x);
wmove(text_widget.window, text_box_y + 1, 1); mvwhline(text_widget.window, text_box_y + 1, begin + 1, ACS_BOARD, length);
for (i = 0; i < text_box_x; ++i)
waddch(text_widget.window, i >= begin && i < end ? ACS_BOARD : ' ');
} }
static void move_x(int delta) static void move_x(int delta)

View file

@ -87,8 +87,8 @@ void widget_init(struct widget *widget, int lines_, int cols, int y, int x,
set_panel_userptr(widget->panel, widget); set_panel_userptr(widget->panel, widget);
} }
if (!widget->handle_key) //if (!widget->handle_key)
widget->handle_key = widget_handle_key; // widget->handle_key = widget_handle_key;
if (old_window) if (old_window)
delwin(old_window); delwin(old_window);