add alsa and datetime

This commit is contained in:
randomuser 2022-09-25 10:35:26 -05:00
parent 59180122d1
commit 7d705c4dcf
8 changed files with 119 additions and 3 deletions

View File

@ -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

1
TODO
View File

@ -1 +1,2 @@
- add overlaying system for other modules
- fix the nasty struct library snaffoo

74
alsa.c Normal file
View File

@ -0,0 +1,74 @@
/* see LICENSE file for details on license */
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <alsa/asoundlib.h>
#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;
}

8
alsa.h Normal file
View File

@ -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

View File

@ -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);

19
datetime.c Normal file
View File

@ -0,0 +1,19 @@
/* see LICENSE file for details on license */
#include <stdio.h>
#include <time.h>
#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;
}

7
datetime.h Normal file
View File

@ -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

View File

@ -1,15 +1,21 @@
/* see LICENSE file for details on license */
#include <stdio.h>
#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;
}