2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
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.
32 * $XConsortium: broadcast.c /main/4 1996/08/12 18:34:24 barstow $
34 * broadcast - dynamic pattern and procedural notification example
40 #include <sys/param.h>
41 #include <sys/types.h>
46 Widget toplevel, base_frame, controls, slider, gauge, button;
49 void broadcast_value();
50 void receive_tt_message();
51 void create_ui_components();
64 * Initialize Motif and create ui components
66 toplevel = XtVaAppInitialize(&app, "ttsample1", NULL, 0,
67 &argc, argv, NULL, NULL);
68 XtVaSetValues(toplevel, XmNtitle, "ToolTalk Sample 1", NULL);
69 create_ui_components();
72 * Initialize ToolTalk, using the initial default session, and
73 * obtain the file descriptor that will become active whenever
74 * ToolTalk has a message for this process.
77 my_procid = tt_open();
80 fprintf(stderr, "Cannot get tt_fd, err=%d\n", ttfd);
86 * Arrange for Motif to call receive_tt_message when the ToolTalk
87 * file descriptor becomes active.
90 XtAppAddInput(app, ttfd, (XtPointer) XtInputReadMask,
91 receive_tt_message, 0);
94 * Create and register a pattern so ToolTalk knows we are interested
95 * in "ttsample1_value" messages within the session we join.
98 pat = tt_pattern_create();
99 tt_pattern_category_set(pat, TT_OBSERVE);
100 tt_pattern_scope_add(pat, TT_SESSION);
101 tt_pattern_op_add(pat, "ttsample1_value");
102 tt_pattern_register(pat);
105 * Join the default session
108 tt_session_join(tt_default_session());
111 * Turn control over to Motif.
113 XtRealizeWidget(toplevel);
117 * Before leaving, allow ToolTalk to clean up.
126 * When the button is pressed, broadcast the new slider value.
129 broadcast_value(widget, client_data, call_data)
131 XtPointer client_data, call_data;
133 XtArgVal slider_value;
137 * Create and send a ToolTalk notice message
138 * ttsample1_value(in int <new value)
141 XtVaGetValues(slider, XmNvalue, &slider_value, 0, NULL);
143 XtVaSetValues(slider, XmNvalue, slider_value, 0, NULL);
145 msg_out = tt_pnotice_create(TT_SESSION, "ttsample1_value");
146 tt_message_arg_add(msg_out, TT_IN, "integer", NULL);
147 tt_message_arg_ival_set(msg_out, 0, slider_value);
148 tt_message_send(msg_out);
151 * Since this message is a notice, we don't expect a reply, so
152 * there's no reason to keep a handle for the message.
155 tt_message_destroy(msg_out);
159 * When a ToolTalk message is available, receive it; if it's a
160 * ttsample1_value message, update the gauge with the new value.
163 receive_tt_message(client_data, fid, id)
164 XtPointer client_data;
174 msg_in = tt_message_receive();
177 * It's possible that the file descriptor would become active
178 * even though ToolTalk doesn't really have a message for us.
179 * The returned message handle is NULL in this case.
182 if (msg_in == NULL) return;
186 * Get a storage mark so we can easily free all the data
187 * ToolTalk returns to us.
192 op = tt_message_op(msg_in);
193 err = tt_ptr_error(op);
194 if (err > TT_WRN_LAST) {
195 printf( "tt_message_op(): %s\n", tt_status_message(err));
196 } else if (op != 0) {
197 if (0==strcmp("ttsample1_value", tt_message_op(msg_in))) {
198 tt_message_arg_ival(msg_in, 0, &val_in);
199 XtVaSetValues(gauge, XmNvalue, val_in, 0, NULL);
203 tt_message_destroy(msg_in);
209 * Straight Motif calls for creating the ui elements. No
210 * ToolTalk-specific code here.
213 create_ui_components()
216 Widget glabel, slabel;
219 base_frame = XtVaCreateManagedWidget("base_frame",
220 xmMainWindowWidgetClass, toplevel,
224 XtVaGetValues(base_frame, XmNmwmDecorations, &decor, 0, NULL);
225 decor &= ~MWM_DECOR_RESIZEH;
226 XtVaSetValues(base_frame, XmNmwmDecorations, &decor, 0, NULL);
228 controls = XtVaCreateManagedWidget("controls",
229 xmFormWidgetClass, base_frame, NULL, 0, NULL);
231 slabel = XtVaCreateManagedWidget("Send:",
232 xmLabelWidgetClass, controls,
233 XmNleftAttachment, XmATTACH_WIDGET,
234 XmNtopAttachment, XmATTACH_WIDGET,
235 XmNtopWidget, controls,
239 slider = XtVaCreateManagedWidget("slider",
240 xmScaleWidgetClass, controls,
241 XmNleftAttachment, XmATTACH_WIDGET,
242 XmNleftWidget, controls,
244 XmNtopAttachment, XmATTACH_WIDGET,
245 XmNtopWidget, slabel,
250 XmNorientation, XmHORIZONTAL,
254 glabel = XtVaCreateManagedWidget("Received:",
255 xmLabelWidgetClass, controls,
256 XmNleftAttachment, XmATTACH_WIDGET,
257 XmNtopAttachment, XmATTACH_WIDGET,
259 XmNtopWidget, slider,
261 gauge = XtVaCreateManagedWidget("gauge",
262 xmScaleWidgetClass, controls,
263 XmNleftAttachment, XmATTACH_WIDGET,
264 XmNleftWidget, controls,
266 XmNtopAttachment, XmATTACH_WIDGET,
267 XmNtopWidget, glabel,
268 XmNorientation, XmHORIZONTAL,
276 label = XmStringCreateSimple("Broadcast");
277 button = XtVaCreateManagedWidget("button",
278 xmPushButtonWidgetClass, controls,
280 XmNtopAttachment, XmATTACH_WIDGET,
283 XmNleftWidget, controls,
284 XmNleftAttachment, XmATTACH_WIDGET,
286 XmNlabelString, label,
289 XtAddCallback(button, XmNactivateCallback, broadcast_value, 0);