Unit Mouse
Return to Unit Reference
Description
Ultibo Mouse Interface unit
This unit provides both the Mouse device interface and the generic USB HID mouse driver.
Constants
 MOUSE_* | MOUSE_NAME_PREFIX = 'Mouse'; | Name prefix for Mouse Devices | 
 MOUSE_TYPE_* | MOUSE_TYPE_NONE = 0; | |
| MOUSE_TYPE_USB = 1; | |
| MOUSE_TYPE_PS2 = 2; | |
| MOUSE_TYPE_SERIAL = 3; | |
| MOUSE_TYPE_MAX = 3; | |
 MOUSE_STATE_* | MOUSE_STATE_DETACHED = 0; | |
| MOUSE_STATE_DETACHING = 1; | |
| MOUSE_STATE_ATTACHING = 2; | |
| MOUSE_STATE_ATTACHED = 3; | |
| MOUSE_STATE_MAX = 3; | |
 
 MOUSE_FLAG_* | MOUSE_FLAG_NONE = $00000000; | |
| MOUSE_FLAG_NON_BLOCK = $00000001; | If set device reads are non blocking (Also supported in Flags parameter of MouseReadEx) | 
| MOUSE_FLAG_DIRECT_READ = $00000002; | If set device writes mouse data to its local buffer and which must be read using MouseDeviceRead | 
| MOUSE_FLAG_SWAP_BUTTONS = $00000004; | If set left and right mouse buttons will be swapped in mouse data | 
| MOUSE_FLAG_PEEK_BUFFER = $00000008; | Peek at the buffer to see if any data is available, don't remove it. (Used only in Flags parameter of MouseReadEx) | 
| Flags supported by MOUSE_CONTROL_GET/SET/CLEAR_FLAG | |
| MOUSE_FLAG_MASK = MOUSE_FLAG_NON_BLOCK or MOUSE_FLAG_DIRECT_READ or MOUSE_FLAG_SWAP_BUTTONS; | |
 
 MOUSE_CONTROL_* | MOUSE_CONTROL_GET_FLAG = 1; | Get Flag | 
| MOUSE_CONTROL_SET_FLAG = 2; | Set Flag | 
| MOUSE_CONTROL_CLEAR_FLAG = 3; | Clear Flag | 
| MOUSE_CONTROL_FLUSH_BUFFER = 4; | Flush Buffer | 
| MOUSE_CONTROL_GET_SAMPLE_RATE = 5; | Get Sample Rate | 
| MOUSE_CONTROL_SET_SAMPLE_RATE = 6; | Set Sample Rate | 
| MOUSE_CONTROL_GET_MAX_X = 7; | Get Maximum X value (Only applies to Absolute X values) | 
| MOUSE_CONTROL_GET_MAX_Y = 8; | Get Maximum Y value (Only applies to Absolute Y values) | 
| MOUSE_CONTROL_GET_MAX_WHEEL = 9; | Get Maximum Wheel value (Only applies to Absolute Wheel values) | 
| MOUSE_CONTROL_GET_MAX_BUTTONS = 10; | Get Maximum Buttons mask (eg MOUSE_LEFT_BUTTON or MOUSE_RIGHT_BUTTON etc) | 
| MOUSE_CONTROL_GET_ROTATION = 11; | Get Rotation value (0, 90, 180, 270) (Only where supported by the driver) | 
| MOUSE_CONTROL_SET_ROTATION = 12; | Set Rotation value (0, 90, 180, 270) (Only where supported by the driver) | 
 
 MOUSE_BUFFER_* | MOUSE_BUFFER_SIZE = 512; | 
 MOUSE_SAMPLE_* | MOUSE_SAMPLE_RATE = 100; | 100 samples/sec | 
 MOUSE_LEFT_*, MOUSE_RIGHT_* | Values for TMouseData.Buttons | |
