summaryrefslogtreecommitdiff
path: root/src/input/vcd/vcdio.c
blob: 387397cf487f9ef67be1c4cb59d0f2425a680572 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
  $Id: vcdio.c,v 1.9 2007/03/23 21:47:31 dsalt Exp $
 
  Copyright (C) 2002, 2003, 2004, 2005 Rocky Bernstein <rocky@panix.com>
  
  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* Standard includes */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <errno.h>

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

#ifdef HAVE_VCDNAV
#include <libvcd/types.h>
#include <libvcd/files.h>
#include <cdio/iso9660.h>
#else
#include "libvcd/types.h"
#include "libvcd/files.h"
#include "cdio/iso9660.h"
#endif

#include "vcdplayer.h"
#include "vcdio.h"

#define LOG_ERR(p_vcdplayer, s, args...) \
       if (p_vcdplayer != NULL && p_vcdplayer->log_err != NULL) \
          p_vcdplayer->log_err("%s:  "s, __func__ , ##args)

#define FREE_AND_NULL(ptr) if (NULL != ptr) free(ptr); ptr = NULL;

/*! Closes VCD device specified via "this", and also wipes memory of it 
   from it inside "this". */
int
vcdio_close(vcdplayer_t *p_vcdplayer) 
{
  p_vcdplayer->b_opened = false;

  FREE_AND_NULL(p_vcdplayer->psz_source);
  FREE_AND_NULL(p_vcdplayer->track);
  FREE_AND_NULL(p_vcdplayer->segment);
  FREE_AND_NULL(p_vcdplayer->entry); 
  
  return vcdinfo_close(p_vcdplayer->vcd);
}


/*! Opens VCD device and initializes things.

   - do nothing if the device had already been open and is the same device. 
   - if the device had been open and is a different, close it before trying
     to open new device. 
*/
bool
vcdio_open(vcdplayer_t *p_vcdplayer, char *intended_vcd_device) 
{
  vcdinfo_obj_t *p_vcdinfo = p_vcdplayer->vcd;
  unsigned int i;

  dbg_print(INPUT_DBG_CALL, "called with %s\n", intended_vcd_device);

  if ( p_vcdplayer->b_opened ) {
    if ( strcmp(intended_vcd_device, p_vcdplayer->psz_source)==0 ) {
      /* Already open and the same device, so do nothing */
      return true;
    } else {
      /* Changing VCD device */
      vcdio_close(p_vcdplayer);
    }
  }

  switch ( vcdinfo_open(&p_vcdplayer->vcd, &intended_vcd_device,
                        DRIVER_UNKNOWN, NULL))
  {
    case VCDINFO_OPEN_ERROR:
      /* Failed to open the device => return failure */
      return false;

    case VCDINFO_OPEN_VCD:
      /* Opened the device, and it's a VCD => proceed */
      break;

    default:
      /* Opened the device, but it's not a VCD => is closed, return failure */
      return false;
  }

  p_vcdinfo = p_vcdplayer->vcd;

  p_vcdplayer->psz_source = strdup(intended_vcd_device);
  p_vcdplayer->b_opened   = true;
  p_vcdplayer->i_lids     = vcdinfo_get_num_LIDs(p_vcdinfo);
  p_vcdplayer->vcd_format = vcdinfo_get_format_version(p_vcdinfo);
  p_vcdplayer->i_still    = 0;

  if (vcdinfo_read_psd (p_vcdinfo)) {

    vcdinfo_visit_lot (p_vcdinfo, false);

    if (VCD_TYPE_VCD2 == p_vcdplayer->vcd_format &&
        vcdinfo_get_psd_x_size(p_vcdinfo)) {
        vcdinfo_visit_lot (p_vcdinfo, true);
    }
  }

  /* 
     Save summary info on tracks, segments and entries... 
   */

  if ( 0 < (p_vcdplayer->i_tracks = vcdinfo_get_num_tracks(p_vcdinfo)) ) {
    p_vcdplayer->track = (vcdplayer_play_item_info_t *) 
      calloc(p_vcdplayer->i_tracks, sizeof(vcdplayer_play_item_info_t));
    
    for (i=0; i<p_vcdplayer->i_tracks; i++) { 
      track_t i_track=i+1;
      p_vcdplayer->track[i].size 
        = vcdinfo_get_track_sect_count(p_vcdinfo, i_track);
      p_vcdplayer->track[i].start_LSN 
        = vcdinfo_get_track_lsn(p_vcdinfo, i_track);
    }
  } else 
    p_vcdplayer->track = NULL;
    
  if ( 0 < (p_vcdplayer->i_entries = vcdinfo_get_num_entries(p_vcdinfo)) ) {
    p_vcdplayer->entry = (vcdplayer_play_item_info_t *) 
      calloc(p_vcdplayer->i_entries, sizeof(vcdplayer_play_item_info_t));

    for (i=0; i<p_vcdplayer->i_entries; i++) { 
      p_vcdplayer->entry[i].size
        = vcdinfo_get_entry_sect_count(p_vcdinfo, i);
      p_vcdplayer->entry[i].start_LSN 
        = vcdinfo_get_entry_lsn(p_vcdinfo, i);
    }
  } else 
    p_vcdplayer->entry = NULL;
  
  if ( 0 < (p_vcdplayer->i_segments = vcdinfo_get_num_segments(p_vcdinfo)) ) {
    p_vcdplayer->segment = (vcdplayer_play_item_info_t *) 
      calloc(p_vcdplayer->i_segments,  sizeof(vcdplayer_play_item_info_t));
    
    for (i=0; i<p_vcdplayer->i_segments; i++) { 
      p_vcdplayer->segment[i].size 
        = vcdinfo_get_seg_sector_count(p_vcdinfo, i);
      p_vcdplayer->segment[i].start_LSN 
        = vcdinfo_get_seg_lsn(p_vcdinfo, i);
    }
  } else 
    p_vcdplayer->segment = NULL;
  
  return true;
}

/*!
  seek position, return new position 

  if seeking failed, -1 is returned
*/
off_t 
vcdio_seek (vcdplayer_t *p_vcdplayer, off_t offset, int origin) 
{

  switch (origin) {
  case SEEK_SET:
    {
      lsn_t old_lsn = p_vcdplayer->i_lsn;
      p_vcdplayer->i_lsn = p_vcdplayer->origin_lsn + (offset / M2F2_SECTOR_SIZE);
      
      dbg_print(INPUT_DBG_SEEK_SET, "seek_set to %ld => %u (start is %u)\n", 
		(long int) offset, p_vcdplayer->i_lsn, p_vcdplayer->origin_lsn);

      /* Seek was successful. Invalidate entry location by setting
         entry number back to 1. Over time it will adjust upward 
         to the correct value. */
      if ( !vcdplayer_pbc_is_on(p_vcdplayer) 
           && p_vcdplayer->play_item.type != VCDINFO_ITEM_TYPE_TRACK 
           && p_vcdplayer->i_lsn < old_lsn) {
        dbg_print(INPUT_DBG_SEEK_SET, "seek_set entry backwards\n");
        p_vcdplayer->next_entry = 1;
      }
      break;
    }
    
  case SEEK_CUR: 
    {
      off_t diff;
      if (offset) {
        LOG_ERR(p_vcdplayer, "%s: %d\n",
                _("SEEK_CUR not implemented for non-zero offset"), 
                (int) offset);
        return (off_t) -1;
      }
      
      if (p_vcdplayer->slider_length == VCDPLAYER_SLIDER_LENGTH_TRACK) {
        diff = p_vcdplayer->i_lsn - p_vcdplayer->track_lsn;
        dbg_print(INPUT_DBG_SEEK_CUR, 
                  "current pos: %u, track diff %ld\n", 
                  p_vcdplayer->i_lsn, (long int) diff);
      } else {
        diff = p_vcdplayer->i_lsn - p_vcdplayer->origin_lsn;
        dbg_print(INPUT_DBG_SEEK_CUR, 
                  "current pos: %u, entry diff %ld\n", 
                  p_vcdplayer->i_lsn, (long int) diff);
      }
      
      if (diff < 0) {
        dbg_print(INPUT_DBG_SEEK_CUR, "Error: diff < 0\n");
        return (off_t) 0;
      } else {
        return (off_t)diff * M2F2_SECTOR_SIZE;
      }
      
      break;
    }
    
  case SEEK_END:
    LOG_ERR(p_vcdplayer, "%s\n", _("SEEK_END not implemented yet."));
    return (off_t) -1;
  default:
    LOG_ERR(p_vcdplayer, "%s %d\n", _("seek not implemented yet for"),
	     origin);
    return (off_t) -1;
  }

  return offset ; /* FIXME */
}

/* 
 * Local variables:
 *  c-file-style: "gnu"
 *  tab-width: 8
 *  indent-tabs-mode: nil
 * End:
 */