Author Topic: events and malloc in QB64?  (Read 4970 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
Re: events and malloc in QB64?
« Reply #15 on: March 31, 2021, 08:43:35 am »
@FellippeHeitor Exactly. I'm actually using callback code shared by Petr here: https://www.qb64.org/forum/index.php?topic=3217.msg124966#msg124966
Shuwatch!

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
Re: events and malloc in QB64?
« Reply #16 on: March 31, 2021, 08:44:01 am »
@madscijr Just so you know that I didn't forget you, here is proof that I'm looking into this:
Note: This isn't to say I've gotten anything to work yet. I'm still just trying to recreate some functions and declarations.

Great - much appreciated!

PS Sorry I wasn't able to join on the discord server (what is a discord server?), I'm often pretty busy after hours on weekdays, this QB stuff is mainly a lunchtime activity for me!
« Last Edit: March 31, 2021, 08:54:19 am by madscijr »

FellippeHeitor

  • Guest
Re: events and malloc in QB64?
« Reply #17 on: March 31, 2021, 08:52:22 am »
Discord is a fancy chatroom. Registration required. http://discord.qb64.org

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
Re: events and malloc in QB64?
« Reply #18 on: March 31, 2021, 08:54:39 am »
Discord is a fancy chatroom. Registration required. http://discord.qb64.org
Cool, thanks for cluing me in.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
Re: events and malloc in QB64?
« Reply #19 on: March 31, 2021, 08:58:23 am »
PPS Down the road (assuming this ever ends up working) it would be cool to also get this working for Linux / macOS.

I'm not sure if there are commands in QB64 code that can auto-detect the OS, and conditionally use Windows / Linux / Mac versions of a library, or if we would just need to have separate versions of the library for each OS.
But the ManyMouse library seems to be cross-platform, with separate versions of code for
  • Windows (windows_wminput.c)
  • Linux (linux_evdev.c)
  • macOS (macosx_hidmanager.c, macosx_hidutilities.c)
and common files for all platforms
  • manymouse.c
  • manymouse.h

I just thought I would mention this to gauge how people approach making a cross-platform library in QB64...

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
Re: events and malloc in QB64?
« Reply #20 on: March 31, 2021, 09:04:47 am »
I'm not sure if there are commands in QB64 code that can auto-detect the OS, and conditionally use Windows / Linux / Mac versions of a library, or if we would just need to have separate versions of the library for each OS.

There are! Check out this Wiki page: https://www.qb64.org/wiki/$IF
Shuwatch!

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
Re: events and malloc in QB64?
« Reply #21 on: March 31, 2021, 09:28:57 am »
There are! Check out this Wiki page: https://www.qb64.org/wiki/$IF

Aha, yes, that's the stuff!
Thanks!
(Now we would just need someone who can understand Mac "C" code and Linux "C" code to help convert the respective modules! I'm not going to worry about this until later...)

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
Re: events and malloc in QB64?
« Reply #22 on: March 31, 2021, 09:34:07 am »
(Now we would just need someone who can understand Mac "C" code and Linux "C" code to help convert the respective modules! I'm not going to worry about this until later...)

Well, I have written code that is multiplatform before which required me to convert C code for Linux and Mac to QB64. Not too much of a challenge. I'll give that a go if I can get some functionality in Windows first.
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
Re: events and malloc in QB64?
« Reply #23 on: April 01, 2021, 09:05:54 am »
@madscijr Well, it turns out that this software you want to recreate or use in QB64 requires SDL. Our older versions of QB64 use SDL. I'm trying now to find a way to do what you want without SDL. May prove difficult
Shuwatch!

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
Re: events and malloc in QB64?
« Reply #24 on: April 01, 2021, 10:04:13 am »
@madscijr Well, it turns out that this software you want to recreate or use in QB64 requires SDL. Our older versions of QB64 use SDL. I'm trying now to find a way to do what you want without SDL. May prove difficult

No kidding... So THAT's how they're doing it cross-platform?
I saw stuff in there about the Raw Input API, though.
I think that's a standard Microsoft API included in all Windows versions from XP on.
With that, we shouldn't need SDL for the Windows version, right?

PS Here are the MS RawInput API docs:
https://docs.microsoft.com/en-us/windows/win32/inputdev/raw-input?redirectedfrom=MSDN

PPS and here is some sample code to use the RawInput API (yet more C code I can flounder around with and try to convert to QB64! yay!):
https://jstookey.com/multiple-mice-raw-input/
« Last Edit: April 01, 2021, 10:13:13 am by madscijr »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • GitHub
Re: events and malloc in QB64?
« Reply #25 on: April 01, 2021, 11:29:25 am »
@madscijr Exactly. I've been looking at some raw input examples online.

https://asawicki.info/news_1533_handling_multiple_mice_with_raw_input
Shuwatch!

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
Re: events and malloc in QB64?
« Reply #26 on: April 01, 2021, 11:48:44 pm »
@madscijr Exactly. I've been looking at some raw input examples online.
https://asawicki.info/news_1533_handling_multiple_mice_with_raw_input

From my end, I think starting off simple is probably the best way to get off the ground, so I started off by figuring out the minimal example from
https://jstookey.com/multiple-mice-raw-input/

Hopefully tomorrow I'll get something working...

Code: C: [Select]
  1. /**********************************************************************************************
  2. FROM: https://jstookey.com/arcade/rawmouse/HelloRawInput.c
  3.  
  4. testmouse.c: A demonstration of how to use Rawinput to access multiple mice in Windows XP.
  5.  
  6. ------------------  COMPILING WITH GCC ------------------------------
  7.  
  8. This program was compiled using MinGW/GCC
  9.  
  10. In order to recreate the compile environment:
  11.  
  12.   1) Install the "Win32 compile utilities" (MinGW/GCC) for Mame from http://www.mame.net/downmain.html
  13.   2) From the command line, run "gcc HelloRawInput.c"
  14.  
  15. -------------------  COMPILING WITH MS VISUAL C++ --------------------
  16.  
  17. This example can also be compiled with Microsoft Visual C++ version 6, but first you need to download
  18.         and install the Microsoft Platform SDK.  In order to access the Microsoft Platform SDK from VStudio,
  19.         I had to go to tools->options->directories and add the SDK's bin, include, lib, and src folders.
  20.         In the same window, I had drag each of the SDK's folders to the top of the respective list
  21.         to give those folders priority over the others.
  22.  
  23.         To create your project for Visual Studio (after you prepare VStudio to work with the MS
  24.              Platform SDK), do the following:
  25.  
  26.         1) Open Visual C++ and start a new Win32 Application as a "blank project"
  27.            (This is necessary because if you simply open the .c file in VStudio
  28.            VStudio will assume it's a "console application" and it won't compile properly.)
  29.         2) Copy the .c file into the new project's root folder
  30.         3) In the left pane, click "File View" and left click "Source Files" and select
  31.              "add files to folder" from the popup menu, and add your file.
  32.         4) You're all set!  Press f5 to build the program.
  33.  
  34. **********************************************************************************************/
  35. #define _WIN32_WINNT 0x0501   // Identify this as a Windows XP application.  This is necessary
  36.                                                           //    so we can access rawinput
  37. #include <stdio.h>
  38. #include <conio.h>                      //  Provides getch()
  39. #include <windows.h>            //  Provides rawinput
  40.  
  41. main()
  42. {
  43.         UINT nInputDevices;
  44.  
  45.         // 1st call to GetRawInputDeviceList: Pass NULL to get the size of the list.
  46.         if (GetRawInputDeviceList(NULL, &nInputDevices, sizeof(RAWINPUTDEVICELIST)) != 0) return 0;
  47.         printf("Number of raw input devices: %i\n\n", nInputDevices);
  48.  
  49.         printf("Press any key...");
  50.         getch();
  51.  
  52.         return 1;
  53. }
  54.  
  55.  
  56. /*
  57.  
  58. -------------------------------------------------------------------------------
  59. What is GetRawInputDeviceList?
  60.         UINT GetRawInputDeviceList(
  61.           PRAWINPUTDEVICELIST pRawInputDeviceList,
  62.           PUINT               puiNumDevices,
  63.           UINT                cbSize
  64.         );
  65.  
  66. -------------------------------------------------------------------------------
  67. What is PRAWINPUTDEVICELIST?
  68.         PRAWINPUTDEVICELIST:
  69.         typedef struct tagRAWINPUTDEVICELIST {
  70.           HANDLE hDevice;
  71.           DWORD  dwType;
  72.         } RAWINPUTDEVICELIST, *PRAWINPUTDEVICELIST;
  73.  
  74. -------------------------------------------------------------------------------
  75. What is getch?
  76.         getch() is a nonstandard function and is present in conio.h header file
  77.         which is mostly used by MS-DOS compilers like Turbo C. It is not part of
  78.         the C standard library or ISO C, nor is it defined by POSIX.
  79.         Like these functions, getch() also reads a single character from the
  80.         keyboard. But it does not use any buffer, so the entered character is
  81.         immediately returned without waiting for the enter key.
  82.         Syntax: int getch(void);
  83.         Parameters: This method does not accept any parameters.
  84.         Return value: This method returns the ASCII value of the key pressed.
  85.        
  86. */
  87.  

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
Re: events and malloc in QB64?
« Reply #27 on: April 02, 2021, 05:09:50 pm »
From my end, I think starting off simple is probably the best way to get off the ground, so I started off by figuring out the minimal example from
https://jstookey.com/multiple-mice-raw-input/
Hopefully tomorrow I'll get something working...

I tried to convert the C code to QB64, using the QB64 equivalents of the C types that SpriggsySpriggs shared, and googling.
I'm currently stuck on line 55 of the below code, when I run the QB64 IDE says "Expected &H... or &O...".
When I try changing & to &H or &O, the QB64 IDE says a ")" is missing. No idea how to get this working!
Any help appreciated...

Code: QB64: [Select]
  1. ' ****************************************************************************************************************************************************************
  2. ' testmouse.c: A demonstration of how to use Rawinput to access multiple mice in Windows XP.
  3. ' https://jstookey.com/arcade/rawmouse/HelloRawInput.c
  4. ' ****************************************************************************************************************************************************************
  5.  
  6. ' #define _WIN32_WINNT 0x0501   // Identify this as a Windows XP application.
  7. '                               // This is necessary so we can access rawinput
  8. ' #include <stdio.h>            
  9. ' #include <conio.h>            //  Provides getch()
  10. ' #include <windows.h>          //  Provides rawinput
  11.  
  12. ' =============================================================================
  13. ' GLOBAL DECLARATIONS a$=string, i%=integer, L&=long, s!=single, d#=double
  14.  
  15. ' -----------------------------------------------------------------------------
  16. ' BOOLEAN CONSTANTS
  17. CONST FALSE = 0
  18. CONST TRUE = NOT FALSE
  19.  
  20. ' -----------------------------------------------------------------------------
  21. ' USER DEFINED TYPES
  22.  
  23. ' What is PRAWINPUTDEVICELIST?
  24. '     PRAWINPUTDEVICELIST:
  25. '     typedef struct tagRAWINPUTDEVICELIST {
  26. '       HANDLE hDevice;
  27. '       DWORD  dwType;
  28. '     } RAWINPUTDEVICELIST, *PRAWINPUTDEVICELIST;
  29.  
  30. TYPE RAWINPUTDEVICELIST
  31.     hDevice AS _OFFSET ' a handle will always be _OFFSET
  32.     dwType AS LONG ' DWORD corresponds to LONG
  33.  
  34. ' -----------------------------------------------------------------------------
  35. ' API FUNCTIONS
  36.  
  37. ' What is GetRawInputDeviceList?
  38. '     UINT GetRawInputDeviceList(
  39. '       PRAWINPUTDEVICELIST pRawInputDeviceList,
  40. '       PUINT               puiNumDevices,        <---- what is a aPUINT?
  41. '       UINT                cbSize
  42. '     );
  43.  
  44. ' what is a aPUINT? According to
  45. '     WIN32 API Data Types
  46. '     https://www.csie.ntu.edu.tw/~r92094/c++/w32api_data_type.html
  47. ' PUINT Pointer to a UINT.
  48. ' Q: What QB64 type is equivalent to a pointer?
  49. ' A: is it _OFFSET ?
  50.  
  51.     ' COMPILER ERROR HERE: Expected &H... or &O...
  52.     ' THEN WHEN I TRY CHANGING & TO &H OR &O, COMPILER SAYS MISSING )
  53.     FUNCTION GetRawInputDeviceList (pRawInputDeviceList AS RAWINPUTDEVICELIST, puiNumDevices AS _OFFSET, cbSize AS _UNSIGNED LONG)&
  54.  
  55. ' =============================================================================
  56. ' GLOBAL VARIABLES
  57. DIM ProgramPath$
  58. DIM ProgramName$
  59.  
  60. ' =============================================================================
  61. ' INITIALIZE
  62. ProgramName$ = MID$(COMMAND$(0), _INSTRREV(COMMAND$(0), "\") + 1)
  63. ProgramPath$ = LEFT$(COMMAND$(0), _INSTRREV(COMMAND$(0), "\"))
  64.  
  65. ' =============================================================================
  66. ' START
  67. main ProgramName$
  68.  
  69. ' =============================================================================
  70. ' FINISH
  71. SYSTEM ' return control to the operating system
  72. PRINT ProgramName$ + " finished."
  73.  
  74. ' /////////////////////////////////////////////////////////////////////////////
  75.  
  76. SUB main
  77.         PRINT "MinimalRawInputTest% RETURNS: " + cstr$( MinimalRawInputTest%() )
  78. END SUB ' main
  79.  
  80. FUNCTION MinimalRawInputTest%
  81.     DIM iResult%
  82.         'UINT nInputDevices;
  83.         DIM nInputDevices AS _UNSIGNED LONG
  84.         DIM MEM AS _MEM
  85.        
  86.     ' 1st call to GetRawInputDeviceList: Pass NULL to get the size of the list.
  87.     'if (GetRawInputDeviceList(NULL, &nInputDevices, sizeof(RAWINPUTDEVICELIST)) != 0) return 0;
  88.        
  89.         ' **********
  90.         ' UNKNOWNS:
  91.         ' Q1: how do you pass NULL in QB64?
  92.         ' A1: ?
  93.        
  94.         ' Q2: what is &nInputDevices vs nInputDevices and what is the QB64 equivalent?
  95.         ' A2: is it the pointer to the variable? in QB64, The _OFFSET function returns the memory offset of/within a given variable.
  96.        
  97.         ' Q3: what is the QB64 equivalent of sizeof?
  98.         ' A3: maybe _MEM and _MEM.SIZE ?
  99.        
  100.         MEM = _MEM(RAWINPUTDEVICELIST)
  101.        
  102.         'IF (GetRawInputDeviceList(NULL, _OFFSET(nInputDevices), sizeof(RAWINPUTDEVICELIST)) <> 0) THEN
  103.         IF (GetRawInputDeviceList(NULL, _OFFSET(nInputDevices), MEM.SIZE) <> 0) THEN
  104.                 iResult% = 0
  105.     ELSE
  106.                 PRINT "Number of raw input devices: " + cstr$(nInputDevices)
  107.                 PRINT "Press any key..."
  108.                 SLEEP ' SLEEP without an argument waits until a keypress.)
  109.                 iResult% = 1
  110.         END IF
  111.    
  112.         ' RETURN RESULT (ONE EXIT POINT!)
  113.         MinimalRawInputTest% = iResult%
  114. END FUNCTION ' MinimalRawInputTest%
  115.  
  116. ' /////////////////////////////////////////////////////////////////////////////
  117. ' Equivalent to vbscript / VBA / VB6 cstr
  118.  
  119. FUNCTION cstr$ (myValue)
  120.     'cstr$ = LTRIM$(RTRIM$(STR$(myValue)))
  121.     cstr$ = _TRIM$(STR$(myValue))
  122. END FUNCTION ' cstr$
  123.  
  124. ' /////////////////////////////////////////////////////////////////////////////
  125. ' Equivalent to getch
  126.  
  127. ' -----------------------------------------------------------------------------
  128. ' What is getch?
  129. '     getch() is a nonstandard function and is present in conio.h header file
  130. '     which is mostly used by MS-DOS compilers like Turbo C. It is not part of
  131. '     the C standard library or ISO C, nor is it defined by POSIX.
  132. '     Like these functions, getch() also reads a single character from the
  133. '     keyboard. But it does not use any buffer, so the entered character is
  134. '     immediately returned without waiting for the enter key.
  135. '     Syntax: int getch(void);
  136. '     Parameters: This method does not accept any parameters.
  137. '     Return value: This method returns the ASCII value of the key pressed.
  138.  
  139. ' -----------------------------------------------------------------------------
  140. ' QB64 alternate to getch
  141. '
  142. ' SLEEP ' SLEEP without an argument waits until a keypress.)
  143. '
  144. ' or
  145. '
  146. ' DO
  147. '       x = _KEYHIT
  148. '       IF x THEN
  149. '               EXIT DO
  150. '       END IF
  151. ' LOOP
  152.  
« Last Edit: April 02, 2021, 05:12:08 pm by madscijr »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: events and malloc in QB64?
« Reply #28 on: April 02, 2021, 06:35:14 pm »
    FUNCTION GetRawInputDeviceList& (pRawInputDeviceList AS RAWINPUTDEVICELIST, puiNumDevices AS _OFFSET, cbSize AS _UNSIGNED LONG)


& *after* the function name.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
Re: events and malloc in QB64?
« Reply #29 on: April 03, 2021, 11:13:42 am »
    FUNCTION GetRawInputDeviceList& (pRawInputDeviceList AS RAWINPUTDEVICELIST, puiNumDevices AS _OFFSET, cbSize AS _UNSIGNED LONG)


& *after* the function name.

Aha... DUH! LoL
Thank you.

Any idea how you would send a NULL value as one of the parameters?