xref: /haiku/src/add-ons/media/media-add-ons/usb_webcam/CamRoster.cpp (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2004-2008, François Revol, <revol@free.fr>.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "CamRoster.h"
7 
8 #include "AddOn.h"
9 #include "CamDevice.h"
10 #include "CamDebug.h"
11 
12 #include <OS.h>
13 
14 #undef B_WEBCAM_MKINTFUNC
15 #define B_WEBCAM_MKINTFUNC(modname) \
16 extern "C" status_t get_webcam_addon_##modname(WebCamMediaAddOn* webcam, CamDeviceAddon **addon);
17 #include "CamInternalAddons.h"
18 #undef B_WEBCAM_MKINTFUNC
19 
20 
21 CamRoster::CamRoster(WebCamMediaAddOn* _addon)
22 	: BUSBRoster(),
23 	fLocker("WebcamRosterLock"),
24 	fAddon(_addon)
25 {
26 	PRINT((CH "()" CT));
27 	LoadInternalAddons();
28 	LoadExternalAddons();
29 }
30 
31 
32 CamRoster::~CamRoster()
33 {
34 }
35 
36 
37 status_t
38 CamRoster::DeviceAdded(BUSBDevice* _device)
39 {
40 	PRINT((CH "()" CT));
41 	status_t err;
42 	for (int16 i = fCamerasAddons.CountItems() - 1; i >= 0; --i ) {
43 		CamDeviceAddon *ao = (CamDeviceAddon *)fCamerasAddons.ItemAt(i);
44 		PRINT((CH ": checking %s for support..." CT, ao->BrandName()));
45 		err = ao->Sniff(_device);
46 		if (err < B_OK)
47 			continue;
48 		CamDevice *cam = ao->Instantiate(*this, _device);
49 		PRINT((CH ": found camera %s:%s!" CT, cam->BrandName(), cam->ModelName()));
50 		err = cam->InitCheck();
51 		if (err >= B_OK) {
52 			fCameras.AddItem(cam);
53 			fAddon->CameraAdded(cam);
54 			return B_OK;
55 		}
56 		PRINT((CH " error 0x%08" B_PRIx32 CT, err));
57 	}
58 	return B_ERROR;
59 }
60 
61 
62 void
63 CamRoster::DeviceRemoved(BUSBDevice* _device)
64 {
65 	PRINT((CH "()" CT));
66 	for (int32 i = 0; i < fCameras.CountItems(); ++i) {
67 		CamDevice* cam = (CamDevice *)fCameras.ItemAt(i);
68 		if (cam->Matches(_device)) {
69 			PRINT((CH ": camera %s:%s removed" CT, cam->BrandName(), cam->ModelName()));
70 			fCameras.RemoveItem(i);
71 			fAddon->CameraRemoved(cam);
72 			// XXX: B_DONT_DO_THAT!
73 			//delete cam;
74 			cam->Unplugged();
75 			return;
76 		}
77 	}
78 }
79 
80 
81 uint32
82 CamRoster::CountCameras()
83 {
84 	int32 count;
85 	fLocker.Lock();
86 	PRINT((CH "(): %" B_PRId32 " cameras" CT, fCameras.CountItems()));
87 	count = fCameras.CountItems();
88 	fLocker.Unlock();
89 	return count;
90 }
91 
92 
93 bool
94 CamRoster::Lock()
95 {
96 	PRINT((CH "()" CT));
97 	return fLocker.Lock();
98 }
99 
100 
101 void
102 CamRoster::Unlock()
103 {
104 	PRINT((CH "()" CT));
105 	fLocker.Unlock();
106 	return;
107 }
108 
109 
110 CamDevice*
111 CamRoster::CameraAt(int32 index)
112 {
113 	PRINT((CH "()" CT));
114 	return (CamDevice *)fCameras.ItemAt(index);
115 }
116 
117 
118 status_t
119 CamRoster::LoadInternalAddons()
120 {
121 	PRINT((CH "()" CT));
122 	CamDeviceAddon *addon;
123 	status_t err;
124 
125 #undef B_WEBCAM_MKINTFUNC
126 #define B_WEBCAM_MKINTFUNC(modname) \
127 	err = get_webcam_addon_##modname(fAddon, &addon); \
128 	if (err >= B_OK) { \
129 		fCamerasAddons.AddItem(addon); \
130 		PRINT((CH ": registered %s addon" CT, addon->BrandName())); \
131 	}
132 
133 #include "CamInternalAddons.h"
134 #undef B_WEBCAM_MKINTFUNC
135 
136 
137 	return B_OK;
138 }
139 
140 
141 status_t
142 CamRoster::LoadExternalAddons()
143 {
144 	PRINT((CH "()" CT));
145 	// TODO implement external add-ons
146 	return B_ERROR;
147 #if 0
148 	int32 index;
149 	int32 sclass;
150 	status_t err;
151 	CamDeviceAddon *addon;
152 	status_t (*get_webcam_addon_func)(WebCamMediaAddOn* webcam, CamDeviceAddon **addon);
153 	for (index = 0; get_nth_image_symbol(fAddon->ImageID(),
154 										index, NULL, NULL,
155 										&sclass,
156 										(void **)&get_webcam_addon_func) == B_OK; index++) {
157 		PRINT((CH ": got sym" CT));
158 //		if (sclass != B_SYMBOL_TYPE_TEXT)
159 //			continue;
160 		err = (*get_webcam_addon_func)(fAddon, &addon);
161 		PRINT((CH ": Loaded addon '%s' with error 0x%08" B_PRIx32 CT,
162 			(err > 0) ? NULL : addon->BrandName(),
163 			err));
164 	}
165 	return B_OK;
166 #endif
167 }
168 
169 
170 
171