occupy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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: occupy.c /main/3 1995/10/27 10:41:19 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 DtWsm 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 setCB();
  43. static void showCB();
  44. static void wsCB();
  45. static void allWsCB();
  46. static void SetupWorkspaceButtons ();
  47. static void ShowWorkspaceOccupancy();
  48. static void SetWorkspaceOccupancy();
  49. /*
  50. * global variables
  51. */
  52. static XmString xms;
  53. static Widget toplevel;
  54. static Widget wChildren [20];
  55. static Widget *wWs;
  56. static Atom *paWsSet = NULL;
  57. static Boolean *pbOccupy;
  58. static unsigned long numWorkspaces;
  59. static Atom *paWs;
  60. /*
  61. * button assignments
  62. */
  63. #define QUIT_BUTTON (wChildren[0])
  64. #define SEPARATOR (wChildren[1])
  65. #define SET_BUTTON (wChildren[2])
  66. #define SHOW_BUTTON (wChildren[3])
  67. #define ALL_WS_BUTTON (wChildren[4])
  68. #define NUM_CHILDREN 5
  69. /*
  70. * main - main logic for program
  71. */
  72. void main (argc,argv)
  73. int argc;
  74. char **argv;
  75. {
  76. Widget frame;
  77. Widget form;
  78. Widget top;
  79. XtAppContext app_context;
  80. Arg args[10];
  81. int n;
  82. int i;
  83. /*
  84. * initialize toolkit
  85. */
  86. n = 0;
  87. XtSetArg (args[n], XmNallowShellResize, True); n++;
  88. toplevel = XtAppInitialize (&app_context, "Dtoccupy", NULL, 0, &argc, argv,
  89. NULL, args, n);
  90. n = 0;
  91. XtSetArg (args[n], XmNmarginWidth, 10); n++;
  92. XtSetArg (args[n], XmNmarginHeight, 10); n++;
  93. frame = XmCreateFrame (toplevel, "frame", args, n);
  94. XtManageChild (frame);
  95. /*
  96. * manager widget
  97. */
  98. n = 0;
  99. form = XmCreateForm (frame, "form", args, n);
  100. XtManageChild (form);
  101. /*
  102. * create compound string for the button text
  103. */
  104. xms = XmStringCreateLocalized("Quit");
  105. /*
  106. * set up arglist
  107. */
  108. n = 0;
  109. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  110. XtSetArg (args[n], XmNlabelString, xms); n++;
  111. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  112. XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  113. /*
  114. * create button
  115. */
  116. top = QUIT_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  117. form, args, n);
  118. XmStringFree(xms);
  119. /*
  120. * add callback
  121. */
  122. XtAddCallback (QUIT_BUTTON, XmNactivateCallback, quitCB, NULL);
  123. /*
  124. * create compound string for the button text
  125. */
  126. xms = XmStringCreateLocalized("Set Occupancy");
  127. /*
  128. * set up arglist
  129. */
  130. n = 0;
  131. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  132. XtSetArg (args[n], XmNlabelString, xms); n++;
  133. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  134. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  135. XtSetArg (args[n], XmNtopWidget, top); n++;
  136. /*
  137. * create button
  138. */
  139. top = SET_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  140. form, args, n);
  141. XmStringFree(xms);
  142. /*
  143. * add callback
  144. */
  145. XtAddCallback (SET_BUTTON, XmNactivateCallback, setCB, NULL);
  146. /*
  147. * create compound string for the button text
  148. */
  149. xms = XmStringCreateLocalized("Occupy All Workspaces");
  150. /*
  151. * set up arglist
  152. */
  153. n = 0;
  154. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  155. XtSetArg (args[n], XmNlabelString, xms); n++;
  156. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  157. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  158. XtSetArg (args[n], XmNtopWidget, top); n++;
  159. /*
  160. * create button
  161. */
  162. top = ALL_WS_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  163. form, args, n);
  164. XmStringFree(xms);
  165. /*
  166. * add callback
  167. */
  168. XtAddCallback (ALL_WS_BUTTON, XmNactivateCallback, allWsCB, NULL);
  169. /*
  170. * create compound string for the button text
  171. */
  172. xms = XmStringCreateLocalized("Show Occupancy");
  173. /*
  174. * set up arglist
  175. */
  176. n = 0;
  177. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  178. XtSetArg (args[n], XmNlabelString, xms); n++;
  179. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  180. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  181. XtSetArg (args[n], XmNtopWidget, top); n++;
  182. /*
  183. * create button
  184. */
  185. top = SHOW_BUTTON = XtCreateWidget ("button", xmPushButtonWidgetClass,
  186. form, args, n);
  187. XmStringFree(xms);
  188. /*
  189. * add callback
  190. */
  191. XtAddCallback (SHOW_BUTTON, XmNactivateCallback, showCB, NULL);
  192. /*
  193. * set up arglist
  194. */
  195. n = 0;
  196. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  197. XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  198. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  199. XtSetArg (args[n], XmNtopWidget, top); n++;
  200. /*
  201. * create separator
  202. */
  203. top = SEPARATOR = (Widget)
  204. XmCreateSeparatorGadget (form, "separator", args, n);
  205. /*
  206. * set up workspace buttons
  207. */
  208. SetupWorkspaceButtons (form, top);
  209. /*
  210. * manage children
  211. */
  212. XtManageChildren (wChildren, NUM_CHILDREN);
  213. XtManageChildren (wWs, numWorkspaces);
  214. /*
  215. * realize widgets
  216. */
  217. XtRealizeWidget (toplevel);
  218. /*
  219. * set up initial workspace occupancy info
  220. */
  221. pbOccupy = (Boolean *) XtMalloc (numWorkspaces * sizeof (Boolean));
  222. for (i=0; i<numWorkspaces; i++)
  223. {
  224. pbOccupy[i] = False;
  225. }
  226. ShowWorkspaceOccupancy ();
  227. /*
  228. * process events
  229. */
  230. XtAppMainLoop (app_context);
  231. }
  232. /*-------------------------------------------------------------
  233. * Create a set of buttons for the current set of workspaces
  234. */
  235. static void SetupWorkspaceButtons (form, top)
  236. Widget form;
  237. Widget top;
  238. {
  239. Window root;
  240. Arg args[10]; /* arg list */
  241. int n; /* arg count */
  242. int i;
  243. DtWsmWorkspaceInfo *pWsInfo;
  244. char *pchWs;
  245. Widget left = None;
  246. Widget *pWid;
  247. /*
  248. * get workspace information
  249. */
  250. root = XRootWindowOfScreen(XtScreen(form));
  251. if (DtWsmGetWorkspaceList (XtDisplay(form), root,
  252. &paWs, (int *)&numWorkspaces) == Success)
  253. {
  254. wWs = (Widget *) XtMalloc (numWorkspaces * sizeof (Widget));
  255. pWid = wWs;
  256. for (i=0; i<numWorkspaces; i++, pWid++)
  257. {
  258. DtWsmGetWorkspaceInfo (XtDisplay(form), root, paWs[i],
  259. &pWsInfo);
  260. pchWs = (char *) XmGetAtomName (XtDisplay(form),
  261. pWsInfo->workspace);
  262. /*
  263. * create compound string for the button text
  264. */
  265. xms = XmStringCreateLocalized(pchWs);
  266. /*
  267. * set up arglist
  268. */
  269. n = 0;
  270. XtSetArg (args[n], XmNlabelType, XmSTRING); n++;
  271. XtSetArg (args[n], XmNlabelString, xms); n++;
  272. XtSetArg (args[n], XmNindicatorOn, True); n++;
  273. XtSetArg (args[n], XmNindicatorType, XmN_OF_MANY); n++;
  274. if (left == None)
  275. {
  276. XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  277. }
  278. else
  279. {
  280. XtSetArg (args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
  281. XtSetArg (args[n], XmNleftWidget, left); n++;
  282. }
  283. XtSetArg (args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
  284. XtSetArg (args[n], XmNtopWidget, top); n++;
  285. /*
  286. * create button
  287. */
  288. *pWid = XtCreateWidget ("button", xmToggleButtonWidgetClass,
  289. form, args, n);
  290. XmStringFree(xms);
  291. /*
  292. * add callback
  293. */
  294. XtAddCallback (*pWid, XmNvalueChangedCallback, wsCB,
  295. (XtPointer) i);
  296. left = *pWid;
  297. }
  298. }
  299. else
  300. {
  301. fprintf(stderr, "Failed to get workspace info -- dtwm not running!\n");
  302. exit (1);
  303. }
  304. }
  305. /*-------------------------------------------------------------
  306. * Update the workspace buttons with where we are now.
  307. */
  308. static void ShowWorkspaceOccupancy ()
  309. {
  310. Arg args[10]; /* arg list */
  311. int n; /* arg count */
  312. int i,j;
  313. Atom *paWsIn = NULL;
  314. unsigned long numWsIn;
  315. int rval;
  316. rval = DtWsmGetWorkspacesOccupied(XtDisplay(toplevel),
  317. XtWindow(toplevel), &paWsIn, &numWsIn );
  318. if (rval == Success)
  319. {
  320. for (i = 0; i<numWorkspaces; i++)
  321. {
  322. pbOccupy[i] = False;
  323. for (j=0; j<numWsIn; j++)
  324. {
  325. if (paWs[i] == paWsIn[j])
  326. {
  327. pbOccupy[i] = True;
  328. break;
  329. }
  330. }
  331. }
  332. for (i = 0; i<numWorkspaces; i++)
  333. {
  334. n = 0;
  335. if (pbOccupy[i])
  336. {
  337. XtSetArg (args[n], XmNset, True); n++;
  338. }
  339. else
  340. {
  341. XtSetArg (args[n], XmNset, False); n++;
  342. }
  343. XtSetValues (wWs[i], args, n);
  344. }
  345. if (paWsIn) XFree ((char *) paWsIn);
  346. }
  347. }
  348. /*-------------------------------------------------------------
  349. * quitCB - callback for button
  350. */
  351. static void quitCB (w, client_data, call_data)
  352. Widget w; /* widget id */
  353. XtPointer client_data; /* data from application */
  354. XtPointer call_data; /* data from widget class */
  355. {
  356. exit (0);
  357. }
  358. /*-------------------------------------------------------------
  359. * showCB - callback for show occupancy button
  360. */
  361. static void showCB (w, client_data, call_data)
  362. Widget w; /* widget id */
  363. XtPointer client_data; /* data from application */
  364. XtPointer call_data; /* data from widget class */
  365. {
  366. ShowWorkspaceOccupancy();
  367. }
  368. /*-------------------------------------------------------------
  369. * setCB - callback for set occupancy button
  370. */
  371. static void setCB (w, client_data, call_data)
  372. Widget w; /* widget id */
  373. XtPointer client_data; /* data from application */
  374. XtPointer call_data; /* data from widget class */
  375. {
  376. int numSet;
  377. int i;
  378. if (!paWsSet)
  379. {
  380. paWsSet = (Atom *) XtMalloc (numWorkspaces * sizeof(Atom));
  381. }
  382. for (i=0, numSet=0; i<numWorkspaces; i++)
  383. {
  384. if (pbOccupy[i])
  385. {
  386. paWsSet[numSet++] = paWs[i];
  387. }
  388. }
  389. DtWsmSetWorkspacesOccupied (XtDisplay(toplevel),
  390. XtWindow(toplevel), paWsSet, numSet);
  391. }
  392. /*-------------------------------------------------------------
  393. * wsCB - callback for set occupancy button
  394. */
  395. static void wsCB (w, client_data, call_data)
  396. Widget w; /* widget id */
  397. XtPointer client_data; /* data from application */
  398. XtPointer call_data; /* data from widget class */
  399. {
  400. int i = (Atom) client_data; /* ws array index */
  401. XmToggleButtonCallbackStruct *ptb =
  402. (XmToggleButtonCallbackStruct *) call_data;
  403. if (ptb->reason == XmCR_VALUE_CHANGED)
  404. {
  405. if (ptb->set)
  406. {
  407. pbOccupy[i] = True;
  408. }
  409. else
  410. {
  411. pbOccupy[i] = False;
  412. }
  413. }
  414. }
  415. /*-------------------------------------------------------------
  416. * allWsCB - callback for occupy all button
  417. */
  418. static void allWsCB (w, client_data, call_data)
  419. Widget w; /* widget id */
  420. XtPointer client_data; /* data from application */
  421. XtPointer call_data; /* data from widget class */
  422. {
  423. DtWsmOccupyAllWorkspaces (XtDisplay(toplevel), XtWindow(toplevel));
  424. }