php-17-fork-conky.php 1.51 KB
Newer Older
epsi sayidina's avatar
epsi sayidina committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#!/usr/bin/php
<?php # using PHP7

function get_lemon_parameters() 
{ 
    # geometry: -g widthxheight+x+y
    $xpos     = 0;
    $ypos     = 0;
    $width    = 640;
    $height   = 24;

    $geom_res = "${width}x${height}+${xpos}+${ypos}";

    # color, with transparency
    $fgcolor  = "'#000000'";
    $bgcolor  = "'#aaffffff'";

    # XFT: require lemonbar_xft_git 
    $font     = 'monospace-9';

    # finally  
    $parameters  =  " -g $geom_res -u 2"
                  . " -B $bgcolor -F $fgcolor"
                  . " -f $font";

    return $parameters;
}

function generated_output($process)
{
    $path    = __dir__."/../assets";
    $cmdin   = 'conky -c '.$path.'/conky.lua';
    $pipein  = popen($cmdin,  "r"); # handle
    
    while(!feof($pipein)) {
        $buffer = fgets($pipein);
        fwrite($process, $buffer);
        flush();
    }
    
    pclose($pipein);
}

function run_lemon() 
{ 
    $cmdout  = 'lemonbar '.get_lemon_parameters();
    $pipeout = popen($cmdout, "w");

    generated_output($pipeout);

    pclose($pipeout);
}

function detach_lemon() 
{ 
    $pid = pcntl_fork();
    
    switch($pid) {         
         case -1 : die('could not fork'); // fork errror         
         case 0  : run_lemon(); break;    // we are the child
         default : return $pid;           // we are the parent             
    }    
}

# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# main

# remove all lemonbar instance
system('pkill lemonbar');

# run process in the background
detach_lemon();