News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Handling mouse

Started by zak100, December 31, 2009, 05:30:56 PM

Previous topic - Next topic

FORTRANS

Hi,

   For my video cards, VESA is supported by the card's
BIOS.  Drivers implement more video modes or newer
versions of VESA.

   For writing a mouse driver, it should be straight
forward, after you figure out how you are going to
support TSR's, separate programs, and memory
management.  As sinsi said, using the serial port is
fairly easy.

Regards,

Steve N.

zak100

Hi,
Thanks for the reply. Actually I dont have the serial mouse and I would prefer ps/1 (not clear) mouse. Memory management is a tough job and people do Phd in this topic. Kindly specify some ideas about memory management in order to load my exe files and TSRs.

Zulfi.

FORTRANS

Hi,

   Well, for memory management, you cold read up on how
MS-DOS does it.  Here is a simplified version of it.  After the
first part of DOS is loaded and takes control, memory is allocated
by DOS (Fn 48H) through the use of Memory Control Blocks
(MCB).  As 16 bytes is the increment allowed by the segment
registers in real mode, that is the "page" size used.  At the start
of the memory allocated 16 bytes are used to hold the MCB,
and then the allocated memory follows.

   The MCB has an identification flag, owner identification,
the number of pages allocated (size of memory block), and an
optional name.  The size is used to find the next  MCB and
forms a linked list of MCB's.


; Microsoft documents the MCB as the ARENA structure.
; This is a mix of MS and "Undocumented DOS"

ARENA   STRUC
        arenaSignature  DB      ?       ; 4DH valid item in chain, 5AH last item.
        arenaOwner      DW      ?       ; Owner, PSP segment of owner.
        arenaSize       DW      ?       ; Size in paragraphs.
        arenaReserved   DB      3 DUP(?)
        arenaName       DB      8 DUP(?)
ARENA   ENDS


   In this way MS-DOS can keep track of how memory is used
both by itself and user programs.  Use "MEM /D | MORE" to
see how this is used.

Regards,

Steve N.

zak100

Thanks for what you have said. I would get back with you people as soon as I get enough information to write a simple prog.

Zulfi.

MichaelW

In my test above of the Interrupt 15h C2h functions, I was apparently testing under Windows 2000. I have not tried booting that system under MS-DOS, but I did an old Micron Millennia Xku with a Phoenix 4.0 BIOS (1998) and a PS/2 mouse that apparently does support these functions (under MS-DOS, but under Windows 2000 I get the same not-implemented error as the first system).

The attachment contains my test app. Running on that system, the app returns:
Reset : ERROR_SUCCESS
GetDeviceID : ERROR_SUCCESS
0
Disable : ERROR_INTERFACE
SetHandler : ERROR_SUCCESS
Press any key to continue . . .
Enable : ERROR_INTERFACE
ReturnStatus: ERROR_SUCCESS
statusByte : 20h
resolution :  2
sampleRate : 100

After trying many different arrangements of the calls, and ensuring that the BIOS is detecting the mouse, and eliminating every other problem that I can see or even suspect, I still get the ERROR_INTERFACE return. And while I can run the program repeatedly, if after running the program I move the mouse or press one of its buttons, the system locks up. So far I have not found any way around the problem, in the Interrupt List, in Frank van Gilluwe's The Undocumented PC, or online.
eschew obfuscation

dedndave

hi Michael
under XP MCE2005, i got a bunch of NOT_IMPLEMENTED's and a few 0's   :P
that may have gotten left in the wake of newer OS's supporting the "we drive damn near anything" approach

FORTRANS

Hi,

   Rebooted, in a full screen VDM I got the following.  But
back in the WPS the mouse had left.  the machine has an
Award BIOS.  Good enough, or do you want another
machine or real DOS?

Regards,

Steve N.


