proc_files.c (3804B)
1 /* 2 * proc_files.c - shows ALSA system information files 3 * Copyright (c) Clemens Ladisch <clemens@ladisch.de> 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include "aconfig.h" 20 #include <assert.h> 21 #include <menu.h> 22 #include <unistd.h> 23 #include "gettext_curses.h" 24 #include "utils.h" 25 #include "die.h" 26 #include "mem.h" 27 #include "colors.h" 28 #include "widget.h" 29 #include "textbox.h" 30 #include "proc_files.h" 31 32 static struct widget proc_widget; 33 static ITEM *items[7]; 34 static unsigned int items_count; 35 static MENU *menu; 36 37 static void on_menu_key(int key) 38 { 39 static const struct { 40 int key; 41 int request; 42 } key_map[] = { 43 { KEY_DOWN, REQ_DOWN_ITEM }, 44 { KEY_UP, REQ_UP_ITEM }, 45 { KEY_HOME, REQ_FIRST_ITEM }, 46 { KEY_NPAGE, REQ_SCR_DPAGE }, 47 { KEY_PPAGE, REQ_SCR_UPAGE }, 48 { KEY_BEG, REQ_FIRST_ITEM }, 49 { KEY_END, REQ_LAST_ITEM }, 50 }; 51 unsigned int i; 52 53 for (i = 0; i < ARRAY_SIZE(key_map); ++i) 54 if (key_map[i].key == key) { 55 menu_driver(menu, key_map[i].request); 56 break; 57 } 58 } 59 60 static void on_handle_key(int key) 61 { 62 ITEM *item; 63 64 switch (key) { 65 case 27: 66 case KEY_CANCEL: 67 proc_widget.close(); 68 break; 69 case 10: 70 case 13: 71 case KEY_ENTER: 72 item = current_item(menu); 73 if (item) 74 show_textfile(item_name(item)); 75 break; 76 default: 77 on_menu_key(key); 78 break; 79 } 80 } 81 82 static bool create(void) 83 { 84 int rows, columns; 85 const char *title; 86 87 if (screen_lines < 3 || screen_cols < 20) { 88 proc_widget.close(); 89 beep(); 90 return FALSE; 91 } 92 scale_menu(menu, &rows, &columns); 93 rows += 2; 94 columns += 2; 95 if (rows > screen_lines) 96 rows = screen_lines; 97 if (columns > screen_cols) 98 columns = screen_cols; 99 100 widget_init(&proc_widget, rows, columns, SCREEN_CENTER, SCREEN_CENTER, 101 attr_menu, WIDGET_BORDER | WIDGET_SUBWINDOW); 102 103 title = _("Select File"); 104 mvwprintw(proc_widget.window, 0, (columns - 2 - get_mbs_width(title)) / 2, " %s ", title); 105 set_menu_win(menu, proc_widget.window); 106 set_menu_sub(menu, proc_widget.subwindow); 107 return TRUE; 108 } 109 110 static void on_window_size_changed(void) 111 { 112 unpost_menu(menu); 113 if (!create()) 114 return; 115 post_menu(menu); 116 } 117 118 static void on_close(void) 119 { 120 unsigned int i; 121 122 unpost_menu(menu); 123 free_menu(menu); 124 for (i = 0; i < items_count; ++i) 125 free_item(items[i]); 126 widget_free(&proc_widget); 127 } 128 129 static void add_item(const char *file_name) 130 { 131 if (access(file_name, F_OK) == 0) { 132 items[items_count] = new_item(file_name, NULL); 133 if (!items[items_count]) 134 fatal_error("cannot create menu item"); 135 ++items_count; 136 assert(items_count < ARRAY_SIZE(items)); 137 } 138 } 139 140 static struct widget proc_widget = { 141 .handle_key = on_handle_key, 142 .window_size_changed = on_window_size_changed, 143 .close = on_close, 144 }; 145 146 void create_proc_files_list(void) 147 { 148 items_count = 0; 149 add_item("/proc/asound/version"); 150 add_item("/proc/asound/cards"); 151 add_item("/proc/asound/devices"); 152 add_item("/proc/asound/oss/devices"); 153 add_item("/proc/asound/timers"); 154 add_item("/proc/asound/pcm"); 155 items[items_count] = NULL; 156 157 menu = new_menu(items); 158 if (!menu) 159 fatal_error("cannot create menu"); 160 set_menu_fore(menu, attr_menu_selected); 161 set_menu_back(menu, attr_menu); 162 set_menu_mark(menu, NULL); 163 menu_opts_off(menu, O_SHOWDESC); 164 165 if (!create()) 166 return; 167 168 post_menu(menu); 169 }