~ubuntu-branches/ubuntu/natty/alsa-utils/natty

« back to all changes in this revision

Viewing changes to alsamixer/mixer_widget.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich, Daniel T Chen
  • Date: 2009-11-06 11:29:35 UTC
  • mfrom: (2.3.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091106112935-stzy6l7u9auw6hm0
Tags: 1.0.21-1ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/init:
    + wait until /usr/bin and /var/lib/alsa exist
    + only display an error when dealing with alsactl if there is no card
      specified
    + Set sane level for 'Speaker' and 'Headphone', needed for Dell Mini 9
      and Dell E series
    + ute PC Beep on hda cards that support it during initial volume setup
    + update lsb header to indicate no running of the script unless the
      udev rule is run
    + Mute *Analog/Digital Control for Creative cards by default
    + Default Digital Input Source to be Digital Mic 1 so that users
      with digital mic will be able to use it out of the box
    + Make use of lsb-functions/log calls
    + Mute "IEC958 Optical Raw" by default (LP: #408370)
  - debian/rules:
    + ship udev rules file in /lib/udev/rules.d
    + Do not install start symlinks for the alsa-utils init script, it gets
      run from a udev rule
  - debian/udev.script: do not use hotplug functions
  - debian/README.init.cs4236: Include in /usr/share/doc/alsa-utils so that
    users of snd-cs4236 (e.g., ThinkPad 600) can have audible sound
  - debian/patches/unset_pulse_internal.patch: We don't want alsamixer to
    show the pulse mixer by default, since it can be controlled from
    pulseaudio itself.
  - debian/patches/fix_misspelling_speaker-test_man_page.patch: Fix
    misspelling in speaker-test(1)
  - Set sane level for headphone 1 for Dell Studio XPS with 2.6.30.
  - Remove alsaconf from build system and remove po files

[ Daniel T Chen ]
"The beginning of the great initscript-sectomy"

* debian/alsa-mixer-save.upstart: Create an upstart job specifically
  saving mixer levels to resolve race (LP: #454265)
* debian/control: Version build-dep to upstart-aware debhelper.
* debian/init:
  + Revert all initscript changes in 1.0.20-2ubuntu[456]. They were
    crackful.
  + Restore more sane behavior/compatibility with 8.10 by not mucking
    with sound card state if alsactl restore fails.
  + Don't wait for 1 second after alsactl store, which already is
    expensive. Also, if store is going to fail, this wait is useless.
* debian/preinst: Handle upgrades from upstart-unaware versions.
* debian/rules: Move the former initscript into /sbin. We now have an
  upstart job just for handling alsactl store.
* debian/NOTES:
  debian/README.Debian:
  debian/modprobe-post-install-part:
  debian/udev.script: Use the new script path.

Show diffs side-by-side

added added

removed removed

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