intf.c 12.4 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3
/*****************************************************************************
 * intf.c: Video CD interface to handle user interaction and still time
 *****************************************************************************
4
 * Copyright (C) 2002,2003 the VideoLAN team
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6
 *
7
 * Author: Rocky Bernstein <rocky@panix.com>
8
 *   from DVD code by Stéphane Borel <stef@via.ecp.fr>
Gildas Bazin's avatar
 
Gildas Bazin committed
9 10 11 12 13
 *
 * 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.
14
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
15 16 17 18 19 20 21
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
 
Gildas Bazin committed
23 24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/intf.h>
30
#include <vlc/input.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
31

32
#include "vlc_keys.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
33 34

#include "vcd.h"
35
#include "vcdplayer.h"
36
#include "intf.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int  InitThread     ( intf_thread_t *p_intf );
static int  KeyEvent       ( vlc_object_t *, char const *,
                             vlc_value_t, vlc_value_t, void * );

/* Exported functions */
static void RunIntf        ( intf_thread_t *p_intf );

/*****************************************************************************
 * OpenIntf: initialize dummy interface
 *****************************************************************************/
51
int VCDOpenIntf ( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
52 53 54
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

55 56
    msg_Dbg( p_intf, "VCDOpenIntf" );

Gildas Bazin's avatar
 
Gildas Bazin committed
57 58 59 60
    /* Allocate instance and initialize some members */
    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
    if( p_intf->p_sys == NULL )
    {
61
        return( VLC_EGENERIC );
Gildas Bazin's avatar
 
Gildas Bazin committed
62 63 64 65
    };

    p_intf->pf_run = RunIntf;

66
    var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
67
    p_intf->p_sys->m_still_time = 0;
68
    p_intf->p_sys->b_infinite_still = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
69 70
    p_intf->p_sys->b_still = 0;

71
    return( VLC_SUCCESS );
Gildas Bazin's avatar
 
Gildas Bazin committed
72 73 74 75 76
}

/*****************************************************************************
 * CloseIntf: destroy dummy interface
 *****************************************************************************/
77
void VCDCloseIntf ( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
78 79
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
80
    var_DelCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
81 82 83 84 85 86 87 88 89

    /* Destroy structure */
    free( p_intf->p_sys );
}


/*****************************************************************************
 * RunIntf: main loop
 *****************************************************************************/
90 91
static void 
RunIntf( intf_thread_t *p_intf )
Gildas Bazin's avatar
 
