src/haptic/linux/SDL_syshaptic.c
changeset 3680 7c18b38f0f9a
parent 3218 81773a1eac83
child 4514 0c71708adbcb
equal deleted inserted replaced
3679:ce290de32f2a 3680:7c18b38f0f9a
   503     Uint32 tmp;
   503     Uint32 tmp;
   504     float f;                    /* Ideally we'd use fixed point math instead of floats... */
   504     float f;                    /* Ideally we'd use fixed point math instead of floats... */
   505 
   505 
   506     switch (dir->type) {
   506     switch (dir->type) {
   507     case SDL_HAPTIC_POLAR:
   507     case SDL_HAPTIC_POLAR:
   508         /* Linux directions are inverted. */
   508         /* Linux directions start from south.
   509         tmp = (((18000 + dir->dir[0]) % 36000) * 0xFFFF) / 36000;
   509         		(and range from 0 to 0xFFFF)
       
   510 				   Quoting include/linux/input.h, line 926:
       
   511 				   Direction of the effect is encoded as follows:
       
   512 						0 deg -> 0x0000 (down)
       
   513 						90 deg -> 0x4000 (left)
       
   514 						180 deg -> 0x8000 (up)
       
   515 						270 deg -> 0xC000 (right)
       
   516 					*/
       
   517         tmp = (((18000 + dir->dir[0]) % 36000) * 0xFFFF) / 36000; // convert to range [0,0xFFFF]
   510         return (Uint16) tmp;
   518         return (Uint16) tmp;
   511 
   519 
       
   520 	   case SDL_HAPTIC_SPHERICAL:
       
   521    			/*
       
   522    				We convert to polar, because that's the only supported direction on Linux.
       
   523    				The first value of a spherical direction is practically the same as a
       
   524    				Polar direction, except that we have to add 90 degrees. It is the angle
       
   525    				from EAST {1,0} towards SOUTH {0,1}.
       
   526    				--> add 9000
       
   527    				--> finally add 18000 and convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
       
   528    			*/
       
   529 		   	tmp = ((dir->dir[0]) + 9000) % 36000;    /* Convert to polars */
       
   530         tmp = (((18000 + tmp) % 36000) * 0xFFFF) / 36000; // convert to range [0,0xFFFF]
       
   531         return (Uint16) tmp;
       
   532 
   512     case SDL_HAPTIC_CARTESIAN:
   533     case SDL_HAPTIC_CARTESIAN:
   513         /* We must invert "x" and "y" to go clockwise. */
   534         f = atan2(dir->dir[1], dir->dir[0]);
   514         f = atan2(dir->dir[0], dir->dir[1]);
   535 					/* 
   515         tmp = (int) (f * 18000. / M_PI) % 36000;
   536 					  atan2 takes the parameters: Y-axis-value and X-axis-value (in that order)
   516         tmp = (tmp * 0xFFFF) / 36000;
   537 					   - Y-axis-value is the second coordinate (from center to SOUTH)
   517         return (Uint16) tmp;
   538 					   - X-axis-value is the first coordinate (from center to EAST)
   518 
   539 						We add 36000, because atan2 also returns negative values. Then we practically
   519     case SDL_HAPTIC_SPHERICAL:
   540 						have the first spherical value. Therefore we proceed as in case
   520         tmp = (36000 - dir->dir[0]) + 27000;    /* Convert to polars */
   541 						SDL_HAPTIC_SPHERICAL and add another 9000 to get the polar value.
   521         tmp = (((18000 + tmp) % 36000) * 0xFFFF) / 36000;
   542 					  --> add 45000 in total
       
   543 					  --> finally add 18000 and convert to [0,0xFFFF] as in case SDL_HAPTIC_POLAR.
       
   544 					*/
       
   545 				tmp = (((int) (f * 18000. / M_PI)) + 45000) % 36000;
       
   546         tmp = (((18000 + tmp) % 36000) * 0xFFFF) / 36000; // convert to range [0,0xFFFF]
   522         return (Uint16) tmp;
   547         return (Uint16) tmp;
   523 
   548 
   524     default:
   549     default:
   525         SDL_SetError("Haptic: Unsupported direction type.");
   550         SDL_SetError("Haptic: Unsupported direction type.");
   526         return (Uint16) - 1;
   551         return (Uint16) - 1;