]> git.sesse.net Git - vlc/blob - src/control/event.c
libvlc events related functions renamed
[vlc] / src / control / event.c
1 /*****************************************************************************
2  * event.c: New libvlc event 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
28 /*
29  * Private functions
30  */
31
32 static int handle_event( vlc_object_t *p_this, char const *psz_cmd,
33                          vlc_value_t oldval, vlc_value_t newval,
34                          void *p_data )
35 {
36     struct libvlc_callback_entry_t *entry = p_data;
37     libvlc_event_t event;
38     event.type = entry->i_event_type;
39     switch ( event.type )
40     {
41         case VOLUME_CHANGED:
42             event.value_type = BOOLEAN_EVENT;
43             break;
44         case INPUT_POSITION_CHANGED:
45             break;
46         default:
47             break;
48     }
49     event.old_value = oldval;
50     event.new_value = newval;
51
52     /* Call the client entry */
53     entry->f_callback( entry->p_instance, &event, entry->p_user_data );
54
55     return VLC_SUCCESS;
56 }
57
58 static inline void add_callback_entry( struct libvlc_callback_entry_t *entry,
59                                        struct libvlc_callback_entry_list_t **list )
60 {
61     struct libvlc_callback_entry_list_t *new_listitem;
62     new_listitem = malloc( sizeof( struct libvlc_callback_entry_list_t ) );
63     new_listitem->elmt = entry;
64     new_listitem->next = *list;
65     new_listitem->prev = NULL;
66
67     if(*list)
68         (*list)->prev = new_listitem;
69
70     *list = new_listitem;
71 }
72
73 /*
74  * Public libvlc functions
75  */
76
77 void libvlc_event_add_callback( libvlc_instance_t *p_instance,
78                                 libvlc_event_type_t i_event_type,
79                                 libvlc_callback_t f_callback,
80                                 void *user_data,
81                                 libvlc_exception_t *p_e )
82 {
83
84     if ( ! &f_callback )
85         RAISEVOID (" Callback function is null ");
86     
87     struct libvlc_callback_entry_t *entry = malloc( sizeof( struct libvlc_callback_entry_t ) );
88     entry->f_callback = f_callback;
89     entry->i_event_type = i_event_type;
90     entry->p_user_data = user_data;
91
92     const char *callback_name;
93     
94     switch ( i_event_type )
95     {
96         case VOLUME_CHANGED:
97             callback_name = "volume-change";
98             break;
99         case INPUT_POSITION_CHANGED:
100             break;
101         default:
102             free( entry );
103             RAISEVOID( "Unsupported event." );
104     }
105
106     int res = var_AddCallback( p_instance->p_libvlc_int,
107                                callback_name,
108                                handle_event,
109                                entry );
110     
111     if (res != VLC_SUCCESS)
112     {
113         free ( entry );
114         RAISEVOID("Internal callback registration was not successful. Callback not registered.");
115     }
116     
117     add_callback_entry( entry, &p_instance->p_callback_list );
118
119     return;
120 }
121
122 void libvlc_event_remove_callback( libvlc_instance_t *p_instance,
123                                    libvlc_event_type_t i_event_type,
124                                    libvlc_callback_t f_callback,
125                                    void *p_user_data,
126                                    libvlc_exception_t *p_e )
127 {
128     struct libvlc_callback_entry_list_t *p_listitem = p_instance->p_callback_list;
129
130     while( p_listitem )
131     {
132         if( p_listitem->elmt->f_callback == f_callback
133             && ( p_listitem->elmt->i_event_type == i_event_type )
134             && ( p_listitem->elmt->p_user_data == p_user_data )
135         
136         )
137         {
138             if( p_listitem->prev )
139                 p_listitem->prev->next = p_listitem->next;
140             else
141                 p_instance->p_callback_list = p_listitem->next;
142             
143             p_listitem->next->prev = p_listitem->prev;
144             free( p_listitem );
145             break;
146         }
147         p_listitem = p_listitem->next;
148     }
149 }