| MOUSE_LEFT_BUTTON = $0001; | The Left mouse button is pressed | 
| MOUSE_RIGHT_BUTTON = $0002; | The Right mouse button is pressed | 
| MOUSE_MIDDLE_BUTTON = $0004; | The Middle mouse button is pressed | 
| MOUSE_SIDE_BUTTON = $0008; | The Side mouse button is pressed | 
| MOUSE_EXTRA_BUTTON = $0010; | The Extra mouse button is pressed | 
| MOUSE_TOUCH_BUTTON = $0020; | The Touch screen is being touched | 
| MOUSE_ABSOLUTE_X = $0040; | The OffsetX value is absolute not relative | 
| MOUSE_ABSOLUTE_Y = $0080; | The OffsetY value is absolute not relative | 
| MOUSE_ABSOLUTE_WHEEL = $0100; | The OffsetWheel value is absolute not relative | 
 MOUSE_ROTATION_* | MOUSE_ROTATION_0 = FRAMEBUFFER_ROTATION_0; | No rotation | 
| MOUSE_ROTATION_90  = FRAMEBUFFER_ROTATION_90; | 90 degree rotation | 
| MOUSE_ROTATION_180 = FRAMEBUFFER_ROTATION_180; | 180 degree rotation | 
| MOUSE_ROTATION_270 = FRAMEBUFFER_ROTATION_270; | 270 degree rotation | 
 
 MOUSE_LOG_* | MOUSE_LOG_LEVEL_DEBUG = LOG_LEVEL_DEBUG; | Mouse debugging messages | 
| MOUSE_LOG_LEVEL_INFO = LOG_LEVEL_INFO; | Mouse informational messages, such as a device being attached or detached | 
| MOUSE_LOG_LEVEL_WARN = LOG_LEVEL_WARN; | Mouse warning messages | 
| MOUSE_LOG_LEVEL_ERROR = LOG_LEVEL_ERROR; | Mouse error messages | 
| MOUSE_LOG_LEVEL_NONE = LOG_LEVEL_NONE; | No Mouse messages | 
 USBMOUSE_* | USBMOUSE_DRIVER_NAME = 'USB Mouse Driver (HID boot protocol)'; | Name of USB mouse driver | 
| USBMOUSE_MOUSE_DESCRIPTION = 'USB HID Mouse'; | Description of USB mouse device | 
| HID Interface Subclass types (See USB HID v1.11 specification) | |
| USB_HID_SUBCLASS_NONE = 0; | Section 4.2 | 
| USB_HID_SUBCLASS_BOOT = 1; | Section 4.2 | 
| HID Interface Protocol types (See USB HID v1.11 specification) | |
| USB_HID_BOOT_PROTOCOL_NONE = 0; | Section 4.3 | 
| USB_HID_BOOT_PROTOCOL_KEYBOARD = 1; | Section 4.3 | 
| USB_HID_BOOT_PROTOCOL_MOUSE = 2; | Section 4.3 | 
| HID Class Descriptor Types (See USB HID v1.11 specification) | |
| USB_HID_DESCRIPTOR_TYPE_HID = $21; | Section 7.1 | 
| USB_HID_DESCRIPTOR_TYPE_REPORT = $22; | Section 7.1 | 
| USB_HID_DESCRIPTOR_TYPE_PHYSICAL_DESCRIPTOR = $23; | Section 7.1 | 
| HID Request types | |
| USB_HID_REQUEST_GET_REPORT = $01; | |
| USB_HID_REQUEST_GET_IDLE = $02; | |
| USB_HID_REQUEST_GET_PROTOCOL = $03; | Section 7.2 | 
| USB_HID_REQUEST_SET_REPORT = $09; | |
| USB_HID_REQUEST_SET_IDLE = $0A; | |
| USB_HID_REQUEST_SET_PROTOCOL = $0B; | Section 7.2 | 
| HID Protocol types | |
| USB_HID_PROTOCOL_BOOT = 0; | Section 7.2.5 | 
| USB_HID_PROTOCOL_REPORT = 1; | Section 7.2.5 | 
| HID Report types | |
| USB_HID_REPORT_INPUT = 1; | Section 7.2.1 | 
| USB_HID_REPORT_OUTPUT = 2; | Section 7.2.1 | 
| USB_HID_REPORT_FEATURE = 3; | Section 7.2.1 | 
| HID Report IDs | |
| USB_HID_REPORTID_NONE = 0; | Section 7.2.1 | 
| HID Boot Protocol Button bits | |
| USB_HID_BOOT_LEFT_BUTTON = (1 shl 0); | |
| USB_HID_BOOT_RIGHT_BUTTON = (1 shl 1); | |
| USB_HID_BOOT_MIDDLE_BUTTON = (1 shl 2); | |
| USB_HID_BOOT_SIDE_BUTTON = (1 shl 3); | |
| USB_HID_BOOT_EXTRA_BUTTON = (1 shl 4); | |
| HID Boot Protocol Report data | |
| USB_HID_BOOT_REPORT_SIZE = 3; | Appendix B of HID Device Class Definition 1.11 | 
| USB_HID_BOOT_DATA_SIZE = 8; | Allocate more than the minimum to allow for extra data | 
 
