broadcast.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 1995 Digital Equipment Corporation.
  25. * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
  26. * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
  27. * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
  28. * (c) Copyright 1993, 1994, 1995 Novell, Inc.
  29. * (c) Copyright 1995 FUJITSU LIMITED.
  30. * (c) Copyright 1995 Hitachi.
  31. *
  32. * $XConsortium: broadcast.c /main/4 1996/08/12 18:34:24 barstow $
  33. *
  34. * broadcast - dynamic pattern and procedural notification example
  35. *
  36. */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <sys/param.h>
  40. #include <sys/types.h>
  41. #include <string.h>
  42. #include <Xm/XmAll.h>
  43. #include <Tt/tt_c.h>
  44. Widget toplevel, base_frame, controls, slider, gauge, button;
  45. char *my_procid;
  46. void broadcast_value();
  47. void receive_tt_message();
  48. void create_ui_components();
  49. int
  50. main(argc, argv)
  51. int argc;
  52. char **argv;
  53. {
  54. int ttfd;
  55. Tt_pattern pat;
  56. XtAppContext app;
  57. /*
  58. * Initialize Motif and create ui components
  59. */
  60. toplevel = XtVaAppInitialize(&app, "ttsample1", NULL, 0,
  61. &argc, argv, NULL, NULL);
  62. XtVaSetValues(toplevel, XmNtitle, "ToolTalk Sample 1", NULL);
  63. create_ui_components();
  64. /*
  65. * Initialize ToolTalk, using the initial default session, and
  66. * obtain the file descriptor that will become active whenever
  67. * ToolTalk has a message for this process.
  68. */
  69. my_procid = tt_open();
  70. ttfd = tt_fd();
  71. if (ttfd < 0) {
  72. fprintf(stderr, "Cannot get tt_fd, err=%d\n", ttfd);
  73. return -1;
  74. }
  75. /*
  76. * Arrange for Motif to call receive_tt_message when the ToolTalk
  77. * file descriptor becomes active.
  78. */
  79. XtAppAddInput(app, ttfd, (XtPointer) XtInputReadMask,
  80. receive_tt_message, 0);
  81. /*
  82. * Create and register a pattern so ToolTalk knows we are interested
  83. * in "ttsample1_value" messages within the session we join.
  84. */
  85. pat = tt_pattern_create();
  86. tt_pattern_category_set(pat, TT_OBSERVE);
  87. tt_pattern_scope_add(pat, TT_SESSION);
  88. tt_pattern_op_add(pat, "ttsample1_value");
  89. tt_pattern_register(pat);
  90. /*
  91. * Join the default session
  92. */
  93. tt_session_join(tt_default_session());
  94. /*
  95. * Turn control over to Motif.
  96. */
  97. XtRealizeWidget(toplevel);
  98. XtAppMainLoop(app);
  99. /*
  100. * Before leaving, allow ToolTalk to clean up.
  101. */
  102. tt_close();
  103. return 0;
  104. }
  105. /*
  106. * When the button is pressed, broadcast the new slider value.
  107. */
  108. void
  109. broadcast_value(widget, client_data, call_data)
  110. Widget widget;
  111. XtPointer client_data, call_data;
  112. {
  113. XtArgVal slider_value;
  114. Tt_message msg_out;
  115. /*
  116. * Create and send a ToolTalk notice message
  117. * ttsample1_value(in int <new value)
  118. */
  119. XtVaGetValues(slider, XmNvalue, &slider_value, 0, NULL);
  120. slider_value++;
  121. XtVaSetValues(slider, XmNvalue, slider_value, 0, NULL);
  122. msg_out = tt_pnotice_create(TT_SESSION, "ttsample1_value");
  123. tt_message_arg_add(msg_out, TT_IN, "integer", NULL);
  124. tt_message_arg_ival_set(msg_out, 0, slider_value);
  125. tt_message_send(msg_out);
  126. /*
  127. * Since this message is a notice, we don't expect a reply, so
  128. * there's no reason to keep a handle for the message.
  129. */
  130. tt_message_destroy(msg_out);
  131. }
  132. /*
  133. * When a ToolTalk message is available, receive it; if it's a
  134. * ttsample1_value message, update the gauge with the new value.
  135. */
  136. void
  137. receive_tt_message(client_data, fid, id)
  138. XtPointer client_data;
  139. int *fid;
  140. XtInputId *id;
  141. {
  142. Tt_message msg_in;
  143. int mark;
  144. int val_in;
  145. char *op;
  146. Tt_status err;
  147. msg_in = tt_message_receive();
  148. /*
  149. * It's possible that the file descriptor would become active
  150. * even though ToolTalk doesn't really have a message for us.
  151. * The returned message handle is NULL in this case.
  152. */
  153. if (msg_in == NULL) return;
  154. /*
  155. * Get a storage mark so we can easily free all the data
  156. * ToolTalk returns to us.
  157. */
  158. mark = tt_mark();
  159. op = tt_message_op(msg_in);
  160. err = tt_ptr_error(op);
  161. if (err > TT_WRN_LAST) {
  162. printf( "tt_message_op(): %s\n", tt_status_message(err));
  163. } else if (op != 0) {
  164. if (0==strcmp("ttsample1_value", tt_message_op(msg_in))) {
  165. tt_message_arg_ival(msg_in, 0, &val_in);
  166. XtVaSetValues(gauge, XmNvalue, val_in, 0, NULL);
  167. }
  168. }
  169. tt_message_destroy(msg_in);
  170. tt_release(mark);
  171. return;
  172. }
  173. /*
  174. * Straight Motif calls for creating the ui elements. No
  175. * ToolTalk-specific code here.
  176. */
  177. void
  178. create_ui_components()
  179. {
  180. int decor;
  181. Widget glabel, slabel;
  182. XmString label;
  183. base_frame = XtVaCreateManagedWidget("base_frame",
  184. xmMainWindowWidgetClass, toplevel,
  185. XmNwidth, 250,
  186. XmNheight, 175,
  187. NULL);
  188. XtVaGetValues(base_frame, XmNmwmDecorations, &decor, 0, NULL);
  189. decor &= ~MWM_DECOR_RESIZEH;
  190. XtVaSetValues(base_frame, XmNmwmDecorations, &decor, 0, NULL);
  191. controls = XtVaCreateManagedWidget("controls",
  192. xmFormWidgetClass, base_frame, NULL, 0, NULL);
  193. slabel = XtVaCreateManagedWidget("Send:",
  194. xmLabelWidgetClass, controls,
  195. XmNleftAttachment, XmATTACH_WIDGET,
  196. XmNtopAttachment, XmATTACH_WIDGET,
  197. XmNtopWidget, controls,
  198. XmNtopOffset, 10,
  199. XmNleftOffset, 5,
  200. NULL);
  201. slider = XtVaCreateManagedWidget("slider",
  202. xmScaleWidgetClass, controls,
  203. XmNleftAttachment, XmATTACH_WIDGET,
  204. XmNleftWidget, controls,
  205. XmNleftOffset, 10,
  206. XmNtopAttachment, XmATTACH_WIDGET,
  207. XmNtopWidget, slabel,
  208. XmNscaleWidth, 225,
  209. XmNscaleHeight, 18,
  210. XmNminimum, 0,
  211. XmNmaximum, 25,
  212. XmNorientation, XmHORIZONTAL,
  213. XmNshowValue, TRUE,
  214. NULL);
  215. glabel = XtVaCreateManagedWidget("Received:",
  216. xmLabelWidgetClass, controls,
  217. XmNleftAttachment, XmATTACH_WIDGET,
  218. XmNtopAttachment, XmATTACH_WIDGET,
  219. XmNleftOffset, 5,
  220. XmNtopWidget, slider,
  221. NULL);
  222. gauge = XtVaCreateManagedWidget("gauge",
  223. xmScaleWidgetClass, controls,
  224. XmNleftAttachment, XmATTACH_WIDGET,
  225. XmNleftWidget, controls,
  226. XmNleftOffset, 10,
  227. XmNtopAttachment, XmATTACH_WIDGET,
  228. XmNtopWidget, glabel,
  229. XmNorientation, XmHORIZONTAL,
  230. XmNscaleWidth, 225,
  231. XmNscaleHeight, 18,
  232. XmNminimum, 0,
  233. XmNmaximum, 25,
  234. XmNshowValue, TRUE,
  235. NULL);
  236. label = XmStringCreateSimple("Broadcast");
  237. button = XtVaCreateManagedWidget("button",
  238. xmPushButtonWidgetClass, controls,
  239. XmNtopWidget, gauge,
  240. XmNtopAttachment, XmATTACH_WIDGET,
  241. XmNtopOffset, 5,
  242. XmNleftOffset, 75,
  243. XmNleftWidget, controls,
  244. XmNleftAttachment, XmATTACH_WIDGET,
  245. XmNbottomOffset, 5,
  246. XmNlabelString, label,
  247. NULL);
  248. XmStringFree(label);
  249. XtAddCallback(button, XmNactivateCallback, broadcast_value, 0);
  250. }