Help with direct mouse input

General FreeBASIC programming questions.
Gunslinger
Posts: 35
Joined: Mar 08, 2016 19:10

Help with direct mouse input

Postby Gunslinger » Sep 07, 2019 9:55

Hello i'm trying to read the mouse pointer relative movement.
I fount this piece of code that works. http://users.freebasic-portal.de/freebasicru/directinput.html
And i have a hard time finding something similar for Linux.
I do remember powerbasic in msdos had the function to read this very simple.

I just like to read the mouse without windows mouse acceleration and without border limits
How can i do this in Linux ?


Code: Select all

#INCLUDE "windows.bi"
#INCLIB "dinput8"
#INCLIB "dxguid"
#INCLUDE "win/dinput.bi"


Sub BDIERR(a As HRESULT, s As String, f As BOOL=0)
    If FAILED(a) And f=1 Then
        ? s
        Sleep
        End
    Elseif FAILED(a) Then
        ? s
    Endif
End Sub

Dim As hwnd hwnd
Dim As IDirectInput8 Ptr g_pDI
Dim As IDirectInputDevice8A Ptr DI_Mouse,DI_KeyBoard
Dim MouseState As DIMOUSESTATE2
Dim dipdw(256) As Ubyte

Screen 17
Screencontrol(2,Cint(hwnd))

BDIERR(DirectInput8Create(GetModuleHandle(Null), DIRECTINPUT_VERSION,_
@IID_IDirectInput8, @g_pDI, NULL),"failed DirectInput8Create")

BDIERR(g_pDI->lpVtbl->CreateDevice(g_pDI,@GUID_SysMouse, @DI_Mouse, Null),_
"failed CreateDevice mouse",1)
BDIERR(g_pDI->lpVtbl->CreateDevice(g_pDI,@GUID_SysKeyboard, @DI_KeyBoard, Null),_
"failed CreateDevice keyboard",1)

BDIERR(DI_Mouse->lpVtbl->SetDataFormat(DI_Mouse,@c_dfDIMouse2),_
"failed SetDataFormat mouse",1)
BDIERR(DI_KeyBoard->lpVtbl->SetDataFormat(DI_KeyBoard,@c_dfDIKeyboard),_
"failed SetDataFormat keyboard",1)

BDIERR(DI_Mouse->lpVtbl->SetCooperativeLevel(DI_Mouse,hwnd, DISCL_FOREGROUND+DISCL_NONEXCLUSIVE),_
"failed SetCooperativeLevel mouse",1)
BDIERR(DI_KeyBoard->lpVtbl->SetCooperativeLevel(DI_KeyBoard,hwnd, DISCL_FOREGROUND+DISCL_NONEXCLUSIVE),_
"failed SetCooperativeLevel keyboard",1)

Do
    BDIERR(DI_Mouse->lpVtbl->Acquire(DI_Mouse),"failed Acquire mouse")
    BDIERR(DI_KeyBoard->lpVtbl->Acquire(DI_KeyBoard),"failed Acquire keyboard")
    DI_Mouse->lpVtbl->GetDeviceState(DI_Mouse,Sizeof(DIMOUSESTATE2), @MouseState )
    DI_KeyBoard->lpVtbl->GetDeviceState(DI_KeyBoard,256, @dipdw(0) )
 
    ? MouseState.lx,MouseState.ly,MouseState.rgbButtons(0),_
    MouseState.rgbButtons(1),MouseState.rgbButtons(2)
 
    For I As Integer =0 To 255
        If dipdw(i)<>0 Then
            ? Chr(MapVirtualKeyEx(i, 1, 0))
            If i =1 Then Exit Do
        Endif
    Next   
    Sleep(100)
Loop

DI_Mouse->lpVtbl->Release(DI_Mouse)
DI_KeyBoard->lpVtbl->Release(DI_KeyBoard)
g_pDI->lpVtbl->Release(g_pDI)
Last edited by Gunslinger on Sep 08, 2019 18:07, edited 1 time in total.
badidea
Posts: 1617
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Help with direct mouse input

Postby badidea » Sep 07, 2019 10:52

Something like this might work. I can try to make some FB code later today...

Update: Translating the linked code will be difficult. The low level I/O functions for linux are missing in freebasic(). Don't know how to get the file descriptors.

Update: The low level I/O functions for linux aren't missing, just a bit hard to find. I'll try some more...

I got the code somewhat converted, but it seems useless:
* Need to be root to access the mouse device
* Need to determine which device is the mouse
* Probably still includes acceleration
Code anyway:

Code: Select all

#include "crt.bi"

declare function read_ alias "read" (byval handle as integer, byval buffer as zstring ptr, byval length as uinteger) as integer

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

'#define MOUSEFILE "/dev/input/mice"
'#define MOUSEFILE "/dev/input/mouse0"
'#define MOUSEFILE "/dev/input/event6"

'ls -l /dev/input/by-path/
#define MOUSEFILE "/dev/input/by-path/platform-i8042-serio-1-event-mouse"

type timeval
   tv_sec as __time_t
   tv_usec as __suseconds_t
end type

type input_event
   dim as timeval time_
   dim as ushort type_
   dim as ushort code_
   dim as long value_
end type

dim as integer fd
dim as input_event ie

fd = open_(MOUSEFILE, O_RDONLY)
if fd = -1 then
   perror("opening device")
   end(EXIT_FAILURE)
end if
print fd

while(read_(fd, cptr(zstring ptr, @ie), sizeof(input_event)))
   print ie.time_.tv_sec, ie.time_.tv_usec, ie.type_, ie.code_, ie.value_
wend

Via XCB (X protocol C-language Binding) it seems possible to change acceleration settings: https://xcb.freedesktop.org/manual/group__XCB____API.html#ga1fc426e9ae1c5cb98723031e009579f0
badidea
Posts: 1617
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Help with direct mouse input

Postby badidea » Sep 08, 2019 22:42

This works here with SDL2 (relative motion, no window or screen borders):

Code: Select all

'https://wiki.libsdl.org/CategoryMouse
'http://gigi.nullneuron.net/gigilabs/handling-keyboard-and-mouse-events-in-sdl2/

#include "SDL2/SDL.bi"

dim sdlversion as SDL_version
SDL_GetVersion(@sdlversion)
print "SDL2 version = "; SDL_VERSIONNUM(sdlversion.major, sdlversion.minor, sdlversion.patch)

print SDL_Init(SDL_INIT_VIDEO)

'var win = SDL_CreateWindow("SDL2 mouse test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0)
'var renderer = SDL_CreateRenderer(win, -1, 0)

SDL_SetRelativeMouseMode(SDL_TRUE)
do
   dim event as SDL_Event
   while SDL_PollEvent(@event)
      select case event.type
      case SDL_QUIT_
         exit do
      case SDL_KEYDOWN
         if event.key.keysym.sym = SDLK_ESCAPE then
            exit do
         end if
      case SDL_MOUSEBUTTONDOWN
         print event.button.button
         print "SDL_GetRelativeMouseMode: "; SDL_GetRelativeMouseMode()
      case SDL_MOUSEMOTION
         dim as integer mouseDX = event.motion.xrel
         dim as integer mouseDY = event.motion.yrel
         print mouseDX, mouseDY
      end select
   wend
   sleep 1,1
loop

'SDL_DestroyWindow(win)
SDL_Quit()

The other events (keys, buttons, quit) don't seem to work with the window not created / shown.
BTW: SDL2 implementation in freeBASIC is still buggy.
Gunslinger
Posts: 35
Joined: Mar 08, 2016 19:10

Re: Help with direct mouse input

Postby Gunslinger » Sep 09, 2019 8:59

Yes. Tested this and only works when a window is created.
And it not doing any thing special. Just keeps moving the mouse back to the middel of the window.

I wil just make a easy fix for linux users.
With get/set mouse function.

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest