xstoxfces/xstoxfces
2025-06-18 16:57:28 +02:00

294 lines
10 KiB
PHP
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/php
<?php
/*
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
const N="\n";
const SNAME='xstoxfces';
const SVERS='0.1.3';
$opts=[
'xscreensaver_user_conf_file'=>null,
'xscreensaver_conf_dir'=>'/usr/share/xscreensaver/config',
'xscreensaver_exec_dir'=>'/usr/lib/xscreensaver',
'xfce_screensaver_desktop_files_dir'=>null,
'preserve_xscreensaver_exclusions'=>false,
'preserve_xscreensaver_arguments'=>false
];
$help=
'SYNOPSIS
'.SNAME.' [options]
DESCRIPTION
This is '.SNAME.' v'.SVERS.', a CLI php script that makes xscreensavers
screensavers available to «Xfce screensaver»s users.
Currently, «Xfce screensaver» supports xscreensavers screensavers, but does
not automatically make them available to users. This script just converts
xscreensavers screensavers «.xml» definition files to «.desktop» files and
saves these «.desktop» files in a directory where «Xfce screensaver» will
consider them (by default «$HOME/.local/share/applications/screensavers»);
in addition, it can optionally exclude screensavers and preserve their
configuration (that is, their arguments) according to an xscreensavers
user configuration file («$HOME/.xscreensaver» by default).
OPTIONS
-h, --help
Show this help text and exit.
-p, --preserve_xscreensaver_exclusions
With this option '.SNAME.' will exclude those screensavers which the user
may have excluded using xscreensaver-settingss «Random Screen Saver»
option, by detecting them from xscreensavers user configuration file
(«$HOME/.xscreensaver» by default).
-P, --preserve_xscreensaver_arguments
With this option '.SNAME.' will preserve every screensavers arguments which
the user may have set using xscreensaver-settings, by detecting them from
xscreensavers user configuration file («$HOME/.xscreensaver» by default).
-C, --xscreensaver_user_conf_file <file path>
If '.SNAME.' is invoked with «--preserve_xscreensaver_exclusions»
or «--preserve_xscreensaver_arguments» (see the previous two options
descriptions), it will try to automatically detect xscreensavers user
configuration files path («$HOME/.xscreensaver» by default). If it fails,
or if you want to specify a different xscreensaver configuration file, you
can do it explicitly with this option.
-c, --xscreensaver_conf_dir <directory path>
With this option you can explicitly specify the xscreensaver configuration
directory, that is the system directory where xscreensaver stores all the
«.xml» files defining its screensavers.
DEFAULT: «'.$opts['xscreensaver_conf_dir'].
-e, --xscreensaver_exec_dir <directory path>
With this option you can explicitly specify the xscreensaver executables
directory, that is the system directory where xscreensaver stores all its
screensavers executable files.
DEFAULT: «'.$opts['xscreensaver_exec_dir'].
-d, --xfce_screensaver_desktop_files_dir <directory path>
With this option you can explicitly specify '.SNAME.'s «output directory»
path, that should be the path to a directory where «Xfce screensaver» expects
to find «.desktop» files defining screensavers (by default
«$HOME/.local/share/applications/screensavers»).
NOTES
You can use '.SNAME.' to make xscreensavers screensavers available to every
user of «Xfce screensaver» by running '.SNAME.' as root and setting
«--xfce_screensaver_desktop_files_dir» (see above) to the system directory
where «Xfce screensaver» expects to find the «.desktop» files defining all the
screensavers and their configurations (usually
«/usr/share/applications/screensavers»).
EXIT VALUES
0: regular run
1: fatal error
DISCLAIMER AND LICENSE
This program comes with ABSOLUTELY NO WARRANTY; for details see the source.
This is free software, and you are welcome to redistribute it under certain
conditions; see <http://www.gnu.org/licenses/> for details.'.N;
/*$chome=getenv('XDG_CONFIG_HOME',true);
echo($chome.N);*/
for ($i=1; $i<$argc; $i++) {
if ($argv[$i][0]=='-') {
switch ($argv[$i]) {
case '--makereadme':
file_put_contents(__DIR__.'/README.md','```text'.N.$help.'```'.N);
exit(0);
break;
case '-h':
case '--help':
echo($help);
exit(0);
break;
case '-C':
case '--xscreensaver_user_conf_file':
if (isset($argv[$i+1])) {
$i++;
$opts['xscreensaver_user_conf_file']=preg_replace('#^(.*)/*$#uU','$1',$argv[$i]);
} else {
fwrite(STDERR,'Error: option «'.$argv[$i].'» requires an argument (use «-h» or «--help» for more info).'.N);
exit(0);
}
break;
case '-c':
case '--xscreensaver_conf_dir':
if (isset($argv[$i+1])) {
$i++;
$opts['xscreensaver_conf_dir']=preg_replace('#^(.*)/*$#uU','$1',$argv[$i]);
} else {
fwrite(STDERR,'Error: option «'.$argv[$i].'» requires an argument (use «-h» or «--help» for more info).'.N);
exit(0);
}
break;
case '-e':
case '--xscreensaver_exec_dir':
if (isset($argv[$i+1])) {
$i++;
$opts['xscreensaver_exec_dir']=preg_replace('#^(.*)/*$#uU','$1',$argv[$i]);
} else {
fwrite(STDERR,'Error: option «'.$argv[$i].'» requires an argument (use «-h» or «--help» for more info).'.N);
exit(0);
}
break;
case '-d':
case '--xfce_screensaver_desktop_files_dir':
if (isset($argv[$i+1])) {
$i++;
$opts['xfce_screensaver_desktop_files_dir']=preg_replace('#^(.*)/*$#uU','$1',$argv[$i]);
} else {
fwrite(STDERR,'Error: option «'.$argv[$i].'» requires an argument (use «-h» or «--help» for more info).'.N);
exit(0);
}
break;
case '-p':
case '--preserve_xscreensaver_exclusions':
$opts['preserve_xscreensaver_exclusions']=true;
break;
case '-P':
case '--preserve_xscreensaver_arguments':
$opts['preserve_xscreensaver_arguments']=true;
break;
default:
fwrite(STDERR,'Error: «'.$argv[$i].'» is not a valid option.'.N);
exit(1);
}
} else {
fwrite(STDERR,'Error: «'.$argv[$i].'» is not a valid option.'.N);
exit(1);
}
}
//print_r($opts); exit(0);
if (is_null($opts['xfce_screensaver_desktop_files_dir'])) {
$home=@getenv('HOME');
if ($home===false) {
fwrite(STDERR,'Error: could not get «HOME» environment variable, youll have to specify «xfce_screensaver_desktop_files_dir» by yourself (use «-h» or «--help» for more info).'.N);
exit(1);
} else {
$opts['xfce_screensaver_desktop_files_dir']=$home.'/.local/share/applications/screensavers';
}
}
if (is_null($opts['xscreensaver_user_conf_file']) && ($opts['preserve_xscreensaver_exclusions'] || $opts['preserve_xscreensaver_arguments'])) {
$home=@getenv('HOME');
if ($home===false) {
fwrite(STDERR,'Error: could not get «HOME» environment variable, youll have to specify «xscreensaver_user_conf_file» by yourself (use «-h» or «--help» for more info).'.N);
exit(1);
} else {
$opts['xscreensaver_user_conf_file']=$home.'/.xscreensaver';
}
}
if ($opts['preserve_xscreensaver_exclusions'] || $opts['preserve_xscreensaver_arguments']) {
$buff=@file($opts['xscreensaver_user_conf_file'],FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
if ($buff===false) {
fwrite(STDERR,'Error: could not open «'.$opts['xscreensaver_user_conf_file'].'».'.N);
exit(1);
}
$xscrs=[];
$i=0;
$c=count($buff);
while ($i<$c) {
if (preg_match('/^([\-\d\S ]+)?\t{4}([\S\d \-_]+)\s+(\\\n)?\\\$/uU',$buff[$i],$matches)===1) {
//print_r($matches);
$cmd=$matches[2];
//echo($cmd.'|'.N);
($matches[1]!='' && $matches[1][0]==='-') ? $active=false : $active=true;
preg_match('/^(\S+).*$/',$matches[2],$matches);
//print_r($matches);
$fn=$matches[1];
//echo($buff[$i].N);
//echo($matches[2].N);
while (substr($buff[$i],-3)!=="\\n\\") {
$i++;
//echo('`-> '.$buff[$i].N);
preg_match('/^\s+([\S\d \-_]+)\s+.*$/u',$buff[$i],$matches);
//print_r($matches);
$cmd.=' '.$matches[1];
}
$xscrs[$fn]=['cmd'=>$cmd,'active'=>$active];
}
$i++;
}
//print_r($xscrs);
}
$dh=@opendir($opts['xscreensaver_conf_dir']);
if ($dh===false) {
fwrite(STDERR,'Error: could not open «'.$opts['xscreensaver_conf_dir'].'».'.N);
exit(1);
}
if (!file_exists($opts['xfce_screensaver_desktop_files_dir'])) {
if (mkdir($opts['xfce_screensaver_desktop_files_dir'],0755,true)===false) {
fwrite(STDERR,'Error: could not create «'.$opts['xfce_screensaver_desktop_files_dir'].'».'.N);
exit(1);
}
}
while (($fp=readdir($dh))!==false) {
if (preg_match('/\.xml$/iu',$fp)===1) {
$fp=$opts['xscreensaver_conf_dir'].'/'.$fp;
$xml=@simplexml_load_file($fp);
if ($xml!==false) {
if (isset($xml['name']) && isset($xml['_label']) && isset($xml->command['arg'])) {
$ofp=$opts['xfce_screensaver_desktop_files_dir'].'/'.$xml['name'].'.desktop';
if (is_file($ofp) && @unlink($ofp)===false)
fwrite(STDERR,'Warning: could not delete «'.$ofp.'».'.N);
$cmd=$xml['name'].' '.$xml->command['arg'];
if ($opts['preserve_xscreensaver_arguments'] && isset($xscrs[(string)$xml['name']]['cmd']))
$cmd=$xscrs[(string)$xml['name']]['cmd'];
if (!$opts['preserve_xscreensaver_exclusions'] || (isset($xscrs[(string)$xml['name']]['active']) && $xscrs[(string)$xml['name']]['active'])) {
$cont=
'[Desktop Entry]
Type=Application
Name='.$xml['_label'].'
TryExec='.$opts['xscreensaver_exec_dir'].'/'.$xml['name'].'
Exec='.$opts['xscreensaver_exec_dir'].'/'.$cmd.'
Categories=Screensaver;
Hidden=true'.N;
if (@file_put_contents($ofp,$cont)===false)
fwrite(STDERR,'Warning: trying to add «'.$xml['name'].'», could not write into «'.$ofp.'».'.N);
else
echo('Added «'.$xml['name'].'»'.N);
} else {
echo('Ignored «'.$xml['name'].'»'.N);
}
} else {
fwrite(STDERR,'Warning: «'.$fp.'» does not contain one or more required info.'.N);
}
} else {
fwrite(STDERR,'Warning: could not parse «'.$fp.'».'.N);
}
}
}
closedir($dh);
exit(0);
?>