motion.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:10k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * motion.c: control VLC with laptop built-in motion sensors
  3.  *****************************************************************************
  4.  * Copyright (C) 2006 - 2007 the VideoLAN team
  5.  * $Id: 03a011192d2e3cabfc315bc3d965fb00b14bdf71 $
  6.  *
  7.  * Author: Sam Hocevar <sam@zoy.org>
  8.  *         Jérôme Decoodt <djc@videolan.org> (unimotion integration)
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  23.  *****************************************************************************/
  24. /*****************************************************************************
  25.  * Preamble
  26.  *****************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. # include "config.h"
  29. #endif
  30. #include <math.h>
  31. #include <vlc_common.h>
  32. #include <vlc_plugin.h>
  33. #include <vlc_interface.h>
  34. #include <vlc_vout.h>
  35. #ifdef HAVE_UNISTD_H
  36. #    include <unistd.h>
  37. #endif
  38. #ifdef __APPLE__
  39. #include "unimotion.h"
  40. #endif
  41. /*****************************************************************************
  42.  * intf_sys_t: description and status of interface
  43.  *****************************************************************************/
  44. struct intf_sys_t
  45. {
  46.     enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR,
  47.            UNIMOTION_SENSOR } sensor;
  48. #ifdef __APPLE__
  49.     enum sms_hardware unimotion_hw;
  50. #endif
  51.     int i_calibrate;
  52.     bool b_use_rotate;
  53. };
  54. /*****************************************************************************
  55.  * Local prototypes.
  56.  *****************************************************************************/
  57. static int  Open   ( vlc_object_t * );
  58. static void Close  ( vlc_object_t * );
  59. static void RunIntf( intf_thread_t *p_intf );
  60. static int GetOrientation( intf_thread_t *p_intf );
  61. #define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")
  62. /*****************************************************************************
  63.  * Module descriptor
  64.  *****************************************************************************/
  65. vlc_module_begin ()
  66.     set_shortname( N_("motion"))
  67.     set_category( CAT_INTERFACE )
  68.     set_subcategory( SUBCAT_INTERFACE_CONTROL )
  69.     set_description( N_("motion control interface") )
  70.     set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " 
  71.                  "to rotate the video") )
  72.     add_bool( "motion-use-rotate", 0, NULL,
  73.               USE_ROTATE_TEXT, USE_ROTATE_TEXT, false )
  74.     set_capability( "interface", 0 )
  75.     set_callbacks( Open, Close )
  76. vlc_module_end ()
  77. /*****************************************************************************
  78.  * OpenIntf: initialise interface
  79.  *****************************************************************************/
  80. int Open ( vlc_object_t *p_this )
  81. {
  82.     intf_thread_t *p_intf = (intf_thread_t *)p_this;
  83.     FILE *f;
  84.     int i_x, i_y;
  85.     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
  86.     if( p_intf->p_sys == NULL )
  87.     {
  88.         return VLC_ENOMEM;
  89.     }
  90.     if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
  91.     {
  92.         /* IBM HDAPS support */
  93.         f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
  94.         if( f )
  95.         {
  96.             i_x = i_y = 0;
  97.             fscanf( f, "(%d,%d)", &i_x, &i_y );
  98.             fclose( f );
  99.             p_intf->p_sys->i_calibrate = i_x;
  100.             p_intf->p_sys->sensor = HDAPS_SENSOR;
  101.         }
  102.         else
  103.         {
  104.             p_intf->p_sys->sensor = NO_SENSOR;
  105.         }
  106.     }
  107.     else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
  108.     {
  109.         /* Apple Motion Sensor support */
  110.         p_intf->p_sys->sensor = AMS_SENSOR;
  111.     }
  112.     else if( access( "/sys/devices/applesmc.768/position", R_OK ) == 0 )
  113.     {
  114.         /* Apple SMC (newer macbooks) */
  115.         /* Should be factorised with HDAPS */
  116.         f = fopen( "/sys/devices/applesmc.768/calibrate", "r" );
  117.         if( f )
  118.         {
  119.             i_x = i_y = 0;
  120.             fscanf( f, "(%d,%d)", &i_x, &i_y );
  121.             fclose( f );
  122.             p_intf->p_sys->i_calibrate = i_x;
  123.             p_intf->p_sys->sensor = APPLESMC_SENSOR;
  124.         }
  125.         else
  126.         {
  127.             p_intf->p_sys->sensor = NO_SENSOR;
  128.         }
  129.     }
  130. #ifdef __APPLE__
  131.     else if( p_intf->p_sys->unimotion_hw = detect_sms() )
  132.         p_intf->p_sys->sensor = UNIMOTION_SENSOR;
  133. #endif
  134.     else
  135.     {
  136.         /* No motion sensor support */
  137.         p_intf->p_sys->sensor = NO_SENSOR;
  138.     }
  139.     p_intf->pf_run = RunIntf;
  140.     p_intf->p_sys->b_use_rotate = config_GetInt( p_intf, "motion-use-rotate" );
  141.     return VLC_SUCCESS;
  142. }
  143. /*****************************************************************************
  144.  * CloseIntf: destroy interface
  145.  *****************************************************************************/
  146. void Close ( vlc_object_t *p_this )
  147. {
  148.     intf_thread_t *p_intf = (intf_thread_t *)p_this;
  149.     free( p_intf->p_sys );
  150. }
  151. /*****************************************************************************
  152.  * RunIntf: main loop
  153.  *****************************************************************************/
  154. #define FILTER_LENGTH 16
  155. #define LOW_THRESHOLD 800
  156. #define HIGH_THRESHOLD 1000
  157. static void RunIntf( intf_thread_t *p_intf )
  158. {
  159.     int i_x, i_oldx = 0, i_sum = 0, i = 0;
  160.     int p_oldx[FILTER_LENGTH];
  161.     memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
  162.     for( ;; )
  163.     {
  164.         vout_thread_t *p_vout;
  165.         const char *psz_filter, *psz_type;
  166.         bool b_change = false;
  167.         /* Wait a bit, get orientation, change filter if necessary */
  168.         msleep( INTF_IDLE_SLEEP );
  169.         int canc = vlc_savecancel();
  170.         i_x = GetOrientation( p_intf );
  171.         i_sum += i_x - p_oldx[i];
  172.         p_oldx[i++] = i_x;
  173.         if( i == FILTER_LENGTH ) i = 0;
  174.         i_x = i_sum / FILTER_LENGTH;
  175.         if( p_intf->p_sys->b_use_rotate )
  176.         {
  177.             if( i_oldx != i_x )
  178.             {
  179.                 /* TODO: cache object pointer */
  180.                 vlc_object_t *p_obj =
  181.                 vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
  182.                 if( p_obj )
  183.                 {
  184.                     var_SetInteger( p_obj, "rotate-deciangle",
  185.                             ((3600+i_x/2)%3600) );
  186.                     i_oldx = i_x;
  187.                     vlc_object_release( p_obj );
  188.                 }
  189.             }
  190.             goto loop;
  191.         }
  192.         if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
  193.         {
  194.             b_change = true;
  195.             psz_filter = "transform";
  196.             psz_type = "270";
  197.         }
  198.         else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
  199.                  || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
  200.         {
  201.             b_change = true;
  202.             psz_filter = "";
  203.             psz_type = "";
  204.         }
  205.         else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
  206.         {
  207.             b_change = true;
  208.             psz_filter = "transform";
  209.             psz_type = "90";
  210.         }
  211.         if( b_change )
  212.         {
  213.             p_vout = (vout_thread_t *)
  214.                 vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
  215.             if( p_vout )
  216.             {
  217.                 config_PutPsz( p_vout, "transform-type", psz_type );
  218.                 var_SetString( p_vout, "vout-filter", psz_filter );
  219.                 vlc_object_release( p_vout );
  220.                 i_oldx = i_x;
  221.             }
  222.         }
  223. loop:
  224.         vlc_restorecancel( canc );
  225.     }
  226. }
  227. #undef FILTER_LENGTH
  228. #undef LOW_THRESHOLD
  229. #undef HIGH_THRESHOLD
  230. /*****************************************************************************
  231.  * GetOrientation: get laptop orientation, range -1800 / +1800
  232.  *****************************************************************************/
  233. static int GetOrientation( intf_thread_t *p_intf )
  234. {
  235.     FILE *f;
  236.     int i_x, i_y, i_z = 0;
  237.     switch( p_intf->p_sys->sensor )
  238.     {
  239.     case HDAPS_SENSOR:
  240.         f = fopen( "/sys/devices/platform/hdaps/position", "r" );
  241.         if( !f )
  242.         {
  243.             return 0;
  244.         }
  245.         i_x = i_y = 0;
  246.         fscanf( f, "(%d,%d)", &i_x, &i_y );
  247.         fclose( f );
  248.         return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
  249.     case AMS_SENSOR:
  250.         f = fopen( "/sys/devices/ams/x", "r" );
  251.         if( !f )
  252.         {
  253.             return 0;
  254.         }
  255.         fscanf( f, "%d", &i_x);
  256.         fclose( f );
  257.         return - i_x * 30; /* FIXME: arbitrary */
  258.     case APPLESMC_SENSOR:
  259.         f = fopen( "/sys/devices/applesmc.768/position", "r" );
  260.         if( !f )
  261.         {
  262.             return 0;
  263.         }
  264.         i_x = i_y = i_z = 0;
  265.         fscanf( f, "(%d,%d,%d)", &i_x, &i_y, &i_z );
  266.         fclose( f );
  267.         return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
  268. #ifdef __APPLE__
  269.     case UNIMOTION_SENSOR:
  270.         if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
  271.         {
  272.             double d_norm = sqrt( i_x*i_x+i_z*i_z );
  273.             if( d_norm < 100 )
  274.                 return 0;
  275.             double d_x = i_x / d_norm;
  276.             if( i_z > 0 )
  277.                 return -asin(d_x)*3600/3.141;
  278.             else
  279.                 return 3600 + asin(d_x)*3600/3.141;
  280.         }
  281.         else
  282.             return 0;
  283. #endif
  284.     default:
  285.         return 0;
  286.     }
  287. }