libexplain  1.4.D001
libexplain/buffer/video_channel.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2011, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as
00007  * published by the Free Software Foundation; either version 3 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/errno.h>
00020 #include <libexplain/ac/linux/videodev.h>
00021 #include <libexplain/ac/string.h>
00022 #include <libexplain/ac/sys/ioctl.h>
00023 
00024 #include <libexplain/buffer/int.h>
00025 #include <libexplain/buffer/int16_t.h>
00026 #include <libexplain/buffer/pointer.h>
00027 #include <libexplain/buffer/video_channel_flags.h>
00028 #include <libexplain/buffer/video_channel.h>
00029 #include <libexplain/buffer/video_channel_type.h>
00030 #include <libexplain/is_efault.h>
00031 
00032 
00033 #if defined(VIDIOCGCHAN) || defined(VIDIOCSCHAN)
00034 
00035 void
00036 explain_buffer_video_channel(explain_string_buffer_t *sb,
00037     const struct video_channel *data, int extra)
00038 {
00039     if (explain_is_efault_pointer(data, sizeof(*data)))
00040     {
00041         explain_buffer_pointer(sb, data);
00042         return;
00043     }
00044 
00045     explain_string_buffer_puts(sb, "{ channel = ");
00046     explain_buffer_int(sb, data->channel);
00047     if (extra)
00048     {
00049         explain_string_buffer_puts(sb, ", name = ");
00050         explain_string_buffer_puts_quoted_n(sb, data->name, sizeof(data->name));
00051         explain_string_buffer_puts(sb, ", tuners = ");
00052         explain_buffer_int(sb, data->tuners);
00053         explain_string_buffer_puts(sb, ", flags = ");
00054         explain_buffer_video_channel_flags(sb, data->flags);
00055         explain_string_buffer_puts(sb, ", type = ");
00056         explain_buffer_video_channel_type(sb, data->type);
00057         explain_string_buffer_puts(sb, ", norm = ");
00058         explain_buffer_uint16_t(sb, data->norm);
00059     }
00060     explain_string_buffer_puts(sb, " }");
00061 }
00062 
00063 
00064 int
00065 explain_video_channel_get_n(int fildes)
00066 {
00067     int lo = 0;
00068     int hi = 200;
00069     for (;;)
00070     {
00071         int mid = (lo + hi) / 2;
00072         struct video_channel qry;
00073         memset(&qry, 0, sizeof(qry));
00074         qry.channel = mid;
00075         if (ioctl(fildes, VIDIOCGCHAN, &qry) >= 0)
00076         {
00077             if (hi <= 0 && lo <= 0)
00078                 return -1;
00079             /* mid < noutputs */
00080             lo = mid + 1;
00081             if (lo > hi)
00082                 return lo;
00083         }
00084         else if (errno != EINVAL)
00085         {
00086             return -1;
00087         }
00088         else
00089         {
00090             if (hi <= 0 && lo <= 0)
00091                 return -1;
00092             /* mid >= noutputs */
00093             hi = mid - 1;
00094             if (lo >= hi)
00095                 return lo;
00096         }
00097     }
00098 }
00099 
00100 #endif
00101 
00102 /* vim: set ts=8 sw=4 et : */