2
* card_select.c - select a card by list or device name
3
* Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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.
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.
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/>.
23
#include <alsa/asoundlib.h>
25
#include "gettext_curses.h"
31
#include "mixer_widget.h"
32
#include "device_name.h"
33
#include "card_select.h"
42
static struct widget list_widget;
43
static struct card first_card;
46
static ITEM *initial_item;
48
static void on_key_enter(void)
50
ITEM *item = current_item(menu);
52
struct card *card = item_userptr(item);
53
if (card->device_name) {
54
if (select_card_by_name(card->device_name))
57
create_device_name_form();
62
static void on_menu_key(int key)
68
{ KEY_DOWN, REQ_DOWN_ITEM },
69
{ KEY_UP, REQ_UP_ITEM },
70
{ KEY_HOME, REQ_FIRST_ITEM },
71
{ KEY_NPAGE, REQ_SCR_DPAGE },
72
{ KEY_PPAGE, REQ_SCR_UPAGE },
73
{ KEY_BEG, REQ_FIRST_ITEM },
74
{ KEY_END, REQ_LAST_ITEM },
78
for (i = 0; i < ARRAY_SIZE(key_map); ++i)
79
if (key_map[i].key == key) {
80
menu_driver(menu, key_map[i].request);
85
static void on_handle_key(int key)
105
static bool create(void)
110
if (screen_lines < 3 || screen_cols < 10) {
115
scale_menu(menu, &rows, &columns);
118
if (rows > screen_lines)
120
if (columns > screen_cols)
121
columns = screen_cols;
123
widget_init(&list_widget, rows, columns, SCREEN_CENTER, SCREEN_CENTER,
124
attr_menu, WIDGET_BORDER | WIDGET_SUBWINDOW);
126
title = _("Sound Card");
127
mvwprintw(list_widget.window, 0, (columns - 2 - get_mbs_width(title)) / 2, " %s ", title);
128
set_menu_win(menu, list_widget.window);
129
set_menu_sub(menu, list_widget.subwindow);
133
static void on_window_size_changed(void)
141
static void on_close(void)
144
struct card *card, *next_card;
148
for (i = 0; items[i]; ++i)
151
for (card = first_card.next; card; card = next_card) {
152
next_card = card->next;
153
free(card->indexstr);
155
free(card->device_name);
158
widget_free(&list_widget);
161
void close_card_select_list(void)
166
static struct widget list_widget = {
167
.handle_key = on_handle_key,
168
.window_size_changed = on_window_size_changed,
172
static int get_cards(void)
174
int count, number, err;
176
snd_ctl_card_info_t *info;
178
struct card *card, *prev_card;
180
first_card.indexstr = "-";
181
first_card.name = _("(default)");
182
first_card.device_name = "default";
185
snd_ctl_card_info_alloca(&info);
186
prev_card = &first_card;
189
err = snd_card_next(&number);
191
fatal_alsa_error(_("cannot enumerate sound cards"), err);
194
sprintf(buf, "hw:%d", number);
195
err = snd_ctl_open(&ctl, buf, 0);
198
err = snd_ctl_card_info(ctl, info);
202
card = ccalloc(1, sizeof *card);
203
sprintf(buf, "%d", number);
204
card->indexstr = cstrdup(buf);
205
card->name = cstrdup(snd_ctl_card_info_get_name(info));
206
sprintf(buf, "hw:%d", number);
207
card->device_name = cstrdup(buf);
208
prev_card->next = card;
213
card = ccalloc(1, sizeof *card);
214
card->indexstr = cstrdup(" ");
215
card->name = cstrdup(_("enter device name..."));
216
prev_card->next = card;
222
static void create_list_items(int cards)
229
items = ccalloc(cards + 1, sizeof(ITEM*));
231
for (card = &first_card; card; card = card->next) {
232
item = new_item(card->indexstr, card->name);
234
fatal_error("cannot create menu item");
235
set_item_userptr(item, card);
239
(!card->device_name ||
240
!strcmp(card->device_name, mixer_device_name)))
246
void create_card_select_list(void)
251
create_list_items(cards);
253
menu = new_menu(items);
255
fatal_error("cannot create menu");
256
set_menu_fore(menu, attr_menu_selected);
257
set_menu_back(menu, attr_menu);
258
set_menu_mark(menu, NULL);
260
set_current_item(menu, initial_item);
261
set_menu_spacing(menu, 2, 1, 1);
262
menu_opts_on(menu, O_SHOWDESC);