we have to let the screen code read the quit message before
[nvi.git] / motif / m_main.c
blob7eedafd3526db9c89e3ef40a67e46d9060e17803
1 /*-
2 * Copyright (c) 1996
3 * Rob Zimmermann. All rights reserved.
4 * Copyright (c) 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: m_main.c,v 8.24 1996/12/11 13:34:34 bostic Exp $ (Berkeley) $Date: 1996/12/11 13:34:34 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <X11/Intrinsic.h>
20 #include <X11/StringDefs.h>
21 #include <Xm/MainW.h>
23 #include <bitstring.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "../common/common.h"
33 #include "../ip_vi/ip.h"
34 #include "ipc_motif.h"
35 #include "ipc_mutil.h"
36 #include "ipc_mextern.h"
37 #include "pathnames.h"
39 #include "nvi.xbm" /* Icon bitmap. */
41 int vi_ofd; /* GLOBAL: known to vi_pipe_input_func(). */
43 static pid_t pid;
44 static Pixel icon_fg,
45 icon_bg;
46 static Pixmap icon_pm;
47 static Widget top_level;
48 static XtAppContext ctx;
50 static void onchld __P((int));
51 static void onexit __P((void));
54 /* resources for the vi widgets unless the user overrides them */
55 String fallback_rsrcs[] = {
57 "*font: -*-*-*-r-*--14-*-*-*-m-*-*-*",
58 "*pointerShape: xterm",
59 "*busyShape: watch",
60 "*iconName: vi",
62 /* coloring for the icons */
63 "*iconForeground: XtDefaultForeground",
64 "*iconBackground: XtDefaultBackground",
66 /* layout for the temporary preferences page */
67 "*toggleOptions.numColumns: 6",
68 "*intOptions.numColumns: 4",
69 "*otherOptions.numColumns: 3",
70 "*intOptions*columns: 10",
71 "*otherOptions*columns: 18",
73 /* --------------------------------------------------------------------- *
74 * anything below this point is only defined when we are not running CDE *
75 * --------------------------------------------------------------------- */
77 /* Do not define default colors when running under CDE
78 * (e.g. VUE on HPUX). The result is that you don't look
79 * like a normal desktop application
81 "?highlightColor: red",
82 "?background: gray75",
83 "?screen.background: wheat",
84 "?highlightColor: red"
87 static XutResource resource[] = {
88 { "iconForeground", XutRKpixel, &icon_fg },
89 { "iconBackground", XutRKpixel, &icon_bg },
92 #if defined(__STDC__)
93 static String *get_fallback_rsrcs( String name )
94 #else
95 static String *get_fallback_rsrcs( name )
96 String name;
97 #endif
99 String *copy = (String *) malloc( (1+XtNumber(fallback_rsrcs))*sizeof(String) );
100 int i, running_cde;
101 Display *d;
103 /* connect to server and see if the CDE atoms are present */
104 d = XOpenDisplay(0);
105 running_cde = __vi_is_cde( d );
106 XCloseDisplay(d);
108 for ( i=0; i<XtNumber(fallback_rsrcs); i++ ) {
110 /* stop here if running CDE */
111 if ( fallback_rsrcs[i][0] == '?' ) {
112 if ( running_cde ) break;
113 fallback_rsrcs[i] = strdup(fallback_rsrcs[i]);
114 fallback_rsrcs[i][0] = '*';
117 copy[i] = malloc( strlen(name) + strlen(fallback_rsrcs[i]) + 1 );
118 strcpy( copy[i], name );
119 strcat( copy[i], fallback_rsrcs[i] );
122 copy[i] = NULL;
123 return copy;
127 /* create the shell widgetry */
129 #if defined(__STDC__)
130 static void create_top_level_shell( int *argc, char **argv )
131 #else
132 static void create_top_level_shell( argc, argv )
133 int *argc;
134 char **argv;
135 #endif
137 char *ptr;
138 Widget main_w, menu_b;
139 Display *display;
141 /* X gets quite upset if the program name is not simple */
142 if (( ptr = strrchr( argv[0], '/' )) != NULL ) argv[0] = ++ptr;
143 vi_progname = argv[0];
145 /* create a top-level shell for the window manager */
146 top_level = XtVaAppInitialize( &ctx,
147 vi_progname,
148 NULL, 0, /* options */
149 (ArgcType) argc,
150 argv, /* might get modified */
151 get_fallback_rsrcs( argv[0] ),
152 NULL
154 display = XtDisplay(top_level);
156 /* might need to go technicolor... */
157 XutInstallColormap( argv[0], top_level );
159 /* create our icon
160 * do this *before* realizing the shell widget in case the -iconic
161 * option was specified.
163 icon_pm = XCreatePixmapFromBitmapData(
164 display,
165 DefaultRootWindow(display),
166 (char *) nvi_bits,
167 nvi_width,
168 nvi_height,
169 icon_fg,
170 icon_bg,
171 DefaultDepth( display, DefaultScreen(display) )
173 XutSetIcon( top_level, nvi_height, nvi_width, icon_pm );
175 /* in the shell, we will stack a menubar an editor */
176 main_w = XtVaCreateManagedWidget( "main",
177 xmMainWindowWidgetClass,
178 top_level,
179 NULL
182 /* create the menubar */
183 menu_b = (Widget) vi_create_menubar( main_w );
184 XtManageChild( menu_b );
186 /* add the VI widget from the library */
187 vi_create_editor( "editor", main_w, onexit );
189 /* put it up */
190 XtRealizeWidget( top_level );
195 main(argc, argv)
196 int argc;
197 char *argv[];
199 int i_fd;
202 * Initialize the X widgetry. We must do this before picking off
203 * arguments as well-behaved X programs have common argument lists
204 * (e.g. -rv for reverse video).
206 create_top_level_shell(&argc, argv);
208 /* We need to know if the child process goes away. */
209 (void)signal(SIGCHLD, onchld);
211 /* Run vi: the parent returns, the child is the vi process. */
212 (void)vi_run(argc, argv, &i_fd, &vi_ofd, &pid);
214 /* Tell X that we are interested in input on the pipe. */
215 XtAppAddInput(ctx, i_fd,
216 (XtPointer)XtInputReadMask, vi_pipe_input_func, NULL);
218 /* Main loop. */
219 XtAppMainLoop(ctx);
221 /* NOTREACHED */
222 abort();
226 * onchld --
227 * Handle SIGCHLD.
229 static void
230 onchld(signo)
231 int signo;
233 /* If the vi process goes away, we exit as well. */
234 if (kill(pid, 0))
235 vi_fatal_message(top_level, "The vi process died. Exiting.");
239 * onexit --
240 * Function called when the editor "quits".
242 static void
243 onexit()
245 exit (0);