xref: /haiku/src/kits/media/MediaFiles.cpp (revision 3cb015b1ee509d69c643506e8ff573808c86dcfc)
1 /***********************************************************************
2  * AUTHOR: Marcus Overhagen, Jérôme Duval
3  *   FILE: MediaFiles.cpp
4  *  DESCR:
5  ***********************************************************************/
6 #include <MediaFiles.h>
7 #include "DataExchange.h"
8 #include "debug.h"
9 
10 /*************************************************************
11  * public BMediaFiles
12  *************************************************************/
13 
14 const char BMediaFiles::B_SOUNDS[] = "Sounds";	/* for "types" */
15 
16 BMediaFiles::BMediaFiles()
17 	: m_type_index(-1),
18 	m_cur_type(""),
19 	m_item_index(-1)
20 {
21 
22 }
23 
24 
25 /* virtual */
26 BMediaFiles::~BMediaFiles()
27 {
28 
29 }
30 
31 
32 /* virtual */ status_t
33 BMediaFiles::RewindTypes()
34 {
35 	CALLED();
36 	status_t rv;
37 	server_rewindtypes_request request;
38 	server_rewindtypes_reply reply;
39 
40 	for(int32 i = 0; i < m_types.CountItems(); i++)
41 		delete (BString*)m_types.ItemAt(i);
42 
43 	m_types.MakeEmpty();
44 
45 	TRACE("BMediaFiles::RewindTypes: sending SERVER_REWINDTYPES\n");
46 
47 	rv = QueryServer(SERVER_REWINDTYPES, &request, sizeof(request), &reply, sizeof(reply));
48 	if (rv != B_OK) {
49 		ERROR("BMediaFiles::RewindTypes: failed to rewindtypes (error %#lx)\n", rv);
50 		return rv;
51 	}
52 
53 	char *types;
54 	area_id clone;
55 
56 	clone = clone_area("rewind types clone", reinterpret_cast<void **>(&types), B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, reply.area);
57 	if (clone < B_OK) {
58 		ERROR("BMediaFiles::RewindTypes failed to clone area, %#lx\n", clone);
59 		delete_area(reply.area);
60 		return B_ERROR;
61 	}
62 
63 	for (int32 i = 0; i < reply.count; i++) {
64 		m_types.AddItem(new BString(types + i * B_MEDIA_NAME_LENGTH));
65 	}
66 
67 	delete_area(clone);
68 	delete_area(reply.area);
69 
70 	m_type_index = 0;
71 
72 	return B_OK;
73 }
74 
75 
76 /* virtual */ status_t
77 BMediaFiles::GetNextType(BString *out_type)
78 {
79 	CALLED();
80 	if(m_type_index < 0 || m_type_index >= m_types.CountItems()) {
81 		m_type_index = -1;
82 		return B_BAD_INDEX;
83 	}
84 
85 	*out_type = *(BString*)m_types.ItemAt(m_type_index);
86 	m_type_index++;
87 
88 	return B_OK;
89 }
90 
91 
92 /* virtual */ status_t
93 BMediaFiles::RewindRefs(const char *type)
94 {
95 	CALLED();
96 	status_t rv;
97 	server_rewindrefs_request request;
98 	server_rewindrefs_reply reply;
99 
100 	for(int32 i = 0; i < m_items.CountItems(); i++)
101 		delete (BString*)m_items.ItemAt(i);
102 
103 	m_items.MakeEmpty();
104 
105 	TRACE("BMediaFiles::RewindRefs: sending SERVER_REWINDREFS for type %s\n", type);
106 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
107 
108 	rv = QueryServer(SERVER_REWINDREFS, &request, sizeof(request), &reply, sizeof(reply));
109 	if (rv != B_OK) {
110 		ERROR("BMediaFiles::RewindRefs: failed to rewindrefs (error %#lx)\n", rv);
111 		return rv;
112 	}
113 
114 	if(reply.count>0) {
115 		char *items;
116 		area_id clone;
117 
118 		clone = clone_area("rewind refs clone", reinterpret_cast<void **>(&items), B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, reply.area);
119 		if (clone < B_OK) {
120 			ERROR("BMediaFiles::RewindRefs failed to clone area, %#lx\n", clone);
121 			delete_area(reply.area);
122 			return B_ERROR;
123 		}
124 
125 		for (int32 i = 0; i < reply.count; i++) {
126 			m_items.AddItem(new BString(items + i * B_MEDIA_NAME_LENGTH));
127 		}
128 
129 		delete_area(clone);
130 		delete_area(reply.area);
131 	}
132 
133 	m_cur_type = BString(type);
134 	m_item_index = 0;
135 
136 	return B_OK;
137 }
138 
139 
140 /* virtual */ status_t
141 BMediaFiles::GetNextRef(BString *out_type,
142 						entry_ref *out_ref)
143 {
144 	CALLED();
145 	if(m_item_index < 0 || m_item_index >= m_items.CountItems()) {
146 		m_item_index = -1;
147 		return B_BAD_INDEX;
148 	}
149 
150 	*out_type = *(BString*)m_items.ItemAt(m_item_index);
151 	m_item_index++;
152 
153 	GetRefFor(m_cur_type.String(), out_type->String(), out_ref);
154 
155 	return B_OK;
156 }
157 
158 
159 /* virtual */ status_t
160 BMediaFiles::GetRefFor(const char *type,
161 					   const char *item,
162 					   entry_ref *out_ref)
163 {
164 	CALLED();
165 	status_t rv;
166 	server_getreffor_request request;
167 	server_getreffor_reply reply;
168 
169 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
170 	strncpy(request.item, item, B_MEDIA_NAME_LENGTH);
171 
172 	TRACE("BMediaFiles::GetRefFor: sending SERVER_GETREFFOR\n");
173 
174 	rv = QueryServer(SERVER_GETREFFOR, &request, sizeof(request), &reply, sizeof(reply));
175 	if (rv != B_OK) {
176 		entry_ref ref;
177 		*out_ref = ref;
178 		ERROR("BMediaFiles::GetRefFor: failed to getreffor (error %#lx)\n", rv);
179 		return rv;
180 	}
181 
182 	*out_ref = reply.ref;
183 
184 	return B_OK;
185 }
186 
187 
188 status_t
189 BMediaFiles::GetAudioGainFor(const char * type,
190 							 const char * item,
191 							 float * out_audio_gain)
192 {
193 	UNIMPLEMENTED();
194 	*out_audio_gain = 1.0f;
195 	return B_OK;
196 }
197 
198 
199 /* virtual */ status_t
200 BMediaFiles::SetRefFor(const char *type,
201 					   const char *item,
202 					   const entry_ref &ref)
203 {
204 	CALLED();
205 	status_t rv;
206 	server_setreffor_request request;
207 	server_setreffor_reply reply;
208 
209 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
210 	strncpy(request.item, item, B_MEDIA_NAME_LENGTH);
211 	request.ref = ref;
212 
213 	TRACE("BMediaFiles::SetRefFor: sending SERVER_SETREFFOR\n");
214 
215 	rv = QueryServer(SERVER_SETREFFOR, &request, sizeof(request), &reply, sizeof(reply));
216 	if (rv != B_OK) {
217 		ERROR("BMediaFiles::SetRefFor: failed to setreffor (error %#lx)\n", rv);
218 		return rv;
219 	}
220 
221 	return B_OK;
222 }
223 
224 
225 status_t
226 BMediaFiles::SetAudioGainFor(const char * type,
227 							 const char * item,
228 							 float audio_gain)
229 {
230 	UNIMPLEMENTED();
231 	return B_OK;
232 }
233 
234 
235 /* virtual */ status_t
236 BMediaFiles::RemoveRefFor(const char *type,
237 						  const char *item,
238 						  const entry_ref &ref)
239 {
240 	status_t rv;
241 	server_removereffor_request request;
242 	server_removereffor_reply reply;
243 
244 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
245 	strncpy(request.item, item, B_MEDIA_NAME_LENGTH);
246 	request.ref = ref;
247 
248 	TRACE("BMediaFiles::RemoveRefFor: sending SERVER_REMOVEREFFOR\n");
249 
250 	rv = QueryServer(SERVER_REMOVEREFFOR, &request, sizeof(request), &reply, sizeof(reply));
251 	if (rv != B_OK) {
252 		ERROR("BMediaFiles::RemoveRefFor: failed to removereffor (error %#lx)\n", rv);
253 		return rv;
254 	}
255 
256 	return B_OK;
257 }
258 
259 
260 /* virtual */ status_t
261 BMediaFiles::RemoveItem(const char *type,
262 						const char *item)
263 {
264 	status_t rv;
265 	server_removeitem_request request;
266 	server_removeitem_reply reply;
267 
268 	strncpy(request.type, type, B_MEDIA_NAME_LENGTH);
269 	strncpy(request.item, item, B_MEDIA_NAME_LENGTH);
270 
271 	TRACE("BMediaFiles::RemoveItem: sending SERVER_REMOVEITEM\n");
272 
273 	rv = QueryServer(SERVER_REMOVEITEM, &request, sizeof(request), &reply, sizeof(reply));
274 	if (rv != B_OK) {
275 		ERROR("BMediaFiles::RemoveItem: failed to removeitem (error %#lx)\n", rv);
276 		return rv;
277 	}
278 
279 	return B_OK;
280 }
281 
282 /*************************************************************
283  * private BMediaFiles
284  *************************************************************/
285 
286 status_t BMediaFiles::_Reserved_MediaFiles_0(void *,...) { return B_ERROR; }
287 status_t BMediaFiles::_Reserved_MediaFiles_1(void *,...) { return B_ERROR; }
288 status_t BMediaFiles::_Reserved_MediaFiles_2(void *,...) { return B_ERROR; }
289 status_t BMediaFiles::_Reserved_MediaFiles_3(void *,...) { return B_ERROR; }
290 status_t BMediaFiles::_Reserved_MediaFiles_4(void *,...) { return B_ERROR; }
291 status_t BMediaFiles::_Reserved_MediaFiles_5(void *,...) { return B_ERROR; }
292 status_t BMediaFiles::_Reserved_MediaFiles_6(void *,...) { return B_ERROR; }
293 status_t BMediaFiles::_Reserved_MediaFiles_7(void *,...) { return B_ERROR; }
294 
295