Saver.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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: Saver.c /main/8 1996/11/21 19:56:41 drk $ */
  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: Saver.c
  34. **
  35. ** Description:
  36. ** -----------
  37. ** This file contains public and private screen saver utilities.
  38. **
  39. ** Public:
  40. ** DtSaverGetWindows() - return array of windows on which saver can draw
  41. **
  42. ** Private:
  43. ** _DtSaverStart() - launch specified screen saver
  44. ** _DtSaverStop() - kill specified screen saver
  45. **
  46. *****************************************************************************
  47. *************************************<+>*************************************/
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #define X_INCLUDE_STRING_H
  51. #define XOS_USE_XT_LOCKING
  52. #include <X11/Xos_r.h>
  53. #include <X11/Intrinsic.h>
  54. #include <Saver.h>
  55. #include <SaverP.h>
  56. #include <Dt/Action.h>
  57. #include "DtSvcLock.h"
  58. /*
  59. * Constants global to this file.
  60. */
  61. #define DT_SAVER_MAX_SCREENS 10
  62. struct saver_state {
  63. unsigned short serial;
  64. Window xid;
  65. struct saver_state *next;
  66. };
  67. static Atom xa_saver_register;
  68. static struct saver_state saver_list = {0, (Window)0, NULL};
  69. /*
  70. * Local functions.
  71. */
  72. static void RegisterSaverCB(
  73. Widget w,
  74. XtPointer client_data,
  75. XEvent *event,
  76. Boolean *continue_to_dispatch
  77. );
  78. /*************************************<->*************************************
  79. *
  80. * _DtSaverStart() - start a screen saver
  81. *
  82. * Description:
  83. * -----------
  84. * _DtSaverStart() is one of a suite of screen saver API's used in the
  85. * desktop. These APIs are:
  86. *
  87. * _DtSaverStart() starts a screen saver (private)
  88. * _DtSaverStop() stops a screen saver (private)
  89. * DtSaverGetWindows() return array of windows on which saver can draw
  90. *
  91. * The _DtSaverStart() API allocates a state variable for the screen saver
  92. * which contains a serial number and NIL window ID. A list of these state
  93. * variables is maintained, one for each call to _DtSaverStart().
  94. * DtSaverStart() then sets up the DTSAVERINFO environment variable containing
  95. * the serial number, window count and window list provided. Finally, it
  96. * launches the provided action and returns an opaque pointer to the state
  97. * variable. The action is expected to be a screen saver which makes use
  98. * of the DtSaverGetWindows() API.
  99. *
  100. * When the screen saver starts, it calls the DtSaverGetWindows() API. From
  101. * the screen saver perspective, the API returns an array of windows on which
  102. * the screen saver can draw. To to this, the API obtains the DTSAVERINFO
  103. * environment variable, and parses out the window array. The API also
  104. * creates a window and sends a ClientMessage to the first DTSAVERINFO window
  105. * containing the serial number and newly created window id.
  106. *
  107. * RegisterSaverCB() is a callback called when the ClientMessage arrives from
  108. * a screen saver. This callback first searches the screen saver state list
  109. * by serial number. If a state variable is not found, RegisterSaverCB()
  110. * assumes the screen saver must have been stopped by a call to
  111. * _DtSaverStop(), so kills the client via XKillClient() using the window id
  112. * provided in the message. If the state variable is located,
  113. * RegisterSaverCB() stores the window id in the state variable.
  114. *
  115. * _DtSaverStop() searches the screen saver list using the serial number
  116. * provided in the input state variable. It should always be found. When
  117. * found, if the state variable window id is set, _DtSaverStop() kills the
  118. * screen saver client via XKillClient(), deletes the state variable from
  119. * the list and deallocates the state variable.
  120. *
  121. * Inputs:
  122. * ------
  123. * display - display structure
  124. * drawArea - array of widgets to be stored drawn upon by saver
  125. * count - number of elements in drawArea array
  126. * saverAction - screen saver action name to invoke
  127. * wAction - widget on which possible DtActionInvoke() errors should display
  128. *
  129. * Outputs:
  130. * -------
  131. *
  132. * Return:
  133. * -------
  134. * state - pointer to opaque state structure
  135. *
  136. * Comments:
  137. * --------
  138. * This function uses DtActionInvoke() to launch an action. As a result,
  139. * the caller is responsible for loading and maintaining the action database
  140. * using the DtDbLoad() function and procedures. The caller
  141. * must call _DtSaverStop() to terminate screen saver
  142. *
  143. *************************************<->***********************************/
  144. void *
  145. _DtSaverStart(
  146. Display *display,
  147. Widget *drawArea,
  148. int count,
  149. char *saverAction,
  150. Widget wAction)
  151. {
  152. static char envdata[(DT_SAVER_MAX_SCREENS * 12) + 20];
  153. struct saver_state *state;
  154. struct saver_state *p;
  155. int i;
  156. _DtSvcProcessLock();
  157. /*
  158. * If first time in, insert envdata in process environment.
  159. */
  160. if (saver_list.serial == 0)
  161. {
  162. #if !defined(__linux__) && !defined(CSRG_BASED)
  163. /* JET - how can this ever work anyway? */
  164. putenv(envdata);
  165. envdata[0] = '\0';
  166. #endif
  167. xa_saver_register = XInternAtom(display, "_DT_SAVER_REGISTER", False);
  168. }
  169. /*
  170. * Add event handler (it might already be there - that's ok).
  171. */
  172. XtAddEventHandler(drawArea[0], 0, True, RegisterSaverCB, NULL);
  173. /*
  174. * Allocate state structure for this saver.
  175. */
  176. if (!(state = (struct saver_state *)malloc(sizeof(struct saver_state))))
  177. {
  178. _DtSvcProcessUnlock();
  179. return(NULL);
  180. }
  181. /*
  182. * Initialize state structure and append to saver_list.
  183. */
  184. state->serial = saver_list.serial++;
  185. state->xid = (Window)0;
  186. state->next = NULL;
  187. p = &saver_list;
  188. while (p->next != NULL)
  189. {
  190. p = p->next;
  191. }
  192. p->next = state;
  193. /*
  194. * Set up environment. It will look like:
  195. * DTSAVERINFO="<serial> <count> <win0> <win1> ... <winN>"
  196. */
  197. sprintf(envdata, "DTSAVERINFO=%u %i %lx",
  198. state->serial, count, XtWindow(drawArea[0]));
  199. for (i = 1; i < count; i++)
  200. {
  201. char *pe = envdata + strlen(envdata);
  202. sprintf(pe, " %lx", XtWindow(drawArea[i]));
  203. }
  204. #if defined(__linux__) || defined(CSRG_BASED)
  205. putenv(envdata);
  206. #endif
  207. _DtSvcProcessUnlock();
  208. /*
  209. * Launch saver.
  210. */
  211. DtActionInvoke(wAction, saverAction, NULL, 0,
  212. NULL, NULL, NULL, 0, NULL, NULL);
  213. /*
  214. * Return array as state information.
  215. */
  216. return((void *)state);
  217. }
  218. /*************************************<->*************************************
  219. *
  220. * _DtSaverStop() - stop a screen saver
  221. *
  222. * Description:
  223. * -----------
  224. * Stop an external screen saver started with DtStartSaver().
  225. *
  226. * _DtSaverStop() searches the screen saver list using the serial number
  227. * provided in the input state variable. It should always be found. When
  228. * found, if the state variable window id is set, _DtSaverStop() kills the
  229. * screen saver client via XKillClient(), deletes the state variable from
  230. * the list and deallocates the state variable.
  231. *
  232. * Inputs:
  233. * ------
  234. * display - display structure
  235. * state - state returned from _DtSaverStart()
  236. *
  237. * Outputs:
  238. * -------
  239. *
  240. * Return:
  241. * -------
  242. *
  243. * Comments:
  244. * --------
  245. *
  246. *************************************<->***********************************/
  247. void
  248. _DtSaverStop(
  249. Display *display,
  250. void *pstate)
  251. {
  252. struct saver_state *state = (struct saver_state *)pstate;
  253. struct saver_state *p;
  254. _DtSvcProcessLock();
  255. /*
  256. * Unlink from saver_list.
  257. */
  258. p = &saver_list;
  259. while (p->next != state)
  260. {
  261. p = p->next;
  262. }
  263. p->next = state->next;
  264. _DtSvcProcessUnlock();
  265. /*
  266. * Kill client using window id provided by RegisterSaverCB().
  267. */
  268. if (state->xid != (Window)0)
  269. {
  270. XKillClient(display, state->xid);
  271. }
  272. /*
  273. * Free state allocated by _DtSaverStart();
  274. */
  275. free(pstate);
  276. }
  277. /*************************************<->*************************************
  278. *
  279. * DtSaverGetWindows() - return array of windows on which saver can draw
  280. *
  281. * Description:
  282. * -----------
  283. *
  284. * This is a PUBLIC API.
  285. *
  286. * When the screen saver starts, it calls the DtSaverGetWindows() API. From
  287. * the screen saver perspective, the API returns an array of windows on which
  288. * the screen saver can draw. To to this, the API obtains the DTSAVERINFO
  289. * environment variable, and parses out the window array. The API also
  290. * creates a window and sends a ClientMessage to the first DTSAVERINFO window
  291. * containing the serial number and newly created window id.
  292. *
  293. * Inputs:
  294. * ------
  295. * display - display structure
  296. * window - pointer to memory in which to place pointer to array
  297. * count - pointer to memory in which to place count
  298. *
  299. * Outputs:
  300. * -------
  301. * *window - pointer to array
  302. * *count - count
  303. *
  304. * Return:
  305. * -------
  306. * True - window list returned
  307. * False - window list not returned
  308. *
  309. * Comments:
  310. * --------
  311. * The array memory should be freed by the caller via free().
  312. *
  313. *************************************<->***********************************/
  314. Boolean
  315. DtSaverGetWindows(
  316. Display *display,
  317. Window **window,
  318. int *count)
  319. {
  320. char *envdata, *p, *q;
  321. unsigned short serial;
  322. int envcount;
  323. XClientMessageEvent event;
  324. Window xid_window;
  325. _Xstrtokparams strtok_buf;
  326. _DtSvcDisplayToAppContext(display);
  327. _DtSvcAppLock(app);
  328. *window = NULL;
  329. *count = 0;
  330. _DtSvcProcessLock();
  331. xa_saver_register = XInternAtom(display, "_DT_SAVER_REGISTER", False);
  332. /*
  333. * Get invocation information from environment.
  334. */
  335. envdata = getenv("DTSAVERINFO");
  336. if (!envdata)
  337. {
  338. _DtSvcProcessUnlock();
  339. _DtSvcAppUnlock(app);
  340. return(False);
  341. }
  342. /*
  343. * Copy string for later strtok() use.
  344. */
  345. p = strdup(envdata);
  346. if (!p)
  347. {
  348. _DtSvcProcessUnlock();
  349. _DtSvcAppUnlock(app);
  350. return(False);
  351. }
  352. /*
  353. * Extract serial.
  354. */
  355. q = _XStrtok(p, " ", strtok_buf);
  356. serial = (unsigned short)strtoul(q, NULL, 10);
  357. /*
  358. * Extract envcount.
  359. */
  360. q = _XStrtok(NULL, " ", strtok_buf);
  361. envcount = (int)strtoul(q, NULL, 10);
  362. /*
  363. * Allocate memory for window array.
  364. */
  365. *window = (Window *)malloc((envcount)*sizeof(Window *));
  366. if (!*window)
  367. {
  368. free(p);
  369. _DtSvcProcessUnlock();
  370. _DtSvcAppUnlock(app);
  371. return(False);
  372. }
  373. /*
  374. * Populate result array and envcount.
  375. */
  376. for (*count = 0; *count < envcount; (*count)++)
  377. {
  378. q = _XStrtok(NULL, " ", strtok_buf);
  379. (*window)[*count] = (Window)strtoul(q, NULL, 16);
  380. }
  381. /*
  382. * Free temp copy of envdata.
  383. */
  384. free(p);
  385. /*
  386. * Create dummy window to obtain XID.
  387. */
  388. xid_window = XCreateWindow(display, DefaultRootWindow(display),
  389. 0, 0, 1, 1, 0,
  390. CopyFromParent, InputOutput, CopyFromParent,
  391. 0, NULL);
  392. if (xid_window == (Window)0)
  393. {
  394. /*
  395. * Could not create dummy window.
  396. */
  397. free((char *)*window);
  398. *window = NULL;
  399. *count = 0;
  400. _DtSvcProcessUnlock();
  401. _DtSvcAppUnlock(app);
  402. return(False);
  403. }
  404. /*
  405. * Send client message to win0 to register.
  406. */
  407. event.type = ClientMessage;
  408. event.window = (*window)[0];
  409. event.message_type = xa_saver_register;
  410. event.format = 32;
  411. event.data.l[0] = (Atom)0;
  412. event.data.l[1] = (long)serial;
  413. event.data.l[2] = (long)xid_window;
  414. event.data.l[3] = CurrentTime;
  415. XSendEvent(display, (*window)[0], False, NoEventMask,
  416. (XEvent *) &event);
  417. _DtSvcProcessUnlock();
  418. /*
  419. * Ensure window creation and client message have been processed by
  420. * the server before continuing.
  421. */
  422. XSync(display, False);
  423. _DtSvcAppUnlock(app);
  424. return(True);
  425. }
  426. /*************************************<->*************************************
  427. *
  428. * RegisterSaverCB() - register a screen saver
  429. *
  430. * Description:
  431. * -----------
  432. * RegisterSaverCB() is a callback called when the ClientMessage arrives from
  433. * a screen saver. This callback first searches the screen saver state list
  434. * by serial number. If a state variable is not found, RegisterSaverCB()
  435. * assumes the screen saver must have been stopped by a call to
  436. * _DtSaverStop(), so kills the client via XKillClient() using the window id
  437. * provided in the message. If the state variable is located,
  438. * RegisterSaverCB() stores the window id in the state variable.
  439. *
  440. * Inputs:
  441. * ------
  442. * w - widget from which we derive the display
  443. * client_data - pointer to client data (unused)
  444. * event - ClientMessage event structure
  445. * continue_to_dispatch - dispatch to remaining event handlers (unused)
  446. *
  447. * Outputs:
  448. * -------
  449. *
  450. * Return:
  451. * -------
  452. *
  453. * Comments:
  454. * --------
  455. *
  456. *************************************<->***********************************/
  457. static void
  458. RegisterSaverCB(
  459. Widget w,
  460. XtPointer client_data,
  461. XEvent *event,
  462. Boolean *continue_to_dispatch)
  463. {
  464. if (event->type == ClientMessage)
  465. {
  466. XClientMessageEvent *cEvent = (XClientMessageEvent *) event;
  467. _DtSvcProcessLock();
  468. if (cEvent->message_type == xa_saver_register)
  469. {
  470. unsigned short serial = (unsigned short)cEvent->data.l[1];
  471. Window win = (Window)cEvent->data.l[2];
  472. struct saver_state *state;
  473. /*
  474. * Find event in saver list.
  475. */
  476. state = saver_list.next;
  477. while (state != NULL && state->serial != serial)
  478. {
  479. state = state->next;
  480. }
  481. if (state != NULL)
  482. {
  483. /*
  484. * _DtSaverStop() not yet called for this saver. Store xid in
  485. * saver's state for _DtSaverStop()'s use.
  486. */
  487. state->xid = win;
  488. }
  489. else
  490. {
  491. /*
  492. * _DtSaverStop() has already been called for this saver, but at the
  493. * time, the saver had not yet registered. Kill the saver client.
  494. */
  495. XKillClient(XtDisplay(w), win);
  496. }
  497. }
  498. _DtSvcProcessUnlock();
  499. }
  500. }