2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
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. *
30 *******************************************************************************
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
38 * $TOG: ChkpntClient.c /main/7 1998/04/09 17:49:06 mgreess $
40 * (C) Copyright 1993, Hewlett-Packard, all rights reserved.
42 *******************************************************************************
45 #define NUMPROPERTIES 8
46 #define INVALID_TIME ((Time) -1)
52 #include "Dt/ChkpntP.h"
53 #include "DtSvcLock.h"
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 */
68 * myCheckClientEvent --- Helper Boolean function to pass to XCheckIfEvent()
69 * Checks for PropertyNotify & SelectionNotify events in the event queue.
71 static Bool myCheckClientEvent(Display *display, XEvent *event, char *args)
75 /* Only check the client window events */
77 onMyWindow = (event->xany.window == dtcp_info.window);
78 _DtSvcProcessUnlock();
98 * myDtChkpntMsgSend --- Helper function: Send a checkpoint message to the listener
101 myDtChkpntMsgSend(char *message, char *type)
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;
116 /* Check to see if checkpoint is actually on */
118 if (dtcp_info.bChkpnt == False) {
119 _DtSvcProcessUnlock();
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 */
133 * We maintain a list of properties and use them round robin -- hoping to
135 * The listener should then track the message count to see if messages are
139 /* Fill the window property with necessary information */
140 status = XStringListToTextProperty(dtcp_msg.array,DT_PERF_CHKPNT_MSG_SIZE, &tp);
142 /* Hang the property on the window */
143 if ( !( (status == Success) || (status > 0) )) {
144 _DtSvcProcessUnlock();
148 XSetTextProperty(dtcp_info.display, dtcp_info.window,
149 &tp, dtcp_info.aProperty[propnum]);
152 if (oldtime != INVALID_TIME) { /* Valid timestamp to be recycled */
155 else { /* Check event queue */
156 bool = XCheckIfEvent(dtcp_info.display, &event,
157 myCheckClientEvent, NULL);
159 if (event.type == PropertyNotify)
160 timestamp = event.xproperty.time;
161 else timestamp = event.xselection.time;
163 else { /* Synthesize time by generating a PropertyNotify */
164 XChangeProperty(dtcp_info.display, dtcp_info.window,
165 dtcp_info.aProperty[propnum], XA_STRING,
167 (unsigned char *) NULL, 0);
168 XFlush(dtcp_info.display);
170 XWindowEvent(dtcp_info.display, dtcp_info.window,
171 PropertyChangeMask, &event);
172 if (event.type == PropertyNotify) {
173 timestamp = event.xproperty.time;
179 * Send the checkpoint message: do a ConvertSelection()
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);
189 * Toss SelectionNotify and PropertyNotify events from the event queue
191 oldtime = INVALID_TIME;
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);
201 * Increment the property and message counters
203 if (++propnum >= dtcp_info.maxprops)
207 _DtSvcProcessUnlock();
212 * _DtPerfChkpntInit --- Initialize the checkpointing mechanism
215 _DtPerfChkpntInit(Display *display,
220 static char winstring[32]; /* Storage for window id */
222 char propname[80]; /* Temp buffer for property name */
227 * Fill the dtcp_info structure
230 dtcp_info.display = display;
231 dtcp_info.pname = prog_name;
232 dtcp_info.bChkpnt = bChkpnt;
233 dtcp_info.aChkpntMsg = XA_STRING;
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,
247 * Check to see if listener is available
249 tmpwin = XGetSelectionOwner(dtcp_info.display, dtcp_info.aSelection);
250 if (tmpwin == None) { /* No listener */
251 dtcp_info.bChkpnt = False;
252 _DtSvcProcessUnlock();
257 * Create a permanent window for hanging messages on
259 tmpdisplay = display;
260 tmpdisplay = XOpenDisplay(""); /* Temporary display connection */
261 XSetCloseDownMode(tmpdisplay, RetainPermanent);
262 dtcp_info.window = XCreateSimpleWindow(tmpdisplay, parentwin,
264 BlackPixel(display,DefaultScreen(display)),
265 WhitePixel(display,DefaultScreen(display)));
266 { /* Hang a name on the permanent window => helps debugging */
269 XTextProperty text_prop;
271 buffer = malloc(strlen(prog_name) + 8);
272 sprintf(buffer, "DtPerf %s", prog_name);
275 XStringListToTextProperty(array, 1, &text_prop);
276 XSetWMName(tmpdisplay, dtcp_info.window, &text_prop);
277 XFree(text_prop.value);
281 XCloseDisplay(tmpdisplay);
284 * Pre-fill the message structure entries for "pname" and "window"
286 dtcp_msg.record.pname = prog_name;
287 sprintf(winstring, "%lx", (long) dtcp_info.window);
288 dtcp_msg.record.window = winstring;
291 * Express interest in Property change events
293 XSelectInput(dtcp_info.display, dtcp_info.window, PropertyChangeMask);
295 /* Inform listener that you are ready to send messages */
296 myDtChkpntMsgSend("Begin checkpoint delivery", DT_PERF_CHKPNT_MSG_INIT);
297 _DtSvcProcessUnlock();
299 } /* DtChkpntInit() */
303 * _DtPerfChkpntMsgSend --- Send a checkpoint message to the listener
306 _DtPerfChkpntMsgSend(char *message)
308 myDtChkpntMsgSend(message, DT_PERF_CHKPNT_MSG_CHKPNT);
312 * myDtPerfChkpntEnd --- End the checkpointing message delivery
317 myDtChkpntMsgSend("End checkpoint delivery", DT_PERF_CHKPNT_MSG_END);