2 Phlips webcam driver for V4L 1
3 Copyright (C) 2005 by Jasem Mutlaq
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
34 #include "pwc-ioctl.h"
36 #include "../eventloop.h"
38 #define ERRMSG_SIZ 1024
61 int V4L1_PWC::connectCam(const char * devpath
, char *errmsg
)
63 options
= (ioNoBlock
|ioUseSelect
|haveBrightness
|haveContrast
|haveColor
);
64 struct pwc_probe probe
;
65 bool IsPhilips
= false;
71 if (-1 == (fd
=open(devpath
,
72 O_RDONLY
| ((options
& ioNoBlock
) ? O_NONBLOCK
: 0)))) {
74 strncpy(errmsg
, strerror(errno
), 1024);
75 cerr
<< strerror(errno
);
79 cerr
<< "Device opened" << endl
;
82 if (-1 == ioctl(fd
,VIDIOCGCAP
,&capability
)) {
83 cerr
<< "Error: ioctl (VIDIOCGCAP)" << endl
;
84 strncpy(errmsg
, "ioctl (VIDIOCGCAP)", 1024);
87 if (-1 == ioctl (fd
, VIDIOCGWIN
, &window
)) {
88 cerr
<< "Error: ioctl (VIDIOCGWIN)" << endl
;
89 strncpy(errmsg
, "ioctl (VIDIOCGWIN)", 1024);
92 if (-1 == ioctl (fd
, VIDIOCGPICT
, &picture_format
)) {
93 cerr
<< "Error: ioctl (VIDIOCGPICT)" << endl
;
94 strncpy(errmsg
, "ioctl (VIDIOCGPICT)", 1024);
100 // Check to see if it's really a philips webcam
101 if (ioctl(fd
, VIDIOCPWCPROBE
, &probe
) == 0)
103 if (!strcmp(capability
.name
,probe
.name
))
111 cerr
<< "Philips webcam type " << type_
<< " detected" << endl
;
114 strncpy(errmsg
, "No Philips webcam detected.", 1024);
118 cerr
<< "initial size w:" << window
.width
<< " -- h: " << window
.height
<< endl
;
123 setWhiteBalanceMode(PWC_WB_AUTO
, errmsg
);
128 cerr
<< "All successful, returning\n";
132 void V4L1_PWC::checkSize(int & x
, int & y
)
134 if (x
>=capability
.maxwidth
&& y
>= capability
.maxheight
)
136 x
=capability
.maxwidth
;
137 y
=capability
.maxheight
;
138 } else if (x
>=352 && y
>=288 && type_
<700) {
140 } else if (x
>=320 && y
>= 240) {
142 } else if (x
>=176 && y
>=144 && type_
<700 ) {
144 } else if (x
>=160 && y
>=120 ) {
147 x
=capability
.minwidth
;
148 y
=capability
.minheight
;
152 bool V4L1_PWC::setSize(int x
, int y
)
156 char msg
[ERRMSG_SIZ
];
160 oldY
= window
.height
;
165 if (ioctl (fd
, VIDIOCSWIN
, &window
))
167 snprintf(msg
, ERRMSG_SIZ
, "ioctl(VIDIOCSWIN) %s", strerror(errno
));
173 ioctl (fd
, VIDIOCGWIN
, &window
);
175 cerr
<< "New size is x=" << window
.width
<< " " << "y=" << window
.height
<<endl
;
182 int V4L1_PWC::saveSettings(char *errmsg
)
184 if (ioctl(fd
, VIDIOCPWCSUSER
)==-1)
186 snprintf(errmsg
, ERRMSG_SIZ
, "VIDIOCPWCSUSER %s", strerror(errno
));
193 void V4L1_PWC::restoreSettings()
195 ioctl(fd
, VIDIOCPWCRUSER
);
196 getPictureSettings();
199 void V4L1_PWC::restoreFactorySettings()
201 ioctl(fd
, VIDIOCPWCFACTORY
);
202 getPictureSettings();
205 int V4L1_PWC::setGain(int val
, char *errmsg
)
207 if(-1==ioctl(fd
, VIDIOCPWCSAGC
, &val
))
209 snprintf(errmsg
, ERRMSG_SIZ
, "VIDIOCPWCSAGC %s", strerror(errno
));
214 cerr
<< "setGain "<<val
<<endl
;
219 int V4L1_PWC::getGain()
222 char msg
[ERRMSG_SIZ
];
226 if (-1==ioctl(fd
, VIDIOCPWCGAGC
, &gain
))
228 //perror("VIDIOCPWCGAGC");
229 snprintf(msg
, ERRMSG_SIZ
, "VIDIOCPWCGAGC %s", strerror(errno
));
242 //cerr << "get gain "<<gain<<endl;
243 if (gain
< 0) gain
*=-1;
247 int V4L1_PWC::setExposure(int val
, char *errmsg
)
249 //cout << "set exposure "<<val<<"\n";
251 if (-1==ioctl(fd
, VIDIOCPWCSSHUTTER
, &val
))
253 snprintf(errmsg
, ERRMSG_SIZ
, "VIDIOCPWCSSHUTTER %s", strerror(errno
));
260 void V4L1_PWC::setCompression(int val
)
262 ioctl(fd
, VIDIOCPWCSCQUAL
, &val
);
265 int V4L1_PWC::getCompression()
268 ioctl(fd
, VIDIOCPWCGCQUAL
, &gain
);
269 if (gain
< 0) gain
*=-1;
273 int V4L1_PWC::setNoiseRemoval(int val
, char *errmsg
)
275 if (-1 == ioctl(fd
, VIDIOCPWCSDYNNOISE
, &val
))
277 snprintf(errmsg
, ERRMSG_SIZ
, "VIDIOCPWCGDYNNOISE %s", strerror(errno
));
284 int V4L1_PWC::getNoiseRemoval()
287 char msg
[ERRMSG_SIZ
];
289 if (-1 == ioctl(fd
, VIDIOCPWCGDYNNOISE
, &gain
))
291 snprintf(msg
, ERRMSG_SIZ
, "VIDIOCPWCGDYNNOISE %s", strerror(errno
));
295 cout
<<"get noise = "<<gain
<<endl
;
299 int V4L1_PWC::setSharpness(int val
, char *errmsg
)
301 if (-1 == ioctl(fd
, VIDIOCPWCSCONTOUR
, &val
))
303 snprintf(errmsg
, ERRMSG_SIZ
, "VIDIOCPWCSCONTOUR %s", strerror(errno
));
310 int V4L1_PWC::getSharpness()
313 char msg
[ERRMSG_SIZ
];
315 if (-1 == ioctl(fd
, VIDIOCPWCGCONTOUR
, &gain
))
317 snprintf(msg
, ERRMSG_SIZ
, "VIDIOCPWCGCONTOUR %s", strerror(errno
));
321 cout
<<"get sharpness = "<<gain
<<endl
;
325 int V4L1_PWC::setBackLight(bool val
, char *errmsg
)
329 if (-1 == ioctl(fd
,VIDIOCPWCSBACKLIGHT
, & val
?&on
:&off
))
331 snprintf(errmsg
, ERRMSG_SIZ
, "VIDIOCPWCSBACKLIGHT %s", strerror(errno
));
338 bool V4L1_PWC::getBackLight()
341 char msg
[ERRMSG_SIZ
];
343 if (-1 == ioctl(fd
,VIDIOCPWCGBACKLIGHT
, & val
))
345 snprintf(msg
, ERRMSG_SIZ
, "VIDIOCPWCSBACKLIGHT %s", strerror(errno
));
352 int V4L1_PWC::setFlicker(bool val
, char *errmsg
)
356 if (-1 == ioctl(fd
,VIDIOCPWCSFLICKER
, val
?&on
:&off
))
358 snprintf(errmsg
, ERRMSG_SIZ
, "VIDIOCPWCSFLICKER %s", strerror(errno
));
366 bool V4L1_PWC::getFlicker()
369 char msg
[ERRMSG_SIZ
];
371 if (-1 == ioctl(fd
,VIDIOCPWCGFLICKER
, & val
))
373 snprintf(msg
, ERRMSG_SIZ
, "VIDIOCPWCGFLICKER %s", strerror(errno
));
380 void V4L1_PWC::setGama(int val
)
382 picture_format
.whiteness
=val
;
383 setPictureSettings();
386 int V4L1_PWC::getGama()
388 return picture_format
.whiteness
;
391 int V4L1_PWC::setFrameRate(int value
, char *errmsg
)
393 window
.flags
= (window
.flags
& ~PWC_FPS_MASK
) | ((value
<< PWC_FPS_SHIFT
) & PWC_FPS_MASK
);
394 if (ioctl(fd
, VIDIOCSWIN
, &window
))
396 snprintf(errmsg
, ERRMSG_SIZ
, "setFrameRate %s", strerror(errno
));
400 ioctl(fd
, VIDIOCGWIN
, &window
);
404 //emit exposureTime(multiplicateur_/(double)getFrameRate());
407 int V4L1_PWC::getFrameRate()
409 return ((window
.flags
&PWC_FPS_FRMASK
)>>PWC_FPS_SHIFT
);
412 int V4L1_PWC::getWhiteBalance()
414 char msg
[ERRMSG_SIZ
];
415 struct pwc_whitebalance tmp_whitebalance
;
416 tmp_whitebalance
.mode
= tmp_whitebalance
.manual_red
= tmp_whitebalance
.manual_blue
= tmp_whitebalance
.read_red
= tmp_whitebalance
.read_blue
= PWC_WB_AUTO
;
418 if (ioctl(fd
, VIDIOCPWCGAWB
, &tmp_whitebalance
))
420 snprintf(msg
, ERRMSG_SIZ
, "getWhiteBalance %s", strerror(errno
));
426 cout
<< "mode="<<tmp_whitebalance
.mode
427 <<" mr="<<tmp_whitebalance
.manual_red
428 <<" mb="<<tmp_whitebalance
.manual_blue
429 <<" ar="<<tmp_whitebalance
.read_red
430 <<" ab="<<tmp_whitebalance
.read_blue
433 /* manual_red and manual_blue are garbage :-( */
434 whiteBalanceMode_
=tmp_whitebalance
.mode
;
437 return whiteBalanceMode_
;
439 /*switch(whiteBalanceMode_) {
441 setProperty("WhiteBalanceMode","Indor");
444 setProperty("WhiteBalanceMode","Outdoor");
447 setProperty("WhiteBalanceMode","Neon");
450 setProperty("WhiteBalanceMode","Manual");
451 whiteBalanceRed_=tmp_whitebalance.manual_red;
452 whiteBalanceBlue_=tmp_whitebalance.manual_blue;
456 setProperty("WhiteBalanceMode","Auto");
457 whiteBalanceRed_=tmp_whitebalance.read_red;
458 whiteBalanceBlue_=tmp_whitebalance.read_blue;
461 setProperty("WhiteBalanceMode","???");
464 emit whiteBalanceModeChange(whiteBalanceMode_);
466 if (((whiteBalanceMode_ == PWC_WB_AUTO) && liveWhiteBalance_)
467 || whiteBalanceMode_ != PWC_WB_AUTO) {
468 setProperty("WhiteBalanceRed",whiteBalanceRed_);
469 emit whiteBalanceRedChange(whiteBalanceRed_);
470 setProperty("WhiteBalanceBlue",whiteBalanceBlue_);
471 emit whiteBalanceBlueChange(whiteBalanceBlue_);
473 remoteCTRLWBred_->show();
474 remoteCTRLWBblue_->show();
478 remoteCTRLWBred_->hide();
479 remoteCTRLWBblue_->hide();
485 int V4L1_PWC::setWhiteBalance(char *errmsg
)
487 struct pwc_whitebalance wb
;
488 wb
.mode
=whiteBalanceMode_
;
489 if (wb
.mode
== PWC_WB_MANUAL
)
491 wb
.manual_red
=whiteBalanceRed_
;
492 wb
.manual_blue
=whiteBalanceBlue_
;
495 if (ioctl(fd
, VIDIOCPWCSAWB
, &wb
))
497 snprintf(errmsg
, ERRMSG_SIZ
, "setWhiteBalance %s", strerror(errno
));
504 int V4L1_PWC::setWhiteBalanceMode(int val
, char *errmsg
)
506 if (val
== whiteBalanceMode_
)
507 return whiteBalanceMode_
;
509 if (val
!= PWC_WB_AUTO
)
511 if ( val
!= PWC_WB_MANUAL
)
513 whiteBalanceMode_
=val
;
514 if (setWhiteBalance(errmsg
) < 0)
518 //whiteBalanceMode_=PWC_WB_AUTO;
519 whiteBalanceMode_
= val
;
520 if (setWhiteBalance(errmsg
) < 0)
526 if (val != PWC_WB_AUTO
527 || ( liveWhiteBalance_ && (val ==PWC_WB_AUTO))) {
528 remoteCTRLWBred_->show();
529 remoteCTRLWBblue_->show();
531 remoteCTRLWBred_->hide();
532 remoteCTRLWBblue_->hide();
536 whiteBalanceMode_
=val
;
537 if (setWhiteBalance(errmsg
) < 0)
544 int V4L1_PWC::setWhiteBalanceRed(int val
, char *errmsg
)
546 whiteBalanceMode_
= PWC_WB_MANUAL
;
547 whiteBalanceRed_
=val
;
548 if (setWhiteBalance(errmsg
) < 0)
554 int V4L1_PWC::setWhiteBalanceBlue(int val
, char *errmsg
)
556 whiteBalanceMode_
= PWC_WB_MANUAL
;
557 whiteBalanceBlue_
=val
;
558 if (setWhiteBalance(errmsg
) < 0)