Commits

Maxime Buquet committed b954144

First version of ocaml-chbg
Now able to simply set a background image, or to round with files of a specific
directory

Comments (0)

Files changed (2)

 syntax: glob
 
-*.cm{i,a,x,xa}
+*.cm{i,a,o,x,xa}
+(*
+** This file is part of ocaml-chbg.
+** 
+** ocaml-chbg is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation, either version 3 of the License, or
+** (at your option) any later version.
+** 
+** ocaml-chbg is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+** 
+** You should have received a copy of the GNU General Public License
+** along with ocaml-chbg.  If not, see <http://www.gnu.org/licenses/>.
+*)
+
+module U = Unix
+module P = Printf
+
+let _bg = ref ""
+let _bgdir = ref ""
+let _tbl = Hashtbl.create 17
+
+let _logstart = U.gettimeofday ()
+
+let log str =
+  let time = U.gettimeofday () -. _logstart in
+  P.printf "%.3f: %s%!" time str
+
+let ios = int_of_string
+
+let isfile str =
+  let stats = U.stat str in
+    match stats.U.st_kind with
+      | U.S_REG -> true
+      | _ -> false
+
+let feh_exec file = match U.fork () with
+  | 0 ->
+      Unix.execvp "feh" [| "feh"; "--bg-scale"; file |]
+  | pid -> let _, _ = U.waitpid [] pid in
+      log (P.sprintf "Changing background to %s\n" file)
+
+let make_tbl dir =
+
+  let to_list str =
+    let handle = U.opendir str in
+    let l, step = ref [], ref true in
+      while !step do
+        try
+          l := (U.readdir handle) :: !l ;
+        with End_of_file -> step := false
+      done;
+      !l
+  in
+
+  let rec make_sub n = function
+    | [] ->
+        log (
+          P.sprintf "Length of _tbl : %d , length of n : %d\n"
+            (Hashtbl.length _tbl)
+            n) ;
+        n
+    | hd :: tl when isfile (P.sprintf "%s/%s" !_bgdir hd) ->
+        Hashtbl.add _tbl n hd ;
+        make_sub (n + 1) tl
+    | _ :: tl -> make_sub n tl
+  in
+    log (P.sprintf "Recording directory name, %s\n" dir) ;
+    _bgdir := dir ;
+    log "Making table\n" ;
+    make_sub 0 (to_list dir)
+
+let chbg n delay =
+  while true do
+    _bg := Hashtbl.find _tbl (Random.int n) ;
+    feh_exec (P.sprintf "%s/%s" !_bgdir !_bg) ;
+    log (P.sprintf "Sleeping for %d seconds\n" delay) ;
+    U.sleep delay
+  done
+
+let usage () =
+  P.printf "usage: %s <directory> <delay in seconds>\n"
+    Sys.argv.(0)
+
+let main () = match Sys.argv with
+  | [| _; bg |] when isfile bg ->
+        feh_exec bg
+
+  | [| _; background; delay |] ->
+          let n = make_tbl background in
+            Random.self_init () ;
+            chbg n (ios delay)
+
+  |  _ -> usage ()
+
+let _ =
+  main ()