Skip to content

Commit

Permalink
Support mouse on WIN32.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Nov 7, 2018
1 parent 11946a6 commit 8a604bb
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 22 deletions.
6 changes: 4 additions & 2 deletions NEWS.VER
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

Major changes between "less" versions 530 and @@VERSION@@

* Add --nohistdups option.

* Add --mouse option.

* Add --nohistdups option.

* Support PCRE2 regular expression library.

* Redraw screen on SIGWINCH even if screen size doesn't change.
Expand All @@ -35,6 +35,8 @@

* Fix bug in v command when filename contains shell metacharacters.

======================================================================

Major changes between "less" versions 487 and 530

* Don't output terminal init sequence if using -F and file fits on one screen.
Expand Down
12 changes: 2 additions & 10 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static unsigned char cmdtable[] =
CONTROL('D'),0, A_F_SCROLL,
'u',0, A_B_SCROLL,
CONTROL('U'),0, A_B_SCROLL,
'\e','[','M',0, A_X11MOUSE_IN,
ESC,'[','M',0, A_X11MOUSE_IN,
' ',0, A_F_SCREEN,
'f',0, A_F_SCREEN,
CONTROL('F'),0, A_F_SCREEN,
Expand Down Expand Up @@ -418,14 +418,6 @@ add_var_table(tlist, buf, len)
static int
x11mouse_action()
{
#define X11MOUSE_BUTTON1 0x20
#define X11MOUSE_BUTTON2 0x21
#define X11MOUSE_BUTTON3 0x22
#define X11MOUSE_BUTTON_REL 0x23
#define X11MOUSE_WHEEL_UP 0x60
#define X11MOUSE_WHEEL_DOWN 0x61
#define X11MOUSE_POS_OFFSET 0x20

int b = getcc();
int x = getcc() - X11MOUSE_POS_OFFSET-1;
int y = getcc() - X11MOUSE_POS_OFFSET-1;
Expand All @@ -439,7 +431,7 @@ x11mouse_action()
case X11MOUSE_BUTTON_REL:
/*
* {{ It would be better to do this in commands()
* but it's nontrival to pass y to it. }}
* but it's nontrivial to pass y to it. }}
*/
setmark('#', y);
screen_trashed = 1;
Expand Down
1 change: 0 additions & 1 deletion defines.wn
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@
#endif

#define popen _popen
#define pclose _pclose
#if !defined(_MSC_VER) || (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
Expand Down
12 changes: 12 additions & 0 deletions less.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,15 @@ struct wchar_range_table
#define time_type long
#endif

/* X11 mouse reporting definitions */
#define X11MOUSE_BUTTON1 0x20
#define X11MOUSE_BUTTON2 0x21
#define X11MOUSE_BUTTON3 0x22
#define X11MOUSE_BUTTON_REL 0x23
#define X11MOUSE_WHEEL_UP 0x60
#define X11MOUSE_WHEEL_DOWN 0x61
#define X11MOUSE_POS_OFFSET 0x20

struct mlist;
struct loption;
struct hilite_tree;
Expand All @@ -534,3 +543,6 @@ struct hilite_tree;
void postoa LESSPARAMS ((POSITION, char*));
void linenumtoa LESSPARAMS ((LINENUM, char*));
void inttoa LESSPARAMS ((int, char*));
#if MSDOS_COMPILER==WIN32C
int pclose LESSPARAMS ((FILE*));
#endif
41 changes: 33 additions & 8 deletions screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ struct keyRecord
static int keyCount = 0;
static WORD curr_attr;
static int pending_scancode = 0;
static char x11mousebuf[] = "[M???"; /* \e is separate */
static int x11mousePos, x11mouseCount;

static HANDLE con_out_save = INVALID_HANDLE_VALUE; /* previous console */
static HANDLE con_out_ours = INVALID_HANDLE_VALUE; /* our own */
Expand Down Expand Up @@ -2412,6 +2414,14 @@ win32_kbhit()
currentKey.ascii = 0;
currentKey.scan = 0;

if (x11mouseCount > 0)
{
currentKey.ascii = x11mousebuf[x11mousePos++];
--x11mouseCount;
keyCount = 1;
return (TRUE);
}

/*
* Wait for a real key-down event, but
* ignore SHIFT and CONTROL key events.
Expand All @@ -2422,16 +2432,31 @@ win32_kbhit()
if (read == 0)
return (FALSE);
ReadConsoleInput(tty, &ip, 1, &read);
/* read mouse wheel and fake up/down arrow key presses */
/* generate an X11 mouse sequence from the mouse event */
if (mousecap && ip.EventType == MOUSE_EVENT &&
ip.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
ip.Event.MouseEvent.dwEventFlags != MOUSE_MOVED)
{
/* {{ This does not support setmark('#') like the X11 version does.
* Also fails if user redefines the arrow actions. }} */
currentKey.scan = ((int)ip.Event.MouseEvent.dwButtonState < 0) ? PCK_DOWN : PCK_UP;
if (mousecap == OPT_ONPLUS)
currentKey.scan = (currentKey.scan == PCK_DOWN) ? PCK_UP : PCK_DOWN;
currentKey.ascii = 0;
x11mousebuf[3] = X11MOUSE_POS_OFFSET + ip.Event.MouseEvent.dwMousePosition.X + 1;
x11mousebuf[4] = X11MOUSE_POS_OFFSET + ip.Event.MouseEvent.dwMousePosition.Y + 1;
switch (ip.Event.MouseEvent.dwEventFlags)
{
case 0: /* press or release */
if (ip.Event.MouseEvent.dwButtonState == 0)
x11mousebuf[2] = X11MOUSE_BUTTON_REL;
else if (ip.Event.MouseEvent.dwButtonState & (FROM_LEFT_3RD_BUTTON_PRESSED | FROM_LEFT_4TH_BUTTON_PRESSED))
continue;
else
x11mousebuf[2] = X11MOUSE_BUTTON1 + ((int)ip.Event.MouseEvent.dwButtonState << 1);
break;
case MOUSE_WHEELED:
x11mousebuf[2] = ((int)ip.Event.MouseEvent.dwButtonState < 0) ? X11MOUSE_WHEEL_DOWN : X11MOUSE_WHEEL_UP;
break;
default:
continue;
}
x11mousePos = 0;
x11mouseCount = 5;
currentKey.ascii = ESC;
keyCount = 1;
return (TRUE);
}
Expand Down
16 changes: 16 additions & 0 deletions ttyin.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ close_getchr()
#endif
}

#if MSDOS_COMPILER==WIN32C
/*
* Close the pipe, restoring the keyboard (CMD resets it, losing the mouse).
*/
int
pclose(f)
FILE *f;
{
int result;

result = _pclose(f);
SetConsoleMode(tty, ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT);
return result;
}
#endif

/*
* Get a character from the keyboard.
*/
Expand Down
3 changes: 2 additions & 1 deletion version.c
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ v538 9/3/18 Clean up some WIN32 code.
v539 9/13/18 Fix spurious input on Windows with CAPSLOCK.
v540 10/29/18 Add --mouse option.
v541 10/30/18 Add --MOUSE option.
v542 11/6/18 Add mouse support for WIN32 (thanks to Jason Hood).
*/

char version[] = "541";
char version[] = "542";

0 comments on commit 8a604bb

Please sign in to comment.