F:\TEMP>test
Reset : ERROR_SUCCESS
GetDeviceID : ERROR_SUCCESS
0
Disable : ERROR_INTERFACE
SetHandler : ERROR_SUCCESS
Press any key to continue...
Enable : ERROR_SUCCESS
ReturnStatus : ERROR_SUCCESS
statusByte : 20h
resolution : 2
sampleRate : 100

F:\TEMP>test
Reset : ERROR_SUCCESS
GetDeviceID : ERROR_SUCCESS
0
Disable : ERROR_SUCCESS
SetHandler : ERROR_SUCCESS
Press any key to continue...
Enable : ERROR_SUCCESS
ReturnStatus : ERROR_SUCCESS
statusByte : 20h
resolution : 2
sampleRate : 100

F:\TEMP>

MichaelW

Steve and Dave, thanks for testing. What I need is to find the key information that appears to be missing in the available sources. Initially, the BIOS setup was expecting a PnP OS (left over from an old Windows 98 installation), and I had to clear this setting before the BIOS would detect the mouse. I was hoping that this would be a relatively painless route to a mouse driver of sorts.
eschew obfuscation

sinsi

Booting into DOS5 with a ps2 mouse connected:
Reset : ERROR_SUCCESS
GetDeviceID : ERROR_SUCCESS
0
Disable : ERROR_NO_HANDLER
SetHandler : ERROR_SUCCESS
Press any key to continue . . .
Enable : ERROR_SUCCESS
ReturnStatus: ERROR_SUCCESS
statusByte : 20h
resolution :  0
sampleRate : 100

No mouse connected:
Reset : ERROR_SUCCESS
GetDeviceID : ERROR_SUCCESS
1
Disable : ERROR_SUCCESS
SetHandler : ERROR_SUCCESS
Press any key to continue . . .
Enable : ERROR_SUCCESS
ReturnStatus: ERROR_SUCCESS
statusByte : E0h
resolution :  -1
sampleRate : 93
Light travels faster than sound, that's why some people seem bright until you hear them.

dedndave

you guys are gonna make me fire up all my old computers, one of these days - lol

zak100

Hi,
I got following at my work place:

D:\masm prog\masm help\PS2Mouse>test
Reset : ERROR_NOT_IMPLEMENTED
GetDeviceID : ERROR_NOT_IMPLEMENTED
0
Disable : ERROR_NOT_IMPLEMENTED
SetHandler : ERROR_NOT_IMPLEMENTED
Press any key to continue...
Enable : ERROR_NOT_IMPLEMENTED
ReturnStatus : ERROR_NOT_IMPLEMENTED
statusByte : 00h
resolution : 0
sampleRate : 0

D:\MASMPR~1\MASMHE~1\PS2Mouse>ver

Microsoft Windows XP [Version 5.1.2600]

D:\MASMPR~1\MASMHE~1\PS2Mouse>

Zulfi.

FORTRANS

Hi,

   Well, I duplicated Michael's program (more or less) and
got the handler routine to print out the mouse events.  Not
too sure what that proves though.  At least one thing van
Gilluwe says is off in a trivial way, he says one of the bits is
set to one, and it's not on this system.  I'll see if I can get
it to do something useful later on.  Or at least verify that
things are progressing properly.  About half of my ideas
so far seem to just be interesting ways to have the program
croak.

Cheers,

Steve N.

MichaelW

I did a little more work on this before I had to set it aside.

;=========================================================================
.model small, c
.386
include support.asm
.stack
;=========================================================================

ERROR_SUCCESS           equ 0
ERROR_INVALID_FUNCTION  equ 1
ERROR_INVALID_INPUT     equ 2
ERROR_INTERFACE         equ 3
ERROR_RESEND_COMMAND    equ 4
ERROR_NO_HANDLER        equ 5
ERROR_NOT_IMPLEMENTED1  equ 80h
ERROR_NOT_IMPLEMENTED2  equ 86h

