vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / media-add-ons / usb_webcam / CamRoster.cpp
blob4054e6c3beadbd54c82ec79f01acce88ce4fdc45
1 /*
2 * Copyright 2004-2008, François Revol, <revol@free.fr>.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "CamRoster.h"
8 #include "AddOn.h"
9 #include "CamDevice.h"
10 #include "CamDebug.h"
12 #include <OS.h>
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
21 CamRoster::CamRoster(WebCamMediaAddOn* _addon)
22 : BUSBRoster(),
23 fLocker("WebcamRosterLock"),
24 fAddon(_addon)
26 PRINT((CH "()" CT));
27 LoadInternalAddons();
28 LoadExternalAddons();
32 CamRoster::~CamRoster()
37 status_t
38 CamRoster::DeviceAdded(BUSBDevice* _device)
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;
56 PRINT((CH " error 0x%08lx" CT, err));
58 return B_ERROR;
62 void
63 CamRoster::DeviceRemoved(BUSBDevice* _device)
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;
81 uint32
82 CamRoster::CountCameras()
84 int32 count;
85 fLocker.Lock();
86 PRINT((CH "(): %ld cameras" CT, fCameras.CountItems()));
87 count = fCameras.CountItems();
88 fLocker.Unlock();
89 return count;
93 bool
94 CamRoster::Lock()
96 PRINT((CH "()" CT));
97 return fLocker.Lock();
101 void
102 CamRoster::Unlock()
104 PRINT((CH "()" CT));
105 fLocker.Unlock();
106 return;
110 CamDevice*
111 CamRoster::CameraAt(int32 index)
113 PRINT((CH "()" CT));
114 return (CamDevice *)fCameras.ItemAt(index);
118 status_t
119 CamRoster::LoadInternalAddons()
121 PRINT((CH "()" CT));
122 CamDeviceAddon *addon;
123 status_t err;
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())); \
133 #include "CamInternalAddons.h"
134 #undef B_WEBCAM_MKINTFUNC
137 return B_OK;
141 status_t
142 CamRoster::LoadExternalAddons()
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));
162 return B_OK;