2 * (c) Copyright 1995 Digital Equipment Corporation.
3 * (c) Copyright 1993, 1994, 1995 Hewlett-Packard Company
4 * (c) Copyright 1993, 1994, 1995 International Business Machines Corp.
5 * (c) Copyright 1993, 1994, 1995 Sun Microsystems, Inc.
6 * (c) Copyright 1993, 1994, 1995 Novell, Inc.
7 * (c) Copyright 1995 FUJITSU LIMITED.
8 * (c) Copyright 1995 Hitachi.
10 * $XConsortium: broadcast.c /main/4 1996/08/12 18:34:24 barstow $
12 * broadcast - dynamic pattern and procedural notification example
17 #include <sys/param.h>
18 #include <sys/types.h>
23 Widget toplevel, base_frame, controls, slider, gauge, button;
26 void broadcast_value();
27 void receive_tt_message();
28 void create_ui_components();
41 * Initialize Motif and create ui components
43 toplevel = XtVaAppInitialize(&app, "ttsample1", NULL, 0,
44 &argc, argv, NULL, NULL);
45 XtVaSetValues(toplevel, XmNtitle, "ToolTalk Sample 1", 0);
46 create_ui_components();
49 * Initialize ToolTalk, using the initial default session, and
50 * obtain the file descriptor that will become active whenever
51 * ToolTalk has a message for this process.
54 my_procid = tt_open();
58 * Arrange for Motif to call receive_tt_message when the ToolTalk
59 * file descriptor becomes active.
62 XtAppAddInput(app, ttfd, (XtPointer) XtInputReadMask,
63 receive_tt_message, 0);
66 * Create and register a pattern so ToolTalk knows we are interested
67 * in "ttsample1_value" messages within the session we join.
70 pat = tt_pattern_create();
71 tt_pattern_category_set(pat, TT_OBSERVE);
72 tt_pattern_scope_add(pat, TT_SESSION);
73 tt_pattern_op_add(pat, "ttsample1_value");
74 tt_pattern_register(pat);
77 * Join the default session
80 tt_session_join(tt_default_session());
83 * Turn control over to Motif.
85 XtRealizeWidget(toplevel);
89 * Before leaving, allow ToolTalk to clean up.
98 * When the button is pressed, broadcast the new slider value.
101 broadcast_value(widget, client_data, call_data)
103 XtPointer client_data, call_data;
109 * Create and send a ToolTalk notice message
110 * ttsample1_value(in int <new value)
113 XtVaGetValues(slider, XmNvalue, &slider_value, 0);
115 XtVaSetValues(slider, XmNvalue, slider_value, 0);
117 msg_out = tt_pnotice_create(TT_SESSION, "ttsample1_value");
118 tt_message_arg_add(msg_out, TT_IN, "integer", NULL);
119 tt_message_arg_ival_set(msg_out, 0, slider_value);
120 tt_message_send(msg_out);
123 * Since this message is a notice, we don't expect a reply, so
124 * there's no reason to keep a handle for the message.
127 tt_message_destroy(msg_out);
131 * When a ToolTalk message is available, receive it; if it's a
132 * ttsample1_value message, update the gauge with the new value.
135 receive_tt_message(client_data, fid, id)
136 XtPointer client_data;
146 msg_in = tt_message_receive();
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.
154 if (msg_in == NULL) return;
158 * Get a storage mark so we can easily free all the data
159 * ToolTalk returns to us.
164 op = tt_message_op(msg_in);
165 err = tt_ptr_error(op);
166 if (err > TT_WRN_LAST) {
167 printf( "tt_message_op(): %s\n", tt_status_message(err));
168 } else if (op != 0) {
169 if (0==strcmp("ttsample1_value", tt_message_op(msg_in))) {
170 tt_message_arg_ival(msg_in, 0, &val_in);
171 XtVaSetValues(gauge, XmNvalue, val_in, 0);
175 tt_message_destroy(msg_in);
181 * Straight Motif calls for creating the ui elements. No
182 * ToolTalk-specific code here.
185 create_ui_components()
188 Widget glabel, slabel;
191 base_frame = XtVaCreateManagedWidget("base_frame",
192 xmMainWindowWidgetClass, toplevel,
196 XtVaGetValues(base_frame, XmNmwmDecorations, &decor, 0);
197 decor &= ~MWM_DECOR_RESIZEH;
198 XtVaSetValues(base_frame, XmNmwmDecorations, &decor, 0);
200 controls = XtVaCreateManagedWidget("controls",
201 xmFormWidgetClass, base_frame, NULL, 0, 0);
203 slabel = XtVaCreateManagedWidget("Send:",
204 xmLabelWidgetClass, controls,
205 XmNleftAttachment, XmATTACH_WIDGET,
206 XmNtopAttachment, XmATTACH_WIDGET,
207 XmNtopWidget, controls,
211 slider = XtVaCreateManagedWidget("slider",
212 xmScaleWidgetClass, controls,
213 XmNleftAttachment, XmATTACH_WIDGET,
214 XmNleftWidget, controls,
216 XmNtopAttachment, XmATTACH_WIDGET,
217 XmNtopWidget, slabel,
222 XmNorientation, XmHORIZONTAL,
226 glabel = XtVaCreateManagedWidget("Received:",
227 xmLabelWidgetClass, controls,
228 XmNleftAttachment, XmATTACH_WIDGET,
229 XmNtopAttachment, XmATTACH_WIDGET,
231 XmNtopWidget, slider,
233 gauge = XtVaCreateManagedWidget("gauge",
234 xmScaleWidgetClass, controls,
235 XmNleftAttachment, XmATTACH_WIDGET,
236 XmNleftWidget, controls,
238 XmNtopAttachment, XmATTACH_WIDGET,
239 XmNtopWidget, glabel,
240 XmNorientation, XmHORIZONTAL,
248 label = XmStringCreateSimple("Broadcast");
249 button = XtVaCreateManagedWidget("button",
250 xmPushButtonWidgetClass, controls,
252 XmNtopAttachment, XmATTACH_WIDGET,
255 XmNleftWidget, controls,
256 XmNleftAttachment, XmATTACH_WIDGET,
258 XmNlabelString, label,
261 XtAddCallback(button, XmNactivateCallback, broadcast_value, 0);