ChkpntClient.c 9.8 KB

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