#!/usr/local/bin/php -q >>REQUIREMENTS<<< # # You will need two things to run this for the most part. # 1) A CGI or CLI install of PHP # 2) wmsetbg # 3) Root access to install it. # Number 3 isn't really needed. It just allows you to install it somepalce # where all users can get at it. # # >>>INSTALATION<<< # # Save this file in /usr/local/bin, or some other directory that's in # your path. Make sure it's executable. chmod 755 should be just fine. # You'll also want to double check that the first line of this script # knows where PHP is. # # Now create a hidden file called .bb_bg. It needs to have one line # for each directory that you want bb_bg to have access too. No more! # If for some reason you just can't defeat the urge to add a directory # that doesn't exist or doesn't have images in it, the program should # deal gracefully with this as a) if it's not a directory, it won't # attempt to open it, and b) if it is a directory, it will only add # files with specific .jpg, .png, and .gif extensions. # # Anyway, that line must also have a trialing '/' or else it's going to choke. # One more thing to remember is that if there is not .bb_bg file, bb_bg # will assume that there is an image directory at ~/GNUstep/Libraray/WindowMaker/Backgrounds. # Ultimately, it will try it's best to operate should you not do too good of a # job reading and grokking these instructions. # # >>>EXECUTION<<< # # The basic usage is as below but with some additions. # # --s Slide show # --d Provide a specific directory of images for the slideshow. # --a Add a directory to the configured list of image directories. # --t Implies --s. Specifies interval for slideshow. The default is 180 seconds. # --u Use a particular image. Must include full path # --i Use a particular image in your image directory. No path or extension needed. # --U Usage! What else would it be? # # So, while the below still applies... # # 1) To set a background do --> 'bb_bg --u /path/to/background' # 2) To select a random background do --> 'bb_bg' # 3) Slideshow in the root window do --> 'bb_bg --s' # # ...you can now do these too as of v2.0 # # 4) Specify the time (in seconds) between image changes do --> 'bb_bg --t 5' # 5) Show images in a specific directory only (ignore config file) do --> 'bb_bg --d '/path/to/directory/'' # 6) Add a directory to those specified in the config file do --> 'bb_bg --a '/path/to/directory/'' # 7) Set background with a particular image in our image directory do --> 'bb_bg --i image_name' # 8) And the usage or help switch do --> 'bb_bg --U' # # # 2 and 3 of the above options may be added to the Blackbox and Fluxbox menus # as such. # [exec] (Background Slideshow) {bb_bg --s} # [exec] (Random Background) {bb_bg} # # # ... and as of v3.0, you can now also do ... # # --p This will tell bb_bg to use the current directory ignoring the config file. # --r Similar to the above, but it will also recurse into sub-directories! # # Let's first talk about '--r'. This one is huge in my opinion. But it has some drawbacks if # care is not taken while using it. Considering this will recurse into subdirectories, you may # want to be careful where you start it from. Starting bb_bg with this option from '/' as an example # would mean that it's going to try to look in EVERY SINGLE DIRECTORY IN YOUR SYSTEM! This will # no doubt send the memory usage through the roof while it makes a list of every file in your box. LOL! # I honestly have no idea of just how bad it could get, but be my guest on trying it. Just # let me know what happens. ROFL # # Now to explain number 7 in the list above. The idea here was that if you knew you had an image # or group of images but was less than certain of the name or extension, bb_bg would # do a little work for you and search the directories in your stead. As an example, # if you type in... # # bb_bg --i GP # # ...one of two things are going to happen. If it finds only one match for this pattern, it # will set the background and be done with. If it finds more than one match, then it will # show a slide show with the images that it finds. So if you have an image directory with # 300 images but only want to use all of your Gwyneth Paltrow backgrounds (and they all # start with GP), then bb_bg will do just that. Only show Gwyneth Paltrow pics that match # the 'GP' that you passed as an argument. # # Now be careful here as this isn't perfect, nor was it meant to be. It's rather fuzzy as a # matter of fact. If you have an image in an image directory that's actually a pic of Pol Pot # titled pol_GP.jpg, then the above example is going to include it with your pics of Gwyneth. # It would kinda' ruins things you know. # # Lastly, I am not sure just how buggy the code is with regards to all of the new # command line options. I am not 100% sure (about 80%) that you can allways get by # with using multiple command line arguments. I know some do work together, but # I have way too much work to do in other areas to finish at this time. So if # strange things happen when using a gang of different switches, you've been warned! # # # >>>WINDOWS<<< # # I imagine this could work in Windows. I have no idea what it would take to do this. If you # know, why not trying making one? # # # >>>TODO<<< # # Refactor! When? When I do it. :-) # # # >>>LIABILITY<<< # # Whatever happens when you run this program on your computer is ON YOU! # I am not responsible for any bad thing it may do as I make no claims that # it's fit for release and consumption by computers across the planet. If # an explosion or some other sort of mayhem jumps off, you agree that I am # not liable. Use of this script is acknowledgement of the above mentioned facts. # # Otherw ise, I hope you enjoy it. I may make yet more changes in time, but I also # have a million other things to change. # # Cheers, BDKR (TRC) ########################################################################### # Variable list $img_sizes=array ( '800x600', '1024x768', '1152x864', '1280x1024', '1600x1200' ); # Create our agument array then parse the command line arguments $arg_list=array ( '--a:', '--d:', '--p', '--r', '--s', '--t:', '--u:', '--i:', '--U', '--max_scale', '--tile', '--scale', '--center' ); $arguments=&get_cli_args($arg_list); # Get and deal with the time interval argument here $timer=0; foreach($arguments as $k) { if(strstr($k, '--t')) { $timer=1; $time=explode(':', $k); break; } } $timer==1 ? define("INTERVAL", $time[1]) : define("INTERVAL", 60); // For compatibility with roadsend reset($arguments); # Check that the rest of the commands passed were all good if($arguments[0]=='false') { show_usage($arguments[1]); exit; } # I guess they were. Now let's run the gauntlet. Based on the passed in switches, we will # decide on how we call the get_image_array() and serve_images() functions. if(strstr($arguments[0], ':')) /* For switches with arguments */ { $tmp_arr=explode(':', $arguments[0]); switch($tmp_arr[0]) { case '--a': /* Adds an additional image directory to the existing list */ { serve_images($timer, get_image_array($tmp_arr[0], $tmp_arr[1])); exit; } case '--d': /* Runs the specified image directory and ignores the config file */ { serve_images($timer, get_image_array($tmp_arr[0], $tmp_arr[1])); exit; } case '--i': /* Searches the image directories for the name specified */ { foreach($arguments as $zz) { if(strstr($zz, '--d')) { $temp_arr=explode(':', $zz); serve_images($timer, get_image_array('--d', $temp_arr[1]), '', $tmp_arr[1]); } elseif(strstr($zz, '--a')) { $temp_arr=explode(':', $zz); serve_images($timer, get_image_array('--a', $temp_arr[1]), '', $tmp_arr[1]); } } serve_images($timer, get_image_array(), '', $tmp_arr[1]); exit; } case '--t': /* Slide show with user specified interval */ { serve_images($timer, get_image_array()); exit; } case '--u': /* Shows only the image specified */ { exec('wmsetbg -a '.$tmp_arr[1]); exit; } } } else /* Deal with switches without arguments */ { switch($arguments[0]) { case '--s': /* Just do the slide show allready! */ { serve_images($timer, get_image_array()); exit; } case '--U': /* Show command line usage */ { show_usage(); exit; } case '--r': /* Much the same as --p, but als recursive */ case '--p': /* Use the current directory ignoring the config file */ { // serve_images(get_image_array('--p', $tmp_arr[1])); exit; if($timer==1) { serve_images($timer, get_image_array($arguments[0])); exit; } else { serve_images($timer, get_image_array($arguments[0], $tmp_arr[1])); exit; } } } } /* No arguments? Then just serve a random image */ serve_images($timer, get_image_array()); exit; ########################################################################### # Show the person how to use this thing function show_usage($errors='') { if($errors!='') { print "\"$errors\" is not a valid option.\n"; } print " Here are the available command line options:\n --s Slide show --d Provide a specific directory of images for the slideshow. --p Use the present direct. --r Much the same as the above, but will recurse into subdirectories. --a Add a directory to the configured list of image directories. --t Implies --s. Specifies interval for slideshow. The default is 180 seconds. --u Use a particular image. Must include full path --i Use a particular image in your image directory. No path or extension needed. --U Usage! What else would it be? --max_scale Scale image while preserving aspect ratio. (In development) --scale Scale the image to the screen dimensions. (In development) --tile Tile the image on the screen. (In development) --center Center the image on the screen. (In development) \n\n"; sleep(.5); } ########################################################################### ########################################################################### function check_slash($directory) { $directory=trim($directory); $len=strlen($directory); if($directory{$len-1}!='/') { $directory.='/'; } return $directory; } ########################################################################### ########################################################################### # Grab the arguments passed to the prog and return an array function get_cli_args($arg_list='') { $x=0; $junk=array(); /* bad things are stored here */ $args=array(); /* good things are stored here */ reset($_SERVER['argv']); /* This is paranoia for the most part */ next($_SERVER['argv']); while(list(, $switch)=each($_SERVER['argv'])) { if( (strstr($switch, '--')) && (in_array($switch.':', $arg_list) || in_array($switch, $arg_list)) ) { $args[]=$switch; } elseif(in_array($args[(count($args)-1)].':', $arg_list)) { $args[(count($args)-1)].=':'.$switch; } else { if(!in_array('false', $junk)) { $junk[]='false'; } $junk[]=$switch; return $junk; } ++$x; } return $args; } ########################################################################### ########################################################################### # Read the config file and generate an image list function get_image_array($mode='', $dir='') { # Since we are doing the random bit, create arrays for it now $d_arr=array(); /* Directory array */ $img_array=array(); /* This is obvious */ $d=array(); /* This assists in the population of the directory array */ # If the user wants to add a directory, do it here if($dir!='' && ($mode=='--a' || $mode=='--d') ) { if(is_dir($dir)) { $d_arr[]=check_slash($dir); } else { print "The provided directory does not exist. Doh!\n"; exit; } } # Now let's open the config file and get the users image directory if($mode=='--a') { if(file_exists($_SERVER['HOME'].'/.bb_bg')) { $file_array=file($_SERVER['HOME'].'/.bb_bg'); foreach($file_array as $k => $v) { $d_arr[]=check_slash($v); } } } elseif($mode=='--d') { /* do nothing as we are using a directory other than those in the config file */ $file_array=file($dir); foreach($file_array as $k => $v) { $d_arr[]=trim($v); } } elseif($mode=='') { if(file_exists($_SERVER['HOME'].'/.bb_bg')) { $file_array=file($_SERVER['HOME'].'/.bb_bg'); // print_r($file_array); foreach($file_array as $k => $v) { $d_arr[]=check_slash($v); } } } elseif($mode=='--p' || $mode=='--r') { $d_arr[]=check_slash($GLOBALS['_ENV']['PWD']); } # Otherwise, let's make an assumption else { $d_arr[]=trim($_SERVER['HOME'].'/GNUstep/Library/WindowMaker/Backgrounds/'); } # If we are recursing into sub-directories, use the directoryToArray function to generate # a list of all files then cull the images files from there with the image_cull function. # The image_cull function will also generate a returnable $img_array. if($mode=='--r') { $img_array=image_cull(directoryToArray($d_arr[0], 'y')); } else { foreach($d_arr as $m => $n) { if( (is_dir($n)) && ($d[$m]=&dir($n)) ) // More efficient using the reference operator { while(false!==($entry=$d[$m]->read())) { if( (stristr($entry, '.png')) || (stristr($entry, '.jpg')) || (stristr($entry, '.gif')) || (stristr($entry, '.jpeg')) ) { $img_array[]=$d[$m]->path.$entry; } } $d[$m]->close(); } } } // print_r($img_array); // exit; return $img_array; } ########################################################################### ########################################################################### # This is used in conjunction with directoryToArray(). While it will generate a list of # all files, this will create a new list from it with only image files greater then # a specified size. 20kb is the default. function &image_cull($f_arr, $min_size=20480) { $ret_arr=array(); $f_size=@sizeof($f_arr); $x=0; # Insert error reporting into this check if(!is_array($f_arr) || $f_size==0) { return false; } # Loop through the $f_arr array and add the image entries into the # returnable array ($ret_arr) for(; $x<=$f_size; ++$x) { if( (stristr($f_arr[$x], '.png')) || (stristr($f_arr[$x], '.jpg')) || (stristr($f_arr[$x], '.gif')) || (stristr($f_arr[$x], '.jpeg')) ) { if(!stristr($f_arr[$x], '_tn.') && !stristr($f_arr[$x], 'thumb') && (filesize($f_arr[$x])>$min_size)) { $ret_arr[]=$f_arr[$x]; } } } # Now return the results return $ret_arr; } ########################################################################### ########################################################################### # This was yanked from someplace online. There was no author name, which is a # shame as I'd love to give the person credit for it. If you wrote, please # let me know and I'll put your name on this script. # # This function will start at the specified directory and generate a list of # files that are in it and it's subdirectories. function &directoryToArray($directory, $recursive) { $array_items = array(); if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (is_dir($directory. "/" . $file)) { if($recursive) { $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive)); } $file = $directory . "/" . $file; $array_items[] = preg_replace("/\/\//si", "/", $file); } else { $file = $directory . "/" . $file; $array_items[] = preg_replace("/\/\//si", "/", $file); } } } closedir($handle); } return $array_items; } ########################################################################### ########################################################################### # Take the image array and iterate over it setting the background at each element function serve_images($timer, $img_array, $interval='', $one_img='', $img_sizes='') { shuffle($img_array); $switch_list=array('--s', '--t', '--d', '--a', '--i', '--p', '--r'); # Is the user specifying an image? if($one_img!='') { $specd_img=array(); if(strstr($one_img, ',')) { $req_img=explode(',', $one_img); foreach($req_img as $g) { foreach($img_array as $i) { if(strstr($i, $g)) { $specd_img[]=$i; } } } } else { srand((float)microtime()*1000000); // $img_array=&user_shuffle($img_array); shuffle($img_array); foreach($img_array as $i) { if(strstr($i, $one_img)) { $specd_img[]=$i; } } } # Now tally 'em up if(count($specd_img)>1) { unset($img_array); /* Why carry this for the duration of the program run if I don't need it? */ set_time_limit(0); $x=count($specd_img); $y=0; shuffle($specd_img); while(true) { if($y==($x-1)) { shuffle($specd_img); $y=0; } exec('wmsetbg -a '. $specd_img[$y]); sleep(INTERVAL); ++$y; } } elseif(count($specd_img)==1) { exec('wmsetbg -a '. $specd_img[0]); exit; } else { print "An image matching \"$one_img\" does not exist.\n"; sleep(1); } exit; } # Start slideshow in earnest srand((float)microtime()*1000000); $img_size=sizeof($img_array); // print_r($img_array); // exit; # Now start looping if we make it past all these conditions. $timer and $img_size have been added to deal # with some unexpected behaviour. if( (isset($_SERVER['argv'][1])) && (in_array($_SERVER['argv'][1], $switch_list)) && ($timer==1) && ($img_size>1) ) { // echo 'The argument is '.$_SERVER['argv'][1]."\n"; exit; set_time_limit(0); $img_cntr=0; $size=count($img_array); for( ; $img_cntr<=$size; ++$img_cntr) { // exec("wmsetbg -a $img_array[$img_cntr]"); exec('playwave /home/big/e*ping.wav'); exec("wmsetbg -a $img_array[$img_cntr]"); sleep(INTERVAL); /* for($s_cntr=0; $s_cntr