]> git.sesse.net Git - vlc/blob - modules/video_filter/dynamicoverlay/dynamicoverlay_commands.c
Used a sar for picture_New/Setup.
[vlc] / modules / video_filter / dynamicoverlay / dynamicoverlay_commands.c
1 /*****************************************************************************
2  * dynamicoverlay_commands.c : dynamic overlay plugin commands
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Author: Søren Bøg <avacore@videolan.org>
8  *         Jean-Paul Saman <jpsaman@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_arrays.h>
31 #include <vlc_vout.h>
32 #include <vlc_filter.h>
33 #include <vlc_osd.h>
34
35 #include <string.h>
36 #include <ctype.h>
37
38 #if defined(HAVE_SYS_SHM_H)
39 #include <sys/shm.h>
40 #endif
41
42 #include "dynamicoverlay.h"
43
44
45 /*****************************************************************************
46  * overlay_t: Overlay descriptor
47  *****************************************************************************/
48
49 overlay_t *OverlayCreate( void )
50 {
51     overlay_t *p_ovl = calloc( 1, sizeof( overlay_t ) );
52     if( p_ovl == NULL )
53        return NULL;
54
55     p_ovl->i_x = p_ovl->i_y = 0;
56     p_ovl->i_alpha = 0xFF;
57     p_ovl->b_active = false;
58     video_format_Setup( &p_ovl->format, VLC_FOURCC( '\0','\0','\0','\0') , 0, 0,
59                         1, 1 );
60     p_ovl->p_fontstyle = text_style_New();
61     p_ovl->data.p_text = NULL;
62
63     return p_ovl;
64 }
65
66 int OverlayDestroy( overlay_t *p_ovl )
67 {
68     free( p_ovl->data.p_text );
69     text_style_Delete( p_ovl->p_fontstyle );
70
71     return VLC_SUCCESS;
72 }
73
74 /*****************************************************************************
75  * Command parsers
76  *****************************************************************************/
77 static int skip_space( char **psz_command )
78 {
79     char *psz_temp = *psz_command;
80
81     while( isspace( *psz_temp ) )
82     {
83         ++psz_temp;
84     }
85     if( psz_temp == *psz_command )
86     {
87         return VLC_EGENERIC;
88     }
89     *psz_command = psz_temp;
90     return VLC_SUCCESS;
91 }
92
93 static int parse_digit( char **psz_command, int32_t *value )
94 {
95     char *psz_temp;
96     *value = strtol( *psz_command, &psz_temp, 10 );
97     if( psz_temp == *psz_command )
98     {
99         return VLC_EGENERIC;
100     }
101     *psz_command = psz_temp;
102     return VLC_SUCCESS;
103 }
104
105 static int parse_char( char **psz_command, char **psz_end,
106                        int count, char *psz_value )
107 {
108     if( *psz_end - *psz_command < count )
109     {
110         return VLC_EGENERIC;
111     }
112     memcpy( psz_value, *psz_command, count );
113     *psz_command += count;
114     return VLC_SUCCESS;
115 }
116
117 static int parser_DataSharedMem( char *psz_command,
118                                  char *psz_end,
119                                  commandparams_t *p_params )
120 {
121     /* Parse: 0 128 128 RGBA 9404459 */
122     skip_space( &psz_command );
123     if( isdigit( *psz_command ) )
124     {
125         if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
126             return VLC_EGENERIC;
127     }
128     skip_space( &psz_command );
129     if( isdigit( *psz_command ) )
130     {
131         if( parse_digit( &psz_command, &p_params->i_width ) == VLC_EGENERIC )
132             return VLC_EGENERIC;
133     }
134     skip_space( &psz_command );
135     if( isdigit( *psz_command ) )
136     {
137         if( parse_digit( &psz_command, &p_params->i_height ) == VLC_EGENERIC )
138             return VLC_EGENERIC;
139     }
140     skip_space( &psz_command );
141     if( isascii( *psz_command ) )
142     {
143         if( parse_char( &psz_command, &psz_end, 4, (char*)&p_params->fourcc )
144             == VLC_EGENERIC )
145             return VLC_EGENERIC;
146     }
147     skip_space( &psz_command );
148     if( isdigit( *psz_command ) )
149     {
150         if( parse_digit( &psz_command, &p_params->i_shmid ) == VLC_EGENERIC )
151             return VLC_EGENERIC;
152     }
153     return VLC_SUCCESS;
154 }
155
156 static int parser_Id( char *psz_command, char *psz_end,
157                       commandparams_t *p_params )
158 {
159     VLC_UNUSED(psz_end);
160     skip_space( &psz_command );
161     if( isdigit( *psz_command ) )
162     {
163         if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
164             return VLC_EGENERIC;
165     }
166     return VLC_SUCCESS;
167 }
168
169 static int parser_None( char *psz_command, char *psz_end,
170                         commandparams_t *p_params )
171 {
172     VLC_UNUSED(psz_command);
173     VLC_UNUSED(psz_end);
174     VLC_UNUSED(p_params);
175     return VLC_SUCCESS;
176 }
177
178 static int parser_SetAlpha( char *psz_command, char *psz_end,
179                             commandparams_t *p_params )
180 {
181     VLC_UNUSED(psz_end);
182     skip_space( &psz_command );
183     if( isdigit( *psz_command ) )
184     {
185         if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC  )
186             return VLC_EGENERIC;
187     }
188     skip_space( &psz_command );
189     if( isdigit( *psz_command ) )
190     {
191         if( parse_digit( &psz_command, &p_params->i_alpha ) == VLC_EGENERIC )
192             return VLC_EGENERIC;
193     }
194     return VLC_SUCCESS;
195 }
196
197 static int parser_SetPosition( char *psz_command, char *psz_end,
198                                commandparams_t *p_params )
199 {
200     VLC_UNUSED(psz_end);
201     skip_space( &psz_command );
202     if( isdigit( *psz_command ) )
203     {
204         if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
205             return VLC_EGENERIC;
206     }
207     skip_space( &psz_command );
208     if( isdigit( *psz_command ) )
209     {
210         if( parse_digit( &psz_command, &p_params->i_x ) == VLC_EGENERIC )
211             return VLC_EGENERIC;
212     }
213     skip_space( &psz_command );
214     if( isdigit( *psz_command ) )
215     {
216         if( parse_digit( &psz_command, &p_params->i_y ) == VLC_EGENERIC )
217             return VLC_EGENERIC;
218     }
219     return VLC_SUCCESS;
220 }
221
222 static int parser_SetTextAlpha( char *psz_command, char *psz_end,
223                                 commandparams_t *p_params )
224 {
225     VLC_UNUSED(psz_end);
226     skip_space( &psz_command );
227     if( isdigit( *psz_command ) )
228     {
229         if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
230             return VLC_EGENERIC;
231     }
232     skip_space( &psz_command );
233     if( isdigit( *psz_command ) )
234     {
235         if( parse_digit( &psz_command, &p_params->fontstyle.i_font_alpha ) == VLC_EGENERIC )
236             return VLC_EGENERIC;
237     }
238     return VLC_SUCCESS;
239 }
240
241 static int parser_SetTextColor( char *psz_command, char *psz_end,
242                                 commandparams_t *p_params )
243 {
244     int r = 0, g = 0, b = 0;
245     VLC_UNUSED(psz_end);
246
247     skip_space( &psz_command );
248     if( isdigit( *psz_command ) )
249     {
250         if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
251             return VLC_EGENERIC;
252     }
253     skip_space( &psz_command );
254     if( isdigit( *psz_command ) )
255     {
256         if( parse_digit( &psz_command, &r ) == VLC_EGENERIC )
257             return VLC_EGENERIC;
258     }
259     skip_space( &psz_command );
260     if( isdigit( *psz_command ) )
261     {
262         if( parse_digit( &psz_command, &g ) == VLC_EGENERIC )
263             return VLC_EGENERIC;
264     }
265     skip_space( &psz_command );
266     if( isdigit( *psz_command ) )
267     {
268         if( parse_digit( &psz_command, &b ) == VLC_EGENERIC )
269             return VLC_EGENERIC;
270     }
271     p_params->fontstyle.i_font_color = (r<<16) | (g<<8) | (b<<0);
272     return VLC_SUCCESS;
273 }
274
275 static int parser_SetTextSize( char *psz_command, char *psz_end,
276                                commandparams_t *p_params )
277 {
278     VLC_UNUSED(psz_end);
279     skip_space( &psz_command );
280     if( isdigit( *psz_command ) )
281     {
282         if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
283             return VLC_EGENERIC;
284     }
285     skip_space( &psz_command );
286     if( isdigit( *psz_command ) )
287     {
288         if( parse_digit( &psz_command, &p_params->fontstyle.i_font_size ) == VLC_EGENERIC )
289             return VLC_EGENERIC;
290     }
291     return VLC_SUCCESS;
292 }
293
294 static int parser_SetVisibility( char *psz_command, char *psz_end,
295                                  commandparams_t *p_params )
296 {
297     VLC_UNUSED(psz_end);
298     skip_space( &psz_command );
299     if( isdigit( *psz_command ) )
300     {
301         if( parse_digit( &psz_command, &p_params->i_id ) == VLC_EGENERIC )
302             return VLC_EGENERIC;
303     }
304     skip_space( &psz_command );
305     if( isdigit( *psz_command ) )
306     {
307         int32_t i_vis = 0;
308         if( parse_digit( &psz_command, &i_vis ) == VLC_EGENERIC )
309             return VLC_EGENERIC;
310         p_params->b_visible = (i_vis == 1) ? true : false;
311     }
312     return VLC_SUCCESS;
313 }
314
315 /*****************************************************************************
316  * Command unparser functions
317  *****************************************************************************/
318
319 static int unparse_default( const commandparams_t *p_results,
320                             buffer_t *p_output )
321 {
322     VLC_UNUSED(p_results);
323     VLC_UNUSED(p_output);
324     return VLC_SUCCESS;
325 }
326
327 static int unparse_GenImage( const commandparams_t *p_results,
328                              buffer_t *p_output )
329 {
330     int ret = BufferPrintf( p_output, " %d", p_results->i_id );
331     if( ret != VLC_SUCCESS )
332         return ret;
333
334     return VLC_SUCCESS;
335 }
336
337 static int unparse_GetAlpha( const commandparams_t *p_results,
338                              buffer_t *p_output )
339 {
340     int ret = BufferPrintf( p_output, " %d", p_results->i_alpha );
341     if( ret != VLC_SUCCESS )
342         return ret;
343
344     return VLC_SUCCESS;
345 }
346
347 static int unparse_GetPosition( const commandparams_t *p_results,
348                                 buffer_t *p_output )
349 {
350     int ret = BufferPrintf( p_output, " %d", p_results->i_x );
351     if( ret != VLC_SUCCESS )
352         return ret;
353
354     ret = BufferPrintf( p_output, " %d", p_results->i_y );
355     if( ret != VLC_SUCCESS )
356         return ret;
357
358     return VLC_SUCCESS;
359 }
360
361 static int unparse_GetTextAlpha( const commandparams_t *p_results,
362                                  buffer_t *p_output )
363 {
364     int ret = BufferPrintf( p_output, " %d", p_results->fontstyle.i_font_alpha );
365     if( ret != VLC_SUCCESS )
366         return ret;
367
368     return VLC_SUCCESS;
369 }
370
371 static int unparse_GetTextColor( const commandparams_t *p_results,
372                                  buffer_t *p_output )
373 {
374     int ret = BufferPrintf( p_output, " %d", (p_results->fontstyle.i_font_color & 0xff0000)>>16 );
375     if( ret != VLC_SUCCESS )
376         return ret;
377
378     ret = BufferPrintf( p_output, " %d", (p_results->fontstyle.i_font_color & 0x00ff00)>>8 );
379     if( ret != VLC_SUCCESS )
380         return ret;
381
382     ret = BufferPrintf( p_output, " %d", (p_results->fontstyle.i_font_color & 0x0000ff) );
383     if( ret != VLC_SUCCESS )
384         return ret;
385
386     return VLC_SUCCESS;
387 }
388
389 static int unparse_GetTextSize( const commandparams_t *p_results,
390                                 buffer_t *p_output )
391 {
392     int ret = BufferPrintf( p_output, " %d", p_results->fontstyle.i_font_size );
393     if( ret != VLC_SUCCESS )
394         return ret;
395
396     return VLC_SUCCESS;
397 }
398
399 static int unparse_GetVisibility( const commandparams_t *p_results,
400                              buffer_t *p_output )
401 {
402     int ret = BufferPrintf( p_output, " %d", (p_results->b_visible ? 1 : 0) );
403     if( ret != VLC_SUCCESS ) {
404         return ret;
405     }
406     return VLC_SUCCESS;
407 }
408
409 /*****************************************************************************
410  * Command functions
411  *****************************************************************************/
412 static int exec_DataSharedMem( filter_t *p_filter,
413                                const commandparams_t *p_params,
414                                commandparams_t *p_results )
415 {
416 #if defined(HAVE_SYS_SHM_H)
417     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
418     struct shmid_ds shminfo;
419     overlay_t *p_ovl;
420     size_t i_size;
421
422     VLC_UNUSED(p_results);
423
424     p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
425     if( p_ovl == NULL )
426     {
427         msg_Err( p_filter, "Invalid overlay: %d", p_params->i_id );
428         return VLC_EGENERIC;
429     }
430
431     if( shmctl( p_params->i_shmid, IPC_STAT, &shminfo ) == -1 )
432     {
433         msg_Err( p_filter, "Unable to access shared memory" );
434         return VLC_EGENERIC;
435     }
436     i_size = shminfo.shm_segsz;
437
438     if( p_params->fourcc == VLC_CODEC_TEXT )
439     {
440         char *p_data;
441
442         if( (p_params->i_height != 1) || (p_params->i_width < 1) )
443         {
444             msg_Err( p_filter,
445                      "Invalid width and/or height. when specifying text height "
446                      "must be 1 and width the number of bytes in the string, "
447                      "including the null terminator" );
448             return VLC_EGENERIC;
449         }
450
451         if( (size_t)p_params->i_width > i_size )
452         {
453             msg_Err( p_filter,
454                      "Insufficient data in shared memory. need %d, got %zu",
455                      p_params->i_width, i_size );
456             return VLC_EGENERIC;
457         }
458
459         p_ovl->data.p_text = malloc( p_params->i_width );
460         if( p_ovl->data.p_text == NULL )
461         {
462             msg_Err( p_filter, "Unable to allocate string storage" );
463             return VLC_ENOMEM;
464         }
465
466         video_format_Setup( &p_ovl->format, VLC_CODEC_TEXT,
467                             0, 0, 0, 1 );
468
469         p_data = shmat( p_params->i_shmid, NULL, SHM_RDONLY );
470         if( p_data == NULL )
471         {
472             msg_Err( p_filter, "Unable to attach to shared memory" );
473             free( p_ovl->data.p_text );
474             p_ovl->data.p_text = NULL;
475             return VLC_ENOMEM;
476         }
477         memcpy( p_ovl->data.p_text, p_data, p_params->i_width );
478
479         shmdt( p_data );
480     }
481     else
482     {
483         uint8_t *p_data, *p_in;
484         size_t i_neededsize = 0;
485
486         p_ovl->data.p_pic = malloc( sizeof( picture_t ) );
487         if( p_ovl->data.p_pic == NULL )
488             return VLC_ENOMEM;
489
490         video_format_Setup( &p_ovl->format, p_params->fourcc,
491                             p_params->i_width, p_params->i_height,
492                             1, 1 );
493         if( vout_AllocatePicture( p_filter, p_ovl->data.p_pic,
494                                   p_ovl->format.i_chroma, p_params->i_width,
495                                   p_params->i_height,
496                                   p_ovl->format.i_aspect * p_params->i_height,
497                                   VOUT_ASPECT_FACTOR     * p_params->i_width ) )
498         {
499             msg_Err( p_filter, "Unable to allocate picture" );
500             free( p_ovl->data.p_pic );
501             p_ovl->data.p_pic = NULL;
502             return VLC_ENOMEM;
503         }
504
505         for( size_t i_plane = 0; i_plane < (size_t)p_ovl->data.p_pic->i_planes;
506              ++i_plane )
507         {
508             i_neededsize += p_ovl->data.p_pic->p[i_plane].i_visible_lines *
509                             p_ovl->data.p_pic->p[i_plane].i_visible_pitch;
510         }
511
512         if( i_neededsize > i_size )
513         {
514             msg_Err( p_filter,
515                      "Insufficient data in shared memory. need %zu, got %zu",
516                      i_neededsize, i_size );
517             picture_Release( p_ovl->data.p_pic );
518             free( p_ovl->data.p_pic );
519             p_ovl->data.p_pic = NULL;
520             return VLC_EGENERIC;
521         }
522
523         p_data = shmat( p_params->i_shmid, NULL, SHM_RDONLY );
524         if( p_data == NULL )
525         {
526             msg_Err( p_filter, "Unable to attach to shared memory" );
527             picture_Release( p_ovl->data.p_pic );
528             free( p_ovl->data.p_pic );
529             p_ovl->data.p_pic = NULL;
530             return VLC_ENOMEM;
531         }
532
533         p_in = p_data;
534         for( size_t i_plane = 0; i_plane < (size_t)p_ovl->data.p_pic->i_planes;
535              ++i_plane )
536         {
537             uint8_t *p_out = p_ovl->data.p_pic->p[i_plane].p_pixels;
538             for( size_t i_line = 0;
539                  i_line < (size_t)p_ovl->data.p_pic->p[i_plane].i_visible_lines;
540                  ++i_line )
541             {
542                 vlc_memcpy( p_out, p_in,
543                             p_ovl->data.p_pic->p[i_plane].i_visible_pitch );
544                 p_out += p_ovl->data.p_pic->p[i_plane].i_pitch;
545                 p_in += p_ovl->data.p_pic->p[i_plane].i_visible_pitch;
546             }
547         }
548         shmdt( p_data );
549     }
550     p_sys->b_updated = p_ovl->b_active;
551
552     return VLC_SUCCESS;
553 #else
554     VLC_UNUSED(p_params);
555     VLC_UNUSED(p_results);
556
557     msg_Err( p_filter, "system doesn't support shared memory" );
558     return VLC_EGENERIC;
559 #endif
560 }
561
562 static int exec_DeleteImage( filter_t *p_filter,
563                              const commandparams_t *p_params,
564                              commandparams_t *p_results )
565 {
566     VLC_UNUSED(p_results);
567     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
568     p_sys->b_updated = true;
569
570     return ListRemove( &p_sys->overlays, p_params->i_id );
571 }
572
573 static int exec_EndAtomic( filter_t *p_filter,
574                            const commandparams_t *p_params,
575                            commandparams_t *p_results )
576 {
577     VLC_UNUSED(p_params);
578     VLC_UNUSED(p_results);
579     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
580     QueueTransfer( &p_sys->pending, &p_sys->atomic );
581     p_sys->b_atomic = false;
582     return VLC_SUCCESS;
583 }
584
585 static int exec_GenImage( filter_t *p_filter,
586                           const commandparams_t *p_params,
587                           commandparams_t *p_results )
588 {
589     VLC_UNUSED(p_params);
590     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
591
592     overlay_t *p_ovl = OverlayCreate();
593     if( p_ovl == NULL )
594         return VLC_ENOMEM;
595
596     ssize_t i_idx = ListAdd( &p_sys->overlays, p_ovl );
597     if( i_idx < 0 )
598         return i_idx;
599
600     p_results->i_id = i_idx;
601     return VLC_SUCCESS;
602 }
603
604 static int exec_GetAlpha( filter_t *p_filter,
605                           const commandparams_t *p_params,
606                           commandparams_t *p_results )
607 {
608     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
609     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
610     if( p_ovl == NULL )
611         return VLC_EGENERIC;
612
613     p_results->i_alpha = p_ovl->i_alpha;
614     return VLC_SUCCESS;
615 }
616
617 static int exec_GetPosition( filter_t *p_filter,
618                              const commandparams_t *p_params,
619                              commandparams_t *p_results )
620 {
621     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
622     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
623     if( p_ovl == NULL )
624         return VLC_EGENERIC;
625
626     p_results->i_x = p_ovl->i_x;
627     p_results->i_y = p_ovl->i_y;
628     return VLC_SUCCESS;
629 }
630
631 static int exec_GetTextAlpha( filter_t *p_filter,
632                               const commandparams_t *p_params,
633                               commandparams_t *p_results )
634 {
635     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
636     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
637     if( p_ovl == NULL )
638         return VLC_EGENERIC;
639
640     p_results->fontstyle.i_font_alpha = p_ovl->p_fontstyle->i_font_alpha;
641     return VLC_SUCCESS;
642 }
643
644 static int exec_GetTextColor( filter_t *p_filter,
645                               const commandparams_t *p_params,
646                               commandparams_t *p_results )
647 {
648     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
649     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
650     if( p_ovl == NULL )
651         return VLC_EGENERIC;
652
653     p_results->fontstyle.i_font_color = p_ovl->p_fontstyle->i_font_color;
654     return VLC_SUCCESS;
655 }
656
657 static int exec_GetTextSize( filter_t *p_filter,
658                              const commandparams_t *p_params,
659                              commandparams_t *p_results )
660 {
661     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
662     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
663     if( p_ovl == NULL )
664         return VLC_EGENERIC;
665
666     p_results->fontstyle.i_font_size = p_ovl->p_fontstyle->i_font_size;
667     return VLC_SUCCESS;
668 }
669
670 static int exec_GetVisibility( filter_t *p_filter,
671                                const commandparams_t *p_params,
672                                commandparams_t *p_results )
673 {
674     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
675
676     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
677     if( p_ovl == NULL )
678         return VLC_EGENERIC;
679
680     p_results->b_visible = ( p_ovl->b_active == true ) ? 1 : 0;
681     return VLC_SUCCESS;
682 }
683
684 static int exec_SetAlpha( filter_t *p_filter,
685                           const commandparams_t *p_params,
686                           commandparams_t *p_results )
687 {
688     VLC_UNUSED(p_results);
689     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
690
691     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
692     if( p_ovl == NULL )
693         return VLC_EGENERIC;
694
695     p_ovl->i_alpha = p_params->i_alpha;
696     p_sys->b_updated = p_ovl->b_active;
697     return VLC_SUCCESS;
698 }
699
700 static int exec_SetPosition( filter_t *p_filter,
701                              const commandparams_t *p_params,
702                              commandparams_t *p_results )
703 {
704     VLC_UNUSED(p_results);
705     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
706
707     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
708     if( p_ovl == NULL )
709         return VLC_EGENERIC;
710
711     p_ovl->i_x = p_params->i_x;
712     p_ovl->i_y = p_params->i_y;
713
714     p_sys->b_updated = p_ovl->b_active;
715     return VLC_SUCCESS;
716 }
717
718 static int exec_SetTextAlpha( filter_t *p_filter,
719                               const commandparams_t *p_params,
720                               commandparams_t *p_results )
721 {
722     VLC_UNUSED(p_results);
723     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
724
725     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
726     if( p_ovl == NULL )
727         return VLC_EGENERIC;
728
729     p_ovl->p_fontstyle->i_font_alpha = p_params->fontstyle.i_font_alpha;
730     p_sys->b_updated = p_ovl->b_active;
731     return VLC_SUCCESS;
732 }
733
734 static int exec_SetTextColor( filter_t *p_filter,
735                               const commandparams_t *p_params,
736                               commandparams_t *p_results )
737 {
738     VLC_UNUSED(p_results);
739     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
740
741     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
742     if( p_ovl == NULL )
743         return VLC_EGENERIC;
744
745     p_ovl->p_fontstyle->i_font_color = p_params->fontstyle.i_font_color;
746     p_sys->b_updated = p_ovl->b_active;
747     return VLC_SUCCESS;
748 }
749
750 static int exec_SetTextSize( filter_t *p_filter,
751                               const commandparams_t *p_params,
752                               commandparams_t *p_results )
753 {
754     VLC_UNUSED(p_results);
755     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
756
757     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
758     if( p_ovl == NULL )
759         return VLC_EGENERIC;
760
761     p_ovl->p_fontstyle->i_font_size = p_params->fontstyle.i_font_size;
762     p_sys->b_updated = p_ovl->b_active;
763     return VLC_SUCCESS;
764 }
765
766 static int exec_SetVisibility( filter_t *p_filter,
767                                const commandparams_t *p_params,
768                                commandparams_t *p_results )
769 {
770     VLC_UNUSED(p_results);
771     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
772
773     overlay_t *p_ovl = ListGet( &p_sys->overlays, p_params->i_id );
774     if( p_ovl == NULL )
775         return VLC_EGENERIC;
776
777     p_ovl->b_active = p_params->b_visible;// ? false : true;
778     p_sys->b_updated = true;
779     return VLC_SUCCESS;
780 }
781
782 static int exec_StartAtomic( filter_t *p_filter,
783                              const commandparams_t *p_params,
784                              commandparams_t *p_results )
785 {
786     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
787     VLC_UNUSED(p_params);
788     VLC_UNUSED(p_results);
789
790     p_sys->b_atomic = true;
791     return VLC_SUCCESS;
792 }
793
794 /*****************************************************************************
795  * Command functions
796  *****************************************************************************/
797 static const commanddesc_static_t p_commands[] =
798 {
799     {   .psz_command = "DataSharedMem",
800         .b_atomic = true,
801         .pf_parser = parser_DataSharedMem,
802         .pf_execute = exec_DataSharedMem,
803         .pf_unparse = unparse_default,
804     },
805     {   .psz_command = "DeleteImage",
806         .b_atomic = true,
807         .pf_parser = parser_Id,
808         .pf_execute = exec_DeleteImage,
809         .pf_unparse = unparse_default,
810     },
811     {   .psz_command = "EndAtomic",
812         .b_atomic = false,
813         .pf_parser = parser_None,
814         .pf_execute = exec_EndAtomic,
815         .pf_unparse = unparse_default,
816     },
817     {   .psz_command = "GenImage",
818         .b_atomic = false,
819         .pf_parser = parser_None,
820         .pf_execute = exec_GenImage,
821         .pf_unparse = unparse_GenImage,
822     },
823     {   .psz_command = "GetAlpha",
824         .b_atomic = false,
825         .pf_parser = parser_Id,
826         .pf_execute = exec_GetAlpha,
827         .pf_unparse = unparse_GetAlpha,
828     },
829     {   .psz_command = "GetPosition",
830         .b_atomic = false,
831         .pf_parser = parser_Id,
832         .pf_execute = exec_GetPosition,
833         .pf_unparse = unparse_GetPosition,
834     },
835     {   .psz_command = "GetTextAlpha",
836         .b_atomic = false,
837         .pf_parser = parser_Id,
838         .pf_execute = exec_GetTextAlpha,
839         .pf_unparse = unparse_GetTextAlpha,
840     },
841     {   .psz_command = "GetTextColor",
842         .b_atomic = false,
843         .pf_parser = parser_Id,
844         .pf_execute = exec_GetTextColor,
845         .pf_unparse = unparse_GetTextColor,
846     },
847     {   .psz_command = "GetTextSize",
848         .b_atomic = true,
849         .pf_parser = parser_Id,
850         .pf_execute = exec_GetTextSize,
851         .pf_unparse = unparse_GetTextSize,
852     },
853     {   .psz_command = "GetVisibility",
854         .b_atomic = false,
855         .pf_parser = parser_Id,
856         .pf_execute = exec_GetVisibility,
857         .pf_unparse = unparse_GetVisibility,
858     },
859     {   .psz_command = "SetAlpha",
860         .b_atomic = true,
861         .pf_parser = parser_SetAlpha,
862         .pf_execute = exec_SetAlpha,
863         .pf_unparse = unparse_default,
864     },
865     {   .psz_command = "SetPosition",
866         .b_atomic = true,
867         .pf_parser = parser_SetPosition,
868         .pf_execute = exec_SetPosition,
869         .pf_unparse = unparse_default,
870     },
871     {   .psz_command = "SetTextAlpha",
872         .b_atomic = true,
873         .pf_parser = parser_SetTextAlpha,
874         .pf_execute = exec_SetTextAlpha,
875         .pf_unparse = unparse_default,
876     },
877     {   .psz_command = "SetTextColor",
878         .b_atomic = true,
879         .pf_parser = parser_SetTextColor,
880         .pf_execute = exec_SetTextColor,
881         .pf_unparse = unparse_default,
882     },
883     {   .psz_command = "SetTextSize",
884         .b_atomic = true,
885         .pf_parser = parser_SetTextSize,
886         .pf_execute = exec_SetTextSize,
887         .pf_unparse = unparse_default,
888     },
889     {   .psz_command = "SetVisibility",
890         .b_atomic = true,
891         .pf_parser = parser_SetVisibility,
892         .pf_execute = exec_SetVisibility,
893         .pf_unparse = unparse_default,
894     },
895     {   .psz_command = "StartAtomic",
896         .b_atomic = true,
897         .pf_parser = parser_None,
898         .pf_execute = exec_StartAtomic,
899         .pf_unparse = unparse_default,
900     }
901 };
902
903 void RegisterCommand( filter_t *p_filter )
904 {
905     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
906     size_t i_index = 0;
907
908     p_sys->i_commands = ARRAY_SIZE(p_commands);
909     p_sys->pp_commands = (commanddesc_t **) calloc( p_sys->i_commands, sizeof(commanddesc_t*) );
910     if( !p_sys->pp_commands ) return;
911     for( i_index = 0; i_index < p_sys->i_commands; i_index ++ )
912     {
913         p_sys->pp_commands[i_index] = (commanddesc_t *) malloc( sizeof(commanddesc_t) );
914         if( !p_sys->pp_commands[i_index] ) return;
915         p_sys->pp_commands[i_index]->psz_command = strdup( p_commands[i_index].psz_command );
916         p_sys->pp_commands[i_index]->b_atomic = p_commands[i_index].b_atomic;
917         p_sys->pp_commands[i_index]->pf_parser = p_commands[i_index].pf_parser;
918         p_sys->pp_commands[i_index]->pf_execute = p_commands[i_index].pf_execute;
919         p_sys->pp_commands[i_index]->pf_unparse = p_commands[i_index].pf_unparse;
920     }
921
922     msg_Dbg( p_filter, "%zu commands are available", p_sys->i_commands );
923     for( size_t i_index = 0; i_index < p_sys->i_commands; i_index++ )
924         msg_Dbg( p_filter, "    %s", p_sys->pp_commands[i_index]->psz_command );
925 }
926
927 void UnregisterCommand( filter_t *p_filter )
928 {
929     filter_sys_t *p_sys = (filter_sys_t*) p_filter->p_sys;
930     size_t i_index = 0;
931
932     for( i_index = 0; i_index < p_sys->i_commands; i_index++ )
933     {
934         free( p_sys->pp_commands[i_index]->psz_command );
935         free( p_sys->pp_commands[i_index] );
936     }
937     free( p_sys->pp_commands );
938     p_sys->pp_commands = NULL;
939     p_sys->i_commands = 0;
940 }