;--------------------------------------------------------------------
; Sample rate is 10, 20, 40, 60, 80, 100, or 200 samples per second.
;--------------------------------------------------------------------

SAMPLE_RATE_10          equ 0
SAMPLE_RATE_20          equ 1
SAMPLE_RATE_40          equ 2
SAMPLE_RATE_60          equ 3
SAMPLE_RATE_80          equ 4
SAMPLE_RATE_100         equ 5
SAMPLE_RATE_200         equ 6

;-----------------------------------------
; Resolution 1, 2, 4, or 8 counts per mm.
;-----------------------------------------

RESOLUTION_1            equ 0
RESOLUTION_2            equ 1
RESOLUTION_3            equ 2
RESOLUTION_8            equ 3

;---------------------------------------------------------------------
; Structure to hold status values returned by ReturnStatus procedure.
;---------------------------------------------------------------------

_STATUS STRUCT
  statusByte db ?
  resolution db ?
  sampleRate db ?
_STATUS ENDS

;=========================================================================
.data
    status    _STATUS <>
    deviceID  db      0
.code
;=========================================================================
ShowErrorStatus proc npCaption:WORD, wStatus:WORD
    .if wStatus == ERROR_SUCCESS
        print npCaption,"ERROR_SUCCESS",NL
    .elseif wStatus == ERROR_INVALID_FUNCTION
        print npCaption,"ERROR_INVALID_FUNCTION",NL
    .elseif wStatus == ERROR_INVALID_INPUT
        print npCaption,"ERROR_INVALID_INPUT",NL
    .elseif wStatus == ERROR_INTERFACE
        print npCaption,"ERROR_INTERFACE",NL
    .elseif wStatus == ERROR_RESEND_COMMAND
        print npCaption,"ERROR_RESEND_COMMAND",NL
    .elseif wStatus == ERROR_NO_HANDLER
        print npCaption,"ERROR_NO_HANDLER",NL
    .elseif wStatus == ERROR_NOT_IMPLEMENTED1 || ERROR_NOT_IMPLEMENTED2
        print npCaption,"ERROR_NOT_IMPLEMENTED",NL
    .endif
    ret
ShowErrorStatus endp
;=========================================================================
DisableMouse proc uses bx
    mov ax, 0c200h
    xor bh, bh
    int 15h
    movzx ax, ah
    jc  @f
    xor ax, ax
  @@:
    ret
DisableMouse endp
;=========================================================================
EnableMouse proc uses bx
    mov ax, 0c200h
    mov bh, 1
    int 15h
    movzx ax, ah
    jc  @f
    xor ax, ax
  @@:
    ret
EnableMouse endp
;=========================================================================
ResetMouse proc
    mov ax, 0c201h
    int 15h
    movzx ax, ah
    jc  @f
    xor ax, ax
  @@:
    ret
ResetMouse endp
;=========================================================================
SetSampleRate proc uses bx sampleRate:WORD
    mov ax, 0c202h
    mov bx, sampleRate
    mov bh, bl
    int 15h
    movzx ax, ah
    jc  @f
    xor ax, ax
  @@:
    ret
SetSampleRate endp
;=========================================================================
SetResolution proc uses bx resolution:WORD
    mov ax, 0c203h
    mov bx, resolution
    mov bh, bl
    int 15h
    movzx ax, ah
    jc  @f
    xor ax, ax
  @@:
    ret
SetResolution endp
;=========================================================================
GetDeviceID proc uses bx di npID:WORD
    mov ax, 0c204h
    int 15h
    movzx ax, ah
    jc  @f
    mov di, npID
    mov [di], bh
    xor ax, ax
  @@:
    ret
GetDeviceID endp
;=========================================================================
InitializeMouse proc uses bx dataPackageSize:WORD
    mov ax, 0c205h
    mov bx, dataPackageSize
    mov bh, bl
    int 15h
    movzx ax, ah
    jc  @f
    xor ax, ax
  @@:
    ret
