summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 500ab9093858c4b4ed6d00e720c109a2bdbd24fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*  main.c -- Eterm main() function
 *         -- 22 August 1998, mej
 *
 * This file is original work by Michael Jennings <mej@eterm.org> and
 * Tuomo Venalainen <vendu@cc.hut.fi>.  This file, and any other file
 * bearing this same message or a similar one, is distributed under
 * the GNU Public License (GPL) as outlined in the COPYING file.
 *
 * Copyright (C) 1997, Michael Jennings and Tuomo Venalainen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 */

static const char cvs_ident[] = "$Id$";

#include "config.h"
#include "feature.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <ctype.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xos.h>

#include "../libmej/debug.h"	/* from libmej */
#include "debug.h"
#include "../libmej/mem.h"
#include "../libmej/strings.h"
#include "main.h"
#include "actions.h"
#include "command.h"
#include "eterm_utmp.h"
#include "events.h"
#include "graphics.h"
#include "options.h"
#include "pixmap.h"
#include "screen.h"
#include "scrollbar.h"
#include "term.h"
#ifdef USE_POSIX_THREADS
# include "threads.h"
#endif
#include "windows.h"

char *orig_argv0;

#ifdef PIXMAP_SUPPORT
/* Set to one in case there is no WM, or a lousy one
   that doesn't send the right events (*cough*
   Window Maker *cough*) -- mej */
short bg_needs_update = 1;
#endif
#ifdef USE_POSIX_THREADS
static void **retval;
static int join_value;
static pthread_t main_loop_thr;
static pthread_attr_t main_loop_attr;
# ifdef MUTEX_SYNCH
pthread_mutex_t mutex;
# endif
#endif
TermWin_t TermWin;
Display *Xdisplay;		/* display */
Colormap cmap;
unsigned int debug_level = 0;	/* Level of debugging information to display */
const char *display_name = NULL;
unsigned int colorfgbg;

