XlationSvc: remove a "'" added in previous spelling commit that causes warnings
[oweals/cde.git] / cde / lib / DtSvc / DtUtil2 / ChkpntClient.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 /*
24  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
25  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
26  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
27  * (c) Copyright 1993, 1994 Novell, Inc.                                *
28  */
29 /* -*-C-*-
30 *******************************************************************************
31 *
32 * File:         ChkpntClient.c
33 * Description:  CDE client-side checkpoint protocol functions. Private API
34 *               functions for use by the CDE client program.
35 * Created:      Mon Sep  6 09:00:00 1993
36 * Language:     C
37 *
38 * $TOG: ChkpntClient.c /main/7 1998/04/09 17:49:06 mgreess $
39 *
40 * (C) Copyright 1993, Hewlett-Packard, all rights reserved.
41 *
42 *******************************************************************************
43 */
44
45 #define NUMPROPERTIES 8
46 #define INVALID_TIME    ((Time) -1)
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <sys/time.h>
51 #include <time.h>
52 #include "Dt/ChkpntP.h"
53 #include "DtSvcLock.h"
54
55 static struct {
56     Display     *display;               /* Display pointer              */
57     Window      window;                 /* Window id for the program    */
58     char        *pname;                 /* Actual name of the program   */
59     Atom        aSelection;             /* Atom  for root selection     */
60     Atom        *aProperty;             /* Atom array for window props  */
61     int         maxprops;               /* Size of above array          */
62     Atom        aChkpntMsg;             /* Atom  for Checkpoint message */
63     Boolean     bChkpnt;                /* Should I do checkpointing ?  */
64 }                     dtcp_info;        /* Data structure for this client*/
65 static DtChkpntMsg    dtcp_msg;         /* Message structure union      */
66
67 /*
68  * myCheckClientEvent --- Helper Boolean function to pass to XCheckIfEvent()
69  * Checks for PropertyNotify & SelectionNotify events in the event queue.
70  */
71 static Bool myCheckClientEvent(Display *display, XEvent *event, char *args)
72 {
73     Boolean onMyWindow;
74
75     /* Only check the client window events */
76     _DtSvcProcessLock();
77     onMyWindow = (event->xany.window == dtcp_info.window);
78     _DtSvcProcessUnlock();
79
80     if (!onMyWindow)
81         return(False);
82
83     switch(event->type)
84     {
85         case PropertyNotify:
86         case SelectionNotify:
87             return(True);
88             break;
89
90         default:
91             break;
92     }
93
94     return (False);
95 }
96
97 /*
98  * myDtChkpntMsgSend --- Helper function: Send a checkpoint message to the listener
99  */
100 static int
101 myDtChkpntMsgSend(char *message, char *type)
102 {
103     static long     msgcount = 0;  /* Running count of messages */
104     static int      propnum  = 0;  /* Which property are we using ? */
105     static Time     oldtime  = INVALID_TIME; /* Recycled from old PropertyNotify events */
106     Time            timestamp= INVALID_TIME;
107     char            buf_msgcount[32];
108     char            buf_seconds[128];
109     struct timeval  time_val;
110     struct timezone time_zone;
111     XTextProperty   tp;
112     Status          status;
113     Bool            bool;
114     XEvent          event;
115
116     /* Check to see if checkpoint is actually on */
117     _DtSvcProcessLock();
118     if (dtcp_info.bChkpnt == False) {
119         _DtSvcProcessUnlock();
120         return(0);
121     }
122
123     /* Fill the message list. ("pname" and "window" were filled at init) */
124     dtcp_msg.record.type = type;
125     sprintf(buf_msgcount, "%ld", msgcount);
126     dtcp_msg.record.count   = buf_msgcount;     /* Running message count */
127     gettimeofday(&time_val, &time_zone);
128     sprintf(buf_seconds,"%lf",time_val.tv_sec + (time_val.tv_usec/1000000.0 ));
129     dtcp_msg.record.seconds = buf_seconds;      /* Info from gettimeofday()*/
130     dtcp_msg.record.message = message;          /* Actual message string */
131
132     /*
133      * We maintain a list of properties and use them round robin -- hoping to
134      * never run out.
135      * The listener should then track the message count to see if messages are
136      * getting dropped.
137      */
138
139     /* Fill the window property with necessary information */
140     status = XStringListToTextProperty(dtcp_msg.array,DT_PERF_CHKPNT_MSG_SIZE, &tp);
141
142     /* Hang the property on the window */
143     if ( !( (status == Success) || (status > 0) )) {
144         _DtSvcProcessUnlock();
145         return(0);
146     }
147
148     XSetTextProperty(dtcp_info.display, dtcp_info.window,
149                         &tp,             dtcp_info.aProperty[propnum]);
150     XFree(tp.value);
151
152     if (oldtime != INVALID_TIME) {  /* Valid timestamp to be recycled */
153         timestamp = oldtime;
154     }
155     else {                   /* Check event queue */
156         bool = XCheckIfEvent(dtcp_info.display, &event,
157                          myCheckClientEvent, NULL);
158         if (bool == True) {
159             if (event.type == PropertyNotify)
160                  timestamp = event.xproperty.time; 
161             else timestamp = event.xselection.time;
162             }
163         else {     /* Synthesize time by generating a PropertyNotify */
164             XChangeProperty(dtcp_info.display,     dtcp_info.window,
165                     dtcp_info.aProperty[propnum],   XA_STRING,
166                     8,                      PropModeAppend,
167                     (unsigned char *) NULL, 0);
168             XFlush(dtcp_info.display);
169             loop:
170             XWindowEvent(dtcp_info.display, dtcp_info.window,
171                         PropertyChangeMask,  &event);
172             if (event.type == PropertyNotify) {
173                 timestamp = event.xproperty.time;
174             }
175             else goto loop;
176         }
177     }
178     /*
179      * Send the checkpoint message: do a ConvertSelection()
180      */
181
182     /* Note: Currently listener makes no use of the "aChkpntMsg" info */
183     XConvertSelection(dtcp_info.display,dtcp_info.aSelection,
184                   dtcp_info.aChkpntMsg, dtcp_info.aProperty[propnum],
185                   dtcp_info.window,     timestamp);
186     XFlush(dtcp_info.display);
187
188     /*
189      * Toss SelectionNotify and PropertyNotify events from the event queue
190      */
191     oldtime = INVALID_TIME;
192     do {
193         bool = XCheckIfEvent(dtcp_info.display, &event,
194                              myCheckClientEvent, NULL);
195         if (event.type == PropertyNotify) /* Save timestamp for recycling */
196              oldtime = event.xproperty.time;
197         else oldtime = event.xselection.time;
198     } while(bool == True);
199
200     /*
201      * Increment the property and message counters
202      */
203     if (++propnum >= dtcp_info.maxprops)
204         propnum = 0;
205     msgcount++;
206
207     _DtSvcProcessUnlock();
208     return(1);
209 }
210
211 /*
212  * _DtPerfChkpntInit --- Initialize the checkpointing mechanism
213  */
214 int
215 _DtPerfChkpntInit(Display      *display,
216              Window       parentwin,
217              char         *prog_name,
218              Boolean      bChkpnt)
219 {
220     static        char winstring[32];   /* Storage for window id */
221     Window        tmpwin;
222     char          propname[80]; /* Temp buffer for property name */
223     Display       *tmpdisplay;
224     int    i;
225
226     /*
227      * Fill the dtcp_info structure 
228      */
229     _DtSvcProcessLock();
230     dtcp_info.display    = display;
231     dtcp_info.pname      = prog_name;
232     dtcp_info.bChkpnt    = bChkpnt;
233     dtcp_info.aChkpntMsg = XA_STRING;
234
235     /* Pre-compute Atom names and save them away in the dtcp_info structure */
236     dtcp_info.aSelection = XInternAtom(dtcp_info.display, 
237                                         DT_PERF_CHKPNT_SEL,     False);
238     dtcp_info.maxprops   = NUMPROPERTIES;
239     dtcp_info.aProperty  = (Atom *) malloc(dtcp_info.maxprops * sizeof(Atom));
240     for (i= 0; i < dtcp_info.maxprops; i++) {
241         sprintf(propname, "%s_%x", DT_PERF_CLIENT_CHKPNT_PROP, i);
242         dtcp_info.aProperty[i]  = XInternAtom(dtcp_info.display,
243                                             propname, False);
244     }
245
246     /*
247      * Check to see if listener is available
248      */
249     tmpwin  = XGetSelectionOwner(dtcp_info.display, dtcp_info.aSelection);
250     if (tmpwin == None) {       /* No listener */
251         dtcp_info.bChkpnt   = False;
252         _DtSvcProcessUnlock();
253         return(0);
254     }
255
256     /*
257      * Create a permanent window for hanging messages on
258      */
259     tmpdisplay = display;
260     tmpdisplay = XOpenDisplay("");      /* Temporary display connection */
261     XSetCloseDownMode(tmpdisplay, RetainPermanent);
262     dtcp_info.window     = XCreateSimpleWindow(tmpdisplay, parentwin, 
263                             1, 1, 100, 100, 1,
264                             BlackPixel(display,DefaultScreen(display)),
265                             WhitePixel(display,DefaultScreen(display)));
266     {   /* Hang a name on the permanent window => helps debugging */
267         char *buffer;
268         char *array[2];
269         XTextProperty text_prop;
270
271         buffer = malloc(strlen(prog_name) + 8);
272         sprintf(buffer, "DtPerf %s", prog_name);
273         array[0] = buffer;
274         array[1] = "";
275         XStringListToTextProperty(array, 1, &text_prop);
276         XSetWMName(tmpdisplay, dtcp_info.window, &text_prop);
277         XFree(text_prop.value);
278         free(buffer);
279     }
280
281     XCloseDisplay(tmpdisplay);
282
283     /*
284      * Pre-fill the message structure entries for "pname" and "window"
285      */
286     dtcp_msg.record.pname  = prog_name;
287     sprintf(winstring, "%lx", (long) dtcp_info.window);
288     dtcp_msg.record.window = winstring;
289
290     /*
291      * Express interest in Property change events
292      */
293     XSelectInput(dtcp_info.display, dtcp_info.window, PropertyChangeMask);
294
295     /* Inform listener that you are ready to send messages */
296     myDtChkpntMsgSend("Begin checkpoint delivery", DT_PERF_CHKPNT_MSG_INIT);
297     _DtSvcProcessUnlock();
298     return(1);
299 }       /* DtChkpntInit() */
300
301
302 /*
303  * _DtPerfChkpntMsgSend --- Send a checkpoint message to the listener
304  */
305 void
306 _DtPerfChkpntMsgSend(char *message) 
307 {
308     myDtChkpntMsgSend(message, DT_PERF_CHKPNT_MSG_CHKPNT);
309 }
310
311 /*
312  * myDtPerfChkpntEnd --- End the checkpointing message delivery
313  */
314 int
315 _DtPerfChkpntEnd() 
316 {
317     myDtChkpntMsgSend("End checkpoint delivery", DT_PERF_CHKPNT_MSG_END);
318     return(1);
319 }
320