From nobody@FreeBSD.org  Sat Apr 26 06:11:38 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id 7F61E43D
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 26 Apr 2014 06:11:38 +0000 (UTC)
Received: from cgiserv.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(Client did not present a certificate)
	by mx1.freebsd.org (Postfix) with ESMTPS id 6AFBB1815
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 26 Apr 2014 06:11:38 +0000 (UTC)
Received: from cgiserv.freebsd.org ([127.0.1.6])
	by cgiserv.freebsd.org (8.14.8/8.14.8) with ESMTP id s3Q6BcAD059140
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 26 Apr 2014 06:11:38 GMT
	(envelope-from nobody@cgiserv.freebsd.org)
Received: (from nobody@localhost)
	by cgiserv.freebsd.org (8.14.8/8.14.8/Submit) id s3Q6Bcoj059131;
	Sat, 26 Apr 2014 06:11:38 GMT
	(envelope-from nobody)
Message-Id: <201404260611.s3Q6Bcoj059131@cgiserv.freebsd.org>
Date: Sat, 26 Apr 2014 06:11:38 GMT
From: Thomas Russo <russo@bogodyn.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: x11-toolkits/plib improperly handles USB joystick min/max/center, and ignores "dial" axes
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         189006
>Category:       ports
>Synopsis:       x11-toolkits/plib improperly handles USB joystick min/max/center, and ignores "dial" axes
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-ports-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Apr 26 06:20:00 UTC 2014
>Closed-Date:    
>Last-Modified:  Sat Apr 26 13:20:00 UTC 2014
>Originator:     Thomas Russo
>Release:        9.2-STABLE
>Organization:
>Environment:
FreeBSD bogodyn.org 9.2-STABLE FreeBSD 9.2-STABLE #0 r261812: Wed Feb 12 17:21:14 MST 2014     russo@bogodyn.org:/usr/obj/usr/src/sys/BOGODYN  i386

>Description:
plib upstream has not produced a release tarball since 2006, and in the release version the min/max/center values of USB uhid joysticks is hard coded as 0/255/127.  This is clearly wrong for many joysticks on the market today.

In 2008 someone submitted a patch to the PLIB developers and it was put into their sourceforge repository, but no release was created after that.

Thus, the plib in the FreeBSD ports collection still has the hard-coded min/max/center.

Further, the jsBSD.cxx file switch statement that detects various axis types doesn't recognize "Dial" type axes.  Joysticks such as the Saitek X45 have such dials, which get ignored by plib.

The result is that UHID devices that have min/max values other than 0/255 fail to perform correctly in games such as flightgear --- they will either not have the full range of values showing up, will not center properly, or will otherwise produce weird, uncalibrated input to the game.


>How-To-Repeat:
Install Flightgear.  Plug a USB joystick device such as the Saitek X45 Flight Controller or the Saitek Pro Flight Rudder Pedals into a BSD machine, and run the "js_demo" program.

For the X45, only 6 of the 7 axes will show up.  Few of the axes will show their full range -1 to 1.  This is because most of the axes don't have logical min/max of 0/255:

Input   rid=0 size=12 count=1 page=Generic_Desktop usage=X, logical range 52..264
Input   rid=0 size=12 count=1 page=Generic_Desktop usage=Y, logical range 22..232
Input   rid=0 size=8 count=1 page=Generic_Desktop usage=Slider, logical range 96..246
Input   rid=0 size=8 count=1 page=Generic_Desktop usage=Rz, logical range 91..253
Input   rid=0 size=8 count=1 page=Generic_Desktop usage=Dial, logical range 1..163
Input   rid=0 size=8 count=1 page=Generic_Desktop usage=Rx, logical range 4..172
Input   rid=0 size=4 count=1 page=Generic_Desktop usage=Hat_Switch, logical range 1..8, physical range 0..315, unit=0x14 exp=0


For the Pro Flight Rudder Pedals, the logical range is 0 to 511, and so 0-255 (left rudder) are mapped to -1 to +1, and 256-511 (right rudder)are treated as "saturated", always showing up as +1
>Fix:
The attached patch completely fixes the problem for me.  It is a combination of the 2008 patch that fixes the min/max/center issue, and an addition of HUG_DIAL to the recognized axis types.


Patch attached with submission follows:

--- src/js/jsBSD.cxx_orig	2014-04-25 23:37:11.000000000 -0600
+++ src/js/jsBSD.cxx	2014-04-25 23:38:54.000000000 -0600
@@ -99,6 +99,8 @@
   // on every read of a USB device
   int              cache_buttons ;
   float            cache_axes [ _JS_MAX_AXES ] ;
+  float            axes_minimum [ _JS_MAX_AXES ] ;
+  float            axes_maximum [ _JS_MAX_AXES ] ;
 };
 
 // Idents lower than USB_IDENT_OFFSET are for analog joysticks.
@@ -196,9 +198,12 @@
          case HUG_Z:
          case HUG_RZ:
          case HUG_SLIDER:
+         case HUG_DIAL:
            if (*num_axes < _JS_MAX_AXES)
            {
              os->axes_usage[*num_axes] = usage;
+             os->axes_minimum[*num_axes] = h.logical_minimum;
+             os->axes_maximum[*num_axes] = h.logical_maximum;
              (*num_axes)++;
            }
            break;
@@ -324,9 +329,6 @@
 
   for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
   {
-	// We really should get this from the HID, but that data seems
-	// to be quite unreliable for analog-to-USB converters. Punt for
-	// now.
     if ( os->axes_usage [ i ] == HUG_HAT_SWITCH )
     {
       max       [ i ] = 1.0f ;
@@ -335,9 +337,9 @@
     }
     else
     {
-      max       [ i ] = 255.0f ;
-      center    [ i ] = 127.0f ;
-      min       [ i ] = 0.0f ;
+      max       [ i ] = os->axes_maximum [ i ];
+      min       [ i ] = os->axes_minimum [ i ];
+      center    [ i ] = (max [ i ] + min [ i ]) / 2.0 ;
     }
     dead_band [ i ] = 0.0f ;
     saturate  [ i ] = 1.0f ;


>Release-Note:
>Audit-Trail:

From: Tom Russo <tom.km5vy@gmail.com>
To: bug-followup@freebsd.org, russo@bogodyn.org
Cc:  
Subject: Re: ports/189006: x11-toolkits/plib improperly handles USB joystick
 min/max/center, and ignores &quot;dial&quot; axes
Date: Sat, 26 Apr 2014 07:15:09 -0600

 It should be obvious, but I should have stated that the  patch I
 attached to this report is intended for the "files" directory of
 ports/x11-toolkits/plib, and should be called  patch-src-js-jsBSD.cxx
 when put there.  It is not a patch to the *port*, but a patch to the
 code installed by the port.  Dropping it into the files directory of
 the port fixes the reported issue.
>Unformatted:
