diff --git a/Makefile b/Makefile index 36cbf2b..8a94eca 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ tstatus: - cc tstatus.c -o tstatus -lxcb -Wall -Wextra -std=c99 + cc tstatus.c -o tstatus -lxcb -lasound -Wall -Wextra -std=c99 debug: cc tstatus.c -o tstatus -lxcb -Wall -Wextra -std=c99 -ggdb diff --git a/TODO b/TODO index 639b70c..ed7b754 100644 --- a/TODO +++ b/TODO @@ -1 +1,2 @@ - add overlaying system for other modules +- fix the nasty struct library snaffoo diff --git a/alsa.c b/alsa.c new file mode 100644 index 0000000..340635e --- /dev/null +++ b/alsa.c @@ -0,0 +1,74 @@ +/* see LICENSE file for details on license */ +#include +#include +#include +#include + +#include "module.h" + +int audio_volume(long *outvol) { + int ret = 0; + snd_mixer_t *handle; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *sid; + + static const char *mix_name = "Master"; + static const char *card = "default"; + static int mix_index = 0; + + long minv, maxv; + + snd_mixer_selem_id_alloca(&sid); + + snd_mixer_selem_id_set_index(sid, mix_index); + snd_mixer_selem_id_set_name(sid, mix_name); + + if((snd_mixer_open(&handle, 0)) < 0) + return -1; + + if((snd_mixer_attach(handle, card)) < 0) { + snd_mixer_close(handle); + return -1; + } + + if((snd_mixer_selem_register(handle, NULL, NULL)) < 0) { + snd_mixer_close(handle); + return -1; + } + + ret = snd_mixer_load(handle); + if(ret < 0) { + snd_mixer_close(handle); + return -1; + } + + elem = snd_mixer_find_selem(handle, sid); + if(!elem) { + snd_mixer_close(handle); + return -1; + } + + snd_mixer_selem_get_playback_volume_range (elem, &minv, &maxv); + + if(snd_mixer_selem_get_playback_volume(elem, 0, outvol) < 0) { + snd_mixer_close(handle); + return -1; + } + + /* make the value bound to 100 */ + *outvol -= minv; + maxv -= minv; + minv = 0; + *outvol = 100 * (*outvol) / maxv; + + snd_mixer_close(handle); + return 0; +} + +int alsa_update(struct module *module) { + long vol = -1; + audio_volume(&vol); + snprintf((char *)&module->buffer, MODULE_BUFFER_LEN, "%l", vol); + + return 0; +} diff --git a/alsa.h b/alsa.h new file mode 100644 index 0000000..6cb8304 --- /dev/null +++ b/alsa.h @@ -0,0 +1,8 @@ +/* see LICENSE file for details on license */ +#ifndef TSTATUS_ALSA_H + +int audio_volume(long *outvol); +int alsa_update(struct module *module); + +#endif +#define TSTATUS_ALSA_H diff --git a/bspwm.h b/bspwm.h index 987fbc2..2b78cab 100644 --- a/bspwm.h +++ b/bspwm.h @@ -29,8 +29,6 @@ #define SOCKET_ENV_VAR "BSPWM_SOCKET" #define FAILURE_MESSAGE "\x07" -#define LENGTH(x) (sizeof(x) / sizeof(*x)) - void err(char *fmt, ...); char *send_msg_to_bspwm(char *args[], int count); int bspwm_update(struct module *module); diff --git a/datetime.c b/datetime.c new file mode 100644 index 0000000..2f7fe27 --- /dev/null +++ b/datetime.c @@ -0,0 +1,19 @@ +/* see LICENSE file for details on license */ +#include +#include + +#include "datetime.h" + +int datetime_update(struct module *module) { + time_t t = time(NULL); + if(t == -1) return -1; + struct tm *tm = localtime(&t); + if(!tm) return -1; + + snprintf((char *)&module->buffer, + MODULE_BUFFER_LEN, "%.02i%.02i-%.02i:%.02i", + tm->tm_mon, tm->tm_mday, + tm->tm_hour, tm->tm_min); + + return 0; +} diff --git a/datetime.h b/datetime.h new file mode 100644 index 0000000..78eb8ba --- /dev/null +++ b/datetime.h @@ -0,0 +1,7 @@ +/* see LICENSE file for details on license */ +#ifndef TSTATUS_TIMEDATE_H + +int timedate_update(struct module *module); + +#endif +#define TSTATUS_TIMEDATE_H diff --git a/tstatus.c b/tstatus.c index 2f4afca..4e16187 100644 --- a/tstatus.c +++ b/tstatus.c @@ -1,15 +1,21 @@ /* see LICENSE file for details on license */ #include +#define LENGTH(x) (sizeof(x) / sizeof(*x)) + #include "module.h" #include "bspwm.c" #include "battery.c" #include "thermal.c" +#include "datetime.c" +#include "alsa.c" struct module table[] = { {0, 10, bspwm_update, {'\0'}}, {0, 10, battery_update, {'\0'}}, {0, 10, thermal_update, {'\0'}}, + {0, 10, datetime_update, {'\0'}}, + {0, 10, alsa_update, {'\0'}}, }; int main(void) { @@ -22,5 +28,8 @@ int main(void) { table[2].updatecallback(&table[2]); printf("%s\n", table[2].buffer); + table[3].updatecallback(&table[2]); + printf("%s\n", table[2].buffer); + return 0; }