First version of VLC Phonon backend
[phonon:phonon-mplayer.git] / phonon-vlc / vlcevents.cpp
1 /*
2  * VLC backend for the Phonon library
3  * Copyright (C) 2007-2008  Tanguy Krotoff <tkrotoff@gmail.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "vlcevents.h"
20
21 #include "vlcloader.h"
22
23 #include <QtCore/QLibrary>
24 #include <QtCore/QtDebug>
25
26 namespace Phonon
27 {
28 namespace VLC
29 {
30
31 //Hack, global variable
32 VLCEvents * VLCEvents::_vlcevents = NULL;
33
34 VLCEvents::VLCEvents(QObject * parent)
35         : QObject(parent) {
36
37         _eventManager = NULL;
38 }
39
40 VLCEvents::~VLCEvents() {
41 }
42
43 VLCEvents * VLCEvents::get() {
44         //Lazy initialization
45         if (!_vlcevents) {
46                 _vlcevents = new VLCEvents(NULL);
47                 
48                 TODO
49 mi = libvlc_media_instance_new (vlc, &ex);
50 catch ();
51
52 em = libvlc_media_instance_event_manager (mi, &ex);
53
54                 libvlc_event_type_t events[] = {
55                         libvlc_MediaDescriptorMetaChanged,
56                         libvlc_MediaDescriptorSubItemAdded,
57                         libvlc_MediaDescriptorDurationChanged,
58                         libvlc_MediaDescriptorPreparsedChanged,
59                         libvlc_MediaDescriptorFreed,
60                         libvlc_MediaDescriptorStateChanged,
61
62                         libvlc_MediaInstancePlayed,
63                         libvlc_MediaInstancePaused,
64                         libvlc_MediaInstanceReachedEnd,
65                         libvlc_MediaInstanceEncounteredError,
66                         libvlc_MediaInstanceTimeChanged,
67                         libvlc_MediaInstancePositionChanged,
68                         libvlc_MediaInstanceSeekableChanged,
69                         libvlc_MediaInstancePausableChanged,
70
71                         libvlc_MediaListItemAdded,
72                         libvlc_MediaListWillAddItem,
73                         libvlc_MediaListItemDeleted,
74                         libvlc_MediaListWillDeleteItem,
75
76                         libvlc_MediaListViewItemAdded,
77                         libvlc_MediaListViewWillAddItem,
78                         libvlc_MediaListViewItemDeleted,
79                         libvlc_MediaListViewWillDeleteItem,
80
81                         libvlc_MediaListPlayerPlayed,
82                         libvlc_MediaListPlayerNextItemSet,
83                         libvlc_MediaListPlayerStopped,
84
85                         libvlc_MediaDiscovererStarted,
86                         libvlc_MediaDiscovererEnded
87                 };
88                 int nbEvents = sizeof(events) / sizeof(*events);
89                 for (int i = 0; i < nbEvents; i++) {
90                         _vlcevents->libvlc_event_attach(events[i]);
91                 }
92         }
93
94         return _vlcevents;
95 }
96
97 void VLCEvents::libvlc_callback(const libvlc_event_t * event, void * user_data) {
98         qDebug() << "VLC event received=" << _vlcevents->libvlc_event_type_name(event->type);
99
100         if (event->type == libvlc_MediaInstanceTimeChanged) {
101                 emit _vlcevents->timeChanged(event, user_data);
102         }
103
104         if (event->type == libvlc_MediaInstancePaused) {
105                 emit _vlcevents->timeChanged(event, user_data);
106         }
107 }
108
109 void VLCEvents::libvlc_event_attach(libvlc_event_type_t event_type) {
110         typedef void (*fct) (libvlc_event_manager_t *, libvlc_event_type_t, libvlc_callback_t, void *, libvlc_exception_t *);
111         fct function = (fct) VLCLoader::get()->_vlc->resolve("libvlc_event_attach");
112         if (function) {
113                 function(_eventManager, event_type, libvlc_callback, this, &(VLCLoader::get()->_exception));
114                 VLCLoader::get()->checkException();
115         } else {
116         }
117 }
118
119 void VLCEvents::libvlc_event_detach(libvlc_event_type_t event_type) {
120         typedef void (*fct) (libvlc_event_manager_t *, libvlc_event_type_t, libvlc_callback_t, void *, libvlc_exception_t *);
121         fct function = (fct) VLCLoader::get()->_vlc->resolve("libvlc_event_detach");
122         if (function) {
123                 function(_eventManager, event_type, libvlc_callback, this, &(VLCLoader::get()->_exception));
124                 VLCLoader::get()->checkException();
125         } else {
126         }
127 }
128
129 const char * VLCEvents::libvlc_event_type_name(libvlc_event_type_t event_type) {
130         typedef const char * (*fct) (libvlc_event_type_t);
131         fct function = (fct) VLCLoader::get()->_vlc->resolve("libvlc_event_type_name");
132         if (function) {
133                 const char * name = function(event_type);
134                 return name;
135         } else {
136                 return NULL;
137         }
138 }
139
140 }}      //Namespace Phonon::VLC