Browse Source

made everything work

master
wes 12 years ago
parent
commit
dbd042fdac
  1. 2
      config_parser.rkt
  2. 66
      lazyplay.rkt
  3. 5
      lazyplay.sexp

2
config_parser.rkt

@ -9,3 +9,5 @@
(define settings (make-hash)) (define settings (make-hash))
(update settings parsed) (update settings parsed)
(provide settings)

66
lazyplay.rkt

@ -3,33 +3,36 @@
(require racket/system) (require racket/system)
(require "helpers.rkt") (require "helpers.rkt")
(require "config_parser.rkt")
(define args (vector->list (current-command-line-arguments))) (define args (vector->list (current-command-line-arguments)))
; args: filename, mplayer-args ; args: filename, mplayer-args
; optional: directory ; optional: directory
(hash-ref settings "args" "")
(define mplcmd (string->path "/usr/bin/mplayer")) ; command used to play files (define file-types (make-hash))
; output file to /dev/null/ (define media-player (string->path (hash-ref settings "player" "/usr/bin/mplayer")))
(define player-args (hash-ref settings "args" ""))
(update file-types (hash-ref settings "file-types" '(("avi" #t))))
(define (file-list) ; list of files in the current working directory (define (file-list) ; list of files in the current working directory
(map path->string ; get the strings from the list of paths (map path->string ; get the strings from the list of paths
(directory-list (current-directory)))) (directory-list (current-directory))))
; check if a filename has a desired suffix ; check if a filename has a desired suffix
(define (check-suffix suffix) (define (check-suffix file-types)
(compose (compose
(partial string=? suffix) (partial hash-has-key? file-types)
(lambda (x) (lambda (x)
(let* (let*
((len (string-length x) )) ((len (string-length x) ))
(substring x (- len 3) len))))) (substring x (- len 3) len)))))
; filter out filenames without the desired suffix ; filter out filenames without the desired suffix
(define (filter-paths suffix paths) (define (filter-paths file-types paths)
(filter (check-suffix suffix) paths)) (filter (check-suffix file-types) paths))
(define (play-list) (define (play-list)
(sort-paths (filter-paths (first args) (file-list)))) ; first commandline argument is the filename (sort-paths (filter-paths file-types (file-list)))) ; first commandline argument is the filename
; sortgetter, gets the episode number and converts it to an integer ; sortgetter, gets the episode number and converts it to an integer
(define (sortgetter file-name) (define (sortgetter file-name)
@ -56,38 +59,47 @@
; (define (controller pid out) ; (define (controller pid out)
(define (play-files filenames) (define (play-files filenames args)
(let* ((nullport (let* ((nullport
(open-output-file (string->path "/dev/null") #:exists 'append))) ; we don't want any output from the process (open-output-file (string->path "/dev/null") #:exists 'append))) ; we don't want any output from the process
(call-with-values (call-with-values
(lambda () (subprocess nullport #f nullport mplcmd (car filenames))) (lambda () (subprocess nullport #f nullport media-player (car filenames) args))
list))) ; convert the 4 return values into a list list))) ; convert the 4 return values into a list
(define (play fnames played) (define (get-args message)
(cond ((false? message) player-args)
(else message)))
(define (play fnames played args)
(cond ((null? fnames) '()) (cond ((null? fnames) '())
(else (else
(let* ((results (play-files fnames))) ; get the pid and the 3 i/o ports (let* ((results (play-files fnames args))) ; get the pid and the 3 i/o ports
; send the other thread the new pid here ; send the other thread the new pid here
; and the output port for piping input to the process ; and the output port for piping input to the process
; (thread-send thd (first results)) ; (thread-send controller-thread ((third results) (first results)))
(subprocess-wait (first results)) (subprocess-wait (first results))
(close-output-port (third results))) (close-output-port (third results)))
(let* ((newfs (new-files played (play-list)))) (let* ((newfs (new-files played (play-list))))
(play (append (cdr fnames) newfs) (update played newfs)))))) (play (append (cdr fnames) newfs) (update played newfs) (get-args (thread-try-receive)))))))
(play (play-list) played) (define (controller player-thread)
(define (controller) (cond
(let* ((input (read (current-input-port)))) ((compose not thread-running?) player-thread ; if the thread is not running then return
(cond ((eof-object? input)) '()))
(let* ((input (read-line (current-input-port) 'linefeed)))
input
(cond ((eof-object? input) '())
(else (else
(printf "output: ~A\n" input) (thread-send player-thread input)
(controller))))) (controller player-thread)))))
; (define (main) (define player-thread (thread (lambda () (play (play-list) played player-args))))
; (printf "Beginning (main)\n") (define controller-thread (thread (lambda () (controller player-thread))))
; (thread controller))
; (sleep 1000)
; check to see if the player is running, and if not then kill the controller
(define (check)
(sleep 20)
(cond (((compose not thread-running?) player-thread) (kill-thread controller-thread))
(else (check))))
; (main) (check)

5
lazyplay.sexp

@ -1,2 +1,3 @@
(("player" "mplayer") (("player" "/usr/bin/mplayer")
("filetypes" ("avi" "mp4"))) ("filetypes" (("avi" #t) ("mp4" #t)))
("args" "-ss 5000"))