elock.h: add possibility to create recursive mutexes
[enigma2.git] / lib / nav / core.cpp
1 #include <lib/nav/core.h>
2 #include <lib/base/eerror.h>
3 #include <lib/python/python.h>
4
5 void eNavigation::serviceEvent(iPlayableService* service, int event)
6 {
7         if (m_runningService && service != m_runningService)
8         {
9                 eDebug("nav: event %d for other service", event);
10                 return;
11         }
12         m_event(event);
13 }
14
15 void eNavigation::recordEvent(iRecordableService* service, int event)
16 {
17         if (m_recordings.find(service) == m_recordings.end())
18         {
19                 eDebug("nav: event for non registered recording service");
20                 return;
21         }
22         m_record_event(service, event);
23 }
24
25 RESULT eNavigation::playService(const eServiceReference &service)
26 {
27         stopService();
28         
29         ASSERT(m_servicehandler);
30         RESULT res = m_servicehandler->play(service, m_runningService);
31         if (m_runningService)
32         {
33                 m_runningService->connectEvent(slot(*this, &eNavigation::serviceEvent), m_service_event_conn);
34                 res = m_runningService->start();
35         }
36         return res;
37 }
38
39 RESULT eNavigation::connectEvent(const Slot1<void,int> &event, ePtr<eConnection> &connection)
40 {
41         connection = new eConnection(this, m_event.connect(event));
42         return 0;
43 }
44
45 RESULT eNavigation::connectRecordEvent(const Slot2<void,ePtr<iRecordableService>,int> &event, ePtr<eConnection> &connection)
46 {
47         connection = new eConnection(this, m_record_event.connect(event));
48         return 0;
49 }
50
51 RESULT eNavigation::getCurrentService(ePtr<iPlayableService> &service)
52 {
53         service = m_runningService;
54         return 0;
55 }
56
57 RESULT eNavigation::stopService(void)
58 {
59                 /* check if there is a running service... */
60         if (!m_runningService)
61                 return 1;
62
63         ePtr<iPlayableService> tmp = m_runningService;
64         m_runningService=0;
65         tmp->stop();
66
67         /* send stop event */
68         m_event(iPlayableService::evEnd);
69
70                 /* kill service. */
71         m_service_event_conn = 0;
72         return 0;
73 }
74
75 RESULT eNavigation::recordService(const eServiceReference &ref, ePtr<iRecordableService> &service, bool simulate)
76 {
77         ASSERT(m_servicehandler);
78         RESULT res = m_servicehandler->record(ref, service);
79         eDebug("record: %d", res);
80         if (res)
81                 service = 0;
82         else
83         {
84                 if (simulate)
85                         m_simulate_recordings.insert(service);
86                 else
87                 {
88                         ePtr<eConnection> conn;
89                         service->connectEvent(slot(*this, &eNavigation::recordEvent), conn);
90                         m_recordings[service]=conn;
91                 }
92         }
93         return res;
94 }
95
96 RESULT eNavigation::stopRecordService(ePtr<iRecordableService> &service)
97 {
98         service->stop();
99         std::set<ePtr<iRecordableService> >::iterator it =
100                 m_simulate_recordings.find(service);
101         if (it != m_simulate_recordings.end())
102         {
103                 m_simulate_recordings.erase(it);
104                 return 0;
105         }
106         else
107         {
108                 std::map<ePtr<iRecordableService>, ePtr<eConnection> >::iterator it =
109                         m_recordings.find(service);
110                 if (it != m_recordings.end())
111                 {
112                         m_recordings.erase(it);
113                         /* send stop event */
114                         m_record_event(service, iRecordableService::evEnd);
115                         return 0;
116                 }
117         }
118
119         eDebug("try to stop non running recording!!");  // this should not happen
120         return -1;
121 }
122
123 PyObject *eNavigation::getRecordings(bool simulate)
124 {
125         ePyObject result = PyList_New(simulate ? m_simulate_recordings.size() : m_recordings.size());
126         int pos=0;
127         if (simulate)
128                 for (std::set<ePtr<iRecordableService> >::iterator it(m_simulate_recordings.begin()); it != m_simulate_recordings.end(); ++it)
129                         PyList_SET_ITEM(result, pos++, NEW_iRecordableServicePtr(*it));
130         else
131                 for (std::map<ePtr<iRecordableService>, ePtr<eConnection> >::iterator it(m_recordings.begin()); it != m_recordings.end(); ++it)
132                         PyList_SET_ITEM(result, pos++, NEW_iRecordableServicePtr(it->first)); 
133         return result;
134 }
135
136 RESULT eNavigation::pause(int dop)
137 {
138         if (!m_runningService)
139                 return -1;
140         ePtr<iPauseableService> p;
141         if (m_runningService->pause(p))
142                 return -2;
143         if (dop)
144                 return p->pause();
145         else
146                 return p->unpause();
147 }
148
149 eNavigation::eNavigation(iServiceHandler *serviceHandler)
150 {
151         ASSERT(serviceHandler);
152         m_servicehandler = serviceHandler;
153 }
154
155 eNavigation::~eNavigation()
156 {
157         stopService();
158 }
159
160 DEFINE_REF(eNavigation);