From 34f0d87a9d8f9c9973a4f2749d158f1f6443e534 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Fri, 3 Jun 2011 20:54:05 +0000 Subject: [PATCH] Add initialize[_to_extended_joystick]() methods to variable_joystick. With those methods the members are populated, the flat data size is calcualted, the data buffer is allocated and the convenience pointers are set up automatically. With initialize() an arbitrary configuration can be created, with initialize_to_extended_joystick() a configuration is created that exactly matches the data structure of the extended_joystick struct. Having them here makes it easy to use the structure from a driver as well without the need to set up everything manually. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41888 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/device/joystick_driver.h | 33 ++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/headers/private/device/joystick_driver.h b/headers/private/device/joystick_driver.h index 6716932af9..8182baa380 100644 --- a/headers/private/device/joystick_driver.h +++ b/headers/private/device/joystick_driver.h @@ -16,6 +16,7 @@ #include #include #include +#include typedef struct _joystick { bigtime_t timestamp; @@ -46,7 +47,35 @@ typedef struct _extended_joystick { // make storing and accessing the flat data in the "data" member easier. When // transferring data via read/write/ioctl only the flat data in "data" is ever // transmitted, not the whole structure. -typedef struct _variable_joystick { +typedef struct variable_joystick { + status_t initialize(uint32 axisCount, uint32 hatCount, uint32 buttonCount) + { + axis_count = axisCount; + hat_count = hatCount; + button_blocks = (buttonCount + 31) / 32; + + data_size = sizeof(bigtime_t) // timestamp + + button_blocks * sizeof(uint32) // bitmaps + + axis_count * sizeof(int16) // axis values + + hat_count * sizeof(uint8); // hat values + + data = (uint8 *)malloc(data_size); + if (data == NULL) + return B_NO_MEMORY; + + // fill in the convenience pointers + timestamp = (bigtime_t *)data; + buttons = (uint32 *)×tamp[1]; + axes = (int16 *)&buttons[button_blocks]; + hats = (uint8 *)&axes[axis_count]; + return B_OK; + } + + status_t initialize_to_extended_joystick() + { + return initialize(MAX_AXES, MAX_HATS, MAX_BUTTONS); + } + uint32 axis_count; uint32 hat_count; uint32 button_blocks; @@ -54,7 +83,7 @@ typedef struct _variable_joystick { // These pointers all point into the data section and are here for // convenience. They need to be set up manually by the one who creates this - // structure. + // structure or by using the initialize() method. bigtime_t * timestamp; uint32 * buttons; int16 * axes;