simple.c 18 KB
Newer Older
1
2
3
/*****************************************************************************
 * simple.c - The OSD Menu simple parser code.
 *****************************************************************************
4
 * Copyright (C) 2005-2008 M2X
Christophe Mutricy's avatar
Christophe Mutricy committed
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 * Authors: Jean-Paul Saman
 *
 * 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
 *****************************************************************************/

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

32
#include <vlc_common.h>
33
#include <vlc_osd.h>
34
#include <vlc_fs.h>
35

36
37
#include <limits.h>

38
39
40
41
#include "osd_menu.h"

int osd_parser_simpleOpen( vlc_object_t *p_this );

42
/*****************************************************************************
43
 * Simple parser open function
44
 *****************************************************************************/
45
int osd_parser_simpleOpen( vlc_object_t *p_this )
46
{
47
    osd_menu_t     *p_menu = (osd_menu_t *) p_this;
48
49
50
51
52
53
    osd_button_t   *p_current = NULL; /* button currently processed */
    osd_button_t   *p_prev = NULL;    /* previous processed button */

    FILE       *fd = NULL;
    int        result = 0;

54
55
    if( !p_menu ) return VLC_ENOOBJ;

56
    msg_Dbg( p_this, "opening osdmenu definition file %s", p_menu->psz_file );
57
    fd = vlc_fopen( p_menu->psz_file, "r" );
58
59
    if( !fd )
    {
60
61
        msg_Err( p_this, "failed to open osdmenu definition file %s",
                p_menu->psz_file );
62
63
64
65
66
67
68
69
        return VLC_EGENERIC;
    }

    /* Read first line */
    if( !feof( fd ) )
    {
        char action[25] = "";
        char cmd[25] = "";
70
        char path[PATH_MAX] = "";
71
72
73
74
        char *psz_path = NULL;
        size_t i_len = 0;
        long pos = 0;

ivoire's avatar
ivoire committed
75
        result = fscanf(fd, "%24s %255s", action, path );
76
77

        /* override images path ? */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
78
        psz_path = var_InheritString( p_this, "osdmenu-file-path" );
79
80
        if( psz_path )
        {
ivoire's avatar
ivoire committed
81
            /* psz_path is not null and therefor path cannot be NULL
82
83
             * it might be null terminated.
             */
ivoire's avatar
ivoire committed
84
            strncpy( path, psz_path, PATH_MAX );
85
86
87
88
            free( psz_path );
            psz_path = NULL;
        }
        /* NULL terminate before asking the length of path[] */
89
        path[PATH_MAX-1] = '\0';
ivoire's avatar
ivoire committed
90
        i_len = strlen(path);
91
92
93
94
95
        /* Protect against buffer overflow:
         * max index is PATH_MAX-1 and we increment by 1 after
         * so PATH_MAX-2 is the bigest we can have */
        if( i_len > PATH_MAX - 2 )
            i_len = PATH_MAX - 2;
96
97
98
99
100
101
102
103
104
105
#if defined(WIN32) || defined(UNDER_CE)
        if( (i_len > 0) && path[i_len] != '\\' )
            path[i_len] = '\\';
#else
        if( (i_len > 0) && path[i_len] != '/' )
            path[i_len] = '/';
#endif
        path[i_len+1] = '\0';
        if( result == 0 || result == EOF )
            goto error;
ivoire's avatar
ivoire committed
106
        msg_Dbg( p_this, "osdmenu dir %s", path );
107
108

        if( i_len == 0 )
109
            p_menu = osd_MenuNew( p_menu, NULL, 0, 0 );
110
        else
ivoire's avatar
ivoire committed
111
            p_menu = osd_MenuNew( p_menu, path, 0, 0 );
112
113
114
115

        /* Peek for 'style' argument */
        pos = ftell( fd );
        if( pos < 0 )
ivoire's avatar
ivoire committed
116
            goto error;
117

ivoire's avatar
ivoire committed
118
        result = fscanf(fd, "%24s %24s", cmd, action );
119
120
121
        if( result == 0 || result == EOF )
            goto error;

ivoire's avatar
ivoire committed
122
123
        msg_Dbg( p_this, "osdmenu %s %s", cmd, action );
        if( strncmp( cmd, "style", 5 ) == 0 )
124
        {
ivoire's avatar
ivoire committed
125
            if( strncmp( action, "default", 7) == 0 )
126
            {
127
                p_menu->i_style = OSD_MENU_STYLE_SIMPLE;
128
            }
ivoire's avatar
ivoire committed
129
            else if( strncmp( action, "concat", 6) == 0 )
130
            {
131
                p_menu->i_style = OSD_MENU_STYLE_CONCAT;
132
133
134
135
136
137
138
139
140
141
            }
        }
        else
        {
            result = fseek( fd, pos, SEEK_SET );
            if( result < 0 )
                goto error;
        }
    }

142
    if( !p_menu )
143
144
145
146
147
148
149
150
151
152
153
154
        goto error;

    /* read successive lines */
    while( !feof( fd ) )
    {
        osd_state_t   *p_state_current = NULL; /* button state currently processed */
        osd_state_t   *p_state_prev = NULL;    /* previous state processed button */

        char cmd[25] = "";
        char action[25] = "";
        char state[25]  = "";
        char file[256]  = "";
155
        char path[PATH_MAX]  = "";
156
157
158
        int  i_x = 0;
        int  i_y = 0;

ivoire's avatar
ivoire committed
159
        result = fscanf( fd, "%24s %24s (%d,%d)", cmd, action, &i_x, &i_y );
160
161
162
163
        if( result == 0 )
            goto error;
        if( strncmp( &cmd[0], "action", 6 ) != 0 )
            break;
ivoire's avatar
ivoire committed
164
        msg_Dbg( p_this, " + %s hotkey=%s (%d,%d)", cmd, action, i_x, i_y );
165
166

        p_prev = p_current;
ivoire's avatar
ivoire committed
167
        p_current = osd_ButtonNew( action, i_x, i_y );
168
169
170
171
172
173
        if( !p_current )
            goto error;

        if( p_prev )
            p_prev->p_next = p_current;
        else
174
            p_menu->p_button = p_current;
175
176
177
178
179
180
181
        p_current->p_prev = p_prev;

        /* parse all states */
        while( !feof( fd ) )
        {
            char type[25] = "";

ivoire's avatar
ivoire committed
182
            result = fscanf( fd, "\t%24s", state );
183
184
185
186
            if( result == 0 )
                goto error;

            /* FIXME: We only parse one level deep now */
ivoire's avatar
ivoire committed
187
            if( strncmp( state, "action", 6 ) == 0 )
188
189
190
            {
                osd_button_t   *p_up = NULL;

ivoire's avatar
ivoire committed
191
                result = fscanf( fd, "%24s (%d,%d)", action, &i_x, &i_y );
192
193
194
                if( result == 0 )
                    goto error;
                /* create new button */
ivoire's avatar
ivoire committed
195
                p_up = osd_ButtonNew( action, i_x, i_y );
196
197
198
199
200
                if( !p_up )
                    goto error;
                /* Link to list */
                p_up->p_down = p_current;
                p_current->p_up = p_up;
ivoire's avatar
ivoire committed
201
                msg_Dbg( p_this, " + (menu up) hotkey=%s (%d,%d)", action, i_x, i_y );
202
                /* Parse type state */
ivoire's avatar
ivoire committed
203
                result = fscanf( fd, "\t%24s %24s", cmd, type );
204
205
                if( result == 0 )
                    goto error;
ivoire's avatar
ivoire committed
206
                if( strncmp( cmd, "type", 4 ) == 0 )
207
                {
ivoire's avatar
ivoire committed
208
                    if( strncmp( type, "volume", 6 ) == 0 )
209
                    {
210
                        p_menu->p_state->p_volume = p_up;
ivoire's avatar
ivoire committed
211
                        msg_Dbg( p_this, " + type=%s", type );
212
213
214
                    }
                }
                /* Parse range state */
ivoire's avatar
ivoire committed
215
                result = fscanf( fd, "\t%24s", state );
216
217
218
                if( result == 0 )
                    goto error;
                /* Parse the range state */
ivoire's avatar
ivoire committed
219
                if( strncmp( state, "range", 5 ) == 0 )
220
221
222
223
224
                {
                    osd_state_t   *p_range_current = NULL; /* range state currently processed */
                    osd_state_t   *p_range_prev = NULL;    /* previous state processed range */
                    int i_index = 0;

225
                    p_up->b_range = true;
226

ivoire's avatar
ivoire committed
227
                    result = fscanf( fd, "\t%24s", action );
228
229
230
231
232
233
234
235
                    if( result == 0 )
                        goto error;

                    result = fscanf( fd, "\t%d", &i_index );
                    if( result == 0 )
                        goto error;

                    msg_Dbg( p_this, " + (menu up) hotkey down %s, file=%s%s",
ivoire's avatar
ivoire committed
236
                             action, p_menu->psz_path, file );
237

ivoire's avatar
ivoire committed
238
                    free( p_up->psz_action_down );
ivoire's avatar
ivoire committed
239
                    p_up->psz_action_down = strdup( action );
240
241
242
243
244
245
246
247
248
249

                    /* Parse range contstruction :
                     * range <hotkey>
                     *      <state1> <file1>
                     *
                     *      <stateN> <fileN>
                     * end
                     */
                    while( !feof( fd ) )
                    {
ivoire's avatar
ivoire committed
250
                        result = fscanf( fd, "\t%255s", file );
251
252
                        if( result == 0 )
                            goto error;
ivoire's avatar
ivoire committed
253
                        if( strncmp( file, "end", 3 ) == 0 )
254
255
256
257
                            break;

                        p_range_prev = p_range_current;

258
                        if( p_menu->psz_path )
259
                        {
260
                            size_t i_path_size = strlen( p_menu->psz_path );
ivoire's avatar
ivoire committed
261
                            size_t i_file_size = strlen( file );
262

263
264
265
266
                            if( (i_path_size + i_file_size >= PATH_MAX) ||
                                (i_path_size >= PATH_MAX) )
                                goto error;

ivoire's avatar
ivoire committed
267
268
                            strncpy( path, p_menu->psz_path, i_path_size );
                            strncpy( &path[i_path_size], file,
269
                                     PATH_MAX - (i_path_size + i_file_size) );
270
271
                            path[ i_path_size + i_file_size ] = '\0';

ivoire's avatar
ivoire committed
272
                            p_range_current = osd_StateNew( p_menu, path, "pressed" );
273
274
                        }
                        else /* absolute paths are used. */
ivoire's avatar
ivoire committed
275
                            p_range_current = osd_StateNew( p_menu, file, "pressed" );
276

277
                        if( !p_range_current )
278
279
                            goto error;

280
281
282
283
284
285
                        if( !p_range_current->p_pic )
                        {
                            osd_StatesFree( p_menu, p_range_current );
                            goto error;
                        }

286
287
288
                        p_range_current->i_x = i_x;
                        p_range_current->i_y = i_y;

289
290
291
292
293
294
295
296
297
298
                        /* increment the number of ranges for this button */
                        p_up->i_ranges++;

                        if( p_range_prev )
                            p_range_prev->p_next = p_range_current;
                        else
                            p_up->p_states = p_range_current;
                        p_range_current->p_prev = p_range_prev;

                        msg_Dbg( p_this, "  |- range=%d, file=%s%s",
299
                                 p_up->i_ranges,
ivoire's avatar
ivoire committed
300
                                 p_menu->psz_path, file );
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
                    }
                    if( i_index > 0 )
                    {
                        osd_state_t *p_range = NULL;

                        /* Find the default index for state range */
                        p_range = p_up->p_states;
                        while( (--i_index > 0) && p_range->p_next )
                        {
                            osd_state_t *p_temp = NULL;
                            p_temp = p_range->p_next;
                            p_range = p_temp;
                        }
                        p_up->p_current_state = p_range;
                    }
                    else p_up->p_current_state = p_up->p_states;

                }
ivoire's avatar
ivoire committed
319
                result = fscanf( fd, "\t%24s", state );
320
321
                if( result == 0 )
                    goto error;
ivoire's avatar
ivoire committed
322
                if( strncmp( state, "end", 3 ) != 0 )
323
324
325
326
327
328
329
                    goto error;

                /* Continue at the beginning of the while() */
                continue;
            }

            /* Parse the range state */
ivoire's avatar
ivoire committed
330
            if( strncmp( state, "range", 5 ) == 0 )
331
332
333
334
335
            {
                osd_state_t   *p_range_current = NULL; /* range state currently processed */
                osd_state_t   *p_range_prev = NULL;    /* previous state processed range */
                int i_index = 0;

336
                p_current->b_range = true;
337

ivoire's avatar
ivoire committed
338
                result = fscanf( fd, "\t%24s", action );
339
340
341
342
343
344
345
                if( result == 0 )
                    goto error;

                result = fscanf( fd, "\t%d", &i_index );
                if( result == 0 )
                    goto error;

346
                msg_Dbg( p_this, " + hotkey down %s, file=%s%s", 
ivoire's avatar
ivoire committed
347
                         action, p_menu->psz_path, file );
ivoire's avatar
ivoire committed
348
                free( p_current->psz_action_down );
ivoire's avatar
ivoire committed
349
                p_current->psz_action_down = strdup( action );
350
351
352
353
354
355
356
357
358
359

                /* Parse range contstruction :
                 * range <hotkey>
                 *      <state1> <file1>
                 *
                 *      <stateN> <fileN>
                 * end
                 */
                while( !feof( fd ) )
                {
ivoire's avatar
ivoire committed
360
                    result = fscanf( fd, "\t%255s", file );
361
362
                    if( result == 0 )
                        goto error;
ivoire's avatar
ivoire committed
363
                    if( strncmp( file, "end", 3 ) == 0 )
364
365
366
367
                        break;

                    p_range_prev = p_range_current;

368
                    if( p_menu->psz_path )
369
                    {
370
                        size_t i_path_size = strlen( p_menu->psz_path );
ivoire's avatar
ivoire committed
371
                        size_t i_file_size = strlen( file );
372

373
374
375
376
                        if( (i_path_size + i_file_size >= PATH_MAX) ||
                            (i_path_size >= PATH_MAX) )
                            goto error;

ivoire's avatar
ivoire committed
377
378
                        strncpy( path, p_menu->psz_path, i_path_size );
                        strncpy( &path[i_path_size], file,
379
                                 PATH_MAX - (i_path_size + i_file_size) );
380
381
                        path[ i_path_size + i_file_size ] = '\0';

ivoire's avatar
ivoire committed
382
                        p_range_current = osd_StateNew( p_menu, path, "pressed" );
383
384
                    }
                    else /* absolute paths are used. */
ivoire's avatar
ivoire committed
385
                        p_range_current = osd_StateNew( p_menu, file, "pressed" );
386

387
388
389
390
391
392
                    if( !p_range_current )
                        goto error;

                    if( !p_range_current->p_pic )
                    {
                        osd_StatesFree( p_menu, p_range_current );
393
                        goto error;
394
                    }
395

396
397
398
                    p_range_current->i_x = i_x;
                    p_range_current->i_y = i_y;

399
400
401
402
403
404
405
406
407
408
                    /* increment the number of ranges for this button */
                    p_current->i_ranges++;

                    if( p_range_prev )
                        p_range_prev->p_next = p_range_current;
                    else
                        p_current->p_states = p_range_current;
                    p_range_current->p_prev = p_range_prev;

                    msg_Dbg( p_this, "  |- range=%d, file=%s%s",
409
                             p_current->i_ranges,
ivoire's avatar
ivoire committed
410
                             p_menu->psz_path, file );
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
                }
                if( i_index > 0 )
                {
                    osd_state_t *p_range = NULL;

                    /* Find the default index for state range */
                    p_range = p_current->p_states;
                    while( (--i_index > 0) && p_range->p_next )
                    {
                        osd_state_t *p_temp = NULL;
                        p_temp = p_range->p_next;
                        p_range = p_temp;
                    }
                    p_current->p_current_state = p_range;
                }
                else p_current->p_current_state = p_current->p_states;
                /* Continue at the beginning of the while() */
                continue;
            }
ivoire's avatar
ivoire committed
430
            if( strncmp( state, "end", 3 ) == 0 )
431
432
                break;

ivoire's avatar
ivoire committed
433
            result = fscanf( fd, "\t%255s", file );
434
435
436
437
438
            if( result == 0 )
                goto error;

            p_state_prev = p_state_current;

ivoire's avatar
ivoire committed
439
440
441
            if( ( strncmp( ppsz_button_states[0], state, strlen(ppsz_button_states[0]) ) != 0 ) &&
                ( strncmp( ppsz_button_states[1], state, strlen(ppsz_button_states[1]) ) != 0 ) &&
                ( strncmp( ppsz_button_states[2], state, strlen(ppsz_button_states[2]) ) != 0 ) )
442
443
444
            {
                msg_Err( p_this, "invalid button state %s for button %s "
                         "expected %u: unselect, select or pressed)",
ivoire's avatar
ivoire committed
445
                         state, action, (unsigned)strlen(state));
446
447
448
                goto error;
            }

449
            if( p_menu->psz_path )
450
            {
451
                size_t i_path_size = strlen( p_menu->psz_path );
ivoire's avatar
ivoire committed
452
                size_t i_file_size = strlen( file );
453

454
455
456
457
                if( (i_path_size + i_file_size >= PATH_MAX) ||
                    (i_path_size >= PATH_MAX) )
                    goto error;

ivoire's avatar
ivoire committed
458
459
                strncpy( path, p_menu->psz_path, i_path_size );
                strncpy( &path[i_path_size], file,
460
                         PATH_MAX - (i_path_size + i_file_size) );
461
462
                path[ i_path_size + i_file_size ] = '\0';

ivoire's avatar
ivoire committed
463
                p_state_current = osd_StateNew( p_menu, path, state );
464
465
            }
            else /* absolute paths are used. */
ivoire's avatar
ivoire committed
466
                p_state_current = osd_StateNew( p_menu, file, state );
467

468
469
470
471
472
473
            if( !p_state_current )
                goto error;

            if( !p_state_current->p_pic )
            {
                osd_StatesFree( p_menu, p_state_current );
474
                goto error;
475
            }
476

477
478
479
            p_state_current->i_x = i_x;
            p_state_current->i_y = i_y;

480
481
482
483
484
485
            if( p_state_prev )
                p_state_prev->p_next = p_state_current;
            else
                p_current->p_states = p_state_current;
            p_state_current->p_prev = p_state_prev;

ivoire's avatar
ivoire committed
486
487
            msg_Dbg( p_this, " |- state=%s, file=%s%s", state,
                     p_menu->psz_path, file );
488
489
490
491
492
493
494
        }
        p_current->p_current_state = p_current->p_states;
    }

    /* Find the last button and store its pointer.
     * The OSD menu behaves like a roundrobin list.
     */
495
    p_current = p_menu->p_button;
496
497
498
499
500
501
    while( p_current && p_current->p_next )
    {
        osd_button_t *p_temp = NULL;
        p_temp = p_current->p_next;
        p_current = p_temp;
    }
502
    p_menu->p_last_button = p_current;
503
504
505
506
    fclose( fd );
    return VLC_SUCCESS;

error:
507
    msg_Err( p_this, "parsing file failed (returned %d)", result );
ivoire's avatar
ivoire committed
508
509
    if( p_menu )
        osd_MenuFree( p_menu );
510
511
512
    fclose( fd );
    return VLC_EGENERIC;
}