dtcalc: change from obsoleted MAXFLOAT to FLT_MAX from std C
[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 <stdlib.h>
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <string.h>
43 #include <Xm/XmAll.h>
44 #include <Tt/tt_c.h>
45
46 Widget toplevel, base_frame, controls, slider, gauge, button;
47 char *my_procid;
48
49 void    broadcast_value();
50 void    receive_tt_message();
51 void    create_ui_components();
52
53
54 int
55 main(argc, argv)
56         int             argc;
57         char            **argv;
58 {
59         int ttfd;
60         Tt_pattern pat;
61         XtAppContext app;
62
63         /*
64          * Initialize Motif and create ui components
65          */
66         toplevel = XtVaAppInitialize(&app, "ttsample1", NULL, 0,
67                                         &argc, argv, NULL, NULL);
68         XtVaSetValues(toplevel, XmNtitle, "ToolTalk Sample 1", NULL);
69         create_ui_components();
70
71         /*
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.
75          */
76
77         my_procid = tt_open();
78         ttfd = tt_fd();
79         if (ttfd < 0) {
80                 fprintf(stderr, "Cannot get tt_fd, err=%d\n", ttfd);
81                 return -1;
82         }
83                 
84
85         /*
86          * Arrange for Motif to call receive_tt_message when the ToolTalk
87          * file descriptor becomes active.
88          */
89
90         XtAppAddInput(app, ttfd, (XtPointer) XtInputReadMask,
91                       receive_tt_message, 0);
92
93         /*
94          * Create and register a pattern so ToolTalk knows we are interested
95          * in "ttsample1_value" messages within the session we join.
96          */
97
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);
103
104         /*
105          * Join the default session
106          */
107
108         tt_session_join(tt_default_session());
109
110         /*
111          * Turn control over to Motif.
112          */
113         XtRealizeWidget(toplevel);
114         XtAppMainLoop(app);
115         
116         /*
117          * Before leaving, allow ToolTalk to clean up.
118          */
119         tt_close();
120
121         return 0;
122 }
123
124
125 /*
126  * When the button is pressed, broadcast the new slider value.
127  */
128 void
129 broadcast_value(widget, client_data, call_data)
130         Widget widget;
131         XtPointer client_data, call_data;
132 {
133         XtArgVal slider_value;
134         Tt_message msg_out;
135
136         /*
137          * Create and send a ToolTalk notice message
138          * ttsample1_value(in int <new value)
139          */
140
141         XtVaGetValues(slider, XmNvalue, &slider_value, 0, NULL);
142         slider_value++;
143         XtVaSetValues(slider, XmNvalue, slider_value, 0, NULL);
144
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);
149
150         /*
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.
153          */
154
155         tt_message_destroy(msg_out);
156 }
157
158 /*
159  * When a ToolTalk message is available, receive it; if it's a
160  * ttsample1_value message, update the gauge with the new value.
161  */
162 void
163 receive_tt_message(client_data, fid, id)
164 XtPointer client_data;
165 int *fid;
166 XtInputId *id;
167 {
168         Tt_message msg_in;
169         int mark;
170         int val_in;
171         char *op;
172         Tt_status err;
173
174         msg_in = tt_message_receive();
175
176         /*
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.
180          */
181
182         if (msg_in == NULL) return;
183
184
185         /*
186          * Get a storage mark so we can easily free all the data
187          * ToolTalk returns to us.
188          */
189
190         mark = tt_mark();
191
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);
200                 }
201         }
202
203         tt_message_destroy(msg_in);
204         tt_release(mark);
205         return;
206 }
207             
208 /*
209  * Straight Motif calls for creating the ui elements.  No
210  * ToolTalk-specific code here.
211  */
212 void
213 create_ui_components()
214 {
215         int decor;
216         Widget glabel, slabel;
217         XmString label;
218
219         base_frame = XtVaCreateManagedWidget("base_frame",
220                 xmMainWindowWidgetClass, toplevel,
221                 XmNwidth,                250,
222                 XmNheight,               175,
223                 NULL);
224         XtVaGetValues(base_frame, XmNmwmDecorations, &decor, 0, NULL);
225         decor &= ~MWM_DECOR_RESIZEH;
226         XtVaSetValues(base_frame, XmNmwmDecorations, &decor, 0, NULL);
227
228         controls = XtVaCreateManagedWidget("controls",
229                 xmFormWidgetClass, base_frame, NULL, 0, NULL);
230
231         slabel = XtVaCreateManagedWidget("Send:",
232                 xmLabelWidgetClass, controls,
233                 XmNleftAttachment,  XmATTACH_WIDGET,
234                 XmNtopAttachment,   XmATTACH_WIDGET,
235                 XmNtopWidget,       controls,
236                 XmNtopOffset,           10,
237                 XmNleftOffset,          5,
238                 NULL);
239         slider = XtVaCreateManagedWidget("slider",
240                 xmScaleWidgetClass, controls,
241                 XmNleftAttachment,  XmATTACH_WIDGET,
242                 XmNleftWidget,      controls,
243                 XmNleftOffset,          10,
244                 XmNtopAttachment,   XmATTACH_WIDGET,
245                 XmNtopWidget,       slabel,
246                 XmNscaleWidth,      225,
247                 XmNscaleHeight,     18,
248                 XmNminimum,         0,
249                 XmNmaximum,         25,
250                 XmNorientation,     XmHORIZONTAL,
251                 XmNshowValue,       TRUE,
252                 NULL);
253
254         glabel = XtVaCreateManagedWidget("Received:",
255                 xmLabelWidgetClass, controls,
256                 XmNleftAttachment,  XmATTACH_WIDGET,
257                 XmNtopAttachment,   XmATTACH_WIDGET,
258                 XmNleftOffset,          5,
259                 XmNtopWidget,       slider,
260                 NULL);
261         gauge = XtVaCreateManagedWidget("gauge",
262                 xmScaleWidgetClass, controls,
263                 XmNleftAttachment,  XmATTACH_WIDGET,
264                 XmNleftWidget,      controls,
265                 XmNleftOffset,          10,
266                 XmNtopAttachment,   XmATTACH_WIDGET,
267                 XmNtopWidget,       glabel,
268                 XmNorientation,     XmHORIZONTAL,
269                 XmNscaleWidth,      225,
270                 XmNscaleHeight,     18,
271                 XmNminimum,         0,
272                 XmNmaximum,         25,
273                 XmNshowValue,       TRUE,
274                 NULL);
275
276         label = XmStringCreateSimple("Broadcast");
277         button = XtVaCreateManagedWidget("button",
278                 xmPushButtonWidgetClass, controls,
279                 XmNtopWidget,           gauge,
280                 XmNtopAttachment,       XmATTACH_WIDGET,
281                 XmNtopOffset,           5,
282                 XmNleftOffset,          75,
283                 XmNleftWidget,          controls,
284                 XmNleftAttachment,  XmATTACH_WIDGET,
285                 XmNbottomOffset,        5,
286                 XmNlabelString, label,
287                 NULL);
288         XmStringFree(label);
289         XtAddCallback(button, XmNactivateCallback, broadcast_value, 0);
290 }