wsinfo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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: wsinfo.c /main/3 1995/10/27 10:41:27 rswiston $ */
  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. /* Demonstrate the CDE Workspace API. */
  31. /*
  32. * include files
  33. */
  34. #include <stdio.h>
  35. #include <Xm/XmAll.h>
  36. #include <Dt/Wsm.h>
  37. /*
  38. * functions
  39. */
  40. void main();
  41. static void quitCB();
  42. static void updateCB();
  43. static void ShowCurrentWorkspaceInfo();
  44. static DtWsmWsChangeProc wschangecb();
  45. static void InstallXErrorHandler();
  46. /*
  47. * global variables
  48. */
  49. static Widget toplevel;
  50. static Widget wChildren[20];
  51. /*
  52. * button assignments
  53. */
  54. #define QUIT_BUTTON (wChildren[0])
  55. #define UPDATE_BUTTON (wChildren[1])
  56. #define SEPARATOR (wChildren[2])
  57. #define WS_LABEL (wChildren[3])
  58. #define WS_VALUE (wChildren[4])
  59. #define TITLE_LABEL (wChildren[5])
  60. #define TITLE_VALUE (wChildren[6])
  61. #define BACKDROP_LABEL (wChildren[7])
  62. #define BACKDROP_VALUE (wChildren[8])
  63. #define COLORSET_LABEL (wChildren[9])
  64. #define COLORSET_VALUE (wChildren[10])
  65. #define SEPARATOR2 (wChildren[11])
  66. #define NUM_CHILDREN 12
  67. /*
  68. * main - main logic for program
  69. */
  70. void main (argc,argv)
  71. int argc;
  72. char **argv;
  73. {
  74. Widget frame;
  75. Widget form;
  76. Widget top;
  77. XtAppContext app_context;
  78. Arg args[10];
  79. int n;
  80. int i;
  81. XmString xms, xmsBlank;
  82. /*
  83. * initialize toolkit
  84. */
  85. n = 0;
  86. XtSetArg (args[n], XmNallowShellResize, True); n++;
  87. toplevel = XtAppInitialize (&app_context, "Dtwsinfo", NULL, 0, &argc, argv,
  88. NULL, args, n);
  89. /*
  90. * Set up X error handling
  91. */
  92. InstallXErrorHandler ();
  93. n = 0;
  94. XtSetArg (args[n], XmNmarginWidth, 10); n++;
  95. XtSetArg (args[n], XmNmarginHeight, 10); n++;
  96. frame = XmCreateFrame (toplevel, "frame", args, n);
  97. XtManageChild (frame);
  98. /*
  99. * manager widget
  100. */
  101. n = 0;
  102. form = XmCreateForm (frame, "form", args, n);
  103. XtManageChild (form);
  104. /*
  105. * create compound string for blank labels
  106. */
  107. xmsBlank = XmStringCreateLocalized(" ");
  108. /*******************************************
  109. * QUIT BUTTON
  110. ******************************************/
  111. /*
  112. * create compound string for the button text
  113. */
  114. xms = XmStringCreateLocalized("Quit");
  115. /*
  116. * set up arglist
  117. */
  118. n = 0;
  119. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  120. XtSetArg (args[n], XmNlabelString, xms); n++;
  121. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  122. XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  123. /*
  124. * create button
  125. */
  126. top = QUIT_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  127. form, args, n);
  128. XmStringFree(xms);
  129. /*
  130. * add callback
  131. */
  132. XtAddCallback (QUIT_BUTTON, XmNactivateCallback, quitCB, NULL);
  133. /*******************************************
  134. * UPDATE WORKSPACE INFO BUTTON
  135. ******************************************/
  136. /*
  137. * create compound string for the button text
  138. */
  139. xms = XmStringCreateLocalized("Get workspace info");
  140. /*
  141. * set up arglist
  142. */
  143. n = 0;
  144. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  145. XtSetArg (args[n], XmNlabelString, xms); n++;
  146. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  147. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  148. XtSetArg (args[n], XmNtopWidget, top); n++;
  149. /*
  150. * create button
  151. */
  152. top = UPDATE_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  153. form, args, n);
  154. XmStringFree(xms);
  155. /*
  156. * add callback
  157. */
  158. XtAddCallback (UPDATE_BUTTON, XmNactivateCallback, updateCB, NULL);
  159. /*******************************************
  160. * SEPARATOR
  161. ******************************************/
  162. /*
  163. * set up arglist
  164. */
  165. n = 0;
  166. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  167. XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  168. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  169. XtSetArg (args[n], XmNtopWidget, top); n++;
  170. /*
  171. * create separator
  172. */
  173. top = SEPARATOR = (Widget)
  174. XmCreateSeparatorGadget (form, "separator", args, n);
  175. /*******************************************
  176. * LABEL FOR CURRENT WORKSPACE
  177. ******************************************/
  178. /*
  179. * NOTE: We assume this is the longest label and lay out the
  180. * other fields based on this assumption.
  181. */
  182. /*
  183. * create compound string for the label text
  184. */
  185. xms = XmStringCreateLocalized("Current workspace: ");
  186. /*
  187. * set up arglist
  188. */
  189. n = 0;
  190. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  191. XtSetArg (args[n], XmNlabelString, xms); n++;
  192. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  193. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  194. XtSetArg (args[n], XmNtopWidget, top); n++;
  195. /*
  196. * create label
  197. */
  198. WS_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
  199. form, args, n);
  200. XmStringFree (xms);
  201. /*******************************************
  202. * VALUE FOR CURRENT WORKSPACE
  203. ******************************************/
  204. /*
  205. * set up arglist
  206. */
  207. n = 0;
  208. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  209. XtSetArg (args[n], XmNlabelString, xmsBlank); n++;
  210. XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  211. XtSetArg (args[n], XmNleftWidget, WS_LABEL); n++;
  212. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  213. XtSetArg (args[n], XmNtopWidget, top); n++;
  214. /*
  215. * create value
  216. */
  217. top = WS_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
  218. form, args, n);
  219. /*******************************************
  220. * LABEL FOR WORKSPACE TITLE
  221. ******************************************/
  222. /*
  223. * create compound string for the label text
  224. */
  225. xms = XmStringCreateLocalized(" title: ");
  226. /*
  227. * set up arglist
  228. */
  229. n = 0;
  230. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  231. XtSetArg (args[n], XmNlabelString, xms); n++;
  232. XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  233. XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
  234. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  235. XtSetArg (args[n], XmNtopWidget, top); n++;
  236. /*
  237. * create label
  238. */
  239. TITLE_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
  240. form, args, n);
  241. XmStringFree(xms);
  242. /*******************************************
  243. * VALUE FOR WORKSPACE TITLE
  244. ******************************************/
  245. /*
  246. * set up arglist
  247. */
  248. n = 0;
  249. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  250. XtSetArg (args[n], XmNlabelString, xmsBlank); n++;
  251. XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  252. XtSetArg (args[n], XmNleftWidget, TITLE_LABEL); n++;
  253. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  254. XtSetArg (args[n], XmNtopWidget, top); n++;
  255. /*
  256. * create value
  257. */
  258. top = TITLE_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
  259. form, args, n);
  260. /*******************************************
  261. * LABEL FOR WORKSPACE BACKDROP
  262. ******************************************/
  263. /*
  264. * create compound string for the label text
  265. */
  266. xms = XmStringCreateLocalized(" backdrop: ");
  267. /*
  268. * set up arglist
  269. */
  270. n = 0;
  271. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  272. XtSetArg (args[n], XmNlabelString, xms); n++;
  273. XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  274. XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
  275. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  276. XtSetArg (args[n], XmNtopWidget, top); n++;
  277. /*
  278. * create label
  279. */
  280. BACKDROP_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
  281. form, args, n);
  282. XmStringFree (xms);
  283. /*******************************************
  284. * VALUE FOR WORKSPACE BACKDROP
  285. ******************************************/
  286. /*
  287. * set up arglist
  288. */
  289. n = 0;
  290. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  291. XtSetArg (args[n], XmNlabelString, xmsBlank); n++;
  292. XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  293. XtSetArg (args[n], XmNleftWidget, BACKDROP_LABEL); n++;
  294. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  295. XtSetArg (args[n], XmNtopWidget, top); n++;
  296. /*
  297. * create value
  298. */
  299. top = BACKDROP_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
  300. form, args, n);
  301. /*******************************************
  302. * LABEL FOR WORKSPACE COLORSET ID
  303. ******************************************/
  304. /*
  305. * create compound string for the label text
  306. */
  307. xms = XmStringCreateLocalized(" color set id: ");
  308. /*
  309. * set up arglist
  310. */
  311. n = 0;
  312. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  313. XtSetArg (args[n], XmNlabelString, xms); n++;
  314. XtSetArg (args[n], XmNrightAttachment, XmATTACH_OPPOSITE_WIDGET); n++;
  315. XtSetArg (args[n], XmNrightWidget, WS_LABEL); n++;
  316. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  317. XtSetArg (args[n], XmNtopWidget, top); n++;
  318. /*
  319. * create label
  320. */
  321. COLORSET_LABEL = XtCreateWidget ("label", xmLabelWidgetClass,
  322. form, args, n);
  323. XmStringFree (xms);
  324. /*******************************************
  325. * VALUE FOR WORKSPACE COLORSET ID
  326. ******************************************/
  327. /*
  328. * set up arglist
  329. */
  330. n = 0;
  331. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  332. XtSetArg (args[n], XmNlabelString, xmsBlank); n++;
  333. XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  334. XtSetArg (args[n], XmNleftWidget, COLORSET_LABEL); n++;
  335. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  336. XtSetArg (args[n], XmNtopWidget, top); n++;
  337. /*
  338. * create value
  339. */
  340. top = COLORSET_VALUE = XtCreateWidget ("label", xmLabelWidgetClass,
  341. form, args, n);
  342. /*******************************************
  343. * SEPARATOR
  344. ******************************************/
  345. /*
  346. * set up arglist
  347. */
  348. n = 0;
  349. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  350. XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  351. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  352. XtSetArg (args[n], XmNtopWidget, top); n++;
  353. /*
  354. * create separator
  355. */
  356. top = SEPARATOR2 = (Widget)
  357. XmCreateSeparatorGadget (form, "separator", args, n);
  358. /*
  359. * manage children
  360. */
  361. XtManageChildren (wChildren, NUM_CHILDREN);
  362. /*
  363. * Show info on current workspace
  364. */
  365. ShowCurrentWorkspaceInfo ();
  366. /*
  367. * realize widgets
  368. */
  369. XtRealizeWidget (toplevel);
  370. /*
  371. * Do these calls after the shell is realized.
  372. * They need a window created for the top level shell.
  373. *
  374. * HINT: See XtSetMappedWhenManaged(1) if you want to
  375. * realize a widget to get a window, but need to
  376. * control when the map of the window occurs.
  377. */
  378. (void) DtWsmAddCurrentWorkspaceCallback (toplevel,
  379. (DtWsmWsChangeProc) wschangecb, NULL);
  380. DtWsmOccupyAllWorkspaces (XtDisplay(toplevel), XtWindow(toplevel));
  381. /*
  382. * process events
  383. */
  384. XtAppMainLoop (app_context);
  385. }
  386. /*-------------------------------------------------------------
  387. * Update the information for the current workspace
  388. */
  389. static void ShowCurrentWorkspaceInfo ()
  390. {
  391. Arg args[10]; /* arg list */
  392. int n; /* arg count */
  393. Atom aWs;
  394. int rval;
  395. Display *dpy;
  396. Window root;
  397. DtWsmWorkspaceInfo *pWsInfo;
  398. XmString xms;
  399. char pch[512];
  400. char *pchName;
  401. dpy = XtDisplay(toplevel);
  402. root = XDefaultRootWindow(dpy);
  403. rval = DtWsmGetCurrentWorkspace(dpy, root, &aWs);
  404. if (rval == Success)
  405. {
  406. rval = DtWsmGetWorkspaceInfo (dpy, root, aWs, &pWsInfo);
  407. if (rval == Success)
  408. {
  409. /* workspace name */
  410. pchName = XmGetAtomName (dpy, pWsInfo->workspace);
  411. xms = XmStringCreateLocalized(pchName);
  412. n = 0;
  413. XtSetArg (args[n], XmNlabelString, xms); n++;
  414. XtSetValues (WS_VALUE, args, n);
  415. XmStringFree (xms);
  416. XtFree (pchName);
  417. /* workspace title */
  418. xms = XmStringCreateLocalized(pWsInfo->pchTitle);
  419. n = 0;
  420. XtSetArg (args[n], XmNlabelString, xms); n++;
  421. XtSetValues (TITLE_VALUE, args, n);
  422. XmStringFree (xms);
  423. /* workspace backdrop name */
  424. if (pWsInfo->backdropName != None)
  425. pchName = XmGetAtomName (dpy, pWsInfo->backdropName);
  426. else
  427. pchName = "<failed to load!>";
  428. xms = XmStringCreateLocalized(pchName);
  429. n = 0;
  430. XtSetArg (args[n], XmNlabelString, xms); n++;
  431. XtSetValues (BACKDROP_VALUE, args, n);
  432. XmStringFree (xms);
  433. if (pWsInfo->backdropName != None)
  434. XtFree (pchName);
  435. /* workspace colorset id */
  436. sprintf (pch, "%d", pWsInfo->colorSetId);
  437. xms = XmStringCreateLocalized(pch);
  438. n = 0;
  439. XtSetArg (args[n], XmNlabelString, xms); n++;
  440. XtSetValues (COLORSET_VALUE, args, n);
  441. XmStringFree (xms);
  442. DtWsmFreeWorkspaceInfo (pWsInfo);
  443. }
  444. }
  445. if (rval != Success)
  446. {
  447. fprintf (stderr, "Failed to get workspace information.\n");
  448. fprintf (stderr, "Is dtwm running?\n");
  449. exit (1);
  450. }
  451. }
  452. /*-------------------------------------------------------------
  453. * quitCB - callback for button
  454. */
  455. static void quitCB (w, client_data, call_data)
  456. Widget w; /* widget id */
  457. XtPointer client_data; /* data from application */
  458. XtPointer call_data; /* data from widget class */
  459. {
  460. exit (0);
  461. }
  462. /*-------------------------------------------------------------
  463. * updateCB - callback for show current workspace info
  464. */
  465. static void updateCB (w, client_data, call_data)
  466. Widget w; /* widget id */
  467. XtPointer client_data; /* data from application */
  468. XtPointer call_data; /* data from widget class */
  469. {
  470. ShowCurrentWorkspaceInfo ();
  471. }
  472. /*-------------------------------------------------------------
  473. ** wschangecb - callback for workspace switch
  474. */
  475. static DtWsmWsChangeProc
  476. wschangecb (w, atom, client_data)
  477. Widget w;
  478. Atom atom;
  479. XtPointer client_data;
  480. {
  481. ShowCurrentWorkspaceInfo ();
  482. }
  483. /*-------------------------------------------------------------
  484. ** data for X error handler
  485. */
  486. #define E_MAJOR_CODE 0
  487. #define E_MINOR_CODE 1
  488. #define E_RESOURCE_ID 2
  489. #define E_ERROR_SERIAL 3
  490. #define E_CURRENT_SERIAL 4
  491. #define NUM_E_STRINGS 5
  492. static char *pchErrorFormatNames [NUM_E_STRINGS] = {
  493. "MajorCode",
  494. "MinorCode",
  495. "ResourceID",
  496. "ErrorSerial",
  497. "CurrentSerial"
  498. };
  499. static char *pchErrorFormat [NUM_E_STRINGS];
  500. /*-------------------------------------------------------------
  501. ** HandleXErrors - catch X protocol errors
  502. */
  503. static int
  504. _HandleXErrors (display, errorEvent)
  505. Display *display;
  506. XErrorEvent *errorEvent;
  507. {
  508. char buffer[BUFSIZ];
  509. char message[BUFSIZ];
  510. XGetErrorText (display, errorEvent->error_code, buffer, BUFSIZ);
  511. fprintf (stderr, "\n*** X error occurred ***\n");
  512. fprintf (stderr, "Description = '%s'\n ", buffer);
  513. fprintf (stderr, pchErrorFormat[E_MAJOR_CODE], errorEvent->request_code);
  514. sprintf(message, "%d", errorEvent->request_code);
  515. XGetErrorDatabaseText (display, "XRequest", message, " ", buffer, BUFSIZ);
  516. fprintf (stderr, " (%s)\n ", buffer);
  517. fprintf (stderr, pchErrorFormat[E_MINOR_CODE], errorEvent->minor_code);
  518. fprintf (stderr, "\n ");
  519. fprintf (stderr, pchErrorFormat[E_RESOURCE_ID], errorEvent->resourceid);
  520. fprintf (stderr, "\n ");
  521. fprintf (stderr, pchErrorFormat[E_ERROR_SERIAL], errorEvent->serial);
  522. fprintf (stderr, "\n ");
  523. fprintf (stderr, pchErrorFormat[E_CURRENT_SERIAL],
  524. LastKnownRequestProcessed(display));
  525. fprintf (stderr, "\n");
  526. return (0);
  527. }
  528. /*-------------------------------------------------------------
  529. ** Setup the X error handler
  530. */
  531. static void
  532. InstallXErrorHandler ( display )
  533. Display *display;
  534. {
  535. char buffer[BUFSIZ];
  536. int i;
  537. /*
  538. * Fetch the X error format strings from XErrorDB
  539. */
  540. for (i = 0; i< NUM_E_STRINGS; i++)
  541. {
  542. XGetErrorDatabaseText (display, "XlibMessage",
  543. pchErrorFormatNames[i], " %d ", buffer, BUFSIZ);
  544. pchErrorFormat[i] = (char *) XtMalloc (1+strlen(buffer));
  545. strcpy(pchErrorFormat[i], buffer);
  546. }
  547. XSetErrorHandler (_HandleXErrors);
  548. }