motion.c 7.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*****************************************************************************
 * motion.c: control VLC with laptop built-in motion sensors
 *****************************************************************************
 * Copyright (C) 2006 the VideoLAN team
 * $Id$
 *
 * Author: Sam Hocevar <sam@zoy.org>
 *
 * 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
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
31 32
#include <vlc_interface.h>
#include <vlc_vout.h>
33 34 35 36 37 38 39 40 41 42 43 44

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif

/*****************************************************************************
 * intf_sys_t: description and status of interface
 *****************************************************************************/
struct intf_sys_t
{
    enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR } sensor;

45 46 47
    int i_calibrate;

    vlc_bool_t b_use_rotate;
48 49 50 51 52 53 54 55 56 57 58
};

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

static void RunIntf( intf_thread_t *p_intf );
static int GetOrientation( intf_thread_t *p_intf );

59 60
#define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")

61 62 63 64 65 66 67 68
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
    set_shortname( _("motion"));
    set_category( CAT_INTERFACE );
    set_description( _("motion control interface") );

69 70 71
    add_bool( "motion-use-rotate", 0, NULL,
              USE_ROTATE_TEXT, USE_ROTATE_TEXT, VLC_FALSE );

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    set_capability( "interface", 0 );
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * OpenIntf: initialise interface
 *****************************************************************************/
int Open ( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    FILE *f;
    int i_x, i_y;

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

    if( access( "/sys/devices/platform/hdaps/position", R_OK ) == 0 )
    {
        /* IBM HDAPS support */
        f = fopen( "/sys/devices/platform/hdaps/calibrate", "r" );
        if( f )
        {
            i_x = i_y = 0;
            fscanf( f, "(%d,%d)", &i_x, &i_y );
            fclose( f );
            p_intf->p_sys->i_calibrate = i_x;
            p_intf->p_sys->sensor = HDAPS_SENSOR;
        }
        else
        {
            p_intf->p_sys->sensor = NO_SENSOR;
        }
    }
    else if( access( "/sys/devices/ams/x", R_OK ) == 0 )
    {
        /* Apple Motion Sensor support */
        p_intf->p_sys->sensor = AMS_SENSOR;
    }
    else
    {
        /* No motion sensor support */
        p_intf->p_sys->sensor = NO_SENSOR;
    }

    p_intf->pf_run = RunIntf;

121 122 123 124 125 126 127
    p_intf->p_sys->b_use_rotate = config_GetInt( p_intf, "motion-use-rotate" );

    if( p_intf->p_sys->b_use_rotate )
    {
        var_Create( p_intf->p_libvlc, "rotate_angle", VLC_VAR_INTEGER );
    }

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    return VLC_SUCCESS;
}

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

    free( p_intf->p_sys );
}

/*****************************************************************************
 * RunIntf: main loop
 *****************************************************************************/
static void RunIntf( intf_thread_t *p_intf )
{
    int i_x, i_oldx = 0;

148
    while( !intf_ShouldDie( p_intf ) )
149 150 151 152
    {
#define LOW_THRESHOLD 80
#define HIGH_THRESHOLD 100
        vout_thread_t *p_vout;
Clément Stenac's avatar
Clément Stenac committed
153
        const char *psz_filter, *psz_type;
154 155 156 157 158 159 160
        vlc_bool_t b_change = VLC_FALSE;

        /* Wait a bit, get orientation, change filter if necessary */
        msleep( INTF_IDLE_SLEEP );

        i_x = GetOrientation( p_intf );

161 162 163 164 165 166 167 168 169 170 171
        if( p_intf->p_sys->b_use_rotate )
        {
            if( i_oldx != i_x )
            {
                var_SetInteger( p_intf->p_libvlc, "rotate_angle",
                            ((360+i_x/2)%360) );
                i_oldx = i_x;
            }
            continue;
        }

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
        {
            b_change = VLC_TRUE;
            psz_filter = "transform";
            psz_type = "270";
        }
        else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
                 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
        {
            b_change = VLC_TRUE;
            psz_filter = "";
            psz_type = "";
        }
        else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
        {
            b_change = VLC_TRUE;
            psz_filter = "transform";
            psz_type = "90";
        }

        if( !b_change )
        {
            continue;
        }

        p_vout = (vout_thread_t *)
            vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
        if( !p_vout )
        {
            continue;
        }

        config_PutPsz( p_vout, "transform-type", psz_type );
        var_SetString( p_vout, "vout-filter", psz_filter );
        vlc_object_release( p_vout );

        i_oldx = i_x;
    }
}

/*****************************************************************************
 * GetOrientation: get laptop orientation, range -180 / +180
 *****************************************************************************/
static int GetOrientation( intf_thread_t *p_intf )
{
    FILE *f;
    int i_x, i_y;

    switch( p_intf->p_sys->sensor )
    {
    case HDAPS_SENSOR:
        f = fopen( "/sys/devices/platform/hdaps/position", "r" );
        if( !f )
        {
            return 0;
        }

        i_x = i_y = 0;
        fscanf( f, "(%d,%d)", &i_x, &i_y );
        fclose( f );

        return i_x - p_intf->p_sys->i_calibrate;

    case AMS_SENSOR:
        f = fopen( "/sys/devices/ams/x", "r" );
        if( !f )
        {
            return 0;
        }

        fscanf( f, "%d", &i_x);
        fclose( f );

        return - i_x * 3; /* FIXME: arbitrary */

    default:
        return 0;
    }
}