xref: /haiku/src/add-ons/media/media-add-ons/usb_webcam/CamRoster.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 #include "CamRoster.h"
2 
3 #include "AddOn.h"
4 #include "CamDevice.h"
5 #include "CamDebug.h"
6 
7 #include <OS.h>
8 
9 #undef B_WEBCAM_MKINTFUNC
10 #define B_WEBCAM_MKINTFUNC(modname) \
11 extern "C" status_t get_webcam_addon_##modname(WebCamMediaAddOn* webcam, CamDeviceAddon **addon);
12 #include "CamInternalAddons.h"
13 #undef B_WEBCAM_MKINTFUNC
14 
15 
16 // ---------------------------------------------------------------------------
17 CamRoster::CamRoster(WebCamMediaAddOn* _addon)
18 	: BUSBRoster(),
19 	fLocker("WebcamRosterLock"),
20 	fAddon(_addon)
21 {
22 	PRINT((CH "()" CT));
23 	LoadInternalAddons();
24 	LoadExternalAddons();
25 }
26 
27 // ---------------------------------------------------------------------------
28 CamRoster::~CamRoster()
29 {
30 }
31 
32 // ---------------------------------------------------------------------------
33 status_t
34 CamRoster::DeviceAdded(BUSBDevice* _device)
35 {
36 	PRINT((CH "()" CT));
37 	status_t err;
38 	for( int16 i = fCamerasAddons.CountItems()-1; i >= 0; --i )
39 	{
40 		CamDeviceAddon *ao = (CamDeviceAddon *)fCamerasAddons.ItemAt(i);
41 		PRINT((CH ": checking %s for support..." CT, ao->BrandName()));
42 		err = ao->Sniff(_device);
43 		if (err < B_OK)
44 			continue;
45 		CamDevice *cam = ao->Instantiate(*this, _device);
46 		PRINT((CH ": found camera %s:%s!" CT, cam->BrandName(), cam->ModelName()));
47 		err = cam->InitCheck();
48 		if (err >= B_OK)
49 		{
50 			fCameras.AddItem(cam);
51 			fAddon->CameraAdded(cam);
52 			return B_OK;
53 		}
54 		PRINT((CH " error 0x%08lx" CT, err));
55 	}
56 	return B_ERROR;
57 }
58 
59 // ---------------------------------------------------------------------------
60 void
61 CamRoster::DeviceRemoved(BUSBDevice* _device)
62 {
63 	PRINT((CH "()" CT));
64 	for(uint32 i = 0; i < fCameras.CountItems(); ++i)
65 	{
66 		CamDevice* cam = (CamDevice *)fCameras.ItemAt(i);
67 		if( cam->Matches(_device) )
68 		{
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 "(): %d 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 	// FIXME
146 	return B_ERROR;
147 	int32 index;
148 	int32 sclass;
149 	status_t err;
150 	CamDeviceAddon *addon;
151 	status_t (*get_webcam_addon_func)(WebCamMediaAddOn* webcam, CamDeviceAddon **addon);
152 	for (index = 0; get_nth_image_symbol(fAddon->ImageID(),
153 										index, NULL, NULL,
154 										&sclass,
155 										(void **)&get_webcam_addon_func) == B_OK; index++) {
156 		PRINT((CH ": got sym" CT));
157 //		if (sclass != B_SYMBOL_TYPE_TEXT)
158 //			continue;
159 		err = (*get_webcam_addon_func)(fAddon, &addon);
160 		PRINT((CH ": Loaded addon '%s' with error 0x%08lx" CT, (err>0)?NULL:addon->BrandName(), err));
161 	}
162 	return B_OK;
163 }
164 
165 // ---------------------------------------------------------------------------
166 // ---------------------------------------------------------------------------
167