/* main() */
int
main(int argc, char *argv[])
{

  int i;
  char *val;
  static char windowid_string[20], *display_string, *term_string;	/* "WINDOWID=\0" = 10 chars, UINT_MAX = 10 chars */
  ImlibInitParams params;

  orig_argv0 = argv[0];

#ifdef USE_POSIX_THREADS
# ifdef MUTEX_SYNCH
  pthread_atfork((void *) &prepare, (void *) &parent, (void *) &child);
# endif
#endif

  /* Security enhancements -- mej */
  putenv("IFS= \t");
  my_ruid = getuid();
  my_euid = geteuid();
  my_rgid = getgid();
  my_egid = getegid();
  privileges(REVERT);
  getcwd(initial_dir, PATH_MAX);

  init_defaults();

  /* Open display, get options/resources and create the window */
  if ((display_name = getenv("DISPLAY")) == NULL)
    display_name = ":0";

  /* This MUST be called before any other Xlib functions */

#ifdef USE_POSIX_THREADS
  if (XInitThreads()) {
    D_THREADS(("XInitThreads() succesful\n"));
  } else {
    D_THREADS(("XInitThreads() failed, I'm outta here\n"));
  }
#endif

  get_initial_options(argc, argv);
#ifdef NEED_LINUX_HACK
  privileges(INVOKE);		/* xdm in new Linux versions requires ruid != root to open the display -- mej */
#endif
  Xdisplay = XOpenDisplay(display_name);
#ifdef NEED_LINUX_HACK
  privileges(REVERT);
#endif
  if (!Xdisplay) {
    print_error("can't open display %s", display_name);
    exit(EXIT_FAILURE);
  }
#if DEBUG >= DEBUG_X
  if (debug_level >= DEBUG_X) {
    XSetErrorHandler((XErrorHandler) abort);
  } else {
    XSetErrorHandler((XErrorHandler) xerror_handler);
  }
#else
  XSetErrorHandler((XErrorHandler) xerror_handler);
#endif

  if (Options & Opt_install) {
    cmap = XCreateColormap(Xdisplay, Xroot, Xvisual, AllocNone);
    XInstallColormap(Xdisplay, cmap);
#ifdef PIXMAP_SUPPORT
    params.cmap = cmap;
    params.flags = PARAMS_COLORMAP;
#endif
  } else {
    cmap = Xcmap;
#ifdef PIXMAP_SUPPORT
    params.flags = 0;
#endif
  }

  /* Since we always use Imlib now, let's initialize it here. */
#ifdef PIXMAP_SUPPORT
  if (params.flags) {
    imlib_id = Imlib_init_with_params(Xdisplay, &params);
  } else {
    imlib_id = Imlib_init(Xdisplay);
  }
  if (!imlib_id) {
    fatal_error("Unable to initialize Imlib.  Aborting.");
  }
#endif

  read_config(THEME_CFG);
  read_config((rs_config_file ? rs_config_file : USER_CFG));

#if defined(PIXMAP_SUPPORT)
  if (rs_path || theme_dir || user_dir) {
    register unsigned long len;
    register char *tmp;

    len = strlen(initial_dir);
    if (rs_path) {
      len += strlen(rs_path) + 1;	/* +1 for the colon */
    }
    if (theme_dir) {
      len += strlen(theme_dir) + 1;
    }
    if (user_dir) {
      len += strlen(user_dir) + 1;
    }
    tmp = MALLOC(len + 1);	/* +1 here for the NUL */
    snprintf(tmp, len + 1, "%s%s%s%s%s%s%s", (rs_path ? rs_path : ""), (rs_path ? ":" : ""), initial_dir,
	     (theme_dir ? ":" : ""), (theme_dir ? theme_dir : ""), (user_dir ? ":" : ""), (user_dir ? user_dir : ""));
    tmp[len] = '\0';
    FREE(rs_path);
    rs_path = tmp;
    D_OPTIONS(("New rs_path set to \"%s\"\n", rs_path));
  }
#endif
  get_options(argc, argv);
  D_UTMP(("Saved real uid/gid = [ %d, %d ]  effective uid/gid = [ %d, %d ]\n", my_ruid, my_rgid, my_euid, my_egid));
  D_UTMP(("Now running with real uid/gid = [ %d, %d ]  effective uid/gid = [ %d, %d ]\n", getuid(), getgid(), geteuid(),
	  getegid()));

  post_parse();

#ifdef PREFER_24BIT
  cmap = DefaultColormap(Xdisplay, Xscreen);

  /*
   * If depth is not 24, look for a 24bit visual.
   */
  if (Xdepth != 24) {
    XVisualInfo vinfo;

    if (XMatchVisualInfo(Xdisplay, Xscreen, 24, TrueColor, &vinfo)) {
      Xdepth = 24;
      Xvisual = vinfo.visual;
      cmap = XCreateColormap(Xdisplay, RootWindow(Xdisplay, Xscreen),
			     Xvisual, AllocNone);
    }
  }
#endif

  Create_Windows(argc, argv);
  scr_reset();			/* initialize screen */
  Gr_reset();			/* reset graphics */

  /* add scrollBar, do it directly to avoid resize() */
  scrollbar_mapping(Options & Opt_scrollBar);

#if DEBUG >= DEBUG_X
  if (debug_level >= DEBUG_X) {
    XSynchronize(Xdisplay, True);
  }
#endif

#ifdef DISPLAY_IS_IP
  /* Fixup display_name for export over pty to any interested terminal
   * clients via "ESC[7n" (e.g. shells).  Note we use the pure IP number
   * (for the first non-loopback interface) that we get from
   * network_display().  This is more "name-resolution-portable", if you
   * will, and probably allows for faster x-client startup if your name
   * server is beyond a slow link or overloaded at client startup.  Of
   * course that only helps the shell's child processes, not us.
   *
   * Giving out the display_name also affords a potential security hole
   */

  val = display_name = network_display(display_name);
  if (val == NULL)
#endif /* DISPLAY_IS_IP */
    val = XDisplayString(Xdisplay);
  if (display_name == NULL)
    display_name = val;		/* use broken `:0' value */

  i = strlen(val);
  display_string = MALLOC((i + 9));

  sprintf(display_string, "DISPLAY=%s", val);
  sprintf(windowid_string, "WINDOWID=%u", (unsigned int) TermWin.parent);

  /* add entries to the environment:
   * DISPLAY:   in case we started with -display
   * WINDOWID:  X window id number of the window
   * COLORTERM: terminal sub-name and also indicates its color
   * TERM:      terminal name
   */
  putenv(display_string);
  putenv(windowid_string);
  if (Xdepth <= 2) {
    putenv("COLORTERM=" COLORTERMENV "-mono");
    putenv("TERM=" TERMENV);
  } else {
    if (rs_term_name != NULL) {
      i = strlen(rs_term_name);
      term_string = MALLOC((i + 6) * sizeof(char));

      sprintf(term_string, "TERM=%s", rs_term_name);
      putenv(term_string);
    } else {
#ifdef DEFINE_XTERM_COLOR
      if (Xdepth <= 2)
	putenv("TERM=" TERMENV);
      else
	putenv("TERM=" TERMENV "-color");
#else
      putenv("TERM=" TERMENV);
#endif
    }
    putenv("COLORTERM=" COLORTERMENV);
  }
  putenv("ETERM_VERSION=" VERSION);

  D_CMD(("init_command()\n"));
  init_command(rs_execArgs);
#ifndef USE_POSIX_THREADS
  if (Options & Opt_borderless) {
    resize_window();
  }
#endif

#ifdef USE_POSIX_THREADS
  D_THREADS(("main_thread:"));
  pthread_attr_init(&main_loop_attr);
  pthread_create(&main_loop_thr, &main_loop_attr,
		 (void *) &main_thread, NULL);
  D_THREADS(("done? :)\n"));
  while (1);
  /*  main_loop(); */
#else
  main_loop();			/* main processing loop */
#endif

  return (EXIT_SUCCESS);
}
/* EOF */