volume.c (1637B)
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #include "../util.h" 6 #include "volume.h" 7 8 #define ICON "墳 " 9 #define ICON_MUTE "婢 " 10 11 /* one test was 243, but could also be higher... */ 12 #define MAX_CHARS_PER_CMD 300 13 14 size_t volumeu(char *str, int sigval) 15 { 16 int volume = 0; 17 char mutesymbol[] = ICON; 18 19 char output[MAX_CHARS_PER_CMD]; 20 char *line; 21 char wordbuffer[20]; 22 23 getcmdout((char *[]){"amixer", "-D", "pulse", "get", "Master", NULL}, 24 output, MAX_CHARS_PER_CMD); 25 26 /* remove \n from the end of the string */ 27 output[strlen(output) - 2] = '\0'; 28 29 /* iterate over lines of output */ 30 line = strtok(output, "\n"); 31 while(line) { 32 /* printf("%s\n", line); */ 33 sscanf(line, "%s", wordbuffer); 34 35 /* printf("%s\n", wordbuffer); */ 36 37 /* this parsing is really crappy */ 38 if(!strcmp(wordbuffer, "Front")) { 39 /* we are at a line with something like this: 40 * Front Left: Playback 45821 [70%] [off] */ 41 42 /* volume */ 43 sscanf(line, "%*s %*s %*s %*s %s", wordbuffer); 44 /* remove first and last two characters */ 45 wordbuffer[strlen(wordbuffer) - 2] = '\0'; 46 volume = atoi(wordbuffer + 1); 47 48 /* mute status */ 49 sscanf(line, "%*s %*s %*s %*s %*s %s", wordbuffer); 50 if (strcmp(wordbuffer, "[on]")) strcpy(mutesymbol, ICON_MUTE); 51 52 break; 53 } 54 55 line = strtok(NULL, "\n"); 56 } 57 58 return SPRINTF(str, "%s%03d%%", mutesymbol, volume); 59 } 60 61 void volumec(int button) 62 { 63 cspawn((char *[]) {"pavucontrol", NULL}); 64 }