Compare commits
No commits in common. "cc78fe00af9d429732ab0fab5d12f89ca9135037" and "f009fcb92b9f65657bdf6e3bf9c1cc4ca809e0e5" have entirely different histories.
cc78fe00af
...
f009fcb92b
18
Makefile
18
Makefile
@ -3,18 +3,13 @@ CC := cc
|
||||
CFLAGS := -pedantic -Wall -Wno-deprecated-declarations -Os
|
||||
LDFLAGS := -lX11
|
||||
PULSEFLAGS := -lpulse
|
||||
NO_X := 0
|
||||
|
||||
ifeq ($(NO_X), 1)
|
||||
CFLAGS := $(CFLAGS) -DNO_X
|
||||
endif
|
||||
|
||||
# FreeBSD (uncomment)
|
||||
#LDFLAGS += -L/usr/local/lib -I/usr/local/include
|
||||
# # OpenBSD (uncomment)
|
||||
#LDFLAGS += -L/usr/X11R6/lib -I/usr/X11R6/include
|
||||
|
||||
all: options dwmblocks
|
||||
all: options dwmblocks dwmblocks-pulse-listener
|
||||
|
||||
options:
|
||||
@echo dwmblocks build options:
|
||||
@ -23,23 +18,28 @@ options:
|
||||
@echo "CC = ${CC}"
|
||||
@echo "PULSEFLAGS = ${PULSEFLAGS}"
|
||||
|
||||
dwmblocks: dwmblocks.c pulse-listener.c blocks.def.h blocks.h
|
||||
${CC} -o dwmblocks dwmblocks.c pulse-listener.c ${CFLAGS} ${PULSEFLAGS} ${LDFLAGS}
|
||||
dwmblocks: dwmblocks.c blocks.def.h blocks.h
|
||||
${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:
|
||||
cp blocks.def.h $@
|
||||
|
||||
clean:
|
||||
rm -f *.o *.gch dwmblocks
|
||||
rm -f *.o *.gch dwmblocks dwmblocks-pulse-listener
|
||||
|
||||
install: dwmblocks
|
||||
mkdir -p ${DESTDIR}${PREFIX}/bin
|
||||
cp -f dwmblocks ${DESTDIR}${PREFIX}/bin
|
||||
cp -f dwmblocks-pulse-listener ${DESTDIR}${PREFIX}/bin
|
||||
chmod 755 ${DESTDIR}${PREFIX}/bin/dwmblocks
|
||||
install -m0755 scripts/* ${DESTDIR}${PREFIX}/bin
|
||||
|
||||
uninstall:
|
||||
rm -f ${DESTDIR}${PREFIX}/bin/dwmblocks \
|
||||
${DESTDIR}${PREFIX}/bin/dwmblocks-pulse-listener \
|
||||
${DESTDIR}${PREFIX}/bin/dwmblocks-battery \
|
||||
${DESTDIR}${PREFIX}/bin/dwmblocks-network \
|
||||
${DESTDIR}${PREFIX}/bin/dwmblocks-volume \
|
||||
|
@ -1,10 +1,8 @@
|
||||
#include "pulse-listener.h"
|
||||
|
||||
#include <pulse/pulseaudio.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <sys/signal.h>
|
||||
#include <limits.h>
|
||||
|
||||
#define ERROR(...) fprintf(stderr, "error: " __VA_ARGS__)
|
||||
@ -17,85 +15,113 @@
|
||||
#define SIGMIN SIGRTMIN
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
size_t pid;
|
||||
unsigned int signum;
|
||||
} TargetData;
|
||||
|
||||
static void sink_info_callback(pa_context *ctx, const pa_sink_info *info,
|
||||
int eol, pthread_t *main_thread) {
|
||||
pthread_kill(*main_thread, SIGMIN + 1);
|
||||
int eol, TargetData *target) {
|
||||
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,
|
||||
pthread_t *main_thread) {
|
||||
TargetData *target) {
|
||||
pa_operation *op = pa_context_get_sink_info_by_name(
|
||||
ctx,
|
||||
info->default_sink_name,
|
||||
(pa_sink_info_cb_t) sink_info_callback,
|
||||
main_thread);
|
||||
target);
|
||||
pa_operation_unref(op);
|
||||
}
|
||||
|
||||
static void subscribe_callback(pa_context *ctx,
|
||||
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) {
|
||||
pa_operation *op = pa_context_get_sink_info_by_index(ctx,
|
||||
idx,
|
||||
(pa_sink_info_cb_t) sink_info_callback,
|
||||
main_thread);
|
||||
target);
|
||||
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;
|
||||
switch (pa_context_get_state(ctx)) {
|
||||
case PA_CONTEXT_READY:
|
||||
op = pa_context_get_server_info(ctx,
|
||||
(pa_server_info_cb_t) server_info_callback,
|
||||
main_thread);
|
||||
target);
|
||||
pa_operation_unref(op);
|
||||
pa_context_set_subscribe_callback(ctx,
|
||||
(pa_context_subscribe_cb_t) subscribe_callback,
|
||||
main_thread);
|
||||
target);
|
||||
op = pa_context_subscribe(ctx, PA_SUBSCRIPTION_MASK_SINK, NULL, NULL);
|
||||
pa_operation_unref(op);
|
||||
break;
|
||||
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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void cleanup(void *data_to_free[2]) {
|
||||
pa_context_unref(data_to_free[1]);
|
||||
pa_mainloop_free(data_to_free[0]);
|
||||
}
|
||||
|
||||
void pulse_listener_main(pthread_t *main_thread) {
|
||||
int main(int argc, const char **argv) {
|
||||
if (argc < 2) {
|
||||
ERROR("no pid\n");
|
||||
return 1;
|
||||
}
|
||||
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();
|
||||
if (!mainloop) {
|
||||
ERROR("could not create pulse mainloop\n");
|
||||
return;
|
||||
return 1;
|
||||
}
|
||||
pa_mainloop_api *api = pa_mainloop_get_api(mainloop);
|
||||
if (!api) {
|
||||
ERROR("could not create pulse mainloop api\n");
|
||||
return;
|
||||
return 1;
|
||||
}
|
||||
pa_context *context = pa_context_new(api, "dwmblocks-listener");
|
||||
if (!context) {
|
||||
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,
|
||||
main_thread);
|
||||
&target);
|
||||
if (pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) < 0) {
|
||||
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);
|
||||
pthread_cleanup_pop(1);
|
||||
return 0;
|
||||
}
|
48
dwmblocks.c
48
dwmblocks.c
@ -5,7 +5,6 @@
|
||||
#include<signal.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
#ifndef NO_X
|
||||
#include<X11/Xlib.h>
|
||||
#endif
|
||||
@ -28,7 +27,6 @@ typedef struct {
|
||||
unsigned int interval;
|
||||
unsigned int signal;
|
||||
} Block;
|
||||
typedef void(*ThreadCallback)(pthread_t *);
|
||||
void getcmd(const Block *block, char *output);
|
||||
void getcmds(int time);
|
||||
void getsigcmds(unsigned int signal);
|
||||
@ -40,9 +38,7 @@ void setupsignals(void);
|
||||
int getstatus(char *str, char *last);
|
||||
void pstdout(void);
|
||||
int gcd(int n1, int n2);
|
||||
void startthreads(void);
|
||||
void statusloop(void);
|
||||
void stopthreads(void);
|
||||
void termhandler(void);
|
||||
#ifndef NO_X
|
||||
void setroot(void);
|
||||
@ -61,10 +57,6 @@ static char statusbar[LENGTH(blocks)][CMDLENGTH] = {0};
|
||||
static char statusstr[2][STATUSLENGTH];
|
||||
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
|
||||
void getcmd(const Block *block, char *output) {
|
||||
strcpy(output, block->icon);
|
||||
@ -95,7 +87,7 @@ void getcmds(int time) {
|
||||
for (unsigned int i = 0; i < LENGTH(blocks); ++i) {
|
||||
current = blocks + i;
|
||||
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) {
|
||||
current = blocks + i;
|
||||
if (current->signal == signal) {
|
||||
getcmd(current, statusbar[i]);
|
||||
getcmd(current,statusbar[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -149,18 +141,10 @@ int getstatus(char *str, char *last) {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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]);
|
||||
printf("%s\n",statusstr[0]);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
@ -174,16 +158,6 @@ int gcd(int n1, int n2) {
|
||||
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() {
|
||||
setupsignals();
|
||||
unsigned int interval = -1;
|
||||
@ -192,7 +166,6 @@ void statusloop() {
|
||||
interval = gcd(blocks[i].interval, interval);
|
||||
}
|
||||
}
|
||||
startthreads();
|
||||
int i = 0;
|
||||
getcmds(-1);
|
||||
long nsec_part = interval * 1000000;
|
||||
@ -201,7 +174,6 @@ void statusloop() {
|
||||
const struct timespec sleeptime = { sec_part, nsec_part };
|
||||
struct timespec tosleep = sleeptime;
|
||||
while (statuscontinue) {
|
||||
initdone = 1;
|
||||
if (nanosleep(&tosleep, &tosleep) < 0) {
|
||||
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() {
|
||||
statuscontinue = 0;
|
||||
}
|
||||
|
||||
#ifndef NO_X
|
||||
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;
|
||||
}
|
||||
XStoreName(dpy, root, statusstr[0]);
|
||||
@ -249,8 +214,6 @@ int main(int argc, char** argv) {
|
||||
strncpy(delim, argv[++i], delimLen);
|
||||
} else if (strcmp("-p", argv[i]) == 0) {
|
||||
writestatus = pstdout;
|
||||
} else if (strcmp("-s", argv[i]) == 0) {
|
||||
writestatus = somebar_pstdout;
|
||||
}
|
||||
}
|
||||
#ifndef NO_X
|
||||
@ -266,6 +229,5 @@ int main(int argc, char** argv) {
|
||||
#ifndef NO_X
|
||||
XCloseDisplay(dpy);
|
||||
#endif
|
||||
stopthreads();
|
||||
return 0;
|
||||
}
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user