Compare commits

..

No commits in common. "cc78fe00af9d429732ab0fab5d12f89ca9135037" and "f009fcb92b9f65657bdf6e3bf9c1cc4ca809e0e5" have entirely different histories.

4 changed files with 68 additions and 88 deletions

View File

@ -3,18 +3,13 @@ CC := cc
CFLAGS := -pedantic -Wall -Wno-deprecated-declarations -Os CFLAGS := -pedantic -Wall -Wno-deprecated-declarations -Os
LDFLAGS := -lX11 LDFLAGS := -lX11
PULSEFLAGS := -lpulse PULSEFLAGS := -lpulse
NO_X := 0
ifeq ($(NO_X), 1)
CFLAGS := $(CFLAGS) -DNO_X
endif
# FreeBSD (uncomment) # FreeBSD (uncomment)
#LDFLAGS += -L/usr/local/lib -I/usr/local/include #LDFLAGS += -L/usr/local/lib -I/usr/local/include
# # OpenBSD (uncomment) # # OpenBSD (uncomment)
#LDFLAGS += -L/usr/X11R6/lib -I/usr/X11R6/include #LDFLAGS += -L/usr/X11R6/lib -I/usr/X11R6/include
all: options dwmblocks all: options dwmblocks dwmblocks-pulse-listener
options: options:
@echo dwmblocks build options: @echo dwmblocks build options:
@ -23,23 +18,28 @@ options:
@echo "CC = ${CC}" @echo "CC = ${CC}"
@echo "PULSEFLAGS = ${PULSEFLAGS}" @echo "PULSEFLAGS = ${PULSEFLAGS}"
dwmblocks: dwmblocks.c pulse-listener.c blocks.def.h blocks.h dwmblocks: dwmblocks.c blocks.def.h blocks.h
${CC} -o dwmblocks dwmblocks.c pulse-listener.c ${CFLAGS} ${PULSEFLAGS} ${LDFLAGS} ${CC} -o dwmblocks dwmblocks.c ${CFLAGS} ${LDFLAGS}
dwmblocks-pulse-listener: dwmblocks-pulse-listener.c
${CC} -o dwmblocks-pulse-listener dwmblocks-pulse-listener.c ${CFLAGS} ${PULSEFLAGS}
blocks.h: blocks.h:
cp blocks.def.h $@ cp blocks.def.h $@
clean: clean:
rm -f *.o *.gch dwmblocks rm -f *.o *.gch dwmblocks dwmblocks-pulse-listener
install: dwmblocks install: dwmblocks
mkdir -p ${DESTDIR}${PREFIX}/bin mkdir -p ${DESTDIR}${PREFIX}/bin
cp -f dwmblocks ${DESTDIR}${PREFIX}/bin cp -f dwmblocks ${DESTDIR}${PREFIX}/bin
cp -f dwmblocks-pulse-listener ${DESTDIR}${PREFIX}/bin
chmod 755 ${DESTDIR}${PREFIX}/bin/dwmblocks chmod 755 ${DESTDIR}${PREFIX}/bin/dwmblocks
install -m0755 scripts/* ${DESTDIR}${PREFIX}/bin install -m0755 scripts/* ${DESTDIR}${PREFIX}/bin
uninstall: uninstall:
rm -f ${DESTDIR}${PREFIX}/bin/dwmblocks \ rm -f ${DESTDIR}${PREFIX}/bin/dwmblocks \
${DESTDIR}${PREFIX}/bin/dwmblocks-pulse-listener \
${DESTDIR}${PREFIX}/bin/dwmblocks-battery \ ${DESTDIR}${PREFIX}/bin/dwmblocks-battery \
${DESTDIR}${PREFIX}/bin/dwmblocks-network \ ${DESTDIR}${PREFIX}/bin/dwmblocks-network \
${DESTDIR}${PREFIX}/bin/dwmblocks-volume \ ${DESTDIR}${PREFIX}/bin/dwmblocks-volume \

View File

@ -1,10 +1,8 @@
#include "pulse-listener.h"
#include <pulse/pulseaudio.h> #include <pulse/pulseaudio.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <signal.h> #include <sys/signal.h>
#include <limits.h> #include <limits.h>
#define ERROR(...) fprintf(stderr, "error: " __VA_ARGS__) #define ERROR(...) fprintf(stderr, "error: " __VA_ARGS__)
@ -17,85 +15,113 @@
#define SIGMIN SIGRTMIN #define SIGMIN SIGRTMIN
#endif #endif
typedef struct {
size_t pid;
unsigned int signum;
} TargetData;
static void sink_info_callback(pa_context *ctx, const pa_sink_info *info, static void sink_info_callback(pa_context *ctx, const pa_sink_info *info,
int eol, pthread_t *main_thread) { int eol, TargetData *target) {
pthread_kill(*main_thread, SIGMIN + 1); if (info && kill(target->pid, target->signum) < 0) {
ERROR("terget process died\n");
exit(1);
}
} }
static void server_info_callback(pa_context *ctx, const pa_server_info *info, static void server_info_callback(pa_context *ctx, const pa_server_info *info,
pthread_t *main_thread) { TargetData *target) {
pa_operation *op = pa_context_get_sink_info_by_name( pa_operation *op = pa_context_get_sink_info_by_name(
ctx, ctx,
info->default_sink_name, info->default_sink_name,
(pa_sink_info_cb_t) sink_info_callback, (pa_sink_info_cb_t) sink_info_callback,
main_thread); target);
pa_operation_unref(op); pa_operation_unref(op);
} }
static void subscribe_callback(pa_context *ctx, static void subscribe_callback(pa_context *ctx,
pa_subscription_event_type_t type, uint32_t idx, pa_subscription_event_type_t type, uint32_t idx,
pthread_t *main_thread) { TargetData *target) {
if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) { if ((type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) {
pa_operation *op = pa_context_get_sink_info_by_index(ctx, pa_operation *op = pa_context_get_sink_info_by_index(ctx,
idx, idx,
(pa_sink_info_cb_t) sink_info_callback, (pa_sink_info_cb_t) sink_info_callback,
main_thread); target);
pa_operation_unref(op); pa_operation_unref(op);
} }
} }
static void state_callback(pa_context *ctx, pthread_t *main_thread) { static void state_callback(pa_context *ctx, TargetData *target) {
pa_operation *op; pa_operation *op;
switch (pa_context_get_state(ctx)) { switch (pa_context_get_state(ctx)) {
case PA_CONTEXT_READY: case PA_CONTEXT_READY:
op = pa_context_get_server_info(ctx, op = pa_context_get_server_info(ctx,
(pa_server_info_cb_t) server_info_callback, (pa_server_info_cb_t) server_info_callback,
main_thread); target);
pa_operation_unref(op); pa_operation_unref(op);
pa_context_set_subscribe_callback(ctx, pa_context_set_subscribe_callback(ctx,
(pa_context_subscribe_cb_t) subscribe_callback, (pa_context_subscribe_cb_t) subscribe_callback,
main_thread); target);
op = pa_context_subscribe(ctx, PA_SUBSCRIPTION_MASK_SINK, NULL, NULL); op = pa_context_subscribe(ctx, PA_SUBSCRIPTION_MASK_SINK, NULL, NULL);
pa_operation_unref(op); pa_operation_unref(op);
break; break;
case PA_CONTEXT_FAILED: case PA_CONTEXT_FAILED:
pthread_kill(*main_thread, SIGMIN + 1); if (kill(target->pid, target->signum) < 0) {
ERROR("terget process died\n");
exit(1);
}
break; break;
default: default:
break; break;
} }
} }
static void cleanup(void *data_to_free[2]) { int main(int argc, const char **argv) {
pa_context_unref(data_to_free[1]); if (argc < 2) {
pa_mainloop_free(data_to_free[0]); ERROR("no pid\n");
} return 1;
}
void pulse_listener_main(pthread_t *main_thread) { char *endptr = NULL;
unsigned int signum = SIGMIN + 1;
if (argc >= 3 && argv[1][0] == '-') {
const char *signumstr = argv[1] + 1;
unsigned long num = strtoul(signumstr, &endptr, 10);
if (num > SIGMAX - SIGMIN) {
ERROR("signal out of range: %lu\n", num);
return 1;
}
signum = SIGMIN + num;
++argv;
}
endptr = NULL;
errno = 0;
unsigned long pid = strtoul(argv[1], &endptr, 10);
if (argv[1][0] == '-' || errno == ERANGE) {
ERROR("pid out of range: \"%s\"\n", argv[1]);
return 1;
}
pa_mainloop *mainloop = pa_mainloop_new(); pa_mainloop *mainloop = pa_mainloop_new();
if (!mainloop) { if (!mainloop) {
ERROR("could not create pulse mainloop\n"); ERROR("could not create pulse mainloop\n");
return; return 1;
} }
pa_mainloop_api *api = pa_mainloop_get_api(mainloop); pa_mainloop_api *api = pa_mainloop_get_api(mainloop);
if (!api) { if (!api) {
ERROR("could not create pulse mainloop api\n"); ERROR("could not create pulse mainloop api\n");
return; return 1;
} }
pa_context *context = pa_context_new(api, "dwmblocks-listener"); pa_context *context = pa_context_new(api, "dwmblocks-listener");
if (!context) { if (!context) {
ERROR("could not create pulse context\n"); ERROR("could not create pulse context\n");
return; return 1;
} }
pa_context_set_state_callback(context, TargetData target = { pid, signum };
pa_context_set_state_callback( context,
(pa_context_notify_cb_t)&state_callback, (pa_context_notify_cb_t)&state_callback,
main_thread); &target);
if (pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) { if (pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) {
ERROR("could not connect to pulse"); ERROR("could not connect to pulse");
return; return 1;
} }
void *data_to_free[2] = { mainloop, context };
pthread_cleanup_push((void(*)(void *)) cleanup, data_to_free);
pa_mainloop_run(mainloop, NULL); pa_mainloop_run(mainloop, NULL);
pthread_cleanup_pop(1); return 0;
} }

View File

@ -5,7 +5,6 @@
#include<signal.h> #include<signal.h>
#include <time.h> #include <time.h>
#include <math.h> #include <math.h>
#include <pthread.h>
#ifndef NO_X #ifndef NO_X
#include<X11/Xlib.h> #include<X11/Xlib.h>
#endif #endif
@ -28,7 +27,6 @@ typedef struct {
unsigned int interval; unsigned int interval;
unsigned int signal; unsigned int signal;
} Block; } Block;
typedef void(*ThreadCallback)(pthread_t *);
void getcmd(const Block *block, char *output); void getcmd(const Block *block, char *output);
void getcmds(int time); void getcmds(int time);
void getsigcmds(unsigned int signal); void getsigcmds(unsigned int signal);
@ -40,9 +38,7 @@ void setupsignals(void);
int getstatus(char *str, char *last); int getstatus(char *str, char *last);
void pstdout(void); void pstdout(void);
int gcd(int n1, int n2); int gcd(int n1, int n2);
void startthreads(void);
void statusloop(void); void statusloop(void);
void stopthreads(void);
void termhandler(void); void termhandler(void);
#ifndef NO_X #ifndef NO_X
void setroot(void); void setroot(void);
@ -61,10 +57,6 @@ static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
static char statusstr[2][STATUSLENGTH]; static char statusstr[2][STATUSLENGTH];
static int statuscontinue = 1; static int statuscontinue = 1;
static pthread_t main_thread;
static pthread_t threads[LENGTH(thread_callbacks)];
static int initdone = 0;
//opens process *cmd and stores output in *output //opens process *cmd and stores output in *output
void getcmd(const Block *block, char *output) { void getcmd(const Block *block, char *output) {
strcpy(output, block->icon); strcpy(output, block->icon);
@ -95,7 +87,7 @@ void getcmds(int time) {
for (unsigned int i = 0; i < LENGTH(blocks); ++i) { for (unsigned int i = 0; i < LENGTH(blocks); ++i) {
current = blocks + i; current = blocks + i;
if ((current->interval != 0 && time % current->interval == 0) || time == -1) { if ((current->interval != 0 && time % current->interval == 0) || time == -1) {
getcmd(current, statusbar[i]); getcmd(current,statusbar[i]);
} }
} }
} }
@ -105,7 +97,7 @@ void getsigcmds(unsigned int signal) {
for (unsigned int i = 0; i < LENGTH(blocks); ++i) { for (unsigned int i = 0; i < LENGTH(blocks); ++i) {
current = blocks + i; current = blocks + i;
if (current->signal == signal) { if (current->signal == signal) {
getcmd(current, statusbar[i]); getcmd(current,statusbar[i]);
} }
} }
} }
@ -149,18 +141,10 @@ int getstatus(char *str, char *last) {
} }
void pstdout() { void pstdout() {
if (!initdone || !getstatus(statusstr[0], statusstr[1])) { //Only write out if text has changed. if (!getstatus(statusstr[0], statusstr[1])) { //Only write out if text has changed.
return; return;
} }
printf("%s\n", statusstr[0]); printf("%s\n",statusstr[0]);
fflush(stdout);
}
void somebar_pstdout() {
if (!initdone || !getstatus(statusstr[0], statusstr[1])) { //Only write out if text has changed.
return;
}
printf("status %s\n", statusstr[0]);
fflush(stdout); fflush(stdout);
} }
@ -174,16 +158,6 @@ int gcd(int n1, int n2) {
return n1; return n1;
} }
void startthreads() {
main_thread = pthread_self();
for (int i = 0; i < LENGTH(thread_callbacks); ++i) {
pthread_create(&threads[i],
NULL,
(void *(*)(void *)) thread_callbacks[i],
&main_thread);
}
}
void statusloop() { void statusloop() {
setupsignals(); setupsignals();
unsigned int interval = -1; unsigned int interval = -1;
@ -192,7 +166,6 @@ void statusloop() {
interval = gcd(blocks[i].interval, interval); interval = gcd(blocks[i].interval, interval);
} }
} }
startthreads();
int i = 0; int i = 0;
getcmds(-1); getcmds(-1);
long nsec_part = interval * 1000000; long nsec_part = interval * 1000000;
@ -201,7 +174,6 @@ void statusloop() {
const struct timespec sleeptime = { sec_part, nsec_part }; const struct timespec sleeptime = { sec_part, nsec_part };
struct timespec tosleep = sleeptime; struct timespec tosleep = sleeptime;
while (statuscontinue) { while (statuscontinue) {
initdone = 1;
if (nanosleep(&tosleep, &tosleep) < 0) { if (nanosleep(&tosleep, &tosleep) < 0) {
continue; continue;
} }
@ -211,20 +183,13 @@ void statusloop() {
} }
} }
void stopthreads() {
for (int i = 0; i < LENGTH(thread_callbacks); ++i) {
pthread_cancel(threads[i]);
pthread_join(threads[i], NULL);
}
}
void termhandler() { void termhandler() {
statuscontinue = 0; statuscontinue = 0;
} }
#ifndef NO_X #ifndef NO_X
void setroot() { void setroot() {
if (!initdone || !getstatus(statusstr[0], statusstr[1])) { //Only set root if text has changed. if (!getstatus(statusstr[0], statusstr[1])) { //Only set root if text has changed.
return; return;
} }
XStoreName(dpy, root, statusstr[0]); XStoreName(dpy, root, statusstr[0]);
@ -249,8 +214,6 @@ int main(int argc, char** argv) {
strncpy(delim, argv[++i], delimLen); strncpy(delim, argv[++i], delimLen);
} else if (strcmp("-p", argv[i]) == 0) { } else if (strcmp("-p", argv[i]) == 0) {
writestatus = pstdout; writestatus = pstdout;
} else if (strcmp("-s", argv[i]) == 0) {
writestatus = somebar_pstdout;
} }
} }
#ifndef NO_X #ifndef NO_X
@ -266,6 +229,5 @@ int main(int argc, char** argv) {
#ifndef NO_X #ifndef NO_X
XCloseDisplay(dpy); XCloseDisplay(dpy);
#endif #endif
stopthreads();
return 0; return 0;
} }

View File

@ -1,8 +0,0 @@
#ifndef INCLUDED_PULSE_LISTENER_H
#define INCLUDED_PULSE_LISTENER_H
#include <pthread.h>
void pulse_listener_main(pthread_t *main_thread);
#endif