1c72fcc34Sopenharmony_ci/* 2c72fcc34Sopenharmony_ci * device_name_form.c - ask for sound control device name 3c72fcc34Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 4c72fcc34Sopenharmony_ci * 5c72fcc34Sopenharmony_ci * This program is free software: you can redistribute it and/or modify 6c72fcc34Sopenharmony_ci * it under the terms of the GNU General Public License as published by 7c72fcc34Sopenharmony_ci * the Free Software Foundation, either version 2 of the License, or 8c72fcc34Sopenharmony_ci * (at your option) any later version. 9c72fcc34Sopenharmony_ci * 10c72fcc34Sopenharmony_ci * This program is distributed in the hope that it will be useful, 11c72fcc34Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12c72fcc34Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13c72fcc34Sopenharmony_ci * GNU General Public License for more details. 14c72fcc34Sopenharmony_ci * 15c72fcc34Sopenharmony_ci * You should have received a copy of the GNU General Public License 16c72fcc34Sopenharmony_ci * along with this program. If not, see <http://www.gnu.org/licenses/>. 17c72fcc34Sopenharmony_ci */ 18c72fcc34Sopenharmony_ci 19c72fcc34Sopenharmony_ci#include "aconfig.h" 20c72fcc34Sopenharmony_ci#include <stdlib.h> 21c72fcc34Sopenharmony_ci#include <string.h> 22c72fcc34Sopenharmony_ci#include CURSESINC 23c72fcc34Sopenharmony_ci#include <form.h> 24c72fcc34Sopenharmony_ci#include "gettext_curses.h" 25c72fcc34Sopenharmony_ci#include "die.h" 26c72fcc34Sopenharmony_ci#include "mem.h" 27c72fcc34Sopenharmony_ci#include "utils.h" 28c72fcc34Sopenharmony_ci#include "colors.h" 29c72fcc34Sopenharmony_ci#include "widget.h" 30c72fcc34Sopenharmony_ci#include "mixer_widget.h" 31c72fcc34Sopenharmony_ci#include "card_select.h" 32c72fcc34Sopenharmony_ci#include "device_name.h" 33c72fcc34Sopenharmony_ci 34c72fcc34Sopenharmony_cistatic struct widget form_widget; 35c72fcc34Sopenharmony_cistatic FIELD *fields[3]; 36c72fcc34Sopenharmony_cistatic FORM *form; 37c72fcc34Sopenharmony_ci 38c72fcc34Sopenharmony_cistatic char *dup_current_name(void) 39c72fcc34Sopenharmony_ci{ 40c72fcc34Sopenharmony_ci int rows, cols, max, i; 41c72fcc34Sopenharmony_ci char *s; 42c72fcc34Sopenharmony_ci 43c72fcc34Sopenharmony_ci if (form_driver(form, REQ_VALIDATION) == E_OK) { 44c72fcc34Sopenharmony_ci dynamic_field_info(fields[1], &rows, &cols, &max); 45c72fcc34Sopenharmony_ci s = ccalloc(1, cols + 1); 46c72fcc34Sopenharmony_ci memcpy(s, field_buffer(fields[1], 0), cols); 47c72fcc34Sopenharmony_ci for (i = strlen(s) - 1; i >= 0 && s[i] == ' '; --i) 48c72fcc34Sopenharmony_ci s[i] = '\0'; 49c72fcc34Sopenharmony_ci return s; 50c72fcc34Sopenharmony_ci } else { 51c72fcc34Sopenharmony_ci return cstrdup(""); 52c72fcc34Sopenharmony_ci } 53c72fcc34Sopenharmony_ci} 54c72fcc34Sopenharmony_ci 55c72fcc34Sopenharmony_cistatic void on_key_enter(void) 56c72fcc34Sopenharmony_ci{ 57c72fcc34Sopenharmony_ci char *s; 58c72fcc34Sopenharmony_ci bool ok; 59c72fcc34Sopenharmony_ci 60c72fcc34Sopenharmony_ci s = dup_current_name(); 61c72fcc34Sopenharmony_ci ok = select_card_by_name(s); 62c72fcc34Sopenharmony_ci free(s); 63c72fcc34Sopenharmony_ci if (ok) { 64c72fcc34Sopenharmony_ci form_widget.close(); 65c72fcc34Sopenharmony_ci close_card_select_list(); 66c72fcc34Sopenharmony_ci } 67c72fcc34Sopenharmony_ci} 68c72fcc34Sopenharmony_ci 69c72fcc34Sopenharmony_cistatic void on_form_key(int key) 70c72fcc34Sopenharmony_ci{ 71c72fcc34Sopenharmony_ci static const struct { 72c72fcc34Sopenharmony_ci int key; 73c72fcc34Sopenharmony_ci int request; 74c72fcc34Sopenharmony_ci } key_map[] = { 75c72fcc34Sopenharmony_ci { KEY_LEFT, REQ_PREV_CHAR }, 76c72fcc34Sopenharmony_ci { KEY_RIGHT, REQ_NEXT_CHAR }, 77c72fcc34Sopenharmony_ci { KEY_HOME, REQ_BEG_FIELD }, 78c72fcc34Sopenharmony_ci { KEY_BACKSPACE, REQ_DEL_PREV }, 79c72fcc34Sopenharmony_ci { 127, REQ_DEL_PREV }, 80c72fcc34Sopenharmony_ci { KEY_DC, REQ_DEL_CHAR }, 81c72fcc34Sopenharmony_ci { KEY_BEG, REQ_BEG_FIELD }, 82c72fcc34Sopenharmony_ci { KEY_END, REQ_END_FIELD }, 83c72fcc34Sopenharmony_ci }; 84c72fcc34Sopenharmony_ci unsigned int i; 85c72fcc34Sopenharmony_ci 86c72fcc34Sopenharmony_ci if (key >= 32 && key < 256 && key != 127) { 87c72fcc34Sopenharmony_ci form_driver(form, key); 88c72fcc34Sopenharmony_ci return; 89c72fcc34Sopenharmony_ci } 90c72fcc34Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(key_map); ++i) 91c72fcc34Sopenharmony_ci if (key_map[i].key == key) { 92c72fcc34Sopenharmony_ci form_driver(form, key_map[i].request); 93c72fcc34Sopenharmony_ci break; 94c72fcc34Sopenharmony_ci } 95c72fcc34Sopenharmony_ci} 96c72fcc34Sopenharmony_ci 97c72fcc34Sopenharmony_cistatic void on_handle_key(int key) 98c72fcc34Sopenharmony_ci{ 99c72fcc34Sopenharmony_ci switch (key) { 100c72fcc34Sopenharmony_ci case 27: 101c72fcc34Sopenharmony_ci case KEY_CANCEL: 102c72fcc34Sopenharmony_ci form_widget.close(); 103c72fcc34Sopenharmony_ci break; 104c72fcc34Sopenharmony_ci case 10: 105c72fcc34Sopenharmony_ci case 13: 106c72fcc34Sopenharmony_ci case KEY_ENTER: 107c72fcc34Sopenharmony_ci on_key_enter(); 108c72fcc34Sopenharmony_ci break; 109c72fcc34Sopenharmony_ci default: 110c72fcc34Sopenharmony_ci on_form_key(key); 111c72fcc34Sopenharmony_ci break; 112c72fcc34Sopenharmony_ci } 113c72fcc34Sopenharmony_ci} 114c72fcc34Sopenharmony_ci 115c72fcc34Sopenharmony_cistatic bool create(void) 116c72fcc34Sopenharmony_ci{ 117c72fcc34Sopenharmony_ci const char *title; 118c72fcc34Sopenharmony_ci 119c72fcc34Sopenharmony_ci if (screen_lines < 6 || screen_cols < 36) { 120c72fcc34Sopenharmony_ci form_widget.close(); 121c72fcc34Sopenharmony_ci beep(); 122c72fcc34Sopenharmony_ci return FALSE; 123c72fcc34Sopenharmony_ci } 124c72fcc34Sopenharmony_ci widget_init(&form_widget, 125c72fcc34Sopenharmony_ci 6, 36, SCREEN_CENTER, SCREEN_CENTER, 126c72fcc34Sopenharmony_ci attrs.textbox, WIDGET_BORDER | WIDGET_SUBWINDOW | WIDGET_CURSOR_VISIBLE); 127c72fcc34Sopenharmony_ci title = _("Sound Card"); 128c72fcc34Sopenharmony_ci mvwprintw(form_widget.window, 0, (36 - 2 - get_mbs_width(title)) / 2, " %s ", title); 129c72fcc34Sopenharmony_ci 130c72fcc34Sopenharmony_ci set_form_win(form, form_widget.window); 131c72fcc34Sopenharmony_ci set_form_sub(form, form_widget.subwindow); 132c72fcc34Sopenharmony_ci return TRUE; 133c72fcc34Sopenharmony_ci} 134c72fcc34Sopenharmony_ci 135c72fcc34Sopenharmony_cistatic void on_window_size_changed(void) 136c72fcc34Sopenharmony_ci{ 137c72fcc34Sopenharmony_ci form_driver(form, REQ_VALIDATION); /* save field value */ 138c72fcc34Sopenharmony_ci unpost_form(form); 139c72fcc34Sopenharmony_ci 140c72fcc34Sopenharmony_ci if (!create()) 141c72fcc34Sopenharmony_ci return; 142c72fcc34Sopenharmony_ci 143c72fcc34Sopenharmony_ci /* 144c72fcc34Sopenharmony_ci * This call fails because ncurses does not allow changing options of 145c72fcc34Sopenharmony_ci * the current field, and we cannot change the current field because 146c72fcc34Sopenharmony_ci * there is only one. The only way to make this work would be to throw 147c72fcc34Sopenharmony_ci * away and recreate all fields. 148c72fcc34Sopenharmony_ci */ 149c72fcc34Sopenharmony_ci field_opts_off(fields[1], O_BLANK); 150c72fcc34Sopenharmony_ci 151c72fcc34Sopenharmony_ci post_form(form); 152c72fcc34Sopenharmony_ci} 153c72fcc34Sopenharmony_ci 154c72fcc34Sopenharmony_cistatic void on_close(void) 155c72fcc34Sopenharmony_ci{ 156c72fcc34Sopenharmony_ci unpost_form(form); 157c72fcc34Sopenharmony_ci free_form(form); 158c72fcc34Sopenharmony_ci free_field(fields[0]); 159c72fcc34Sopenharmony_ci free_field(fields[1]); 160c72fcc34Sopenharmony_ci widget_free(&form_widget); 161c72fcc34Sopenharmony_ci} 162c72fcc34Sopenharmony_ci 163c72fcc34Sopenharmony_cistatic struct widget form_widget = { 164c72fcc34Sopenharmony_ci .handle_key = on_handle_key, 165c72fcc34Sopenharmony_ci .window_size_changed = on_window_size_changed, 166c72fcc34Sopenharmony_ci .close = on_close, 167c72fcc34Sopenharmony_ci}; 168c72fcc34Sopenharmony_ci 169c72fcc34Sopenharmony_civoid create_device_name_form(void) 170c72fcc34Sopenharmony_ci{ 171c72fcc34Sopenharmony_ci fields[0] = new_field(1, 32, 1, 1, 0, 0); 172c72fcc34Sopenharmony_ci if (!fields[0]) 173c72fcc34Sopenharmony_ci fatal_error("cannot create field"); 174c72fcc34Sopenharmony_ci field_opts_off(fields[0], O_ACTIVE|O_EDIT); 175c72fcc34Sopenharmony_ci set_field_fore(fields[0], attrs.textbox); 176c72fcc34Sopenharmony_ci set_field_back(fields[0], attrs.textbox); 177c72fcc34Sopenharmony_ci set_field_buffer(fields[0], 0, _("Device name:")); 178c72fcc34Sopenharmony_ci 179c72fcc34Sopenharmony_ci fields[1] = new_field(1, 32, 2, 1, 0, 0); 180c72fcc34Sopenharmony_ci if (!fields[1]) 181c72fcc34Sopenharmony_ci fatal_error("cannot create field"); 182c72fcc34Sopenharmony_ci field_opts_off(fields[1], O_AUTOSKIP|O_NULLOK|O_STATIC); 183c72fcc34Sopenharmony_ci set_field_fore(fields[1], attrs.textfield); 184c72fcc34Sopenharmony_ci set_field_back(fields[1], attrs.textfield); 185c72fcc34Sopenharmony_ci set_field_buffer(fields[1], 0, mixer_device_name); 186c72fcc34Sopenharmony_ci 187c72fcc34Sopenharmony_ci form = new_form(fields); 188c72fcc34Sopenharmony_ci if (!form) 189c72fcc34Sopenharmony_ci fatal_error("cannot create form"); 190c72fcc34Sopenharmony_ci 191c72fcc34Sopenharmony_ci if (!create()) 192c72fcc34Sopenharmony_ci return; 193c72fcc34Sopenharmony_ci 194c72fcc34Sopenharmony_ci post_form(form); 195c72fcc34Sopenharmony_ci} 196