1 // This is core/vidl/vidl_v4l2_control.h
2 #ifndef vidl_v4l2_control_h_
3 #define vidl_v4l2_control_h_
4 //:
5 // \file
6 // \brief A base control class for user controls
7 //
8 // \author Antonio Garrido
9 // \verbatim
10 //  Modifications
11 //     10 Nov 2008 Created (A. Garrido)
12 //\endverbatim
13 
14 #include <string>
15 #include <vector>
16 #ifdef _MSC_VER
17 #  include <vcl_msvc_warnings.h>
18 #endif
19 
20 extern "C" {
21 //#include <asm/types.h>          /* for videodev2.h */
22 #include <sys/time.h>
23 #include <linux/videodev2.h>
24 };
25 
26 //: A base class for handle a control.
27 class vidl_v4l2_control
28 {
29   protected:
30     int fd;
31     struct v4l2_queryctrl ctrl_;
vidl_v4l2_control(const v4l2_queryctrl & ctr,int f)32     vidl_v4l2_control(const v4l2_queryctrl& ctr, int f):  fd(f), ctrl_(ctr) {}
33     void set_value(int v) const;
34     int get_value() const;
35   public:
~vidl_v4l2_control()36     virtual ~vidl_v4l2_control() {}
37     //: Factory method to create new controls
38     // \param ctr details from VIDIOC_QUERYCTRL
39     // \param f associated file descriptor
40     // \return pointer to base class of new control
41     static vidl_v4l2_control * new_control(const v4l2_queryctrl& ctr, int f);
42     //: Type of control
43     // \return type as indicated in v4l2 specification
type()44     v4l2_ctrl_type type() const { return (v4l2_ctrl_type)ctrl_.type; }
45     //: Name of control
46     // \return name the driver assign to the control
name()47     std::string name() const { return  (const char *) ctrl_.name; }
48     //: A 1-line brief description
49     virtual std::string description() const= 0;
50     //: Id of control
51     // \return ID (for example, V4L2_CID_BRIGHTNESS corresponds to brightness)
id()52     int id() const { return ctrl_.id; }
53     //: Control is read only
read_only()54     bool read_only() const
55     {
56 #ifdef V4L2_CTRL_FLAG_READ_ONLY
57       return ctrl_.flags & V4L2_CTRL_FLAG_READ_ONLY;
58 #else
59       return false;
60 #endif
61     }
62     //: Control can change value of other controls
affect_other_controls()63     bool affect_other_controls() const
64     {
65 #ifdef V4L2_CTRL_FLAG_UPDATE
66       return ctrl_.flags & V4L2_CTRL_FLAG_UPDATE;
67 #else
68       return false;
69 #endif
70      }
71     //: Reset control
72     // \note no-op fot button controls
reset()73     virtual void reset() const {}
74 };
75 
76 //: A class for handle a control of type integer
77 class vidl_v4l2_control_integer: public vidl_v4l2_control
78 {
79   public:
vidl_v4l2_control_integer(const v4l2_queryctrl & ctr,int f)80     vidl_v4l2_control_integer(const v4l2_queryctrl& ctr, int f): vidl_v4l2_control(ctr,f) {}
81     //: Minimum value of the control
minimum()82     int minimum() const { return ctrl_.minimum; }
83     //: Maximum value of the control
maximum()84     int maximum() const { return ctrl_.maximum; }
85     //: Step size
86     // Indicates the increment between values which are actually different on the hardware
step()87     int step() const { return ctrl_.step; }
88     //: Default value of this control
default_value()89     int default_value() const { return ctrl_.default_value; }
90     //: Set the value of the control
91     // \param value to be set in range determined by driver
92     void set(int value) const;
93     //: Change control
94     // \param value to be set in range 0-100
95     void set_100 ( int value) const;
96     //: Get the value of the control
97     // \return value in range determined by driver
get()98     int get() const { return get_value(); }
99     //: Get the value of the control
100     // \return value in range 0-100
get_100()101     int get_100() const { return (get_value()-ctrl_.minimum)*100/(ctrl_.maximum-ctrl_.minimum); }
102     //: A 1-line brief description
103     virtual std::string description() const;
104     //: Reset control
reset()105     virtual void reset() const { set(default_value()); }
106 };
107 
108 //: A class for handle a control of type menu
109 class vidl_v4l2_control_menu: public vidl_v4l2_control
110 {
111     std::vector<std::string> items;
112   public:
113     vidl_v4l2_control_menu(const v4l2_queryctrl& ctr, int f);
114     //: Number of items
115     // \return number of entries in the menu
n_items()116     unsigned int n_items() const { return items.size();}
117     //: Item i
118     // \return string assigned to item i
get(unsigned int i)119     std::string get(unsigned int i) const { return items[i]; }
120     //: Select item i
set(unsigned int i)121     void set(unsigned int i) const { if (i<n_items()) set_value((int) i); }
122     //: Get the value of the control
123     // \return index in the menu
get()124     unsigned int get() const { return (unsigned int) get_value(); }
125     //: Default value of this control
default_value()126     unsigned int default_value() const { return ctrl_.default_value; }
127     //: A 1-line brief description
128     virtual std::string description() const;
129     //: Reset control
reset()130     virtual void reset() const { set(default_value()); }
131 };
132 
133 //: A class for handle a control of type boolean
134 class vidl_v4l2_control_boolean: public vidl_v4l2_control
135 {
136   public:
vidl_v4l2_control_boolean(const v4l2_queryctrl & ctr,int f)137     vidl_v4l2_control_boolean(const v4l2_queryctrl& ctr, int f): vidl_v4l2_control(ctr,f) {}
138     //: Set the value of the control
set(bool v)139     void set(bool v) const { set_value(v?1:0); }
140     //: Get the value of the control
get()141     bool get() const { return get_value(); }
142     //: Default value of this control
default_value()143     bool default_value() const { return ctrl_.default_value; }
144     //: A 1-line brief description
description()145     virtual std::string description() const
146        { return "Control \""+name()+"\": boolean (default: "+(default_value()?"true":"false")+")"; }
147     //: Reset control
reset()148     virtual void reset() const { set(default_value()); }
149 };
150 
151 //: A class for handle a control of type button
152 class vidl_v4l2_control_button: public vidl_v4l2_control
153 {
154   public:
vidl_v4l2_control_button(const v4l2_queryctrl & ctr,int f)155     vidl_v4l2_control_button(const v4l2_queryctrl& ctr, int f): vidl_v4l2_control(ctr,f) {}
156     //: Push button
push()157     void push() const { set_value(1); }
158     //: A 1-line brief description
description()159     virtual std::string description() const { return "Control \""+name()+"\": button"; }
160 };
161 
162 #endif // vidl_v4l2_control_h_
163