338f6b37b606a2e7f09baca3ea476673f39143ff
[vlc.git] / src / control / event.c
1 /*****************************************************************************
2  * libvlc_callback.c: New libvlc callback control API
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id $
6  *
7  * Authors: Filippo Carone <filippo@carone.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26
27 static int handle_callback( vlc_object_t *p_this, char const *psz_cmd,
28                             vlc_value_t oldval, vlc_value_t newval,
29                             void *p_data )
30 {
31     struct libvlc_callback_entry_t *entry = p_data;
32     libvlc_event_t event;
33     event.type = entry->i_event_type;
34     switch ( event.type )
35     {
36         case VOLUME_CHANGED:
37             event.value_type = BOOLEAN_EVENT;
38             break;
39         case INPUT_POSITION_CHANGED:
40             break;
41         default:
42             break;
43     }
44     event.old_value = oldval;
45     event.new_value = newval;
46
47     /* Call the client entry */
48     entry->f_callback( entry->p_instance, &event, entry->p_user_data );
49
50     return VLC_SUCCESS;
51 }
52
53 void libvlc_callback_register_for_event( libvlc_instance_t *p_instance,
54                                         libvlc_event_type_t i_event_type,
55                                         libvlc_callback_t f_callback,
56                                         void *user_data,
57                                         libvlc_exception_t *p_e )
58 {
59
60     if ( ! &f_callback )
61         RAISEVOID (" Callback function is null ");
62     
63     struct libvlc_callback_entry_t *entry = malloc( sizeof( struct libvlc_callback_entry_t ) );
64     entry->f_callback = f_callback;
65     entry->i_event_type = i_event_type;
66     entry->p_user_data = user_data;
67
68     const char *callback_name;
69     
70     switch ( i_event_type )
71     {
72         case VOLUME_CHANGED:
73             callback_name = "volume-change";
74             break;
75         case INPUT_POSITION_CHANGED:
76             break;
77         default:
78             free( entry );
79             RAISEVOID( "Unsupported event." );
80     }
81
82     int res = var_AddCallback( p_instance->p_libvlc_int,
83                                callback_name,
84                                handle_callback,
85                                entry );
86     
87     if (res != VLC_SUCCESS)
88     {
89         free ( entry );
90         RAISEVOID("Internal callback registration was not successful. Callback not registered.");
91     }
92     
93     add_callback_entry( entry, &p_instance->p_callback_list );
94
95     return;
96 }
97
98 void libvlc_callback_unregister_for_event( libvlc_instance_t *p_instance,
99                                            libvlc_event_type_t i_event_type,
100                                            libvlc_callback_t f_callback,
101                                            void *p_user_data,
102                                            libvlc_exception_t *p_e )
103 {
104     struct libvlc_callback_entry_list_t *p_listitem = p_instance->p_callback_list;
105
106     while( p_listitem )
107     {
108         if( p_listitem->elmt->f_callback == f_callback
109             && ( p_listitem->elmt->i_event_type == i_event_type )
110             && ( p_listitem->elmt->p_user_data == p_user_data )
111         
112         )
113         {
114             if( p_listitem->prev )
115                 p_listitem->prev->next = p_listitem->next;
116             else
117                 p_instance->p_callback_list = p_listitem->next;
118             
119             p_listitem->next->prev = p_listitem->prev;
120             free( p_listitem );
121             break;
122         }
123         p_listitem = p_listitem->next;
124     }
125 }