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
23 /* $TOG: sunDtTermServer.c /main/6 1999/09/17 13:29:51 mgreess $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
26 * (c) Copyright 1993, 1994 International Business Machines Corp. *
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
28 * (c) Copyright 1993, 1994 Novell, Inc. *
31 #include "TermHeader.h"
32 #include "TermPrimDebug.h"
34 #include "TermPrimMessageCatI.h"
35 #include "TermPrimSetPty.h"
37 #include "DtTermLogit.h"
38 #endif /* LOG_USAGE */
41 #include <Dt/Service.h>
42 #include <Dt/UserMsg.h>
45 #include <X11/IntrinsicP.h>
46 #include <X11/CoreP.h>
49 #define GETXMSTRING(s, m, d) XmStringCreateLocalized(GETMESSAGE(s,m,d))
55 static Tt_pattern * DtTermToolTalkPattern = NULL;
56 static Boolean sendStopped = False;
57 XtInputId ProcessToolTalkInputId = 0;
59 char DTTERM_CLASS_NAME[] = "Dtterm";
60 char *displayString = NULL;
64 #define STICKAROUND 15 /* 15 minutes... */
65 static XtIntervalId waitId = (XtIntervalId) 0;
72 DieFromToolTalkError(Widget, char*, Tt_status);
75 SessionCallback( Tt_message msg, void * client_data, Tt_message contract);
78 HandleTtRequest(Tt_message msg, Tt_pattern pat);
81 dttermNewHandler(Tt_message msg);
84 static void TimeOut(XtPointer clientData, XtIntervalId *id);
88 FinalizeToolTalkSession( )
93 if (DtTermToolTalkPattern && tt_ptr_error(DtTermToolTalkPattern) == TT_OK) {
94 ttRc = ttdt_session_quit(NULL, DtTermToolTalkPattern, 1);
95 if (ProcessToolTalkInputId)
96 XtRemoveInput(ProcessToolTalkInputId);
99 ttRc = ttdt_close(NULL, NULL, sendStopped);
104 SessionCallback( Tt_message msg, void * client_data, Tt_message contract)
106 char *opString = tt_message_op(msg);
107 Tttk_op op = tttk_string_op(opString);
118 tt_message_reply(msg);
119 FinalizeToolTalkSession( );
122 /* Should we send some type of tt failure message? */
129 InitializeToolTalkSession(Widget topLevel, Boolean sendStarted)
134 sendStopped = sendStarted;
135 procId = ttdt_open(&ttFd,
141 ttstat = tt_ptr_error(procId);
143 GETMESSAGE(10, 2, "Could not connect to ToolTalk:\n%s\nExiting ...");
144 DieFromToolTalkError( topLevel, errfmt, ttstat );
146 ttstat = tt_ptype_declare("DT_Terminal");
147 DieFromToolTalkError( topLevel, "tt_ptype_declare", ttstat );
149 ttstat = tt_ptype_opnum_callback_add("DT_Terminal", 0, HandleTtRequest);
150 DieFromToolTalkError( topLevel, "tt_ptype_opnum_callback_add", ttstat );
152 ttstat = tt_ptype_declare("SDT_Terminal");
153 DieFromToolTalkError( topLevel, "tt_ptype_declare", ttstat );
155 ttstat = tt_ptype_opnum_callback_add("SDT_Terminal", 0, HandleTtRequest);
156 DieFromToolTalkError( topLevel, "tt_ptype_opnum_callback_add", ttstat );
159 * If we were started by a message, the following call to
160 * tttk_Xt_input_handler will process it. Otherwise,
161 * tttk_Xt_input_handler will just return.
163 tttk_Xt_input_handler(NULL, 0, 0);
169 FinishToolTalkInit(Widget topLevel)
171 ProcessToolTalkInputId =
172 XtAppAddInput(XtWidgetToApplicationContext(topLevel),
173 ttFd, (XtPointer)XtInputReadMask,
174 tttk_Xt_input_handler, procId);
175 DtTermToolTalkPattern = ttdt_session_join(tt_default_session( ),
180 if (tt_is_err(tt_ptr_error(DtTermToolTalkPattern))) {
181 ttdt_close(NULL, NULL, sendStopped);
184 refWidget = topLevel;
185 if (!displayString) {
186 /* This dtterm -sdtserver must have been started up from either a
187 * session startup or the command line. */
188 displayString = DisplayString(XtDisplay(refWidget));
195 HandleTtRequest(Tt_message msg, Tt_pattern pat)
200 op = tt_message_op( msg );
201 status = tt_ptr_error( op );
202 if (tt_is_err(status) || (op == 0)) {
203 /* Let tttk_Xt_input_handler() Do The Right Thing */
204 return TT_CALLBACK_CONTINUE;
206 if ((!strcmp(op, "SDtTerminal_New")) || (!strcmp(op, "DtTerminal_New"))) {
207 if ((getuid() == tt_message_uid(msg)) &&
208 (getgid() == tt_message_gid(msg))) {
209 dttermNewHandler( msg );
211 tt_message_reject(msg);
212 tt_message_destroy(msg);
213 return TT_CALLBACK_PROCESSED;
217 return TT_CALLBACK_CONTINUE;
221 return TT_CALLBACK_PROCESSED;
236 char *displayEnv, *newDisplayString;
238 msgFile = tt_message_file(msg);
239 if (tt_is_err(tt_ptr_error(msgFile))) msgFile = 0;
240 numArgs = tt_message_args_count(msg);
241 if (tt_is_err( tt_int_error(numArgs))) numArgs = 0;
242 for (i = 0; i < numArgs; i++) {
245 vtype = tt_message_arg_type(msg, i);
246 if ((vtype == 0) || (tt_is_err(tt_ptr_error(vtype)))) {
249 val = tt_message_arg_val(msg, i);
250 if(strcmp(vtype, "-display") == 0) {
251 newDisplayString = XtNewString(val);
257 if (!displayString) {
258 /* This tt message is part of an action dtterm -server startup. */
259 displayString = newDisplayString;
260 displayEnv = (char *)malloc(strlen("DISPLAY=") +
261 strlen(displayString) + 2);
263 strcat(displayEnv, "DISPLAY=");
264 strcat(displayEnv, displayString);
267 tt_message_reply(msg);
268 tttk_message_destroy(msg);
271 if (strcmp(displayString, newDisplayString)) {
273 tt_message_reject(msg);
274 XtFree(newDisplayString);
277 XtFree(newDisplayString);
281 (void) XtSetArg(arglist[argcnt], XmNallowShellResize, True);
283 shellWidget = XtAppCreateShell((char *) 0, "Dtterm",
284 applicationShellWidgetClass, XtDisplay((Widget) refWidget),
287 /* parse off messageFields and build the dttermview arglist... */
290 /* create the dtterm... */
291 (void) CreateInstance(shellWidget, "dtTermView", arglist, argcnt, True);
293 (void) XtRealizeWidget(shellWidget);
295 sunSetupIA(shellWidget);
299 /* since we now have active instances, we can remove our
303 (void) XtRemoveTimeOut(waitId);
304 waitId = (XtIntervalId) 0;
309 tt_message_reply(msg);
310 tttk_message_destroy(msg);
316 ServerStartSession(Widget topLevel, int argc, char **argv, Boolean loginShell,
317 char **commandToExec)
319 return(InitializeToolTalkSession(topLevel, True));
325 XtPointer clientData,
329 /* if we have no instances active, go away... */
330 if (InstanceCount <= 0) {
334 /* otherwise, clear the waitId... */
336 waitId = (XtIntervalId) 0;
343 ExitCB (Widget dialog, XtPointer client_data, XtPointer call_data)
345 exit((int) client_data);
349 DieFromToolTalkError(Widget parent, char *errfmt, Tt_status status)
352 Widget dialog, dialogShell;
353 char *errmsg, *statmsg, *title;
354 XmString xms_errmsg, xms_ok, xms_title;
357 if (! tt_is_err(status)) return;
358 statmsg = tt_status_message(status);
359 errmsg = XtMalloc(strlen(errfmt) + strlen(statmsg) + 2);
360 sprintf(errmsg, errfmt, statmsg);
362 xms_ok = GETXMSTRING(10, 3, "OK");
363 xms_errmsg = XmStringCreateLocalized(errmsg);
364 xms_title = GETXMSTRING(10, 1, "Terminal - Error");
367 XtSetArg(args[n], XmNautoUnmanage, False); n++;
368 XtSetArg(args[n], XmNokLabelString, xms_ok); n++;
369 XtSetArg(args[n], XmNdialogTitle, xms_title); n++;
370 XtSetArg(args[n], XmNmessageString, xms_errmsg); n++;
371 XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
373 dialog = XmCreateErrorDialog(parent, "IconEditorError", args, n);
374 XtAddCallback(dialog, XmNokCallback, ExitCB, (XtPointer) status);
375 XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_CANCEL_BUTTON));
376 XtUnmanageChild(XmMessageBoxGetChild(dialog, XmDIALOG_HELP_BUTTON));
379 * Disable the frame menu from dialog since we don't want the user
380 * to be able to close dialogs with the frame menu
382 dialogShell = XtParent(dialog);
384 XtSetArg(args[n], XmNmwmDecorations, MWM_DECOR_ALL | MWM_DECOR_MENU); n++;
385 XtSetValues(dialogShell, args, n);
386 XtManageChild(dialog);
387 XtRealizeWidget(dialogShell);
389 _DtSimpleError("Dtterm", DtFatalError, NULL, errmsg);
392 XmStringFree(xms_ok);
393 XmStringFree(xms_errmsg);
394 XmStringFree(xms_title);
397 XtAppProcessEvent(XtWidgetToApplicationContext(dialog), XtIMAll);