From cba09593aba658e71ddb9e52f12bdf91b22014a6 Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Tue, 19 Mar 2024 16:18:55 +0300 Subject: [PATCH] split mouse code to separate file --- 32/Makefile | 4 +- src/Makefile | 2 +- src/command.c | 225 +-------------------------------------------- src/ms.c | 246 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/ms.h | 10 ++ 5 files changed, 260 insertions(+), 227 deletions(-) create mode 100644 src/ms.c create mode 100644 src/ms.h diff --git a/32/Makefile b/32/Makefile index 8a214d9..4381bf1 100644 --- a/32/Makefile +++ b/32/Makefile @@ -10,8 +10,8 @@ C_OPT = -Wall -O2 -Wmissing-declarations -Wwrite-strings -I $(SRCDIR) -I . # avoid CMOVxx instructions C_OPT += -march=i386 LINK_OPT = -SRCS = $(SRCDIR)/command.c $(SRCDIR)/cmdbuf.c $(SRCDIR)/version.c \ - memmem.c fmemcpy.c +SRCS = $(SRCDIR)/command.c $(SRCDIR)/cmdbuf.c $(SRCDIR)/ms.c \ + $(SRCDIR)/version.c memmem.c fmemcpy.c ASSRCS = $(SRCDIR)/asm.S $(SRCDIR)/int23.S $(SRCDIR)/int0.S $(SRCDIR)/mouse.S OBJS = $(notdir $(SRCS:.c=.o)) $(notdir $(ASSRCS:.S=.o)) CMD = comcom32.exe diff --git a/src/Makefile b/src/Makefile index c8b1211..2a9eae9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -24,7 +24,7 @@ C_OPT = -Wall -Og -Wmissing-declarations -Wwrite-strings -fpic \ LINK_OPT = $(shell pkg-config --libs dj64) -shared -Wl,-Bsymbolic \ -Wl,-rpath=/usr/local/i386-pc-dj64/lib64 \ -Wl,-rpath=/usr/i386-pc-dj64/lib64 -SRCS = command.c cmdbuf.c version.c thunks_a.c thunks_c.c +SRCS = command.c cmdbuf.c ms.c version.c thunks_a.c thunks_c.c OBJS = $(SRCS:.c=.o) ASSRCS = int23.S int0.S asm.S plt.S mouse.S ASOBJS = $(ASSRCS:.S=.o) diff --git a/src/command.c b/src/command.c index 9d2fddf..6b44b41 100644 --- a/src/command.c +++ b/src/command.c @@ -73,7 +73,6 @@ #include #include #include -#include #include #include "cmdbuf.h" @@ -85,6 +84,7 @@ #endif #include "asm.h" #include "version.h" +#include "ms.h" #include "command.h" /* @@ -229,9 +229,6 @@ static void set_break(int on); static void get_env(void); static int compl_cmds(const char *prefix, int print, int *r_len, char *r_p); static int compl_fname(const char *prefix, int print, int *r_len, char *r_p); -static void mouse_show(void); -static void mouse_hide(void); -static void mouse_reset(void); struct built_in_cmd { @@ -4803,226 +4800,6 @@ void do_int0(void) __dpmi_set_real_mode_interrupt_vector(0, &int0_vec); } -#ifdef DJ64 -static unsigned mouse_regs; -#else -static __dpmi_regs *mouse_regs; -#define djaddr2ptr(a) ((unsigned char *)((a) - __djgpp_base_address)) -#endif -static __dpmi_meminfo mregs = { .size = sizeof(__dpmi_regs) }; -static __dpmi_raddr newm; -static __dpmi_raddr oldm; -static unsigned old_mask; -#define MEV_MASK 0xab - -static unsigned short popw(__dpmi_regs *r) -{ - unsigned lina = (r->x.ss << 4) + r->x.sp; - unsigned short ret = _farpeekw(_dos_ds, lina); - r->x.sp += 2; - return ret; -} - -static void do_retf(__dpmi_regs *r) -{ - r->x.ip = popw(r); - r->x.cs = popw(r); -} - -static void mvxl(int d) -{ - __dpmi_regs r = {}; - - while (d--) - { - r.x.ax = 0x500; - r.x.cx = 0x4BE0; - __dpmi_int(0x16, &r); - } -} - -static void mvxr(int d) -{ - __dpmi_regs r = {}; - - while (d--) - { - r.x.ax = 0x500; - r.x.cx = 0x4DE0; - __dpmi_int(0x16, &r); - } -} - -static void mlb(int alt_fn, int x, int y) -{ - __dpmi_regs r = {}; - short c; - - if (alt_fn) - { - int cx = wherex(); - if (cx == x) - return; - if (x < cx) - mvxl(cx - x); - else - mvxr(x - cx); - return; - } - - _conio_gettext(x, y, x, y, &c); - - r.x.ax = 0x500; - r.x.cx = c & 0xff; - __dpmi_int(0x16, &r); -} - -static void mrb(int alt_fn) -{ - __dpmi_regs r = {}; - - r.x.ax = 0x500; - r.x.cx = alt_fn ? 0x3 : 0x0F09; // ^C or TAB - __dpmi_int(0x16, &r); -} - -static void mmb(int alt_fn) -{ - __dpmi_regs r = {}; - - r.x.ax = 0x500; - r.x.cx = alt_fn ? 0x0E08 : 0x1c0d; // BkSp or ENTER - __dpmi_int(0x16, &r); -} - -static void mw(int delta) -{ - __dpmi_regs r = {}; - - r.x.ax = 0x500; - if (delta < 0) - r.x.cx = 0x48E0; // UP - else - r.x.cx = 0x50E0; // DOWN - __dpmi_int(0x16, &r); -} - -void do_mouse(void) -{ - __dpmi_regs *r; - unsigned char rows = _farpeekb(_dos_ds, 0x484) + 1; - static unsigned char prev_col, prev_row; - unsigned char col, row; - int dragged; - -#ifndef DJ64 - __djgpp_nearptr_enable(); -#endif - r = (__dpmi_regs *)djaddr2ptr(mregs.address); - do_retf(r); - - col = r->x.cx / 8 + 1; - row = r->x.dx / 8 + 1; - dragged = (r->x.ax & r->x.bx & 1) && (col != prev_col || row != prev_row); - - if ((r->x.ax & 2) || dragged) - mlb(row == rows, col, row); - if (r->x.ax & 8) - mrb(row == rows); - if (r->x.ax & 0x20) - mmb(row == rows); - if (r->x.ax & 0x80) - mw((char)r->h.bh); - - prev_col = r->x.cx / 8 + 1; - prev_row = r->x.dx / 8 + 1; - -#ifndef DJ64 - __djgpp_nearptr_disable(); -#endif -} - -static int mouse_init(void) -{ - __dpmi_regs r = {}; - - __dpmi_int(0x33, &r); - if ((r.x.flags & CF) || r.x.ax != 0xffff || r.x.bx != 3) - { - puts("mouse not detected"); - return 0; - } - /* check the wheel */ - r.x.ax = 0x11; - __dpmi_int(0x33, &r); - if ((r.x.flags & CF) || r.x.ax != 0x574d || (r.x.cx & 1) == 0) - { - puts("mouse wheel not supported"); -// return 0; - } - - __dpmi_allocate_memory(&mregs); -#ifdef DJ64 - mouse_regs = mregs.address - __djgpp_base_address; -#else - mouse_regs = (__dpmi_regs *)djaddr2ptr(mregs.address); -#endif - __dpmi_allocate_real_mode_callback(my_mouse_handler, mouse_regs, &newm); - r.x.ax = 0x14; - r.x.cx = MEV_MASK; - r.x.es = newm.segment; - r.x.dx = newm.offset16; - __dpmi_int(0x33, &r); - oldm.segment = r.x.es; - oldm.offset16 = r.x.dx; - old_mask = r.x.cx; - - mouse_show(); - return 1; -} - -static void mouse_reset(void) -{ - __dpmi_regs r = {}; - - r.x.ax = 0x0c; - r.x.cx = MEV_MASK; - r.x.es = newm.segment; - r.x.dx = newm.offset16; - __dpmi_int(0x33, &r); -} - -static void mouse_done(void) -{ - __dpmi_regs r = {}; - - mouse_hide(); - - r.x.ax = 0x0c; - r.x.cx = old_mask; - r.x.es = oldm.segment; - r.x.dx = oldm.offset16; - __dpmi_int(0x33, &r); - __dpmi_free_real_mode_callback(&newm); - __dpmi_free_memory(mregs.handle); -} - -static void mouse_show(void) -{ - __dpmi_regs r = {}; - - r.x.ax = 1; - __dpmi_int(0x33, &r); -} - -static void mouse_hide(void) -{ - __dpmi_regs r = {}; - - r.x.ax = 2; - __dpmi_int(0x33, &r); -} - int main(int argc, const char *argv[], const char *envp[]) { int a; diff --git a/src/ms.c b/src/ms.c new file mode 100644 index 0000000..725d1a2 --- /dev/null +++ b/src/ms.c @@ -0,0 +1,246 @@ +/* + * comcom64 - 64bit command.com + * Copyright (C) 2023-2024 @stsp + * + * 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 3 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, see . + */ + +#include +#include +#include +#include +#include +#include +#include "asm.h" +#include "ms.h" + +#define CF 1 + +#ifdef DJ64 +static unsigned mouse_regs; +#else +static __dpmi_regs *mouse_regs; +#define djaddr2ptr(a) ((unsigned char *)((a) - __djgpp_base_address)) +#endif +static __dpmi_meminfo mregs = {.size = sizeof(__dpmi_regs) }; + +static __dpmi_raddr newm; +static __dpmi_raddr oldm; +static unsigned old_mask; +#define MEV_MASK 0xab + +static unsigned short popw(__dpmi_regs *r) +{ + unsigned lina = (r->x.ss << 4) + r->x.sp; + unsigned short ret = _farpeekw(_dos_ds, lina); + r->x.sp += 2; + return ret; +} + +static void do_retf(__dpmi_regs *r) +{ + r->x.ip = popw(r); + r->x.cs = popw(r); +} + +static void mvxl(int d) +{ + __dpmi_regs r = { }; + + while (d--) { + r.x.ax = 0x500; + r.x.cx = 0x4BE0; + __dpmi_int(0x16, &r); + } +} + +static void mvxr(int d) +{ + __dpmi_regs r = { }; + + while (d--) { + r.x.ax = 0x500; + r.x.cx = 0x4DE0; + __dpmi_int(0x16, &r); + } +} + +static void mlb(int alt_fn, int x, int y) +{ + __dpmi_regs r = { }; + short c; + + if (alt_fn) { + int cx = wherex(); + if (cx == x) + return; + if (x < cx) + mvxl(cx - x); + else + mvxr(x - cx); + return; + } + + _conio_gettext(x, y, x, y, &c); + + r.x.ax = 0x500; + r.x.cx = c & 0xff; + __dpmi_int(0x16, &r); +} + +static void mrb(int alt_fn) +{ + __dpmi_regs r = { }; + + r.x.ax = 0x500; + r.x.cx = alt_fn ? 0x3 : 0x0F09; // ^C or TAB + __dpmi_int(0x16, &r); +} + +static void mmb(int alt_fn) +{ + __dpmi_regs r = { }; + + r.x.ax = 0x500; + r.x.cx = alt_fn ? 0x0E08 : 0x1c0d; // BkSp or ENTER + __dpmi_int(0x16, &r); +} + +static void mw(int delta) +{ + __dpmi_regs r = { }; + + r.x.ax = 0x500; + if (delta < 0) + r.x.cx = 0x48E0; // UP + else + r.x.cx = 0x50E0; // DOWN + __dpmi_int(0x16, &r); +} + +void do_mouse(void) +{ + __dpmi_regs *r; + unsigned char rows = _farpeekb(_dos_ds, 0x484) + 1; + static unsigned char prev_col, prev_row; + unsigned char col, row; + int dragged; + +#ifndef DJ64 + __djgpp_nearptr_enable(); +#endif + r = (__dpmi_regs *) djaddr2ptr(mregs.address); + do_retf(r); + + col = r->x.cx / 8 + 1; + row = r->x.dx / 8 + 1; + dragged = (r->x.ax & r->x.bx & 1) && (col != prev_col + || row != prev_row); + + if ((r->x.ax & 2) || dragged) + mlb(row == rows, col, row); + if (r->x.ax & 8) + mrb(row == rows); + if (r->x.ax & 0x20) + mmb(row == rows); + if (r->x.ax & 0x80) + mw((char) r->h.bh); + + prev_col = r->x.cx / 8 + 1; + prev_row = r->x.dx / 8 + 1; + +#ifndef DJ64 + __djgpp_nearptr_disable(); +#endif +} + +int mouse_init(void) +{ + __dpmi_regs r = { }; + + __dpmi_int(0x33, &r); + if ((r.x.flags & CF) || r.x.ax != 0xffff || r.x.bx != 3) { + puts("mouse not detected"); + return 0; + } + /* check the wheel */ + r.x.ax = 0x11; + __dpmi_int(0x33, &r); + if ((r.x.flags & CF) || r.x.ax != 0x574d || (r.x.cx & 1) == 0) { + puts("mouse wheel not supported"); +// return 0; + } + + __dpmi_allocate_memory(&mregs); +#ifdef DJ64 + mouse_regs = mregs.address - __djgpp_base_address; +#else + mouse_regs = (__dpmi_regs *) djaddr2ptr(mregs.address); +#endif + __dpmi_allocate_real_mode_callback(my_mouse_handler, mouse_regs, + &newm); + r.x.ax = 0x14; + r.x.cx = MEV_MASK; + r.x.es = newm.segment; + r.x.dx = newm.offset16; + __dpmi_int(0x33, &r); + oldm.segment = r.x.es; + oldm.offset16 = r.x.dx; + old_mask = r.x.cx; + + mouse_show(); + return 1; +} + +void mouse_reset(void) +{ + __dpmi_regs r = { }; + + r.x.ax = 0x0c; + r.x.cx = MEV_MASK; + r.x.es = newm.segment; + r.x.dx = newm.offset16; + __dpmi_int(0x33, &r); +} + +void mouse_done(void) +{ + __dpmi_regs r = { }; + + mouse_hide(); + + r.x.ax = 0x0c; + r.x.cx = old_mask; + r.x.es = oldm.segment; + r.x.dx = oldm.offset16; + __dpmi_int(0x33, &r); + __dpmi_free_real_mode_callback(&newm); + __dpmi_free_memory(mregs.handle); +} + +void mouse_show(void) +{ + __dpmi_regs r = { }; + + r.x.ax = 1; + __dpmi_int(0x33, &r); +} + +void mouse_hide(void) +{ + __dpmi_regs r = { }; + + r.x.ax = 2; + __dpmi_int(0x33, &r); +} diff --git a/src/ms.h b/src/ms.h new file mode 100644 index 0000000..ea62d8f --- /dev/null +++ b/src/ms.h @@ -0,0 +1,10 @@ +#ifndef MS_H +#define MS_H + +int mouse_init(void); +void mouse_reset(void); +void mouse_done(void); +void mouse_show(void); +void mouse_hide(void); + +#endif