session.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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: session.c /main/3 1995/11/01 10:39:26 rswiston $ */
  24. /**********************************<+>*************************************
  25. ***************************************************************************
  26. **
  27. ** File: session.c
  28. **
  29. ** Project: DT dtpad, a memo maker type editor based on the motif
  30. ** text widget.
  31. **
  32. ** Description: Provides the functionality for saving and restoring the
  33. ** user's session.
  34. **
  35. **
  36. *******************************************************************
  37. ** (c) Copyright Hewlett-Packard Company, 1990, 1991, 1992, 1993.
  38. ** All rights are
  39. ** reserved. Copying or other reproduction of this program
  40. ** except for archival purposes is prohibited without prior
  41. ** written consent of Hewlett-Packard Company.
  42. ********************************************************************
  43. **
  44. ********************************************************************
  45. ** (c) Copyright 1993, 1994 Hewlett-Packard Company
  46. ** (c) Copyright 1993, 1994 International Business Machines Corp.
  47. ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  48. ** (c) Copyright 1993, 1994 Novell, Inc.
  49. ********************************************************************
  50. **
  51. **
  52. **************************************************************************
  53. **********************************<+>*************************************/
  54. #include "dtpad.h"
  55. #include <Dt/Wsm.h>
  56. /*
  57. * This for the sleazy hack to get the window manager frame width/height
  58. */
  59. #include <Xm/VendorSEP.h>
  60. /* Copied from BaseClassI.h */
  61. extern XmWidgetExtData _XmGetWidgetExtData(
  62. Widget widget,
  63. #if NeedWidePrototypes
  64. unsigned int extType) ;
  65. #else
  66. unsigned char extType) ;
  67. #endif /* NeedWidePrototypes */
  68. #define MSG1 ((char *)GETMESSAGE(10, 1, "Check the file permissions."))
  69. #define MSG2 ((char *)GETMESSAGE(10, 2, "%s is the file that would have been used to save your session."))
  70. extern Widget topLevelWithWmCommand; /* declared in main.c */
  71. extern Editor *pPadList;
  72. extern int numActivePads;
  73. /* Structure used on a save session to see if a dt is iconic */
  74. static Atom wm_state_atom;
  75. typedef struct
  76. {
  77. int state;
  78. Window icon;
  79. } WM_STATE;
  80. /************************************************************************
  81. *
  82. * SaveMain - saves the parameters associated with a Pad's mainWindow
  83. * (called from SaveSessionCB() below).
  84. *
  85. ***********************************************************************/
  86. void
  87. SaveMain(
  88. Editor *pPad,
  89. int padNum,
  90. int fd)
  91. {
  92. char bufr[1024];
  93. Position x,y;
  94. Dimension width, height;
  95. Atom *pWsPresence;
  96. unsigned long numInfo;
  97. Atom actual_type;
  98. int actual_format;
  99. unsigned long nitems;
  100. unsigned long leftover;
  101. WM_STATE * wm_state;
  102. if(pPad->mainWindow != (Widget)NULL)
  103. {
  104. XmVendorShellExtObject vendorExt;
  105. XmWidgetExtData extData;
  106. if(XtIsRealized(pPad->mainWindow))
  107. sprintf(bufr,"*mainWindow%d.ismapped: True\n", padNum);
  108. /* Get and write out the geometry info for our Window */
  109. x = XtX(XtParent(pPad->mainWindow));
  110. y = XtY(XtParent(pPad->mainWindow));
  111. /*
  112. * Modify x & y to take into account window mgr frames
  113. * This is pretty bogus, but I don't know a better way to do it.
  114. */
  115. extData = _XmGetWidgetExtData(pPad->app_shell, XmSHELL_EXTENSION);
  116. vendorExt = (XmVendorShellExtObject)extData->widget;
  117. x -= vendorExt->vendor.xOffset;
  118. y -= vendorExt->vendor.yOffset;
  119. width = XtWidth(XtParent(pPad->mainWindow));
  120. height = XtHeight(XtParent(pPad->mainWindow));
  121. sprintf(bufr, "%s*mainWindow%d.x: %d\n", bufr, padNum, x);
  122. sprintf(bufr, "%s*mainWindow%d.y: %d\n", bufr, padNum, y);
  123. sprintf(bufr, "%s*mainWindow%d.width: %d\n", bufr, padNum, width);
  124. sprintf(bufr, "%s*mainWindow%d.height: %d\n", bufr, padNum, height);
  125. wm_state_atom = XmInternAtom (XtDisplay(pPad->app_shell), "WM_STATE",
  126. False);
  127. /* Getting the WM_STATE property to see if iconified or not */
  128. XGetWindowProperty (XtDisplay(pPad->app_shell),
  129. XtWindow (pPad->app_shell),
  130. wm_state_atom, 0L, (long) BUFSIZ, False,
  131. wm_state_atom, &actual_type, &actual_format,
  132. &nitems, &leftover, (unsigned char **) &wm_state);
  133. /* Write out if iconified our not */
  134. sprintf(bufr, "%s*mainWindow%d.iconify: ", bufr, padNum);
  135. if (wm_state->state == IconicState)
  136. sprintf (bufr, "%sTrue\n", bufr);
  137. else
  138. sprintf (bufr, "%sFalse\n", bufr);
  139. if(DtWsmGetWorkspacesOccupied(XtDisplay(pPad->app_shell),
  140. XtWindow(pPad->app_shell), &pWsPresence,
  141. &numInfo) == Success)
  142. {
  143. int i;
  144. sprintf(bufr, "%s*mainWindow%d.workspaceList: ", bufr, padNum);
  145. for(i = 0; i < numInfo; i++)
  146. {
  147. char *name = XGetAtomName(XtDisplay(pPad->app_shell),
  148. pWsPresence[i]);
  149. sprintf(bufr, "%s %s", bufr, name);
  150. XtFree(name);
  151. }
  152. sprintf(bufr, "%s\n", bufr);
  153. XtFree((char *)pWsPresence);
  154. }
  155. write (fd, bufr, strlen(bufr));
  156. }
  157. if(pPad->fileStuff.fileName != (char *)NULL)
  158. {
  159. sprintf(bufr, "*mainWindow%d.fileName: %s\n", padNum,
  160. pPad->fileStuff.fileName);
  161. write (fd, bufr, strlen(bufr));
  162. }
  163. }
  164. /************************************************************************
  165. *
  166. * SaveSessionCB - saves the editor state (just filename) - does not save
  167. * the file at this time.
  168. *
  169. * This routines is setup as the "WM_SAVE_YOURSELF" WMProtocolCallback
  170. * on the top level widget (created via XtInitialize).
  171. *
  172. ************************************************************************/
  173. /* ARGSUSED */
  174. void
  175. SaveSessionCB(
  176. Widget w, /* widget id */
  177. caddr_t client_data, /* data from application */
  178. caddr_t call_data ) /* data from widget class */
  179. {
  180. char *longpath, *fileName;
  181. int fd, numPadsToSave;
  182. char *xa_CommandStr[10];
  183. char *tmpStr, bufr[1024];
  184. Editor *pPad;
  185. int i;
  186. /* Xt may not pass a widget as advertised (??? is this needed? - hp) */
  187. if(!XtIsShell(w))
  188. w = XtParent(w);
  189. for(pPad = pPadList, numPadsToSave = 0; pPad != (Editor *)NULL;
  190. pPad = pPad->pNextPad)
  191. {
  192. if(pPad->inUse == True)
  193. numPadsToSave++;
  194. }
  195. if(numPadsToSave < 1)
  196. {
  197. xa_CommandStr[0] = (char *)NULL;
  198. XSetCommand(XtDisplay(w), XtWindow(w), xa_CommandStr, 1);
  199. return;
  200. }
  201. DtSessionSavePath(w, &longpath, &fileName);
  202. /* Create the session file */
  203. if ((fd = creat (longpath, S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP)) == -1)
  204. {
  205. tmpStr = (char *)malloc(strlen(MSG2) + strlen(longpath)+ 1);
  206. sprintf(tmpStr, MSG2, longpath);
  207. _DtSimpleErrnoError(pPad->progname, DtError, MSG1, tmpStr, NULL);
  208. free(tmpStr);
  209. XtFree ((char *)longpath);
  210. return;
  211. }
  212. sprintf(bufr, "*pads.numActivePads: %d\n", numPadsToSave);
  213. write (fd, bufr, strlen(bufr));
  214. for(pPad = pPadList, i = 0; pPad != (Editor *)NULL;
  215. pPad = pPad->pNextPad, i++)
  216. {
  217. if(pPad->inUse == True)
  218. SaveMain(pPad, i, fd);
  219. }
  220. close(fd);
  221. i = 0;
  222. xa_CommandStr[i] = pPadList->progname; i++;
  223. xa_CommandStr[i] = "-session"; i++;
  224. xa_CommandStr[i] = fileName; i++;
  225. XSetCommand(XtDisplay(topLevelWithWmCommand),
  226. XtWindow(topLevelWithWmCommand), xa_CommandStr, i);
  227. XtFree ((char *)fileName);
  228. }
  229. /***********************************************************************
  230. *
  231. * closeCB - set up as the "WM_DELETE_WINDOW" WMProtocolCallback on the
  232. * application shell for each Editor instance - and called when
  233. * a delete window command is received from the Window Manager.
  234. * Waits for CloseWindow to become false before it continues.
  235. *
  236. ***********************************************************************/
  237. /* ARGSUSED */
  238. void
  239. closeCB(
  240. Widget w,
  241. caddr_t client_data,
  242. caddr_t call_data )
  243. {
  244. Editor *pPad = (Editor *)client_data;
  245. /* call the callback for Exit within the File Menu pulldown */
  246. XtCallCallbacks(pPad->ExitWid, XmNactivateCallback, (XtPointer)pPad);
  247. }
  248. /***********************************************************************
  249. *
  250. * restoreSession - gets the valid x and y location of where to put the
  251. * Text Edit on the root window. Sets the global varible
  252. * dtpad.saveRestore to True so the rest of the program knows that
  253. * a session is being restored.
  254. *
  255. ***********************************************************************/
  256. void
  257. restoreSession(
  258. Editor *pPad)
  259. {
  260. XrmDatabase db;
  261. char *tmpStr;
  262. XrmName xrm_name[5];
  263. XrmRepresentation rep_type;
  264. XrmValue value;
  265. char *fileName = pPad->xrdb.session;
  266. char *path;
  267. int numPadsToRestore, i;
  268. Boolean foundPad;
  269. if(DtSessionRestorePath(topLevelWithWmCommand, &path, fileName) == False)
  270. path = fileName;
  271. /* Open the file as a resource database */
  272. if ((db = XrmGetFileDatabase (path)) == NULL)
  273. {
  274. tmpStr = (char *)XtMalloc(strlen(MSG2) + strlen(path)+ 1);
  275. sprintf(tmpStr, MSG2, path);
  276. _DtSimpleErrnoError(pPad->progname, DtError, MSG1, tmpStr, NULL);
  277. XtFree(tmpStr);
  278. return;
  279. }
  280. xrm_name[0] = XrmStringToQuark ("pads");
  281. xrm_name[1] = XrmStringToQuark ("numActivePads");
  282. xrm_name[2] = 0;
  283. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  284. numPadsToRestore = atoi((char *)value.addr);
  285. if(numPadsToRestore == 0)
  286. {
  287. /*
  288. * Either it's an old (i.e. 2.01) session file,
  289. * or it's bogus. Either way, we'll create one
  290. * window, taking whatever mainWindow: resources
  291. * we can find.
  292. */
  293. xrm_name[0] = XrmStringToQuark ("mainWindow");
  294. xrm_name[2] = 0;
  295. /* get x position */
  296. xrm_name[1] = XrmStringToQuark ("x");
  297. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  298. pPad->x = atoi((char *)value.addr);
  299. /* get y position */
  300. xrm_name[1] = XrmStringToQuark ("y");
  301. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  302. pPad->y = atoi((char *)value.addr);
  303. pPad->saveRestore = True;
  304. return;
  305. }
  306. RestorePad(pPad, 0, db);
  307. for(i = 1; i < numPadsToRestore; i++)
  308. {
  309. foundPad = FindOrCreatePad(&pPad);
  310. RestorePad(pPad, i, db);
  311. if(foundPad == False)
  312. RealizeNewPad(pPad);
  313. else
  314. ManageOldPad(pPad, False);
  315. }
  316. }
  317. /************************************************************************
  318. *
  319. * RestoreMain -
  320. *
  321. ***********************************************************************/
  322. static void
  323. RestoreMain(
  324. Editor *pPad,
  325. int padNum,
  326. XrmDatabase db)
  327. {
  328. char * iconify = NULL;
  329. char buf[1024];
  330. XrmName xrm_name[5];
  331. XrmRepresentation rep_type;
  332. XrmValue value;
  333. sprintf(buf, "mainWindow%d", padNum);
  334. xrm_name[0] = XrmStringToQuark(buf);
  335. xrm_name[2] = 0;
  336. /* get x position */
  337. xrm_name[1] = XrmStringToQuark ("x");
  338. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  339. pPad->x = atoi((char *)value.addr);
  340. /* get y position */
  341. xrm_name [1] = XrmStringToQuark ("y");
  342. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  343. pPad->y = atoi((char *)value.addr);
  344. /* get width */
  345. xrm_name [1] = XrmStringToQuark ("width");
  346. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  347. pPad->width = atoi((char *)value.addr);
  348. /* get height */
  349. xrm_name [1] = XrmStringToQuark ("height");
  350. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  351. pPad->height = atoi((char *)value.addr);
  352. /* Get and set whether the view is iconic */
  353. xrm_name [1] = XrmStringToQuark ("iconify");
  354. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  355. /* If there is an iconify resource and its value is True, */
  356. /* then mark the window as iconified. */
  357. if ((iconify = (char *) value.addr) != NULL &&
  358. strcmp (iconify, "True") == 0)
  359. pPad->iconic = True;
  360. else
  361. pPad->iconic = False;
  362. /* get the file name */
  363. xrm_name [1] = XrmStringToQuark ("fileName");
  364. XrmQGetResource (db, xrm_name, xrm_name, &rep_type, &value);
  365. pPad->fileStuff.fileName = strdup((char *) value.addr);
  366. /* get workspace list */
  367. xrm_name[1] = XrmStringToQuark("workspaceList");
  368. XrmQGetResource(db, xrm_name, xrm_name, &rep_type, &value);
  369. pPad->xrdb.workspaceList = strdup((char *) value.addr);
  370. }
  371. /************************************************************************
  372. *
  373. * RestorePad -
  374. *
  375. ***********************************************************************/
  376. void
  377. RestorePad(
  378. Editor *pPad,
  379. int padNum,
  380. XrmDatabase db)
  381. {
  382. RestoreMain(pPad, padNum, db);
  383. pPad->saveRestore = True;
  384. }