repo: music action: commit revision: path_from: revision_from: b53daee348e0aadabd5c077c9b3aa68d2554cc85: path_to: revision_to:
commit b53daee348e0aadabd5c077c9b3aa68d2554cc85 Author: epochDate: Mon Nov 18 22:00:10 2019 -0600 well, it all works here. might as well commit it to git diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..4c205008613d0817a662f39b5f500df5f97d960c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +qargs diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..bc5ca8ae76fbda476fd9c09fdd0b1904aabfe501 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +PREFIX:=/usr/local + +.PHONY: install + +qargs: qargs.c + +install: qargs + install qargs $(PREFIX)/bin/ + install music-player $(PREFIX)/bin/ + install music-playpause $(PREFIX)/bin/ + install music-init $(PREFIX)/bin/ + install music-pause $(PREFIX)/bin/ + install music-unpause $(PREFIX)/bin/ + install music-stop $(PREFIX)/bin/ + install music-prev $(PREFIX)/bin/ + install music-next $(PREFIX)/bin/ diff --git a/music-init b/music-init new file mode 100755 index 0000000000000000000000000000000000000000..7c8d5c750e926bbff32e27633a41c4976cc97cf7 --- /dev/null +++ b/music-init @@ -0,0 +1,4 @@ +#!/bin/sh +mkdir -p "${PREFIX}/var/run" +export "QARGS_CHILD_PIDFILE=${PREFIX}/var/run/music-player.pid" +find ~/Music -name '*.mp3' | shuf | qargs music-player > "${PREFIX}/var/run/music-init.pid" diff --git a/music-next b/music-next new file mode 100755 index 0000000000000000000000000000000000000000..3571a3d38a6146aa3335db24f713c08a3b0b9c80 --- /dev/null +++ b/music-next @@ -0,0 +1,2 @@ +#!/bin/sh +exec kill -USR2 "$(cat ${PREFIX}/var/run/music-init.pid)" diff --git a/music-pause b/music-pause new file mode 100755 index 0000000000000000000000000000000000000000..7c70fe2b948bcf64ef8a746cd2d48d800a77cf6b --- /dev/null +++ b/music-pause @@ -0,0 +1,3 @@ +#!/bin/sh +exec pkill -STOP mpv +#exec pkill -STOP $(cat ${PREFIX}/var/run/music-init.pid) diff --git a/music-player b/music-player new file mode 100755 index 0000000000000000000000000000000000000000..a815b2547b0a563f89b54e4e3e551b305570735b --- /dev/null +++ b/music-player @@ -0,0 +1,2 @@ +#!/bin/bash +exec mpv --no-video --no-terminal "$1" diff --git a/music-playpause b/music-playpause new file mode 100755 index 0000000000000000000000000000000000000000..45179a3e8699e0348d483aa9bea9a0bead0f0e79 --- /dev/null +++ b/music-playpause @@ -0,0 +1,14 @@ +#!/bin/bash +pid="$(cat "${PREFIX}/var/run/music-player.pid")" +state="$(ps -o state= -p "$pid")" +if [ "$state" ];then + if [ "$state" = "T" ];then #it is stopped. let's send it a CONT + exec music-unpause $pid + fi + if [ "$state" = "S" ];then #it is running, let's pause it. + exec music-pause $pid + fi +else + nohup music-init & + disown +fi diff --git a/music-prev b/music-prev new file mode 100755 index 0000000000000000000000000000000000000000..e640ffa69dfc1211f75c303df8a729f4ad99b570 --- /dev/null +++ b/music-prev @@ -0,0 +1,2 @@ +#!/bin/sh +exec kill -USR1 "$(cat ${PREFIX}/var/run/music-init.pid)" diff --git a/music-stop b/music-stop new file mode 100755 index 0000000000000000000000000000000000000000..1c070250d0725a8002b4fe18ae43b3c29646ba48 --- /dev/null +++ b/music-stop @@ -0,0 +1,5 @@ +#!/bin/sh +kill -9 $(cat ${PREFIX}/var/run/music-init.pid) +rm -f ${PREFIX}/var/run/music-init.pid +kill -9 $(cat ${PREFIX}/var/run/music-player.pid) +rm -f ${PREFIX}/var/run/music-player.pid diff --git a/music-unpause b/music-unpause new file mode 100755 index 0000000000000000000000000000000000000000..9bb25b2e4327ffc0064fca44f3d2d27cf3302a16 --- /dev/null +++ b/music-unpause @@ -0,0 +1,3 @@ +#!/bin/sh +exec pkill -CONT mpv +#exec pkill -CONT $(cat ${PREFIX}/var/run/music-init.pid) diff --git a/qargs.c b/qargs.c new file mode 100644 index 0000000000000000000000000000000000000000..b27a3c24167999a593b3d2ca30f434df193a3818 --- /dev/null +++ b/qargs.c @@ -0,0 +1,90 @@ +#define _POSIX_C_SOURCE 1 +#include +#include +#include +#include +#include +#include +#include + +int pid;//THE child process +int direction; + +#define DIR_NEXT 1 +#define DIR_PREV -1 + +void sig_handler(int sig) { + fprintf(stderr,"got a signal. %d\n",sig); + switch(sig) { + case SIGUSR1: + if(pid > 0) kill(pid,9); + else fprintf(stderr,"shit. pid is %d\n",pid); + direction=DIR_PREV; + break; + case SIGUSR2: + if(pid > 0) kill(pid,9); + else fprintf(stderr,"shit. pid is %d\n",pid); + direction=DIR_NEXT; + break; + default: + //should NOT happen. + break; + } +} + +#define LINES 1024 + +int main(int argc,char *argv[]) { + int status; + int index=0; + direction=1; + int lines_read=0; + char line[LINES][PATH_MAX+1];//rather be off by one in the safe direction. + FILE *fp; + printf("%d\n",getpid()); + fflush(stdout); + while(1) {//I should come up with a better condition for the main loop + signal(SIGUSR1,sig_handler); + signal(SIGUSR2,sig_handler); + if(index == lines_read) { + if(fgets(line[index],sizeof(line[index])-1,stdin) == 0) { + fprintf(stderr,"Reached EOF on stdin.\n"); + return 0;//EOF. this is normal. + } + if(strchr(line[index],'\n')) *strchr(line[index],'\n')=0; + lines_read++; + } + switch(pid=fork()) { + case -1://fuck me + perror("fork"); + return 1; + case 0://we're the child + fprintf(stderr,"running line %d in pid %d: %s %s\n",index,getpid(),argv[1],line[index]); + if(getenv("QARGS_CHILD_PIDFILE")) { + if((fp=fopen(getenv("QARGS_CHILD_PIDFILE"),"w+"))) { + fprintf(fp,"%d\n",getpid()); + fclose(fp); + } else { + fprintf(stderr,"couldn't open child pid file: %s\n",getenv("QARGS_CHILD_PIDFILE")); + } + } else { + fprintf(stderr,"QARGS_CHILD_PIDFILE is not set in env. can't write pidfile for children.\n"); + } + signal(SIGUSR1,SIG_IGN); + signal(SIGUSR2,SIG_IGN); + execlp(argv[1],argv[1],line[index],NULL); + perror("execlp"); + return -2;//child failed to exec. tell it to fuck off. + break; + default://we're the parent + break; + } + while(wait(&status) == -1) {//wait for child process to finish... + perror("wait"); + } + fprintf(stderr,"child %d exited. going to %d the index.\n",pid,direction); + index+=direction; + if(index < 0) index=0; + direction=DIR_NEXT; + } +}
-----END OF PAGE-----