sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

slstatus-alsa-master-20230420-84a2f11.diff (2439B)


      1 diff --git a/Makefile b/Makefile
      2 index 2f93b87..41d5e9a 100644
      3 --- a/Makefile
      4 +++ b/Makefile
      5 @@ -6,6 +6,7 @@ include config.mk
      6  
      7  REQ = util
      8  COM =\
      9 +	components/alsa_master_vol\
     10  	components/battery\
     11  	components/cpu\
     12  	components/datetime\
     13 diff --git a/components/alsa_master_vol.c b/components/alsa_master_vol.c
     14 new file mode 100644
     15 index 0000000..acc283b
     16 --- /dev/null
     17 +++ b/components/alsa_master_vol.c
     18 @@ -0,0 +1,64 @@
     19 +/* Created by William Rabbermann */
     20 +#include <stdio.h>
     21 +#include <stdbool.h>
     22 +#include <string.h>
     23 +#include "../util.h"
     24 +
     25 +#define TMP_BUF_SIZE 14
     26 +#define VOL_BUF_SIZE 5
     27 +
     28 +const char *
     29 +alsa_master_vol(void)
     30 +{
     31 +	bool MASTER_IS_MUTED = true;
     32 +	char tmp_buf[TMP_BUF_SIZE];
     33 +	short b;
     34 +	unsigned short i = 0;
     35 +	
     36 +	FILE *fp = popen("amixer get Master | tail -c13", "r");
     37 +	char ch;
     38 +	while ((ch = fgetc(fp)) != EOF && i < TMP_BUF_SIZE)
     39 +		tmp_buf[i++] = ch;
     40 +	tmp_buf[i] = '\0';
     41 +	pclose(fp);
     42 +
     43 +	b = i - 1;
     44 +	while (b >= 0)
     45 +	{
     46 +		if ('[' == tmp_buf[b])
     47 +		{
     48 +			if (tmp_buf[b+1] == 'o' && tmp_buf[b+2] == 'n')
     49 +				MASTER_IS_MUTED = false;
     50 +			b -= 3;
     51 +			break;
     52 +		}
     53 +		b--;
     54 +	}
     55 +	
     56 +	if (MASTER_IS_MUTED) return bprintf("MUTE");
     57 +	else
     58 +	{
     59 +		char vol_buf[VOL_BUF_SIZE];
     60 +		while (b >= 0)
     61 +		{
     62 +			if ('[' == tmp_buf[b])
     63 +				break;	
     64 +			b--;
     65 +		}
     66 +		
     67 +		i = 0;
     68 +		while (i < VOL_BUF_SIZE)
     69 +		{
     70 +			b++;
     71 +			if (']' == tmp_buf[b])
     72 +			{
     73 +				vol_buf[i] = '\0'; 
     74 +				break;
     75 +			}
     76 +			else
     77 +				vol_buf[i++] = tmp_buf[b];
     78 +		}
     79 +		
     80 +		return bprintf("%s", vol_buf);
     81 +	}
     82 +}
     83 diff --git a/config.def.h b/config.def.h
     84 index 93a875a..6074441 100644
     85 --- a/config.def.h
     86 +++ b/config.def.h
     87 @@ -58,6 +58,7 @@ static const char unknown_str[] = "n/a";
     88   * uid                 UID of current user             NULL
     89   * uptime              system uptime                   NULL
     90   * username            username of current user        NULL
     91 + * alsa_master_vol     ALSA Master device volume       NULL
     92   * vol_perc            OSS/ALSA volume in percent      mixer file (/dev/mixer)
     93   *                                                     NULL on OpenBSD
     94   * wifi_perc           WiFi signal in percent          interface name (wlan0)
     95 diff --git a/slstatus.h b/slstatus.h
     96 index b0f2564..415afc1 100644
     97 --- a/slstatus.h
     98 +++ b/slstatus.h
     99 @@ -78,6 +78,7 @@ const char *uid(void);
    100  
    101  /* volume */
    102  const char *vol_perc(const char *card);
    103 +const char *alsa_master_vol(void);
    104  
    105  /* wifi */
    106  const char *wifi_perc(const char *interface);