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