From 0657b2a55961def14a3b288f8aad5f445ac285cd Mon Sep 17 00:00:00 2001 From: braph Date: Thu, 3 Oct 2019 20:18:23 +0200 Subject: [PATCH] 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 Signed-off-by: Jaroslav Kysela --- alsamixer/card_select.c | 57 ++++++--------------------------------- alsamixer/device_name.c | 10 +++---- alsamixer/menu_widget.c | 29 ++++++++++++++++++++ alsamixer/menu_widget.h | 1 + alsamixer/mixer_display.c | 12 +++------ alsamixer/proc_files.c | 42 +++-------------------------- alsamixer/textbox.c | 18 +++++-------- alsamixer/widget.c | 4 +-- 8 files changed, 58 insertions(+), 115 deletions(-) diff --git a/alsamixer/card_select.c b/alsamixer/card_select.c index c5fb727..a58c037 100644 --- a/alsamixer/card_select.c +++ b/alsamixer/card_select.c @@ -72,43 +72,12 @@ static void on_handle_key(int key) } } -static bool create(void) +static void create(void) { - int rows, columns; - 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; + menu_widget_create(&list_widget, menu, _("Sound Card")); } -static void on_window_size_changed(void) -{ - unpost_menu(menu); - if (!create()) - return; - post_menu(menu); -} - -static void on_close(void) +void close_card_select_list(void) { unsigned int i; struct card *card, *next_card; @@ -128,15 +97,10 @@ static void on_close(void) widget_free(&list_widget); } -void close_card_select_list(void) -{ - on_close(); -} - static struct widget list_widget = { .handle_key = on_handle_key, - .window_size_changed = on_window_size_changed, - .close = on_close, + .window_size_changed = create, + .close = close_card_select_list, }; static int get_cards(void) @@ -170,11 +134,9 @@ static int get_cards(void) if (err < 0) continue; 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->indexstr = cstrdup(buf + 3); + card->name = cstrdup(snd_ctl_card_info_get_name(info)); prev_card->next = card; prev_card = card; ++count; @@ -231,8 +193,5 @@ void create_card_select_list(void) set_menu_spacing(menu, 2, 1, 1); menu_opts_on(menu, O_SHOWDESC); - if (!create()) - return; - - post_menu(menu); + create(); } diff --git a/alsamixer/device_name.c b/alsamixer/device_name.c index e796de4..26b03d5 100644 --- a/alsamixer/device_name.c +++ b/alsamixer/device_name.c @@ -76,13 +76,14 @@ static void on_form_key(int key) { KEY_RIGHT, REQ_NEXT_CHAR }, { KEY_HOME, REQ_BEG_FIELD }, { KEY_BACKSPACE, REQ_DEL_PREV }, + { 127, REQ_DEL_PREV }, { KEY_DC, REQ_DEL_CHAR }, { KEY_BEG, REQ_BEG_FIELD }, { KEY_END, REQ_END_FIELD }, }; unsigned int i; - if (key >= 32 && key < 256) { + if (key >= 32 && key < 256 && key != 127) { form_driver(form, key); return; } @@ -170,8 +171,7 @@ void create_device_name_form(void) fields[0] = new_field(1, 32, 1, 1, 0, 0); if (!fields[0]) fatal_error("cannot create field"); - field_opts_off(fields[0], O_ACTIVE); - field_opts_off(fields[0], O_EDIT); + field_opts_off(fields[0], O_ACTIVE|O_EDIT); set_field_fore(fields[0], attrs.textbox); set_field_back(fields[0], attrs.textbox); 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); if (!fields[1]) fatal_error("cannot create field"); - field_opts_off(fields[1], O_AUTOSKIP); - field_opts_off(fields[1], O_NULLOK); - field_opts_off(fields[1], O_STATIC); + field_opts_off(fields[1], O_AUTOSKIP|O_NULLOK|O_STATIC); set_field_fore(fields[1], attrs.textfield); set_field_back(fields[1], attrs.textfield); set_field_buffer(fields[1], 0, mixer_device_name); diff --git a/alsamixer/menu_widget.c b/alsamixer/menu_widget.c index 30940a7..bf5ed52 100644 --- a/alsamixer/menu_widget.c +++ b/alsamixer/menu_widget.c @@ -61,3 +61,32 @@ int menu_widget_handle_key(MENU *menu, int key) 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); +} diff --git a/alsamixer/menu_widget.h b/alsamixer/menu_widget.h index ad3ed0e..e8e8a1e 100644 --- a/alsamixer/menu_widget.h +++ b/alsamixer/menu_widget.h @@ -5,5 +5,6 @@ #include int menu_widget_handle_key(MENU *menu, int key); +void menu_widget_create(struct widget *widget, MENU *menu, const char *title); #endif diff --git a/alsamixer/mixer_display.c b/alsamixer/mixer_display.c index 947b6a9..882781d 100644 --- a/alsamixer/mixer_display.c +++ b/alsamixer/mixer_display.c @@ -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_URCORNER); - for (i = 0; i < volume_height; ++i) { - mvwaddch(mixer_widget.window, base_y - i - 1, frame_left, ACS_VLINE); - mvwaddch(mixer_widget.window, base_y - i - 1, frame_left + 3, ACS_VLINE); - } + mvwvline(mixer_widget.window, base_y - volume_height, frame_left, ACS_VLINE, volume_height); + mvwvline(mixer_widget.window, base_y - volume_height, frame_left + 3, ACS_VLINE, volume_height); mvwaddch(mixer_widget.window, base_y, frame_left, control->flags & TYPE_PSWITCH ? ACS_LTEE : ACS_LLCORNER); 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 ? ACS_RARROW : ACS_VLINE; wattrset(mixer_widget.window, attrs.mixer_frame); - for (y = y0; y <= y1; ++y) { - mvwaddch(mixer_widget.window, y, 0, left); - mvwaddch(mixer_widget.window, y, screen_cols - 1, right); - } + mvwvline(mixer_widget.window, y0, 0, left, y1 - y0 + 1); + mvwvline(mixer_widget.window, y0, screen_cols -1, right, y1 - y0 + 1); clickable_set(y0, 0, y1, 0, CMD_WITH_ARG(CMD_MIXER_PREVIOUS, visible_controls), -1); clickable_set(y0, screen_cols - 1, y1, screen_cols - 1, diff --git a/alsamixer/proc_files.c b/alsamixer/proc_files.c index 147fbe4..0ca003b 100644 --- a/alsamixer/proc_files.c +++ b/alsamixer/proc_files.c @@ -51,40 +51,9 @@ static void on_handle_key(int key) } } -static bool create(void) +static void create(void) { - int rows, columns; - 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); + menu_widget_create(&proc_widget, menu, _("Select File")); } static void on_close(void) @@ -111,7 +80,7 @@ static void add_item(const char *file_name) static struct widget proc_widget = { .handle_key = on_handle_key, - .window_size_changed = on_window_size_changed, + .window_size_changed = create, .close = on_close, }; @@ -134,8 +103,5 @@ void create_proc_files_list(void) set_menu_mark(menu, NULL); menu_opts_off(menu, O_SHOWDESC); - if (!create()) - return; - - post_menu(menu); + create(); } diff --git a/alsamixer/textbox.c b/alsamixer/textbox.c index d85df0e..ebac9fa 100644 --- a/alsamixer/textbox.c +++ b/alsamixer/textbox.c @@ -155,8 +155,7 @@ static void update_text_lines(void) static void update_y_scroll_bar(void) { int length; - int begin, end; - int i; + int begin; if (max_scroll_y <= 0 || text_lines_count == 0) return; @@ -164,17 +163,14 @@ static void update_y_scroll_bar(void) if (length >= text_box_y) return; begin = current_top * (text_box_y - length) / max_scroll_y; - end = begin + length; - for (i = 0; i < text_box_y; ++i) - mvwaddch(text_widget.window, i + 1, text_box_x + 1, - i >= begin && i < end ? ACS_BOARD : ' '); + mvwvline(text_widget.window, 1, text_box_x + 1, ' ', text_box_y); + mvwvline(text_widget.window, begin + 1, text_box_x + 1, ACS_BOARD, length); } static void update_x_scroll_bar(void) { int length; - int begin, end; - int i; + int begin; if (max_scroll_x <= 0 || max_line_width <= 0) return; @@ -182,10 +178,8 @@ static void update_x_scroll_bar(void) if (length >= text_box_x) return; begin = current_left * (text_box_x - length) / max_scroll_x; - end = begin + length; - wmove(text_widget.window, text_box_y + 1, 1); - for (i = 0; i < text_box_x; ++i) - waddch(text_widget.window, i >= begin && i < end ? ACS_BOARD : ' '); + mvwhline(text_widget.window, text_box_y + 1, 1, ' ', text_box_x); + mvwhline(text_widget.window, text_box_y + 1, begin + 1, ACS_BOARD, length); } static void move_x(int delta) diff --git a/alsamixer/widget.c b/alsamixer/widget.c index 6f1644a..17f3ace 100644 --- a/alsamixer/widget.c +++ b/alsamixer/widget.c @@ -87,8 +87,8 @@ void widget_init(struct widget *widget, int lines_, int cols, int y, int x, set_panel_userptr(widget->panel, widget); } - if (!widget->handle_key) - widget->handle_key = widget_handle_key; + //if (!widget->handle_key) + // widget->handle_key = widget_handle_key; if (old_window) delwin(old_window);