vol.c (2400B)
1 #include <stddef.h> 2 #ifdef __linux__ 3 #include <alsa/asoundlib.h> 4 #endif /* __linux__ */ 5 #include "status2d.h" 6 7 void 8 draw_on(void) { 9 s2d_rect(11, 3, 1, 3); 10 s2d_rect(2, 4, 3, 1); 11 s2d_rect(9, 4, 1, 2); 12 s2d_rect(1, 5, 1, 5); 13 s2d_rect(5, 5, 1, 5); 14 s2d_rect(7, 5, 1, 1); 15 s2d_rect(8, 6, 1, 3); 16 s2d_rect(10, 6, 1, 3); 17 s2d_rect(12, 6, 1, 3); 18 s2d_rect(7, 9, 1, 1); 19 s2d_rect(9, 9, 1, 2); 20 s2d_rect(11, 9, 1, 3); 21 s2d_rect(2, 10, 4, 1); 22 }; 23 24 void 25 draw_off(void) { 26 s2d_rect(2, 4, 4, 1); 27 s2d_rect(1, 5, 1, 5); 28 s2d_rect(5, 5, 1, 5); 29 s2d_rect(7, 5, 1, 1); 30 s2d_rect(11, 5, 1, 1); 31 s2d_rect(8, 6, 1, 1); 32 s2d_rect(10, 6, 1, 1); 33 s2d_rect(9, 7, 1, 1); 34 s2d_rect(8, 8, 1, 1); 35 s2d_rect(10, 8, 1, 1); 36 s2d_rect(7, 9, 1, 1); 37 s2d_rect(11, 9, 1, 1); 38 s2d_rect(2, 10, 4, 1); 39 } 40 41 int 42 main(void) { 43 int on = 0, percent = 0; 44 int ret; 45 #ifdef __linux__ 46 long min, max, vol; 47 snd_mixer_t *handle; 48 snd_mixer_elem_t *elem; 49 snd_mixer_selem_id_t *sid; 50 51 if ((ret = snd_mixer_open(&handle, 0)) < 0) { 52 s2d_print("could not open mixer: %s\n", snd_strerror(ret)); 53 s2d_finish(); 54 return 1; 55 } 56 if ((ret = snd_mixer_attach(handle, "default")) < 0) { 57 s2d_print("could not attach mixer: %s\n", snd_strerror(ret)); 58 s2d_finish(); 59 snd_mixer_close(handle); 60 return 1; 61 } 62 if ((ret = snd_mixer_selem_register(handle, NULL, NULL)) < 0) { 63 s2d_print("could not register element: %s\n", snd_strerror(ret)); 64 s2d_finish(); 65 snd_mixer_close(handle); 66 return 1; 67 } 68 if ((ret = snd_mixer_load(handle)) < 0) { 69 s2d_print("could not load mixer: %s\n", snd_strerror(ret)); 70 s2d_finish(); 71 snd_mixer_close(handle); 72 return 1; 73 } 74 snd_mixer_selem_id_malloc(&sid); 75 snd_mixer_selem_id_set_index(sid, 0); 76 snd_mixer_selem_id_set_name(sid, "Master"); 77 if (!(elem = snd_mixer_find_selem(handle, sid))) { 78 s2d_print("could not find element 'Master'"); 79 s2d_finish(); 80 snd_mixer_close(handle); 81 return 1; 82 } 83 snd_mixer_selem_get_playback_volume_range(elem, &min, &max); 84 snd_mixer_selem_get_playback_volume(elem, 0, &vol); 85 snd_mixer_selem_get_playback_switch(elem, 0, &on); 86 percent = 100 * vol / max; 87 snd_mixer_selem_id_free(sid); 88 snd_mixer_close(handle); 89 #else 90 s2d_print("no handler for this OS"); 91 s2d_finish(); 92 #endif 93 if (on) 94 draw_on(); 95 else 96 draw_off(); 97 s2d_forward(-1); 98 s2d_forward(2); 99 s2d_bg(BRBG); 100 s2d_bar(0, 2, 3, bar_height - 4, 1, percent); 101 s2d_finish(); 102 }