motion.c 9.62 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 34
#include <math.h>

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

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

44 45 46 47
#ifdef __APPLE__
#include "unimotion.h"
#endif

48 49 50 51 52
/*****************************************************************************
 * intf_sys_t: description and status of interface
 *****************************************************************************/
struct intf_sys_t
{
53 54
    enum { NO_SENSOR, HDAPS_SENSOR, AMS_SENSOR, APPLESMC_SENSOR,
           UNIMOTION_SENSOR } sensor;
Jérome Decoodt's avatar
Jérome Decoodt committed
55
#ifdef __APPLE__
56
    enum sms_hardware unimotion_hw;
Jérome Decoodt's avatar
Jérome Decoodt committed
57
#endif
58 59
    int i_calibrate;

60
    bool b_use_rotate;
61 62 63 64 65 66 67 68 69 70 71
};

/*****************************************************************************
 * 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 );

72 73
#define USE_ROTATE_TEXT N_("Use the rotate video filter instead of transform")

74 75 76
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
77 78 79
vlc_module_begin ()
    set_shortname( N_("motion"))
    set_category( CAT_INTERFACE )
80
    set_subcategory( SUBCAT_INTERFACE_CONTROL )
81
    set_description( N_("motion control interface") )
82 83
    set_help( N_("Use HDAPS, AMS, APPLESMC or UNIMOTION motion sensors " \
                 "to rotate the video") )
84

Rémi Duraffort's avatar
Rémi Duraffort committed
85
    add_bool( "motion-use-rotate", false, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
86
              USE_ROTATE_TEXT, USE_ROTATE_TEXT, false )
87

88 89 90
    set_capability( "interface", 0 )
    set_callbacks( Open, Close )
vlc_module_end ()
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 121 122 123 124 125 126 127 128

/*****************************************************************************
 * 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;
    }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    else if( access( "/sys/devices/applesmc.768/position", R_OK ) == 0 )
    {
        /* Apple SMC (newer macbooks) */
        /* Should be factorised with HDAPS */
        f = fopen( "/sys/devices/applesmc.768/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 = APPLESMC_SENSOR;
        }
        else
        {
            p_intf->p_sys->sensor = NO_SENSOR;
        }
    }
147
#ifdef __APPLE__
148
    else if((p_intf->p_sys->unimotion_hw = detect_sms()))
149 150
        p_intf->p_sys->sensor = UNIMOTION_SENSOR;
#endif
151 152 153 154 155 156 157 158
    else
    {
        /* No motion sensor support */
        p_intf->p_sys->sensor = NO_SENSOR;
    }

    p_intf->pf_run = RunIntf;

159 160
    p_intf->p_sys->b_use_rotate =
        var_InheritInteger( p_intf, "motion-use-rotate" );
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    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
 *****************************************************************************/
178 179 180
#define FILTER_LENGTH 16
#define LOW_THRESHOLD 800
#define HIGH_THRESHOLD 1000
181 182
static void RunIntf( intf_thread_t *p_intf )
{
183 184 185
    int i_x, i_oldx = 0, i_sum = 0, i = 0;
    int p_oldx[FILTER_LENGTH];
    memset( p_oldx, 0, FILTER_LENGTH * sizeof( int ) );
186

187
    for( ;; )
188 189
    {
        vout_thread_t *p_vout;
Clément Stenac's avatar
Clément Stenac committed
190
        const char *psz_filter, *psz_type;
191
        bool b_change = false;
192 193 194 195

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

196
        int canc = vlc_savecancel();
197
        i_x = GetOrientation( p_intf );
198 199 200 201
        i_sum += i_x - p_oldx[i];
        p_oldx[i++] = i_x;
        if( i == FILTER_LENGTH ) i = 0;
        i_x = i_sum / FILTER_LENGTH;
202

203 204 205 206
        if( p_intf->p_sys->b_use_rotate )
        {
            if( i_oldx != i_x )
            {
207 208 209 210 211
                /* TODO: cache object pointer */
                vlc_object_t *p_obj =
                vlc_object_find_name( p_intf->p_libvlc, "rotate", FIND_CHILD );
                if( p_obj )
                {
212 213
                    var_SetInteger( p_obj, "rotate-deciangle",
                            ((3600+i_x/2)%3600) );
214 215 216
                    i_oldx = i_x;
                    vlc_object_release( p_obj );
                }
217
            }
218
            goto loop;
219 220
        }

221 222
        if( i_x < -HIGH_THRESHOLD && i_oldx > -LOW_THRESHOLD )
        {
223
            b_change = true;
224 225 226 227 228 229
            psz_filter = "transform";
            psz_type = "270";
        }
        else if( ( i_x > -LOW_THRESHOLD && i_oldx < -HIGH_THRESHOLD )
                 || ( i_x < LOW_THRESHOLD && i_oldx > HIGH_THRESHOLD ) )
        {
230
            b_change = true;
231 232 233 234 235
            psz_filter = "";
            psz_type = "";
        }
        else if( i_x > HIGH_THRESHOLD && i_oldx < LOW_THRESHOLD )
        {
236
            b_change = true;
237 238 239 240
            psz_filter = "transform";
            psz_type = "90";
        }

241
        if( b_change )
242
        {
243 244 245 246 247 248 249
            p_vout = (vout_thread_t *)
                vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
            if( p_vout )
            {
                config_PutPsz( p_vout, "transform-type", psz_type );
                var_SetString( p_vout, "vout-filter", psz_filter );
                vlc_object_release( p_vout );
250

251 252
                i_oldx = i_x;
            }
253
        }
254 255
loop:
        vlc_restorecancel( canc );
256 257
    }
}
258 259 260
#undef FILTER_LENGTH
#undef LOW_THRESHOLD
#undef HIGH_THRESHOLD
261 262

/*****************************************************************************
263
 * GetOrientation: get laptop orientation, range -1800 / +1800
264 265 266 267
 *****************************************************************************/
static int GetOrientation( intf_thread_t *p_intf )
{
    FILE *f;
268 269
    int i_x, i_y, i_z = 0;

270 271 272 273 274 275 276 277 278 279 280 281 282
    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 );

283
        return ( i_x - p_intf->p_sys->i_calibrate ) * 10;
284 285 286 287 288 289 290 291 292 293 294

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

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

295
        return - i_x * 30; /* FIXME: arbitrary */
296 297 298 299 300 301 302 303 304 305 306 307 308 309

    case APPLESMC_SENSOR:
        f = fopen( "/sys/devices/applesmc.768/position", "r" );
        if( !f )
        {
            return 0;
        }

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

        return ( i_x - p_intf->p_sys->i_calibrate ) * 10;

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
#ifdef __APPLE__
    case UNIMOTION_SENSOR:
        if( read_sms_raw( p_intf->p_sys->unimotion_hw, &i_x, &i_y, &i_z ) )
        {
            double d_norm = sqrt( i_x*i_x+i_z*i_z );
            if( d_norm < 100 )
                return 0;
            double d_x = i_x / d_norm;
            if( i_z > 0 )
                return -asin(d_x)*3600/3.141;
            else
                return 3600 + asin(d_x)*3600/3.141;
        }
        else
            return 0;
Jérome Decoodt's avatar
Jérome Decoodt committed
325
#endif
326 327 328 329 330
    default:
        return 0;
    }
}