Gildas Bazin committed
92
{
93
    vlc_object_t      * p_vout = NULL;
94 95
    mtime_t             mtime = 0;
    mtime_t             mlast = 0;
96
    vcdplayer_t       * p_vcdplayer;
97
    input_thread_t    * p_input;
98
    access_t          * p_access;
99

100 101
    /* What you add to the last input number entry. It accumulates all of
       the 10_ADD keypresses */
102 103
    int number_addend = 0;

Gildas Bazin's avatar
 
Gildas Bazin committed
104 105 106 107 108
    if( InitThread( p_intf ) < 0 )
    {
        msg_Err( p_intf, "can't initialize intf" );
        return;
    }
109 110

    p_input = p_intf->p_sys->p_input;
111

112
    while ( !p_intf->p_sys->p_vcdplayer )
113 114 115 116
    {
        msleep( INTF_IDLE_SLEEP );
    }
    
117 118
    p_vcdplayer = p_intf->p_sys->p_vcdplayer;
    p_access    = p_vcdplayer->p_access;
119 120

    dbg_print( INPUT_DBG_CALL, "intf initialized" );
Gildas Bazin's avatar
 
Gildas Bazin committed
121 122 123 124 125 126

    /* Main loop */
    while( !p_intf->b_die )
    {
      vlc_mutex_lock( &p_intf->change_lock );

127
        /*
128
         * Have we timed-out in showing a still frame?
129
         */
130
        if( p_intf->p_sys->b_still && !p_intf->p_sys->b_infinite_still )
131 132 133
        {
            if( p_intf->p_sys->m_still_time > 0 )
            {
134
                /* Update remaining still time */
135
                dbg_print(INPUT_DBG_STILL, "updating still time");
136 137 138 139 140 141 142 143 144 145
                mtime = mdate();
                if( mlast )
                {
                    p_intf->p_sys->m_still_time -= mtime - mlast;
                }

                mlast = mtime;
            }
            else
            {
146
                /* Still time has elapsed; set to continue playing. */
147
                dbg_print(INPUT_DBG_STILL, "wait time done - setting play");
148
                var_SetInteger( p_intf->p_sys->p_input, "state", PLAYING_S );
149 150 151 152 153 154
                p_intf->p_sys->m_still_time = 0;
                p_intf->p_sys->b_still = 0;
                mlast = 0;
            }
        }

155
      /*
156
       * Do we have a keyboard event?
157 158
       */
      if( p_vout && p_intf->p_sys->b_key_pressed )
Gildas Bazin's avatar
 
Gildas Bazin committed
159
        {
160 161 162 163 164
          vlc_value_t val;
          int i, i_action = -1;
          struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;

          p_intf->p_sys->b_key_pressed = VLC_FALSE;
165

166 167
          /* Find action triggered by hotkey (if any) */
          var_Get( p_intf->p_vlc, "key-pressed", &val );
168

169
          dbg_print( INPUT_DBG_EVENT, "Key pressed %d", val.i_int );
170

171
          for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
172
            {
173
              if( p_hotkeys[i].i_key == val.i_int )
174
                {
175
                  i_action = p_hotkeys[i].i_action;
176 177
                }
            }
178 179 180 181 182 183 184 185

          if( i_action != -1) {
            switch (i_action) {

            case ACTIONID_NAV_LEFT:
              dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_LEFT - prev (%d)",
                         number_addend );
              do {
186
                vcdplayer_play_prev( p_access );
187 188 189 190 191 192 193
              }        while (number_addend-- > 0);
              break;

            case ACTIONID_NAV_RIGHT:
              dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_RIGHT - next (%d)",
                         number_addend );
              do {
194
                vcdplayer_play_next( p_access );
195 196 197 198 199 200
              } while (number_addend-- > 0);
              break;

            case ACTIONID_NAV_UP:
              dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_UP - return" );
              do {
201
                vcdplayer_play_return( p_access );
202 203 204 205 206
              } while (number_addend-- > 0);
              break;

            case ACTIONID_NAV_DOWN:
              dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_DOWN - default"  );
207
              vcdplayer_play_default( p_access );
208 209 210 211 212
              break;

            case ACTIONID_NAV_ACTIVATE:
              {
                vcdinfo_itemid_t itemid;
213
                itemid.type=p_vcdplayer->play_item.type;
214 215 216

                dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_ACTIVATE" );

217 218 219 220
                if ( vcdplayer_pbc_is_on( p_vcdplayer ) 
		     && number_addend != 0 ) {
                  lid_t next_num=vcdinfo_selection_get_lid(p_vcdplayer->vcd,
                                                           p_vcdplayer->i_lid,
221 222 223 224
                                                           number_addend);
                  if (VCDINFO_INVALID_LID != next_num) {
                    itemid.num  = next_num;
                    itemid.type = VCDINFO_ITEM_TYPE_LID;
225
                    vcdplayer_play( p_access, itemid );
226 227 228
                  }
                } else {
                  itemid.num = number_addend;
229
                  vcdplayer_play( p_access, itemid );
230 231 232 233 234 235 236 237 238 239 240 241
                }
                break;
              }
            }
            number_addend = 0;

            /* Any keypress gets rid of still frame waiting.
               FIXME - should handle just the ones that cause an action.
            */
            if( p_intf->p_sys->b_still )
              {
                dbg_print(INPUT_DBG_STILL, "Playing still after activate");
242
                var_SetInteger( p_intf->p_sys->p_input, "state", PLAYING_S );
243
                p_intf->p_sys->b_still = 0;
244
                p_intf->p_sys->b_infinite_still = 0;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
                p_intf->p_sys->m_still_time = 0;
              }

          } else {
            unsigned int digit_entered=0;

            switch (val.i_int) {
            case '9':
              digit_entered++;
            case '8':
              digit_entered++;
            case '7':
              digit_entered++;
            case '6':
              digit_entered++;
            case '5':
              digit_entered++;
            case '4':
              digit_entered++;
            case '3':
              digit_entered++;
            case '2':
              digit_entered++;
            case '1':
              digit_entered++;
            case '0':
              {
                number_addend *= 10;
                number_addend += digit_entered;
                dbg_print( INPUT_DBG_EVENT,
                           "Added %d. Number is now: %d\n",
                           digit_entered, number_addend);
                break;
              }
            }
          }
Gildas Bazin's avatar
 
Gildas Bazin committed
281 282
        }

