#include "xf86config.h"

/*
 * Mouse configuration.
 */

static ListItem mousemenu[] = {
    { "None",           "No mouse" },
    { "MouseSystems",	"The mouse installed by the system" },
    { "Microsoft",	"Microsoft compatible (2-button protocol)" },
    { "MouseSystems",	"Mouse Systems (3-button protocol)" },
    { "Busmouse",	"Bus Mouse" },
    { "PS/2",		"PS/2 Mouse" },
    { "Logitech",	"Logitech Mouse (serial, old Logitech protocol)" },
    { "MouseMan",	"Logitech MouseMan (Microsoft compatible)" },
    { "MMSeries",	"MM Series" },
    { "MMHitTab",	"MM HitTablet" },
    { "IntelliMouse",	"Microsoft IntelliMouse" }
};
static int NRMOUSE = (sizeof mousemenu / sizeof mousemenu[0]);

char *
Mouse::protocol()
{
    return (_mousetype > 0) ? mousemenu[_mousetype].id : 0;
}

char *
Mouse::description()
{
    return (_mousetype > 0) ? mousemenu[_mousetype].item : 0;
}

/*
 * mouse_configuration() sets up the mouse configuration (as if you couldn't guess)
 */
int
Mouse::configure(int stage2)
{
    int i;
    int rc;

    while (1) {
	rc = Xmenu(TITLE, "Please select a mouse protocol", 8, mousemenu, NRMOUSE, 0, 0);
	if (rc < 0)
	    return 0;

	_mousetype = rc;

	if (_mousetype == SYSMOUSE) {
	    /* this one is easy! */
	    strcpy(device, "/dev/gpmdata");
	    return 1;
	}
	if (_mousetype == LOGIMOUSE) {
	    /* Logitech. */

	    rc = dialog_yesno(TITLE,
	"You have selected a Logitech protocol mouse. This is only valid\n"
	"for old Logitech mice.  Are you sure it's really not a Microsoft\n"
	"compatable mouse?", -1, -1);

	    if (rc != 0)
		continue;
	}

	chordmiddle = 0;
	if (_mousetype == MICROSOFT || _mousetype == MOUSEMAN) {
	    /* Microsoft or MouseMan. */

	    rc = dialog_yesno(TITLE, (char*)((_mousetype == MICROSOFT) ?
    "You have selected a Microsoft protocol mouse. If your mouse was\n"
    "made by Logitech, you might need to have ChordMiddle enabled to\n"
    "make the third button work properly.\n"
    "\n"
    "Do you want to enable ChordMiddle?"
		    :
    "Older Logitech MouseMan mice may need to have ChordMiddle enabled\n"
    "to make the third button work properly.\n"
    "\n"
    "Do you want to enable ChordMiddle?"), -1, -1);

	    if (rc == 0)
		chordmiddle = 1;
	    else if (rc < 0)
		continue;
	}
	cleardtrrts = 0;
	if (_mousetype == MOUSESYSTEMS) {
	    /* Mouse Systems. */

	    rc = dialog_yesno(TITLE, 
    "You have selected a Mouse Systems protocol mouse. If your mouse is\n"
    "normally in Microsoft-compatible mode, enabling the ClearDTR and\n"
    "ClearRTS options may cause it to switch to Mouse Systems mode when the\n"
    "server starts.\n"
    "\n"
    "Do you want to enable ClearDTR and ClearRTS? ", -1, -1);

	    if (rc == 0)
		cleardtrrts = 1;
	    else if (rc < 0)
		continue;
	}


	char *msg = 0;

	emulate3buttons = 0;
	switch (_mousetype) {
	case MICROSOFT:		/* Microsoft compatible */
	    if (!chordmiddle) {
		emulate3buttons = 1;
		break;
	    }
	    /* else fall through to... */
	case MOUSESYSTEMS:	/* Mouse Systems. */
	case INTELLIMOUSE:	/* IntelliMouse */
    /*
    "You have selected a three-button mouse protocol.  Don't bother to enable\n"
    "Emulate3Buttons unless the third button doesn't work.\n"
    */
	    break;
	default:
	    msg =
    "If your mouse has only two buttons, it is recommended that you enable\n"
    "Emulate3Buttons.\n";
	    break;
	}

	if (msg) {
	    char text[4000];

	    sprintf(text, "%s\n\nDo you want to enable Emulate3Buttons?", msg);
	    rc = dialog_yesno(TITLE, text, -1, -1);
	    if (rc == 0)
		emulate3buttons = 1;
	    else if (rc < 0)
		continue;
	}

	while (1) {
	    if (device[0] == 0)
		switch (_mousetype) {
		case PS2MOUSE:	strcpy(device, "/dev/psaux");
				break;
		case BUSMOUSE:	strcpy(device, "/dev/");
				break;
		default:	strcpy(device, "/dev/tty");
				break;
		}

	    rc = dialog_inputbox(TITLE, 
	    "Now give the full device name that the mouse is connected to,\n"
	    "for example /dev/ttyS0 for COM1, /dev/ttyS1 for COM2.\n", -1, 60,
		    device);

	    if (rc == 0 && strlen(device) == 0)
		continue;
	    else if (rc < 0)
		break;

	    if (access(device, F_OK) != 0) {
		char text[2000];

		sprintf(text, "%s does not seem to exist; use it anyway?",
			device);

		if (dialog_yesno(TITLE, text, -1, -1) != 0)
		    continue;
	    }
	    return 1;
	} /* while loop around `select a mouse device' */
    } /* while loop around `select a mouse protocol' */
} /* Mouse::configure */
