module XMonad.Wallpaper where

import XMonad.Core
import XMonad.Wallpaper.Find
import XMonad.Wallpaper.Expand

import Control.Applicative
import System.Random

{- |
Example usage: (~/.xmonad/xmonad.hs)

> import XMonad
> import XMonad.Config.Desktop
> import XMonad.Wallpaper
> main = do
>     setRandomWallpaper ["paths-of-your choice", "$HOME/Pictures/Wallpapers"]
>     xmonad $ desktopConfig
>        { terminal    = "urxvt"
>        , modMask     = mod4Mask
>        }

paths will be expanded using environment variables, and paths are not exist will be ignored during scan phase.

For more information about path expansion, see also 'expand'.

-}

setRandomWallpaper :: [String] -> IO ()
setRandomWallpaper filepaths :: [String]
filepaths = do
    [String]
rootPaths  <- (String -> IO String) -> [String] -> IO [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM String -> IO String
expand [String]
filepaths
    [String]
candidates <- [String] -> IO [String]
findImages [String]
rootPaths
    String
wallpaper  <- ([String] -> Int -> String
forall a. [a] -> Int -> a
(!!) [String]
candidates) (Int -> String) -> IO Int -> IO String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (StdGen -> (Int, StdGen)) -> IO Int
forall a. (StdGen -> (a, StdGen)) -> IO a
getStdRandom ((Int, Int) -> StdGen -> (Int, StdGen)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (0, [String] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
candidates Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1)) 
    String -> IO ()
forall (m :: * -> *). MonadIO m => String -> m ()
spawn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ "feh --bg-scale " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
wallpaper