#ifdef XVIEW
#include <xview/xview.h>
#include <xview/panel.h>
#include <xview/attr.h>
#include <xview/tty.h>
#include <xview/ttysw.h>
#include <xview/termsw.h>
#include <xview/font.h>
#else
#include <suntool/sunview.h>
#include <suntool/tty.h>
#include <suntool/ttysw.h>
#endif XVIEW
#ifdef JLE
# include <locale.h>
#endif JLE
#include <stdio.h>
#include <sys/file.h>
#define BUFFER_SIZE 128
#define WANT_CAPS_LOCK
#ifdef WANT_CAPS_LOCK
int caps_lock;
static char *Caps = "[CAPS] ";
#define CAPS_LEN 7
#endif
static char *mouse_prefix = "\030\000";
static int m_prefix_length = 2;
static char *key_prefix = "\030*";
static int k_prefix_length = 2;
#ifdef JLE
static char *emacs_name = "nemacs";
static char *title = "NEmacstool - ";
#else
static char *emacs_name = "emacs";
static char *title = "Emacstool - ";
#endif JLE
static char buffer[BUFFER_SIZE];
static char *bold_name = 0;
Frame frame;
#ifndef TTERM
#define SWTYPE TTY
Tty tty_win;
#else
#define SWTYPE TERMSW
Termsw tty_win;
#endif TTERM
#ifdef XVIEW
Xv_Window tty_view;
#else
Tty tty_view;
#endif XVIEW
int font_width, font_height;
int left_margin = 0;
int console_fd = 0;
FILE *console;
Icon frame_icon;
static short default_image[258] =
{
#include <images/terminal.icon>
};
mpr_static(icon_image, 64, 64, 1, default_image);
int
button_value (event)
Event *event;
{
int retval = 0;
if (MS_LEFT == (event_id (event))) retval = 1;
if (MS_MIDDLE == (event_id (event))) retval = 2;
if (MS_RIGHT == (event_id (event))) retval = 4;
if (event_shift_is_down (event)) retval += 8;
if (event_ctrl_is_down (event)) retval += 16;
if (event_meta_is_down (event)) retval += 32;
if (event_is_up (event)) retval += 128;
return retval;
}
static long prev_event_sec = 0;
static long prev_event_usec = 0;
int
time_delta (now_sec, now_usec, prev_sec, prev_usec)
long now_sec, now_usec, prev_sec, prev_usec;
{
long sec_delta = now_sec - prev_sec;
long usec_delta = now_usec - prev_usec;
if (usec_delta < 0) {
usec_delta += 1000000;
--sec_delta;
}
if (sec_delta >= 10)
return (9999);
else
return ((sec_delta * 1000) + (usec_delta / 1000));
}
static Notify_value
input_event_filter_function (window, event, arg, type)
#ifdef XVIEW
Xv_Window window;
#else
Window window;
#endif XVIEW
Event *event;
Notify_arg arg;
Notify_event_type type;
{
struct timeval time_stamp;
if (console_fd) fprintf(console, "Event: %d\n", event_id(event));
if (event_id(event) == WIN_STOP) {
ttysw_input(tty_win, "\007\007\007\007\007\007\007", 7);
return NOTIFY_IGNORED;
}
if (event_id(event) == KEY_LEFT(5) || event_id(event) == KEY_LEFT(7))
if(event_is_up (event))
return notify_next_event_func (window, event, arg, type);
else return NOTIFY_IGNORED;
if (event_is_button (event)) {
time_stamp = event_time (event);
ttysw_input (tty_win, mouse_prefix, m_prefix_length);
sprintf (buffer, "(%d %d %d %d)\015",
button_value (event),
(event_x (event) - left_margin) / font_width,
event_y (event) / font_height,
time_delta (time_stamp.tv_sec, time_stamp.tv_usec,
prev_event_sec, prev_event_usec)
);
ttysw_input (tty_win, buffer, strlen(buffer));
prev_event_sec = time_stamp.tv_sec;
prev_event_usec = time_stamp.tv_usec;
return NOTIFY_IGNORED;
}
{
int d;
char c = (char) 0;
if ((event_is_key_left (event)) ?
((d = event_id(event) - KEY_LEFT(1) + 'a'), c='l') :
((event_is_key_right (event)) ?
((d = event_id(event) - KEY_RIGHT(1) + 'a'), c='r') :
((event_is_key_top (event)) ?
((d = event_id(event) - KEY_TOP(1) + 'a'), c='t') : 0)))
{
if (event_is_up(event)) return NOTIFY_IGNORED;
if (event_shift_is_down (event)) c = c - 32;
if (event_ctrl_is_down (event)) c = c - 64;
if (event_meta_is_down (event)) c = c + 128;
#ifdef WANT_CAPS_LOCK
if (event_id(event) == KEY_TOP(1))
{
strcpy (buffer, Caps);
title = &buffer[CAPS_LEN];
strncpy (title, (char *)window_get (frame, FRAME_LABEL),
BUFFER_SIZE - CAPS_LEN);
buffer[BUFFER_SIZE] = (char) 0;
if (strncmp (title, Caps, CAPS_LEN) == 0)
title += CAPS_LEN;
caps_lock = (caps_lock ? 0 : CAPS_LEN);
window_set(frame, FRAME_LABEL, (title -= caps_lock), 0);
return NOTIFY_IGNORED;
}
#endif
ttysw_input (tty_win, key_prefix, k_prefix_length);
sprintf (buffer, "%c%c", d, c);
ttysw_input(tty_win, buffer, strlen(buffer));
return NOTIFY_IGNORED;
}
}
if ((event_is_ascii(event) || event_is_meta(event))
&& event_is_up(event)) return NOTIFY_IGNORED;
#ifdef WANT_CAPS_LOCK
if ((caps_lock) && event_is_ascii(event)
&& (event_id(event) >= 'a') && (event_id(event) <= 'z'))
event_set_id(event, (event_id(event) - 32));
#endif
#ifndef NO_META_BIT
if (event_is_ascii(event) && event_meta_is_down(event))
event_set_id(event, 128 | event_id(event));
#endif
return notify_next_event_func (window, event, arg, type);
}
main (argc, argv)
int argc;
char **argv;
{
int error_code;
#ifdef JLE
setlocale(LC_ALL, "");
#endif JLE
if(getenv("DEBUGEMACSTOOL"))
console = fdopen (console_fd = open("/dev/console",O_WRONLY), "w");
putenv("IN_EMACSTOOL=t");
if (putenv("TERM=sun") != 0)
{fprintf (stderr, "%s: Could not set TERM=sun, using `%s'\n",
argv[0], (char *)getenv("TERM")) ;};
{
char *termcap ;
termcap = (char *)getenv("TERMCAP") ;
if (termcap && (*termcap != '/'))
{
if (putenv("TERMCAP=") != 0)
{fprintf (stderr, "%s: Could not clear TERMCAP\n", argv[0]) ;} ;
} ;
} ;
if (!(argv[0] = (char *)getenv("EMACSTOOL")))
argv[0] = emacs_name;
for (argc = 1; argv[argc]; argc++)
{
if(!(strcmp ("-rc", argv[argc])))
{int i = argc;
argv[argc--]=0;
if (argv[i+1]) {
argv[0]=argv[i+1];
for (; argv[i+2]; (argv[i]=argv[i+2],argv[++i]=0));
}
}
if (!(strcmp ("-bold", argv[argc])))
{int i = argc;
argv[argc--]=0;
if (argv[i+1]) {
bold_name = argv[i+1];
for (; argv[i+2]; (argv[i]=argv[i+2],argv[++i]=0));
}
}
};
strcpy (buffer, title);
strncat (buffer, argv[0],
(BUFFER_SIZE - (strlen (buffer)) - (strlen (argv[0]))) - 1);
error_code = interpose_on_window(argc,argv);
if (error_code != 0) {
fprintf (stderr, "notify_interpose_event_func returns %d.\n", error_code);
exit (1);
}
#ifdef XVIEW
xv_main_loop (frame);
#else
window_main_loop (frame);
#endif XVIEW
}
#ifdef XVIEW
int interpose_on_window(argc,argv)
int argc;
char **argv;
{
#ifndef TTERM
int i, font_width_adjust = 1;
for (i = 1; argv[i]; i++) {
if (!(strcmp ("-Wt", argv[i])))
{font_width_adjust = 0;
if (console_fd) fprintf(console, "-Wt = %d\n", font_width_adjust);
break;}
}
#endif TTERM
xv_init(XV_INIT_ARGC_PTR_ARGV, &argc, argv, 0);
frame_icon = icon_create (ICON_LABEL, "Emacstool",
ICON_IMAGE, &icon_image,
0);
frame = xv_create ((Xv_Window)NULL, FRAME,
FRAME_LABEL, buffer,
FRAME_ICON, frame_icon,
0);
tty_win = xv_create (frame, SWTYPE, WIN_IS_CLIENT_PANE,
TTY_QUIT_ON_CHILD_DEATH, TRUE,
TTY_BOLDSTYLE, TTYSW_BOLD_INVERT,
TTY_ARGV, argv,
0);
if (bold_name) {
(void)xv_set(tty_win, TTY_BOLDSTYLE_NAME, bold_name, 0);
}
{
Xv_font font;
font = (Xv_font)xv_get (tty_win, XV_FONT);
font_height = (int)xv_get (font, FONT_DEFAULT_CHAR_HEIGHT);
font_width = (int)xv_get (font, FONT_DEFAULT_CHAR_WIDTH);
}
if (console_fd) fprintf(console, "Width = %d\n", font_width);
#ifndef TTERM
font_width -= font_width_adjust;
#else
xv_set(tty_win, TERMSW_MODE, TTYSW_MODE_TYPE, 0);
left_margin = (int)xv_get (tty_win, TEXTSW_LEFT_MARGIN);
#endif TTERM
tty_view = (Xv_Window) xv_get (tty_win, OPENWIN_NTH_VIEW, 0);
xv_set(tty_view,
WIN_CONSUME_EVENTS,
WIN_MOUSE_BUTTONS, WIN_UP_EVENTS,
ACTION_ADJUST, ACTION_MENU,
WIN_ASCII_EVENTS,
WIN_LEFT_KEYS, WIN_TOP_KEYS, WIN_RIGHT_KEYS,
0,
0);
return (int) notify_interpose_event_func
(tty_view, input_event_filter_function, NOTIFY_SAFE);
}
#else
int interpose_on_window (argc, argv)
int argc;
char **argv;
{
frame_icon = icon_create (ICON_LABEL, "Emacstool",
ICON_IMAGE, &icon_image,
0);
frame = window_create ((Window)NULL, FRAME,
FRAME_LABEL, buffer,
FRAME_ICON, frame_icon,
FRAME_ARGC_PTR_ARGV, &argc, argv,
0);
tty_win = window_create (frame, TTY,
TTY_QUIT_ON_CHILD_DEATH, TRUE,
TTY_BOLDSTYLE, TTYSW_BOLD_INVERT,
TTY_ARGV, argv,
0);
if (bold_name) {
(void)window_set(tty_win, TTY_BOLDSTYLE_NAME, bold_name, 0);
}
window_set (tty_win, WIN_FONT, pf_default(), 0);
font_height = (int)window_get (tty_win, WIN_ROW_HEIGHT);
font_width = (int)window_get (tty_win, WIN_COLUMN_WIDTH);
tty_view = tty_win;
window_set(tty_view,
WIN_CONSUME_PICK_EVENTS,
WIN_STOP,
WIN_MOUSE_BUTTONS, WIN_UP_EVENTS,
0,
WIN_CONSUME_KBD_EVENTS,
WIN_STOP,
WIN_ASCII_EVENTS,
WIN_LEFT_KEYS, WIN_TOP_KEYS, WIN_RIGHT_KEYS,
0,
0);
return (int) notify_interpose_event_func
(tty_view, input_event_filter_function, NOTIFY_SAFE);
}
#endif XVIEW