1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
|
/*
* mixer_widget.c - mixer widget and keys handling
* Copyright (c) 1998,1999 Tim Janik
* Jaroslav Kysela <perex@perex.cz>
* Copyright (c) 2009 Clemens Ladisch <clemens@ladisch.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "aconfig.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <alsa/asoundlib.h>
#include "gettext_curses.h"
#include "version.h"
#include "utils.h"
#include "die.h"
#include "mem.h"
#include "colors.h"
#include "widget.h"
#include "textbox.h"
#include "proc_files.h"
#include "card_select.h"
#include "volume_mapping.h"
#include "mixer_controls.h"
#include "mixer_display.h"
#include "mixer_widget.h"
snd_mixer_t *mixer;
char *mixer_device_name;
bool unplugged;
struct widget mixer_widget;
enum view_mode view_mode;
int focus_control_index;
snd_mixer_selem_id_t *current_selem_id;
unsigned int current_control_flags;
bool controls_changed;
enum channel_mask {
LEFT = 1,
RIGHT = 2,
};
static int elem_callback(snd_mixer_elem_t *elem, unsigned int mask)
{
unsigned int i;
if (mask & (SND_CTL_EVENT_MASK_REMOVE |
SND_CTL_EVENT_MASK_INFO |
SND_CTL_EVENT_MASK_VALUE))
controls_changed = TRUE;
if (mask & SND_CTL_EVENT_MASK_INFO)
for (i = 0; i < controls_count; ++i)
if (controls[i].elem == elem) {
controls[i].flags &= ~IS_ACTIVE;
if (snd_mixer_selem_is_active(controls[i].elem))
controls[i].flags |= IS_ACTIVE;
}
return 0;
}
static int mixer_callback(snd_mixer_t *mixer, unsigned int mask, snd_mixer_elem_t *elem)
{
if (mask & SND_CTL_EVENT_MASK_ADD) {
snd_mixer_elem_set_callback(elem, elem_callback);
controls_changed = TRUE;
}
return 0;
}
void create_mixer_object(struct snd_mixer_selem_regopt *selem_regopt)
{
int err;
err = snd_mixer_open(&mixer, 0);
if (err < 0)
fatal_alsa_error(_("cannot open mixer"), err);
mixer_device_name = cstrdup(selem_regopt->device);
err = snd_mixer_selem_register(mixer, selem_regopt, NULL);
if (err < 0)
fatal_alsa_error(_("cannot open mixer"), err);
snd_mixer_set_callback(mixer, mixer_callback);
err = snd_mixer_load(mixer);
if (err < 0)
fatal_alsa_error(_("cannot load mixer controls"), err);
err = snd_mixer_selem_id_malloc(¤t_selem_id);
if (err < 0)
fatal_error("out of memory");
}
static void set_view_mode(enum view_mode m)
{
view_mode = m;
create_controls();
}
static void close_hctl(void)
{
free_controls();
if (mixer_device_name) {
snd_mixer_detach(mixer, mixer_device_name);
free(mixer_device_name);
mixer_device_name = NULL;
}
}
static void check_unplugged(void)
{
snd_hctl_t *hctl;
snd_ctl_t *ctl;
unsigned int state;
int err;
unplugged = FALSE;
if (mixer_device_name) {
err = snd_mixer_get_hctl(mixer, mixer_device_name, &hctl);
if (err >= 0) {
ctl = snd_hctl_ctl(hctl);
/* just any random function that does an ioctl() */
err = snd_ctl_get_power_state(ctl, &state);
if (err == -ENODEV)
unplugged = TRUE;
}
}
}
void close_mixer_device(void)
{
check_unplugged();
close_hctl();
display_card_info();
set_view_mode(view_mode);
}
bool select_card_by_name(const char *device_name)
{
int err;
bool opened;
char *msg;
close_hctl();
unplugged = FALSE;
opened = FALSE;
if (device_name) {
err = snd_mixer_attach(mixer, device_name);
if (err >= 0)
opened = TRUE;
else {
msg = casprintf(_("Cannot open mixer device '%s'."), device_name);
show_alsa_error(msg, err);
free(msg);
}
}
if (opened) {
mixer_device_name = cstrdup(device_name);
err = snd_mixer_load(mixer);
if (err < 0)
fatal_alsa_error(_("cannot load mixer controls"), err);
}
display_card_info();
set_view_mode(view_mode);
return opened;
}
static void show_help(void)
{
const char *help[] = {
_("Esc Exit"),
_("F1 ? H Help"),
_("F2 / System information"),
_("F3 Show playback controls"),
_("F4 Show capture controls"),
_("F5 Show all controls"),
_("Tab Toggle view mode (F3/F4/F5)"),
_("F6 S Select sound card"),
_("L Redraw screen"),
"",
_("Left Move to the previous control"),
_("Right Move to the next control"),
"",
_("Up/Down Change volume"),
_("+ - Change volume"),
_("Page Up/Dn Change volume in big steps"),
_("End Set volume to 0%"),
_("0-9 Set volume to 0%-90%"),
_("Q W E Increase left/both/right volumes"),
/* TRANSLATORS: or Y instead of Z */
_("Z X C Decrease left/both/right volumes"),
_("B Balance left and right volumes"),
"",
_("M Toggle mute"),
/* TRANSLATORS: or , . */
_("< > Toggle left/right mute"),
"",
_("Space Toggle capture"),
/* TRANSLATORS: or Insert Delete */
_("; ' Toggle left/right capture"),
"",
_("Authors:"),
_(" Tim Janik"),
_(" Jaroslav Kysela <perex@perex.cz>"),
_(" Clemens Ladisch <clemens@ladisch.de>"),
};
show_text(help, ARRAY_SIZE(help), _("Help"));
}
void refocus_control(void)
{
if (focus_control_index < controls_count) {
snd_mixer_selem_get_id(controls[focus_control_index].elem, current_selem_id);
current_control_flags = controls[focus_control_index].flags;
}
display_controls();
}
static struct control *get_focus_control(unsigned int type)
{
if (focus_control_index >= 0 &&
focus_control_index < controls_count &&
(controls[focus_control_index].flags & IS_ACTIVE) &&
(controls[focus_control_index].flags & type))
return &controls[focus_control_index];
else
return NULL;
}
static void change_enum_to_percent(struct control *control, int value)
{
unsigned int i;
unsigned int index;
unsigned int new_index;
int items;
int err;
i = ffs(control->enum_channel_bits) - 1;
err = snd_mixer_selem_get_enum_item(control->elem, i, &index);
if (err < 0)
return;
new_index = index;
if (value == 0) {
new_index = 0;
} else if (value == 100) {
items = snd_mixer_selem_get_enum_items(control->elem);
if (items < 1)
return;
new_index = items - 1;
}
if (new_index == index)
return;
for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
if (control->enum_channel_bits & (1 << i))
snd_mixer_selem_set_enum_item(control->elem, i, new_index);
}
static void change_enum_relative(struct control *control, int delta)
{
int items;
unsigned int i;
unsigned int index;
int new_index;
int err;
items = snd_mixer_selem_get_enum_items(control->elem);
if (items < 1)
return;
err = snd_mixer_selem_get_enum_item(control->elem, 0, &index);
if (err < 0)
return;
new_index = (int)index + delta;
if (new_index < 0)
new_index = 0;
else if (new_index >= items)
new_index = items - 1;
if (new_index == index)
return;
for (i = 0; i <= SND_MIXER_SCHN_LAST; ++i)
if (control->enum_channel_bits & (1 << i))
snd_mixer_selem_set_enum_item(control->elem, i, new_index);
}
static void change_volume_to_percent(struct control *control, int value, unsigned int channels)
{
int (*set_func)(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t, double, int);
if (!(control->flags & HAS_VOLUME_1))
channels = LEFT;
if (control->flags & TYPE_PVOLUME)
set_func = set_normalized_playback_volume;
else
set_func = set_normalized_capture_volume;
if (channels & LEFT)
set_func(control->elem, control->volume_channels[0], value / 100.0, 0);
if (channels & RIGHT)
set_func(control->elem, control->volume_channels[1], value / 100.0, 0);
}
static double clamp_volume(double v)
{
if (v < 0)
return 0;
if (v > 1)
return 1;
return v;
}
static void change_volume_relative(struct control *control, int delta, unsigned int channels)
{
double (*get_func)(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t);
int (*set_func)(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t, double, int);
double left, right;
int dir;
if (!(control->flags & HAS_VOLUME_1))
channels = LEFT;
if (control->flags & TYPE_PVOLUME) {
get_func = get_normalized_playback_volume;
set_func = set_normalized_playback_volume;
} else {
get_func = get_normalized_capture_volume;
set_func = set_normalized_capture_volume;
}
if (channels & LEFT)
left = get_func(control->elem, control->volume_channels[0]);
if (channels & RIGHT)
right = get_func(control->elem, control->volume_channels[1]);
dir = delta > 0 ? 1 : -1;
if (channels & LEFT) {
left = clamp_volume(left + delta / 100.0);
set_func(control->elem, control->volume_channels[0], left, dir);
}
if (channels & RIGHT) {
right = clamp_volume(right + delta / 100.0);
set_func(control->elem, control->volume_channels[1], right, dir);
}
}
static void change_control_to_percent(int value, unsigned int channels)
{
struct control *control;
control = get_focus_control(TYPE_PVOLUME | TYPE_CVOLUME | TYPE_ENUM);
if (!control)
return;
if (control->flags & TYPE_ENUM)
change_enum_to_percent(control, value);
else
change_volume_to_percent(control, value, channels);
display_controls();
}
static void change_control_relative(int delta, unsigned int channels)
{
struct control *control;
control = get_focus_control(TYPE_PVOLUME | TYPE_CVOLUME | TYPE_ENUM);
if (!control)
return;
if (control->flags & TYPE_ENUM)
change_enum_relative(control, delta);
else
change_volume_relative(control, delta, channels);
display_controls();
}
static void toggle_switches(unsigned int type, unsigned int channels)
{
struct control *control;
unsigned int switch_1_mask;
int (*get_func)(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t, int *);
int (*set_func)(snd_mixer_elem_t *, snd_mixer_selem_channel_id_t, int);
snd_mixer_selem_channel_id_t channel_ids[2];
int left, right;
int err;
control = get_focus_control(type);
if (!control)
return;
if (type == TYPE_PSWITCH) {
switch_1_mask = HAS_PSWITCH_1;
get_func = snd_mixer_selem_get_playback_switch;
set_func = snd_mixer_selem_set_playback_switch;
channel_ids[0] = control->pswitch_channels[0];
channel_ids[1] = control->pswitch_channels[1];
} else {
switch_1_mask = HAS_CSWITCH_1;
get_func = snd_mixer_selem_get_capture_switch;
set_func = snd_mixer_selem_set_capture_switch;
channel_ids[0] = control->cswitch_channels[0];
channel_ids[1] = control->cswitch_channels[1];
}
if (!(control->flags & switch_1_mask))
channels = LEFT;
if (channels & LEFT) {
err = get_func(control->elem, channel_ids[0], &left);
if (err < 0)
return;
}
if (channels & RIGHT) {
err = get_func(control->elem, channel_ids[1], &right);
if (err < 0)
return;
}
if (channels & LEFT)
set_func(control->elem, channel_ids[0], !left);
if (channels & RIGHT)
set_func(control->elem, channel_ids[1], !right);
display_controls();
}
static void toggle_mute(unsigned int channels)
{
toggle_switches(TYPE_PSWITCH, channels);
}
static void toggle_capture(unsigned int channels)
{
toggle_switches(TYPE_CSWITCH, channels);
}
static void balance_volumes(void)
{
struct control *control;
double left, right;
int err;
control = get_focus_control(TYPE_PVOLUME | TYPE_CVOLUME);
if (!control || !(control->flags & HAS_VOLUME_1))
return;
if (control->flags & TYPE_PVOLUME) {
left = get_normalized_playback_volume(control->elem, control->volume_channels[0]);
right = get_normalized_playback_volume(control->elem, control->volume_channels[1]);
} else {
left = get_normalized_capture_volume(control->elem, control->volume_channels[0]);
right = get_normalized_capture_volume(control->elem, control->volume_channels[1]);
}
left = (left + right) / 2;
if (control->flags & TYPE_PVOLUME) {
set_normalized_playback_volume(control->elem, control->volume_channels[0], left, 0);
set_normalized_playback_volume(control->elem, control->volume_channels[1], left, 0);
} else {
set_normalized_capture_volume(control->elem, control->volume_channels[0], left, 0);
set_normalized_capture_volume(control->elem, control->volume_channels[1], left, 0);
}
display_controls();
}
static void on_handle_key(int key)
{
switch (key) {
case 27:
case KEY_CANCEL:
case KEY_F(10):
mixer_widget.close();
break;
case KEY_F(1):
case KEY_HELP:
case 'H':
case 'h':
case '?':
show_help();
break;
case KEY_F(2):
case '/':
create_proc_files_list();
break;
case KEY_F(3):
set_view_mode(VIEW_MODE_PLAYBACK);
break;
case KEY_F(4):
set_view_mode(VIEW_MODE_CAPTURE);
break;
case KEY_F(5):
set_view_mode(VIEW_MODE_ALL);
break;
case '\t':
set_view_mode((enum view_mode)((view_mode + 1) % VIEW_MODE_COUNT));
break;
case KEY_F(6):
case 'S':
case 's':
create_card_select_list();
break;
case KEY_REFRESH:
case 12:
case 'L':
case 'l':
clearok(mixer_widget.window, TRUE);
display_controls();
break;
case KEY_LEFT:
case 'P':
case 'p':
if (focus_control_index > 0) {
--focus_control_index;
refocus_control();
}
break;
case KEY_RIGHT:
case 'N':
case 'n':
if (focus_control_index < controls_count - 1) {
++focus_control_index;
refocus_control();
}
break;
case KEY_PPAGE:
change_control_relative(5, LEFT | RIGHT);
break;
case KEY_NPAGE:
change_control_relative(-5, LEFT | RIGHT);
break;
#if 0
case KEY_BEG:
case KEY_HOME:
change_control_to_percent(100, LEFT | RIGHT);
break;
#endif
case KEY_LL:
case KEY_END:
change_control_to_percent(0, LEFT | RIGHT);
break;
case KEY_UP:
case '+':
case 'K':
case 'k':
case 'W':
case 'w':
change_control_relative(1, LEFT | RIGHT);
break;
case KEY_DOWN:
case '-':
case 'J':
case 'j':
case 'X':
case 'x':
change_control_relative(-1, LEFT | RIGHT);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
change_control_to_percent((key - '0') * 10, LEFT | RIGHT);
break;
case 'Q':
case 'q':
change_control_relative(1, LEFT);
break;
case 'Y':
case 'y':
case 'Z':
case 'z':
change_control_relative(-1, LEFT);
break;
case 'E':
case 'e':
change_control_relative(1, RIGHT);
break;
case 'C':
case 'c':
change_control_relative(-1, RIGHT);
break;
case 'M':
case 'm':
toggle_mute(LEFT | RIGHT);
break;
case 'B':
case 'b':
case '=':
balance_volumes();
break;
case '<':
case ',':
toggle_mute(LEFT);
break;
case '>':
case '.':
toggle_mute(RIGHT);
break;
case ' ':
toggle_capture(LEFT | RIGHT);
break;
case KEY_IC:
case ';':
toggle_capture(LEFT);
break;
case KEY_DC:
case '\'':
toggle_capture(RIGHT);
break;
}
}
static void create(void)
{
static const char title[] = " AlsaMixer v" SND_UTIL_VERSION_STR " ";
widget_init(&mixer_widget, screen_lines, screen_cols, 0, 0,
attr_mixer_frame, WIDGET_BORDER);
if (screen_cols >= (sizeof(title) - 1) + 2) {
wattrset(mixer_widget.window, attr_mixer_active);
mvwaddstr(mixer_widget.window, 0, (screen_cols - (sizeof(title) - 1)) / 2, title);
}
init_mixer_layout();
display_card_info();
set_view_mode(view_mode);
}
static void on_window_size_changed(void)
{
create();
}
static void on_close(void)
{
widget_free(&mixer_widget);
}
void mixer_shutdown(void)
{
free_controls();
if (mixer)
snd_mixer_close(mixer);
if (current_selem_id)
snd_mixer_selem_id_free(current_selem_id);
}
struct widget mixer_widget = {
.handle_key = on_handle_key,
.window_size_changed = on_window_size_changed,
.close = on_close,
};
void create_mixer_widget(void)
{
create();
}
|