InitializeMouse endp
;=========================================================================
ReturnStatus proc uses di bx cx dx npStatus:WORD
    mov ax, 0c206h
    xor bh, bh
    int 15h
    movzx ax, ah
    jc  @f
    mov di, npStatus
    mov [di]._STATUS.statusByte, bl
    mov [di]._STATUS.resolution, cl
    mov [di]._STATUS.sampleRate, dl
    xor ax, ax
  @@:
    ret
ReturnStatus endp
;=========================================================================
SetHandler proc uses es bx handlerSeg:WORD, handlerOffset:WORD
    mov ax, 0c207h
    mov es, handlerSeg
    mov bx, handlerOffset
    int 15h
    movzx ax, ah
    jc  @f
    xor ax, ax
  @@:
    ret
SetHandler endp
;=========================================================================
;-----------------------------------------------------------------------
; The current handler partially processes the X and Y deltas and stores
; the result in two word vars in the code segement. Note that I am
; depending on MASM to take care of the segment overrides.
;
; WORD 1: status (see #00525)
; WORD 2: X data (high byte = 00h)
; WORD 3: Y data (high byte = 00h)
; 15-8 reserved (0)
;  7   Y data overflowed
;  6   X data overflowed
;  5   Y data is negative
;  4   X data is negative
;  3   reserved (1)
;  2   reserved (0)
;  1   right button pressed
;  0   left button pressed
;
; I had to explicitly set the packet size to 3 bytes.
;
; I decided to try a 640x200 virtual screen, so the cursor
; positions should be limited to 0-639, 0-199.
;
; I left the resolution at the default 4 counts per mm.
;
; And after a few days away from it, I see some obvious errors in the
; handler code.
;-----------------------------------------------------------------------

_X dw 0
_Y dw 0

Handler proc far uses ax bx cx dx w3:WORD,w2:WORD,w1:WORD,w0:WORD
    mov dx, w2      ; dY low 8 bits
    neg dx
    mov cx, w1      ; dX low 8 bits
    mov ax, w0
    shl ax, 3       ; shift Y sign into bit 8
    mov dh, ah      ; copy to dY
    xor ah, ah
    shl ax, 1       ; shift X sign into bit 8
    mov ch, ah      ; copy to dX
    add _X, cx
    test _X, 8000h
    jz  @f
    mov _X, 0
  @@:
    add _Y, dx
    test _Y, 8000h
    jz  @f
    mov _Y, 0
  @@:
    ret
Handler endp

;=========================================================================
.startup
;=========================================================================

    ;-------------------------------------------------------
    ; This initialization sequence works on my test system.
    ;-------------------------------------------------------
   
    invoke DisableMouse
    invoke ShowErrorStatus, chr$("Disable : "), ax
    invoke InitializeMouse, 3
    invoke ShowErrorStatus, chr$("Initialize : "), ax
    invoke SetHandler, seg handler, offset handler
    invoke ShowErrorStatus, chr$("SetHandler : "), ax
    invoke EnableMouse
    invoke ShowErrorStatus, chr$("Enable : "), ax
    call WaitKey
    cls
  @@:
    loc 0,0
    mov ax, _X
    xor dx, dx
    mov cx, 640
    div cx
    print word$(dx),chr$(9)
    mov ax, _Y
    xor dx, dx
    mov cx, 200
    div cx
    print word$(dx)
    jmp @b

    call WaitKey
;=========================================================================
.exit
end

eschew obfuscation

zak100

Thanks for this great work.

Zulfi.

FORTRANS

Hi,

   On a system with a three button mouse, the third
button seems to cause a mouse event with spurious
movement indicated.  Or maybe just spurious data.
There does not seem to be any real indication of a
button press.

   Normal movement and the other two buttons work
as expected.  I'll move the mouse around and see if
the other system responds in the same way.

Regards,

Steve