motion.c 5.84 KB
Newer Older
1 2 3
/*****************************************************************************
 * motion.c: control VLC with laptop built-in motion sensors
 *****************************************************************************
4
 * Copyright (C) 2006 - 2007 the VideoLAN team
5 6 7
 * $Id$
 *
 * Author: Sam Hocevar <sam@zoy.org>
8
 *         Jérôme Decoodt <djc@videolan.org> (unimotion integration)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <unistd.h>
34

35
#include <vlc_common.h>
36
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
37
#include <vlc_interface.h>
38
#include <vlc_playlist.h>
Clément Stenac's avatar
Clément Stenac committed
39
#include <vlc_vout.h>
40

41
#include "motionlib.h"
42

43 44 45 46 47
/*****************************************************************************
 * intf_sys_t: description and status of interface
 *****************************************************************************/
struct intf_sys_t
{
48
    motion_sensors_t *p_motion;
49 50 51 52 53 54 55 56 57 58 59 60 61
};

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int  Open   ( vlc_object_t * );
static void Close  ( vlc_object_t * );

static void RunIntf( intf_thread_t *p_intf );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
62 63 64
vlc_module_begin ()
    set_shortname( N_("motion"))
    set_category( CAT_INTERFACE )
65
    set_subcategory( SUBCAT_INTERFACE_CONTROL )
66
    set_description( N_("motion control interface") )
67 68
    set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
                 "to rotate the video") )
69

70
    add_obsolete_bool( "motion-use-rotate" ) /* since 2.1.0 */
71

72 73 74
    set_capability( "interface", 0 )
    set_callbacks( Open, Close )
vlc_module_end ()
75 76 77 78 79 80 81 82 83 84 85 86 87 88

/*****************************************************************************
 * OpenIntf: initialise interface
 *****************************************************************************/
int Open ( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( p_intf->p_sys == NULL )
    {
        return VLC_ENOMEM;
    }

89 90
    p_intf->p_sys->p_motion = motion_create( VLC_OBJECT( p_intf ) );
    if( p_intf->p_sys->p_motion == NULL )
91
    {
92 93
        free( p_intf->p_sys );
        return VLC_EGENERIC;
94 95 96 97 98 99 100 101 102 103 104 105 106 107
    }

    p_intf->pf_run = RunIntf;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * CloseIntf: destroy interface
 *****************************************************************************/
void Close ( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

108
    free( p_intf->p_sys->p_motion );
109 110 111 112 113 114
    free( p_intf->p_sys );
}

/*****************************************************************************
 * RunIntf: main loop
 *****************************************************************************/
115 116
#define LOW_THRESHOLD 800
#define HIGH_THRESHOLD 1000
117 118
static void RunIntf( intf_thread_t *p_intf )
{
119
    int i_x, i_oldx = 0;
120

121
    for( ;; )
122
    {
Clément Stenac's avatar
Clément Stenac committed
123
        const char *psz_filter, *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 129
        msleep( INTF_IDLE_SLEEP );

130
        int canc = vlc_savecancel();
131
        i_x = motion_get_angle( p_intf->p_sys->p_motion );
132 133 134

        if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
        {
135
            b_change = true;
136 137 138 139 140 141
            psz_filter = "transform";
            psz_type = "270";
        }
        else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
                 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
        {
142
            b_change = true;
143 144 145 146 147
            psz_filter = "";
            psz_type = "";
        }
        else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
        {
148
            b_change = true;
149 150 151 152
            psz_filter = "transform";
            psz_type = "90";
        }

153
        if( b_change )
154
        {
155 156 157 158 159
#warning FIXME: refactor this plugin as a video filter!
            input_thread_t *p_input;

            p_input = playlist_CurrentInput( pl_Get( p_intf ) );
            if( p_input )
160
            {
161
                vout_thread_t *p_vout;
162

163 164 165 166
                p_vout = input_GetVout( p_input );
                if( p_vout )
                {
#warning FIXME: transform-type does not exist anymore
167 168
                    var_Create( p_vout, "transform-type", VLC_VAR_STRING );
                    var_SetString( p_vout, "transform-type", psz_type );
169 170 171 172
                    var_SetString( p_vout, "video-filter", psz_filter );
                    vlc_object_release( p_vout );
                }
                vlc_object_release( p_input );
173 174
                i_oldx = i_x;
            }
175
        }
176

177
        vlc_restorecancel( canc );
178 179
    }
}
180 181
#undef LOW_THRESHOLD
#undef HIGH_THRESHOLD
182