xstoxfces 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/bin/php
  2. <?php
  3. /*
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. const N="\n";
  16. const SNAME='xstoxfces';
  17. const SVERS='0.1.3';
  18. $opts=[
  19. 'xscreensaver_user_conf_file'=>null,
  20. 'xscreensaver_conf_dir'=>'/usr/share/xscreensaver/config',
  21. 'xscreensaver_exec_dir'=>'/usr/lib/xscreensaver',
  22. 'xfce_screensaver_desktop_files_dir'=>null,
  23. 'preserve_xscreensaver_exclusions'=>false,
  24. 'preserve_xscreensaver_arguments'=>false
  25. ];
  26. $help=
  27. 'SYNOPSIS
  28. '.SNAME.' [options]
  29. DESCRIPTION
  30. This is '.SNAME.' v'.SVERS.', a CLI php script that makes xscreensaver’s
  31. screensavers available to «Xfce screensaver»’s users.
  32. Currently, «Xfce screensaver» supports xscreensaver’s screensavers, but does
  33. not automatically make them available to users. This script just converts
  34. xscreensaver’s screensavers «.xml» definition files to «.desktop» files and
  35. saves these «.desktop» files in a directory where «Xfce screensaver» will
  36. consider them (by default «$HOME/.local/share/applications/screensavers»);
  37. in addition, it can optionally exclude screensavers and preserve their
  38. configuration (that is, their arguments) according to an xscreensaver’s
  39. user configuration file («$HOME/.xscreensaver» by default).
  40. OPTIONS
  41. -h, --help
  42. Show this help text and exit.
  43. -p, --preserve_xscreensaver_exclusions
  44. With this option '.SNAME.' will exclude those screensavers which the user
  45. may have excluded using xscreensaver-settings’s «Random Screen Saver»
  46. option, by detecting them from xscreensaver’s user configuration file
  47. («$HOME/.xscreensaver» by default).
  48. -P, --preserve_xscreensaver_arguments
  49. With this option '.SNAME.' will preserve every screensaver’s arguments which
  50. the user may have set using xscreensaver-settings, by detecting them from
  51. xscreensaver’s user configuration file («$HOME/.xscreensaver» by default).
  52. -C, --xscreensaver_user_conf_file <file path>
  53. If '.SNAME.' is invoked with «--preserve_xscreensaver_exclusions»
  54. or «--preserve_xscreensaver_arguments» (see the previous two options’
  55. descriptions), it will try to automatically detect xscreensaver’s user
  56. configuration file’s path («$HOME/.xscreensaver» by default). If it fails,
  57. or if you want to specify a different xscreensaver configuration file, you
  58. can do it explicitly with this option.
  59. -c, --xscreensaver_conf_dir <directory path>
  60. With this option you can explicitly specify the xscreensaver configuration
  61. directory, that is the system directory where xscreensaver stores all the
  62. «.xml» files defining its screensavers.
  63. DEFAULT: «'.$opts['xscreensaver_conf_dir'].'»
  64. -e, --xscreensaver_exec_dir <directory path>
  65. With this option you can explicitly specify the xscreensaver executables
  66. directory, that is the system directory where xscreensaver stores all its
  67. screensavers’ executable files.
  68. DEFAULT: «'.$opts['xscreensaver_exec_dir'].'»
  69. -d, --xfce_screensaver_desktop_files_dir <directory path>
  70. With this option you can explicitly specify '.SNAME.'’s «output directory»
  71. path, that should be the path to a directory where «Xfce screensaver» expects
  72. to find «.desktop» files defining screensavers (by default
  73. «$HOME/.local/share/applications/screensavers»).
  74. NOTES
  75. You can use '.SNAME.' to make xscreensaver’s screensavers available to every
  76. user of «Xfce screensaver» by running '.SNAME.' as root and setting
  77. «--xfce_screensaver_desktop_files_dir» (see above) to the system directory
  78. where «Xfce screensaver» expects to find the «.desktop» files defining all the
  79. screensavers and their configurations (usually
  80. «/usr/share/applications/screensavers»).
  81. EXIT VALUES
  82. 0: regular run
  83. 1: fatal error
  84. DISCLAIMER AND LICENSE
  85. This program comes with ABSOLUTELY NO WARRANTY; for details see the source.
  86. This is free software, and you are welcome to redistribute it under certain
  87. conditions; see <http://www.gnu.org/licenses/> for details.'.N;
  88. /*$chome=getenv('XDG_CONFIG_HOME',true);
  89. echo($chome.N);*/
  90. for ($i=1; $i<$argc; $i++) {
  91. if ($argv[$i][0]=='-') {
  92. switch ($argv[$i]) {
  93. case '--makereadme':
  94. file_put_contents(__DIR__.'/README.md','```text'.N.$help.'```'.N);
  95. exit(0);
  96. break;
  97. case '-h':
  98. case '--help':
  99. echo($help);
  100. exit(0);
  101. break;
  102. case '-C':
  103. case '--xscreensaver_user_conf_file':
  104. if (isset($argv[$i+1])) {
  105. $i++;
  106. $opts['xscreensaver_user_conf_file']=preg_replace('#^(.*)/*$#uU','$1',$argv[$i]);
  107. } else {
  108. fwrite(STDERR,'Error: option «'.$argv[$i].'» requires an argument (use «-h» or «--help» for more info).'.N);
  109. exit(0);
  110. }
  111. break;
  112. case '-c':
  113. case '--xscreensaver_conf_dir':
  114. if (isset($argv[$i+1])) {
  115. $i++;
  116. $opts['xscreensaver_conf_dir']=preg_replace('#^(.*)/*$#uU','$1',$argv[$i]);
  117. } else {
  118. fwrite(STDERR,'Error: option «'.$argv[$i].'» requires an argument (use «-h» or «--help» for more info).'.N);
  119. exit(0);
  120. }
  121. break;
  122. case '-e':
  123. case '--xscreensaver_exec_dir':
  124. if (isset($argv[$i+1])) {
  125. $i++;
  126. $opts['xscreensaver_exec_dir']=preg_replace('#^(.*)/*$#uU','$1',$argv[$i]);
  127. } else {
  128. fwrite(STDERR,'Error: option «'.$argv[$i].'» requires an argument (use «-h» or «--help» for more info).'.N);
  129. exit(0);
  130. }
  131. break;
  132. case '-d':
  133. case '--xfce_screensaver_desktop_files_dir':
  134. if (isset($argv[$i+1])) {
  135. $i++;
  136. $opts['xfce_screensaver_desktop_files_dir']=preg_replace('#^(.*)/*$#uU','$1',$argv[$i]);
  137. } else {
  138. fwrite(STDERR,'Error: option «'.$argv[$i].'» requires an argument (use «-h» or «--help» for more info).'.N);
  139. exit(0);
  140. }
  141. break;
  142. case '-p':
  143. case '--preserve_xscreensaver_exclusions':
  144. $opts['preserve_xscreensaver_exclusions']=true;
  145. break;
  146. case '-P':
  147. case '--preserve_xscreensaver_arguments':
  148. $opts['preserve_xscreensaver_arguments']=true;
  149. break;
  150. default:
  151. fwrite(STDERR,'Error: «'.$argv[$i].'» is not a valid option.'.N);
  152. exit(1);
  153. }
  154. } else {
  155. fwrite(STDERR,'Error: «'.$argv[$i].'» is not a valid option.'.N);
  156. exit(1);
  157. }
  158. }
  159. //print_r($opts); exit(0);
  160. if (is_null($opts['xfce_screensaver_desktop_files_dir'])) {
  161. $home=@getenv('HOME');
  162. if ($home===false) {
  163. fwrite(STDERR,'Error: could not get «HOME» environment variable, you’ll have to specify «xfce_screensaver_desktop_files_dir» by yourself (use «-h» or «--help» for more info).'.N);
  164. exit(1);
  165. } else {
  166. $opts['xfce_screensaver_desktop_files_dir']=$home.'/.local/share/applications/screensavers';
  167. }
  168. }
  169. if (is_null($opts['xscreensaver_user_conf_file']) && ($opts['preserve_xscreensaver_exclusions'] || $opts['preserve_xscreensaver_arguments'])) {
  170. $home=@getenv('HOME');
  171. if ($home===false) {
  172. fwrite(STDERR,'Error: could not get «HOME» environment variable, you’ll have to specify «xscreensaver_user_conf_file» by yourself (use «-h» or «--help» for more info).'.N);
  173. exit(1);
  174. } else {
  175. $opts['xscreensaver_user_conf_file']=$home.'/.xscreensaver';
  176. }
  177. }
  178. if ($opts['preserve_xscreensaver_exclusions'] || $opts['preserve_xscreensaver_arguments']) {
  179. $buff=@file($opts['xscreensaver_user_conf_file'],FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
  180. if ($buff===false) {
  181. fwrite(STDERR,'Error: could not open «'.$opts['xscreensaver_user_conf_file'].'».'.N);
  182. exit(1);
  183. }
  184. $xscrs=[];
  185. $i=0;
  186. $c=count($buff);
  187. while ($i<$c) {
  188. if (preg_match('/^([\-\d\S ]+)?\t{4}([\S\d \-_]+)\s+(\\\n)?\\\$/uU',$buff[$i],$matches)===1) {
  189. //print_r($matches);
  190. $cmd=$matches[2];
  191. //echo($cmd.'|'.N);
  192. ($matches[1]!='' && $matches[1][0]==='-') ? $active=false : $active=true;
  193. preg_match('/^(\S+).*$/',$matches[2],$matches);
  194. //print_r($matches);
  195. $fn=$matches[1];
  196. //echo($buff[$i].N);
  197. //echo($matches[2].N);
  198. while (substr($buff[$i],-3)!=="\\n\\") {
  199. $i++;
  200. //echo('`-> '.$buff[$i].N);
  201. preg_match('/^\s+([\S\d \-_]+)\s+.*$/u',$buff[$i],$matches);
  202. //print_r($matches);
  203. $cmd.=' '.$matches[1];
  204. }
  205. $xscrs[$fn]=['cmd'=>$cmd,'active'=>$active];
  206. }
  207. $i++;
  208. }
  209. //print_r($xscrs);
  210. }
  211. $dh=@opendir($opts['xscreensaver_conf_dir']);
  212. if ($dh===false) {
  213. fwrite(STDERR,'Error: could not open «'.$opts['xscreensaver_conf_dir'].'».'.N);
  214. exit(1);
  215. }
  216. if (!file_exists($opts['xfce_screensaver_desktop_files_dir'])) {
  217. if (mkdir($opts['xfce_screensaver_desktop_files_dir'],0755,true)===false) {
  218. fwrite(STDERR,'Error: could not create «'.$opts['xfce_screensaver_desktop_files_dir'].'».'.N);
  219. exit(1);
  220. }
  221. }
  222. while (($fp=readdir($dh))!==false) {
  223. if (preg_match('/\.xml$/iu',$fp)===1) {
  224. $fp=$opts['xscreensaver_conf_dir'].'/'.$fp;
  225. $xml=@simplexml_load_file($fp);
  226. if ($xml!==false) {
  227. if (isset($xml['name']) && isset($xml['_label']) && isset($xml->command['arg'])) {
  228. $ofp=$opts['xfce_screensaver_desktop_files_dir'].'/'.$xml['name'].'.desktop';
  229. if (is_file($ofp) && @unlink($ofp)===false)
  230. fwrite(STDERR,'Warning: could not delete «'.$ofp.'».'.N);
  231. $cmd=$xml['name'].' '.$xml->command['arg'];
  232. if ($opts['preserve_xscreensaver_arguments'] && isset($xscrs[(string)$xml['name']]['cmd']))
  233. $cmd=$xscrs[(string)$xml['name']]['cmd'];
  234. if (!$opts['preserve_xscreensaver_exclusions'] || $xscrs[(string)$xml['name']]['active']) {
  235. $cont=
  236. '[Desktop Entry]
  237. Type=Application
  238. Name='.$xml['_label'].'
  239. TryExec='.$opts['xscreensaver_exec_dir'].'/'.$xml['name'].'
  240. Exec='.$opts['xscreensaver_exec_dir'].'/'.$cmd.'
  241. Categories=Screensaver;
  242. Hidden=true'.N;
  243. if (@file_put_contents($ofp,$cont)===false)
  244. fwrite(STDERR,'Warning: trying to add «'.$xml['name'].'», could not write into «'.$ofp.'».'.N);
  245. else
  246. echo('Added «'.$xml['name'].'»'.N);
  247. } else {
  248. echo('Ignored «'.$xml['name'].'»'.N);
  249. }
  250. } else {
  251. fwrite(STDERR,'Warning: «'.$fp.'» does not contain one or more required info.'.N);
  252. }
  253. } else {
  254. fwrite(STDERR,'Warning: could not parse «'.$fp.'».'.N);
  255. }
  256. }
  257. }
  258. closedir($dh);
  259. exit(0);
  260. ?>