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