]> git.sesse.net Git - vlc/blob - modules/control/motion.c
motion: fix orientation of transform filter
[vlc] / modules / control / motion.c
1 /*****************************************************************************
2  * motion.c: control VLC with laptop built-in motion sensors
3  *****************************************************************************
4  * Copyright (C) 2006 - 2007 the VideoLAN team
5  * $Id$
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 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <unistd.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_interface.h>
38 #include <vlc_playlist.h>
39 #include <vlc_vout.h>
40
41 #include "motionlib.h"
42
43 /*****************************************************************************
44  * intf_sys_t: description and status of interface
45  *****************************************************************************/
46 struct intf_sys_t
47 {
48     motion_sensors_t *p_motion;
49 };
50
51 /*****************************************************************************
52  * Local prototypes.
53  *****************************************************************************/
54 static int  Open   ( vlc_object_t * );
55 static void Close  ( vlc_object_t * );
56
57 static void RunIntf( intf_thread_t *p_intf );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin ()
63     set_shortname( N_("motion"))
64     set_category( CAT_INTERFACE )
65     set_subcategory( SUBCAT_INTERFACE_CONTROL )
66     set_description( N_("motion control interface") )
67     set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
68                  "to rotate the video") )
69
70     add_obsolete_bool( "motion-use-rotate" ) /* since 2.1.0 */
71
72     set_capability( "interface", 0 )
73     set_callbacks( Open, Close )
74 vlc_module_end ()
75
76 /*****************************************************************************
77  * OpenIntf: initialise interface
78  *****************************************************************************/
79 int Open ( vlc_object_t *p_this )
80 {
81     intf_thread_t *p_intf = (intf_thread_t *)p_this;
82
83     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
84     if( p_intf->p_sys == NULL )
85     {
86         return VLC_ENOMEM;
87     }
88
89     p_intf->p_sys->p_motion = motion_create( VLC_OBJECT( p_intf ) );
90     if( p_intf->p_sys->p_motion == NULL )
91     {
92         free( p_intf->p_sys );
93         return VLC_EGENERIC;
94     }
95
96     p_intf->pf_run = RunIntf;
97
98     return VLC_SUCCESS;
99 }
100
101 /*****************************************************************************
102  * CloseIntf: destroy interface
103  *****************************************************************************/
104 void Close ( vlc_object_t *p_this )
105 {
106     intf_thread_t *p_intf = (intf_thread_t *)p_this;
107
108     motion_destroy( p_intf->p_sys->p_motion );
109     free( p_intf->p_sys );
110 }
111
112 /*****************************************************************************
113  * RunIntf: main loop
114  *****************************************************************************/
115 #define LOW_THRESHOLD 800
116 #define HIGH_THRESHOLD 1000
117 static void RunIntf( intf_thread_t *p_intf )
118 {
119     int i_oldx = 0;
120
121     for( ;; )
122     {
123         const char *psz_type;
124         bool b_change = false;
125
126         /* Wait a bit, get orientation, change filter if necessary */
127 #warning FIXME: check once (or less) per picture, not once per interval
128         msleep( INTF_IDLE_SLEEP );
129
130         int canc = vlc_savecancel();
131         int i_x = motion_get_angle( p_intf->p_sys->p_motion );
132
133         if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
134         {
135             b_change = true;
136             psz_type = "90";
137         }
138         else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
139                  || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
140         {
141             b_change = true;
142             psz_type = NULL;
143         }
144         else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
145         {
146             b_change = true;
147             psz_type = "270";
148         }
149
150         if( b_change )
151         {
152 #warning FIXME: refactor this plugin as a video filter!
153             input_thread_t *p_input;
154
155             p_input = playlist_CurrentInput( pl_Get( p_intf ) );
156             if( p_input )
157             {
158                 vout_thread_t *p_vout;
159
160                 p_vout = input_GetVout( p_input );
161                 if( p_vout )
162                 {
163                     if( psz_type != NULL )
164                     {
165                         var_Create( p_vout, "transform-type", VLC_VAR_STRING );
166                         var_SetString( p_vout, "transform-type", psz_type );
167                     }
168                     else
169                         var_Destroy( p_vout, "transform-type" );
170
171                     var_SetString( p_vout, "video-filter",
172                                    psz_type != NULL ? "transform" : "" );
173                     vlc_object_release( p_vout );
174                 }
175                 vlc_object_release( p_input );
176                 i_oldx = i_x;
177             }
178         }
179
180         vlc_restorecancel( canc );
181     }
182 }
183 #undef LOW_THRESHOLD
184 #undef HIGH_THRESHOLD
185