Type definitions
Mouse data
PMouseData = ^TMouseData;
TMouseData = record
| Buttons:Word; | The bitmap of current button values (eg MOUSE_LEFT_BUTTON or MOUSE_RIGHT_BUTTON) | 
| OffsetX:SmallInt; | The X offset of the mouse pointer (May be negative, read as absolute if Buttons includes MOUSE_ABSOLUTE_X) | 
| OffsetY:SmallInt; | The Y offset of the mouse pointer (May be negative, read as absolute if Buttons includes MOUSE_ABSOLUTE_Y) | 
| OffsetWheel:SmallInt; | The X offset of the mouse pointer (May be negative, read as absolute if Buttons includes MOUSE_ABSOLUTE_WHEEL) | 
| MaximumX:Word; | The maximum X value of the mouse (Only applicable if Buttons includes MOUSE_ABSOLUTE_X, otherwise must be 0) | 
| MaximumY:Word; | The maximum Y value of the mouse (Only applicable if Buttons includes MOUSE_ABSOLUTE_Y, otherwise must be 0) | 
| MaximumWheel:Word; | The maximum Wheel value of the mouse (Only applicable if Buttons includes MOUSE_ABSOLUTE_WHEEL, otherwise must be 0) | 
| Reserved:Word; | Reserved field (Round structure to 16 bytes) | 
Mouse buffer
PMouseBuffer = ^TMouseBuffer;
TMouseBuffer = record
| Wait:TSemaphoreHandle; | Buffer ready semaphore | 
| Start:LongWord; | Index of first buffer ready | 
| Count:LongWord; | Number of entries ready in buffer | 
| Buffer:array[0..(MOUSE_BUFFER_SIZE - 1)] of TMouseData;  | 
Mouse enumeration callback
| TMouseEnumerate = function(Mouse:PMouseDevice; Data:Pointer):LongWord; | 
Mouse notification callback
| TMouseNotification = function(Device:PDevice; Data:Pointer; Notification:LongWord):LongWord; | 
Mouse device read
| TMouseDeviceRead = function(Mouse:PMouseDevice; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord; | 
Mouse device control
| TMouseDeviceControl = function(Mouse:PMouseDevice; Request:Integer; Argument1:LongWord; var Argument2:LongWord):LongWord; | 
Mouse device types
PMouseDevice = ^TMouseDevice;
TMouseDevice = record
| Device Properties | |
| Device:TDevice; | The Device entry for this Mouse | 
| Mouse Properties | |
| MouseId:LongWord; | Unique Id of this Mouse in the Mouse table | 
| MouseState:LongWord; | Mouse state (eg MOUSE_STATE_ATTACHED) | 
| MouseRate:LongWord; | Mouse sample rate (Samples per second) | 
| DeviceRead:TMouseDeviceRead; | A Device specific DeviceRead method implementing a standard Mouse device interface (Or nil if the default method is suitable) | 
| DeviceControl:TMouseDeviceControl; | A Device specific DeviceControl method implementing a standard Mouse device interface (Or nil if the default method is suitable) | 
| Driver Properties | |
| Lock:TMutexHandle; | Mouse lock | 
| Buffer:TMouseBuffer; | Mouse input buffer | 
| Statistics Properties | |
| ReceiveCount:LongWord; | |
| ReceiveErrors:LongWord; | |
| BufferOverruns:LongWord; | |
| Internal Properties | |
| Prev:PMouseDevice; | Previous entry in Mouse table | 
| Next:PMouseDevice; | Next entry in Mouse table | 
USB HID descriptor
PUSBHIDDescriptor = ^TUSBHIDDescriptor;
TUSBHIDDescriptor = packed record
| bLength:Byte; | |
| bDescriptorType:Byte; | |
| bcdHID:Word; | |
| bCountryCode:Byte; | |
| bNumDescriptors:Byte; | |
| bHIDDescriptorType:Byte; | |
| wHIDDescriptorLength:Word; | |
| Note: Up to two optional bHIDDescriptorType/wHIDDescriptorLength pairs after the Report descriptor details | |
USB mouse device types
PUSBMouseDevice = ^TUSBMouseDevice;
TUSBMouseDevice = record
| Mouse Properties | |
| Mouse:TMouseDevice; | |
| USB Properties | |
| HIDInterface:PUSBInterface; | USB HID Mouse Interface | 
| ReportRequest:PUSBRequest; | USB request for mouse report data | 
| ReportEndpoint:PUSBEndpointDescriptor; | USB Mouse Interrupt IN Endpoint | 
| HIDDescriptor:PUSBHIDDescriptor; | USB HID Descriptor for mouse | 
| ReportDescriptor:Pointer; | USB HID Report Descriptor for mouse | 
| PendingCount:LongWord; | Number of USB requests pending for this mouse | 
| WaiterThread:TThreadId; | Thread waiting for pending requests to complete (for mouse detachment) | 
Public variables
Mouse logging
| MOUSE_DEFAULT_LOG_LEVEL:LongWord = MOUSE_LOG_LEVEL_DEBUG; | Minimum level for Mouse messages. Only messages with level greater than or equal to this will be printed. | 
| MOUSE_LOG_ENABLED:Boolean; | 
Function declarations
Initialization functions
procedure MouseInit;
| Note | Called only during system startup | 
|---|
Mouse functions
function MousePeek:LongWord;
| Return | ERROR_SUCCESS if packets are ready, ERROR_NO_MORE_ITEMS if not or another error code on failure | 
|---|
function MouseRead(Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord; inline;
| Buffer | Pointer to a buffer to copy the mouse data packets to | 
|---|---|
| Size | The size of the buffer in bytes (Must be at least TMouseData or greater) | 
| Count | The number of mouse data packets copied to the bufferm | 
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseReadEx(Buffer:Pointer; Size,Flags:LongWord; var Count:LongWord):LongWord;
| Buffer | Pointer to a buffer to copy the mouse data packets to | 
|---|---|
| Size | The size of the buffer in bytes (Must be at least TMouseData or greater) | 
| Flags | The flags for the behaviour of the read (eg MOUSE_FLAG_NON_BLOCK) | 
| Count | The number of mouse data packets copied to the buffer | 
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseWrite(Buffer:Pointer; Size,Count:LongWord):LongWord;
| Buffer | Pointer to a buffer to copy the mouse data packets from | 
|---|---|
| Size | The size of the buffer in bytes (Must be at least TMouseData or greater) | 
| Count | The number of mouse data packets to copy from the buffer | 
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseFlush:LongWord;
| Return | ERROR_SUCCESS if completed or another error code on failure | 
|---|
function MouseDeviceRead(Mouse:PMouseDevice; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;
| Mouse | The mouse device to read from | 
|---|---|
| Buffer | Pointer to a buffer to copy the mouse data packets to | 
| Size | The size of the buffer in bytes (Must be at least TMouseData or greater) | 
| Count | The number of mouse data packets copied to the buffer | 
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseDeviceControl(Mouse:PMouseDevice; Request:Integer; Argument1:LongWord; var Argument2:LongWord):LongWord;
| Mouse | The mouse device to control | 
|---|---|
| Request | The request code for the operation (eg MOUSE_CONTROL_GET_FLAG) | 
| Argument1 | The first argument for the operation (Dependent on request code) | 
| Argument2 | The second argument for the operation (Dependent on request code) | 
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseDeviceSetState(Mouse:PMouseDevice; State:LongWord):LongWord;
| Mouse | The mouse to set the state for | 
|---|---|
| State | The new state to set and notify | 
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseDeviceCreate:PMouseDevice;
| Return | Pointer to new Mouse device entry or nil if mouse could not be created | 
|---|
function MouseDeviceCreateEx(Size:LongWord):PMouseDevice;
| Size | Size in bytes to allocate for new mouse (Including the mouse device entry) | 
|---|---|
| Return | Pointer to new Mouse device entry or nil if mouse could not be created | 
function MouseDeviceDestroy(Mouse:PMouseDevice):LongWord;
| Mouse | The mouse device to destroy | 
|---|---|
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseDeviceRegister(Mouse:PMouseDevice):LongWord;
| Mouse | The mouse device to register | 
|---|---|
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseDeviceDeregister(Mouse:PMouseDevice):LongWord;
| Mouse | The mouse device to deregister | 
|---|---|
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseDeviceFind(MouseId:LongWord):PMouseDevice;
| MouseId | The ID number of the mouse to find | 
|---|---|
| Return | Pointer to mouse device entry or nil if not found | 
function MouseDeviceFindByName(const Name:String):PMouseDevice; inline;
| Name | The name of the mouse to find (eg Mouse0) | 
|---|---|
| Return | Pointer to mouse device entry or nil if not found | 
function MouseDeviceFindByDescription(const Description:String):PMouseDevice; inline;
| Description | The description of the mouse to find (eg USB HID Mouse) | 
|---|---|
| Return | Pointer to mouse device entry or nil if not found | 
function MouseDeviceEnumerate(Callback:TMouseEnumerate; Data:Pointer):LongWord;
| Callback | The callback function to call for each mouse in the table | 
|---|---|
| Data | A private data pointer to pass to callback for each mouse in the table | 
| Return | ERROR_SUCCESS if completed or another error code on failure | 
function MouseDeviceNotification(Mouse:PMouseDevice; Callback:TMouseNotification; Data:Pointer; Notification,Flags:LongWord):LongWord;
| Mouse | The mouse device to notify changes for (Optional, pass nil for all mice) | 
|---|---|
| Callback | The function to call when a notification event occurs | 
| Data | A private data pointer to pass to callback when a notification event occurs | 
| Notification | The events to register for notification of (eg DEVICE_NOTIFICATION_REGISTER) | 
| Flags | The flags to control the notification (eg NOTIFIER_FLAG_WORKER) | 
RTL console functions
function SysConsoleHideMouse(AUserData:Pointer):Boolean;
| Note | None documented | 
|---|
function SysConsoleShowMouse(X,Y:LongWord; AUserData:Pointer):Boolean;
| Note | None documented | 
|---|
function SysConsoleReadMouse(var X,Y,Buttons:LongWord; AUserData:Pointer):Boolean;
| Note | None documented | 
|---|
USB mouse functions
function USBMouseDeviceRead(Mouse:PMouseDevice; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;
| Note | Not intended to be called directly by applications, use MouseDeviceRead instead. | 
|---|
function USBMouseDeviceControl(Mouse:PMouseDevice; Request:Integer; Argument1:LongWord; var Argument2:LongWord):LongWord;
| Note | Not intended to be called directly by applications, use MouseDeviceControl instead. | 
|---|
function USBMouseDriverBind(Device:PUSBDevice; Interrface:PUSBInterface):LongWord;
| Device | The USB device to attempt to bind to | 
|---|---|
| Interrface | The USB interface to attempt to bind to (or nil for whole device) | 
| Return | USB_STATUS_SUCCESS if completed, USB_STATUS_DEVICE_UNSUPPORTED if unsupported or another error code on failure. | 
function USBMouseDriverUnbind(Device:PUSBDevice; Interrface:PUSBInterface):LongWord;
| Device | The USB device to unbind from | 
|---|---|
| Interrface | The USB interface to unbind from (or nil for whole device) | 
| Return | USB_STATUS_SUCCESS if completed or another error code on failure | 
procedure USBMouseReportWorker(Request:PUSBRequest);
| Request | The USB request which has completed | 
|---|
procedure USBMouseReportComplete(Request:PUSBRequest);
| Request | The USB request which has completed | 
|---|---|
| Note | Request is passed to worker thread for processing to prevent blocking the USB completion | 
Mouse helper functions
function MouseGetCount:LongWord; inline;
| Note | None documented | 
|---|
function MouseDeviceCheck(Mouse:PMouseDevice):PMouseDevice;
| Note | None documented | 
|---|
function MouseDeviceTypeToString(MouseType:LongWord):String;
| Note | None documented | 
|---|
function MouseDeviceStateToString(MouseState:LongWord):String;
| Note | None documented | 
|---|
function MouseDeviceStateToNotification(State:LongWord):LongWord;
| Note | None documented | 
|---|
function MouseInsertData(Mouse:PMouseDevice; Data:PMouseData; Signal:Boolean):LongWord;
| Mouse | The mouse device to insert data for | 
|---|---|
| Data | The TMouseData entry to insert | 
| Signal | If True then signal that new data is available in the buffer | 
| Return | ERROR_SUCCESS if completed or another error code on failure | 
| Note | Caller must hold the mouse lock | 
procedure MouseLog(Level:LongWord; Mouse:PMouseDevice; const AText:String);
| Note | None documented | 
|---|
procedure MouseLogInfo(Mouse:PMouseDevice; const AText:String); inline;
| Note | None documented | 
|---|
procedure MouseLogWarn(Mouse:PMouseDevice; const AText:String); inline;
| Note | None documented | 
|---|
procedure MouseLogError(Mouse:PMouseDevice; const AText:String); inline;
| Note | None documented | 
|---|
procedure MouseLogDebug(Mouse:PMouseDevice;const AText:String); inline;
| Note | None documented | 
|---|
USB mouse helper functions
function USBMouseCheckDevice(Device:PUSBDevice):Boolean;
| Device | The USB device to check | 
|---|---|
| Return | True if the device is suitable or False if it is not | 
function USBMouseDeviceSetProtocol(Mouse:PUSBMouseDevice; Protocol:Byte):LongWord;
| Mouse | The USB mouse device to set the report protocol for | 
|---|---|
| Protocol | The report protocol to set (eg USB_HID_PROTOCOL_BOOT) | 
| Return | USB_STATUS_SUCCESS if completed or another USB error code on failure | 
function USBMouseDeviceGetHIDDescriptor(Mouse:PUSBMouseDevice; Descriptor:PUSBHIDDescriptor):LongWord;
| Mouse | The USB mouse device to get the descriptor for | 
|---|---|
| Descriptor | Pointer to a USB HID Descriptor structure for the returned data | 
| Return | USB_STATUS_SUCCESS if completed or another USB error code on failure | 
function USBMouseDeviceGetReportDescriptor(Mouse:PUSBMouseDevice; Descriptor:Pointer; Size:LongWord):LongWord;
| Mouse | The USB mouse device to get the descriptor for | 
|---|---|
| Descriptor | Pointer to a buffer to return the USB Report Descriptor | 
| Size | The size in bytes of the buffer pointed to by Descriptor | 
| Return | USB_STATUS_SUCCESS if completed or another USB error code on failure | 
Return to Unit Reference

