Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtsession / SmScreen.c
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 librararies 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
45 #include <stdio.h>
46 #include <signal.h>
47 #include <X11/Intrinsic.h>
48 #include <X11/Xutil.h>
49 #include <X11/Xatom.h>
50 #include <Dt/Wsm.h>
51 #include <Dt/UserMsg.h>
52 #include <Dt/SaverP.h>
53 #include "Sm.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 /*
62  * Structures visible to this module only.
63  */
64 typedef struct {
65   int count;
66   char *saver[1];   
67   /* variable length saver[] array */
68   /* saver command strings */
69 } SmSaverParseStruct;
70
71 /*
72  * Variables global to this module only
73  */
74 static int savernum;             /* current screen saver number */
75 static void *saverstate = NULL;  /* current running screen saver state */
76 static int firsttime = 1;        /* first call to StartScreenSaver */
77
78 /*
79  * Local Function declarations
80  */
81 static void ParseSaverList(char *, int *, int *, SmSaverParseStruct *);
82
83
84 \f
85 /*************************************<->*************************************
86  *
87  *  StartScreenSaver ()
88  *
89  *
90  *  Description:
91  *  -----------
92  *  Start an external screen saver.
93  *
94  *  Inputs:
95  *  ------
96  *
97  * 
98  *  Outputs:
99  *  -------
100  *
101  *  Comments:
102  *  --------
103  * 
104  *************************************<->***********************************/
105
106 void
107 StartScreenSaver( void )
108 {
109   int i;
110   SmSaverParseStruct *parse;
111
112   if (!smGD.saverListParse)
113   {
114    /*
115     * Parse the screen saver list.
116     */
117     smGD.saverListParse = SmSaverParseSaverList(smGD.saverList);
118
119     if (!smGD.saverListParse)
120     {
121       return;
122     }
123     savernum = -1;
124   }
125
126   parse = (SmSaverParseStruct *)smGD.saverListParse;
127
128   if (parse->count == 0)
129   {
130     return;
131   }
132
133  /*
134   * Decide which saver number to use.
135   */
136   savernum = (savernum + 1) % parse->count;
137
138   if (firsttime)
139   {
140    /*
141     * Load actions database.
142     */
143     ProcessReloadActionsDatabase();
144     firsttime = 0;
145   }
146
147  /*
148   * Start screen saver. _DtSaverStop() must be called to terminate the
149   * screen saver.
150   */
151   saverstate = _DtSaverStart(smGD.display, smDD.coverDrawing,
152                   smGD.numSavedScreens, parse->saver[savernum],
153                   smGD.topLevelWid);
154
155 }
156
157 \f
158 /*************************************<->*************************************
159  *
160  *  StopScreenSaver ()
161  *
162  *
163  *  Description:
164  *  -----------
165  *  Stop an external screen saver.
166  *
167  *  Inputs:
168  *  ------
169  *
170  * 
171  *  Outputs:
172  *  -------
173  *
174  *  Comments:
175  *  --------
176  * 
177  *************************************<->***********************************/
178 void
179 StopScreenSaver( void )
180 {
181   if (saverstate)
182   {
183    /*
184     * Terminate screen saver.
185     */
186     _DtSaverStop(smGD.display, saverstate);
187     saverstate = NULL;
188   }
189 }
190
191 /*************************************<->*************************************
192  *
193  *  SmSaverParseSaverList()
194  *
195  *
196  *  Description:
197  *  -----------
198  *  Parse screen saver list into allocated buffer.
199  *
200  *  SaverLine = {SaverSpec|WhiteSpace}
201  *  SaverSpec = WhiteSpace Command WhiteSpace
202  *  Command = <valid action name>
203  *  WhiteSpace = {<space>|<horizontal tab>|<line feed>}
204  *
205  *  For example, a saverList resource might be specified as:
206  *    *saverList:   \n \
207  *       StartDtscreenSwarm      \n\
208  *       StartDtscreenQix        \n\
209  *       StartDtscreenLife
210  *
211  *  And be represented in memory as:
212  *    "StartDtscreenSwarm   \n StartDtscreenQix\n  StartDtscreenLife"
213  *
214  *
215  *  Inputs:
216  *  ------
217  *  saverList - pointer to screen saver list. This memory is not changed.
218  *
219  *  Outputs:
220  *  -------
221  *  none
222  *
223  *  Return:
224  *  -------
225  *  pointer to allocated memory containing parsed saver list
226  *
227  *  Comments:
228  *  --------
229  *
230  *************************************<->***********************************/
231
232 void *
233 SmSaverParseSaverList(
234   char *saverList)
235 {
236    char tokenSep[] = " \n\t";
237    char * token;
238    int i = 0;
239    char * tmpStr;
240    int len = strlen(saverList);
241    int bytes = sizeof(int);
242    char *p;
243    SmSaverParseStruct *pstruct;
244
245    tmpStr = (char *)XtMalloc(len + 1);
246    memcpy(tmpStr, saverList, len+1);
247    token = strtok(tmpStr, tokenSep);
248    while(token != NULL)
249    {
250      i++;
251      bytes += sizeof(char *) + strlen(token) + 1;
252      token = strtok(NULL, tokenSep);
253    }
254
255    pstruct = (SmSaverParseStruct *)XtMalloc(bytes);
256
257    if (pstruct)
258    {
259      memcpy(tmpStr, saverList, len+1);
260      token = strtok(tmpStr, tokenSep);
261      pstruct->count = 0;
262      p = (char *)(pstruct->saver + i);
263
264      while(token != NULL)
265      {
266        pstruct->saver[pstruct->count] = p;
267        strcpy(pstruct->saver[pstruct->count], token);
268        p += strlen(token) + 1;
269        token = strtok(NULL, tokenSep);
270        pstruct->count++;
271      }
272    }
273    XtFree ((char *) tmpStr);
274    return((void *)pstruct);
275 }