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