283

284
      vlc_mutex_unlock( &p_intf->change_lock );
285

286
      if( p_vout == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
287
        {
288 289 290
          p_vout = vlc_object_find( p_intf->p_sys->p_input,
                                    VLC_OBJECT_VOUT, FIND_CHILD );
          if( p_vout )
Gildas Bazin's avatar
 
Gildas Bazin committed
291
            {
292
              var_AddCallback( p_vout, "key-pressed", KeyEvent, p_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
293 294
            }
        }
295 296


297 298 299
      /* Wait a bit */
      msleep( INTF_IDLE_SLEEP );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
300

301 302
    if( p_vout )
    {
303
        var_DelCallback( p_vout, "key-pressed", KeyEvent, p_intf );
304
        vlc_object_release( p_vout );
Gildas Bazin's avatar
 
Gildas Bazin committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    }

    vlc_object_release( p_intf->p_sys->p_input );
}

/*****************************************************************************
 * InitThread:
 *****************************************************************************/
static int InitThread( intf_thread_t * p_intf )
{
    /* We might need some locking here */
    if( !p_intf->b_die )
    {
        input_thread_t * p_input;

        p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );

        /* Maybe the input just died */
        if( p_input == NULL )
        {
            return VLC_EGENERIC;
        }

        vlc_mutex_lock( &p_intf->change_lock );

330 331
        p_intf->p_sys->p_input     = p_input;
        p_intf->p_sys->p_vcdplayer = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
332

333
        p_intf->p_sys->b_move  = VLC_FALSE;
Gildas Bazin's avatar
 
Gildas Bazin committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        p_intf->p_sys->b_click = VLC_FALSE;
        p_intf->p_sys->b_key_pressed = VLC_FALSE;

        vlc_mutex_unlock( &p_intf->change_lock );

        return VLC_SUCCESS;
    }
    else
    {
        return VLC_EGENERIC;
    }
}

/*****************************************************************************
 * KeyEvent: callback for keyboard events
 *****************************************************************************/
static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_data;
    vlc_mutex_lock( &p_intf->change_lock );

    p_intf->p_sys->b_key_pressed = VLC_TRUE;
357

Gildas Bazin's avatar
 
Gildas Bazin committed
358 359 360 361 362 363
    vlc_mutex_unlock( &p_intf->change_lock );

    return VLC_SUCCESS;
}

/*****************************************************************************
364
 * vcdIntfStillTime: function provided to demux plugin to request
Gildas Bazin's avatar
 
Gildas Bazin committed
365 366
 * still images
 *****************************************************************************/
367
int vcdIntfStillTime( intf_thread_t *p_intf, uint8_t i_sec )
Gildas Bazin's avatar
 
Gildas Bazin committed
368 369 370
{
    vlc_mutex_lock( &p_intf->change_lock );

371 372
    p_intf->p_sys->b_still = 1;
    if( 255 == i_sec )
Gildas Bazin's avatar
 
Gildas Bazin committed
373
    {
374
        p_intf->p_sys->b_infinite_still = VLC_TRUE;
Gildas Bazin's avatar
 
Gildas Bazin committed
375
    }
376
    else 
Gildas Bazin's avatar
 
Gildas Bazin committed
377
    {
378
        p_intf->p_sys->m_still_time = MILLISECONDS_PER_SEC * i_sec;
Gildas Bazin's avatar
 
Gildas Bazin committed
379 380 381 382 383 384 385 386 387 388 389 390 391
    }
    vlc_mutex_unlock( &p_intf->change_lock );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * vcdIntfStillTime: function provided to reset still image
 *****************************************************************************/
int vcdIntfResetStillTime( intf_thread_t *p_intf )
{
    vlc_mutex_lock( &p_intf->change_lock );
    p_intf->p_sys->m_still_time = 0;
392
    var_SetInteger( p_intf->p_sys->p_input, "state", PLAYING_S );
Gildas Bazin's avatar
 
Gildas Bazin committed
393 394 395 396
    vlc_mutex_unlock( &p_intf->change_lock );

    return VLC_SUCCESS;
}