#include <xview/xview.h>
#include <xview/textsw.h
#include <xview/frame.h>
#include <xview/panel.h>
#include <xview/notice.h>
#include <XInput.h>
#include <sys/types.h>
#include <sys/timeb.h>
/*
    This program checks the xinput extension and xview compatibility
    by reading dials and buttons events.
*/

/* getopt variables */
int         debug_level = 1 << 30;
void        debug();
int         expert = 0;

Frame       frame;
Canvas      canvas[8];
Canvas      cv_vp[8];

Frame       textframe;
Textsw      tty;
Canvas      text_cv;
Xv_Server   server;
extern      _Xdebug;
extern int
            motion_notify_type,
            button_press_type,
            button_release_type,
            focus_in_type,
            focus_out_type;

void        debug();

main(argc, argv)
    int         argc;
    char      **argv;
{
    void        quit();
    void        tell_me_about_it();
    char       *xvargv[256];
    int         xvargc = 0;
    int         i = 1;
    void        extension_handler(), time_to_leave();

    /* _Xdebug = 1; */
    xvargv[0] = argv[0];
    xvargc = 1;
    while (i != argc) {
	if (argv[i][0] == '-')
	    switch (argv[i][1]) {
	    case 'd':
		debug_level = strtol(argv[++i], (char **) NULL, 10);
		break;
	    case 'v':
		debug_level = 0;
		break;
	    case 'x':
		expert = strtol(argv[++i], (char **) NULL, 10);
		break;
	    default:
		xvargv[xvargc] = argv[i];
		xvargc++;
	} else {
	    xvargv[xvargc] = argv[i];
	    xvargc++;
	}
	i++;
    }
    xvargv[xvargc] = '\0';
    xv_init(XV_INIT_ARGC_PTR_ARGV, &(xvargc), xvargv, NULL);

    textframe = xv_create(NULL, FRAME, FRAME_LABEL,
			  "XNeWS Input extension Event Test",
			  XV_X, 0,
			  XV_Y, 0,
			  NULL);

    tty = xv_create(textframe, TEXTSW,
		    WIN_ROWS, 20,
		    WIN_COLUMNS, 80,
		    TEXTSW_READ_ONLY, TRUE,
		    NULL);

    notify_interpose_destroy_func(textframe, time_to_leave);

    xv_set(textframe, XV_SHOW, TRUE, NULL);

    window_fit(textframe);

    text_msg("This program tests the XInputDeviceEvents extension to\n");
    text_msg("the X11/NeWS server. It also tests that the xview\n");
    text_msg("library correctly passes on extension events.\n");
    one_window(1);
    init_x(xv_get(frame, XV_DISPLAY),
	   xv_get(canvas_paint_window(canvas[0]), XV_XID));
    xv_main_loop(textframe);
    window_done(textframe);
    return(0);
}

text_msg(what)
    char       *what;
{
    textsw_insert(tty, what, strlen(what));
}

/*
   This function lets you see some or all of the debugging messages.  Once
   you get one part working, you don't have to look at its messages, but if
   it breaks again later, you can find out all about it, without recompiling.
*/

/* VARARGS1 */
void
debug(va_alist) va_dcl
{
    va_list     ap;
    int         i;
    char       *format;

    va_start(ap);
    i = va_arg(ap, int);
    if (i < debug_level)
	return;
    format = va_arg(ap, char *);
    _doprnt(format, ap, stderr);
    va_end(ap);
}

one_window(test_number)
    int         test_number;
{
    char        header[256];

    bzero(header, 256);
    sprintf(header, "Test #%d Parent", test_number);
    frame = xv_create(NULL, FRAME, FRAME_LABEL,
		      header,
		      XV_WIDTH, 200,
		      XV_HEIGHT, 100,
		      XV_X, 500,
		      XV_Y, 100,
		      NULL);

    canvas[0] = xv_create(frame, CANVAS, NULL);
    cv_vp[0] = xv_get(canvas[0], CANVAS_PIXWIN);

    xv_set(cv_vp[0], WIN_CONSUME_EVENTS, FALSE, WIN_NOTIFY_EVENT_PROC,
	   tell_me_about_it, NULL);
    notify_interpose_event_func(cv_vp[0], tell_me_about_it, NOTIFY_IMMEDIATE);

    xv_set(frame, XV_SHOW, TRUE, NULL);
    init_x(xv_get(frame, XV_DISPLAY),
	   xv_get(canvas_paint_window(canvas[0]), XV_XID));
    if (test_number == 1) {
	server = (Xv_Server) xv_get(xv_get(frame, XV_SCREEN), SCREEN_SERVER);
	xv_set(server, SERVER_EXTENSION_PROC, extension_handler, NULL);
    }
}
void
tell_me_about_it(win, event, arg, type)
    Xv_Window   win;
    Event      *event;
    Notify_arg  arg;
    Notify_event_type type;
{
    void        do_focus();

    debug(1, "WID %D event.ie_code %D\n", win, event->ie_code);
    if (arg != 0 || type != 0)
	debug(1, "Notify arg %X Notify event type %X\n", arg, type);
    do_focus(xv_get(win, XV_XID));
}
void
extension_handler(display, event, object)
    Display    *display;
    XEvent     *event;
    Xv_object   object;
{
    debug(2, "object %d Event.type %D\n", object, event->type);
    if (event->type == motion_notify_type) {
	XDeviceMotionEvent *othr = (XDeviceMotionEvent *) event;

	printf(" axes count %d first axis %d axis data %d\n",
	       othr->axes_count, othr->first_axis, othr->axis_data[0]);
    } else if (event->type == button_press_type) {
	XDeviceButtonEvent *othr = (XDeviceButtonEvent *) event;

	printf("Press button %d\n", othr->button);
    } else if (event->type == button_release_type) {
	XDeviceButtonEvent *othr = (XDeviceButtonEvent *) event;

	printf("Release button %d\n", othr->button);
    } else if (event->type == focus_in_type) {
	XDeviceFocusChangeEvent *othr = (XDeviceFocusChangeEvent *) event;
    } else if (event->type == focus_out_type) {
	XDeviceFocusChangeEvent *othr = (XDeviceFocusChangeEvent *) event;
    }
}
void
time_to_leave()
{
window_done(frame);
exit(0);
}
