xstoxfces 10.0 KB

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