SmScreen.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: SmScreen.c /main/4 1995/10/30 09:38:03 rswiston $ */
  24. /* *
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  26. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  27. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  28. * (c) Copyright 1993, 1994 Novell, Inc. *
  29. */
  30. /*************************************<+>*************************************
  31. *****************************************************************************
  32. **
  33. ** File: SmScreen.c
  34. **
  35. ** Project: DT Session Manager (dtsession)
  36. **
  37. ** Description:
  38. ** -----------
  39. ** This file contains all routines needed to manage external
  40. ** screen savers.
  41. **
  42. *****************************************************************************
  43. *************************************<+>*************************************/
  44. #include <stdio.h>
  45. #include <signal.h>
  46. #include <X11/Intrinsic.h>
  47. #include <X11/Xutil.h>
  48. #include <X11/Xatom.h>
  49. #include <Dt/Wsm.h>
  50. #include <Dt/UserMsg.h>
  51. #include <Dt/SaverP.h>
  52. #include "Sm.h"
  53. #include "SmCommun.h"
  54. #include "SmUI.h" /* smDD.* */
  55. #include "SmError.h"
  56. #include "SmWindow.h"
  57. #include "SmProtocol.h"
  58. #include "SmGlobals.h"
  59. #include "SmScreen.h"
  60. /*
  61. * Structures visible to this module only.
  62. */
  63. typedef struct {
  64. int count;
  65. char *saver[1];
  66. /* variable length saver[] array */
  67. /* saver command strings */
  68. } SmSaverParseStruct;
  69. /*
  70. * Variables global to this module only
  71. */
  72. static int savernum; /* current screen saver number */
  73. static void *saverstate = NULL; /* current running screen saver state */
  74. static int firsttime = 1; /* first call to StartScreenSaver */
  75. /*
  76. * Local Function declarations
  77. */
  78. static void ParseSaverList(char *, int *, int *, SmSaverParseStruct *);
  79. /*************************************<->*************************************
  80. *
  81. * StartScreenSaver ()
  82. *
  83. *
  84. * Description:
  85. * -----------
  86. * Start an external screen saver.
  87. *
  88. * Inputs:
  89. * ------
  90. *
  91. *
  92. * Outputs:
  93. * -------
  94. *
  95. * Comments:
  96. * --------
  97. *
  98. *************************************<->***********************************/
  99. void
  100. StartScreenSaver( void )
  101. {
  102. int i;
  103. SmSaverParseStruct *parse;
  104. if (!smGD.saverListParse)
  105. {
  106. /*
  107. * Parse the screen saver list.
  108. */
  109. smGD.saverListParse = SmSaverParseSaverList(smGD.saverList);
  110. if (!smGD.saverListParse)
  111. {
  112. return;
  113. }
  114. savernum = -1;
  115. }
  116. parse = (SmSaverParseStruct *)smGD.saverListParse;
  117. if (parse->count == 0)
  118. {
  119. return;
  120. }
  121. /*
  122. * Decide which saver number to use.
  123. */
  124. savernum = (savernum + 1) % parse->count;
  125. if (firsttime)
  126. {
  127. /*
  128. * Load actions database.
  129. */
  130. ProcessReloadActionsDatabase();
  131. firsttime = 0;
  132. }
  133. /*
  134. * Start screen saver. _DtSaverStop() must be called to terminate the
  135. * screen saver.
  136. */
  137. saverstate = _DtSaverStart(smGD.display, smDD.coverDrawing,
  138. smGD.numSavedScreens, parse->saver[savernum],
  139. smGD.topLevelWid);
  140. }
  141. /*************************************<->*************************************
  142. *
  143. * StopScreenSaver ()
  144. *
  145. *
  146. * Description:
  147. * -----------
  148. * Stop an external screen saver.
  149. *
  150. * Inputs:
  151. * ------
  152. *
  153. *
  154. * Outputs:
  155. * -------
  156. *
  157. * Comments:
  158. * --------
  159. *
  160. *************************************<->***********************************/
  161. void
  162. StopScreenSaver( void )
  163. {
  164. if (saverstate)
  165. {
  166. /*
  167. * Terminate screen saver.
  168. */
  169. _DtSaverStop(smGD.display, saverstate);
  170. saverstate = NULL;
  171. }
  172. }
  173. /*************************************<->*************************************
  174. *
  175. * SmSaverParseSaverList()
  176. *
  177. *
  178. * Description:
  179. * -----------
  180. * Parse screen saver list into allocated buffer.
  181. *
  182. * SaverLine = {SaverSpec|WhiteSpace}
  183. * SaverSpec = WhiteSpace Command WhiteSpace
  184. * Command = <valid action name>
  185. * WhiteSpace = {<space>|<horizontal tab>|<line feed>}
  186. *
  187. * For example, a saverList resource might be specified as:
  188. * *saverList: \n \
  189. * StartDtscreenSwarm \n\
  190. * StartDtscreenQix \n\
  191. * StartDtscreenLife
  192. *
  193. * And be represented in memory as:
  194. * "StartDtscreenSwarm \n StartDtscreenQix\n StartDtscreenLife"
  195. *
  196. *
  197. * Inputs:
  198. * ------
  199. * saverList - pointer to screen saver list. This memory is not changed.
  200. *
  201. * Outputs:
  202. * -------
  203. * none
  204. *
  205. * Return:
  206. * -------
  207. * pointer to allocated memory containing parsed saver list
  208. *
  209. * Comments:
  210. * --------
  211. *
  212. *************************************<->***********************************/
  213. void *
  214. SmSaverParseSaverList(
  215. char *saverList)
  216. {
  217. char tokenSep[] = " \n\t";
  218. char * token;
  219. int i = 0;
  220. char * tmpStr;
  221. int len = strlen(saverList);
  222. int bytes = sizeof(long);
  223. char *p;
  224. SmSaverParseStruct *pstruct;
  225. tmpStr = (char *)XtMalloc(len + 1);
  226. memcpy(tmpStr, saverList, len+1);
  227. token = strtok(tmpStr, tokenSep);
  228. while(token != NULL)
  229. {
  230. i++;
  231. bytes += sizeof(char *) + strlen(token) + 1;
  232. token = strtok(NULL, tokenSep);
  233. }
  234. pstruct = (SmSaverParseStruct *)XtMalloc(bytes);
  235. if (pstruct)
  236. {
  237. memcpy(tmpStr, saverList, len+1);
  238. token = strtok(tmpStr, tokenSep);
  239. pstruct->count = 0;
  240. p = (char *)(pstruct->saver + i);
  241. while(token != NULL)
  242. {
  243. pstruct->saver[pstruct->count] = p;
  244. strcpy(pstruct->saver[pstruct->count], token);
  245. p += strlen(token) + 1;
  246. token = strtok(NULL, tokenSep);
  247. pstruct->count++;
  248. }
  249. }
  250. XtFree ((char *) tmpStr);
  251. return((void *)pstruct);
  252. }