Skip to content

Commit

Permalink
List device names (not just filenames)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsheeler committed Apr 11, 2018
1 parent 87d0e25 commit 03e969e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ LFLAGS = $(shell pkg-config --libs cairo) \
-lccv -lm -lpng -ljpeg -lswscale -lavutil -lswresample \
-lavformat -lavcodec -lpthread -ljack
LIBS =
CFLAGS = -O3 -ffast-math -Wall
#CFLAGS =-g -Wall
#CFLAGS = -O3 -ffast-math -Wall
CFLAGS =-g -Wall
CFLAGS += $(shell pkg-config --cflags pangocairo) \
$(shell pkg-config --cflags gtk+-3.0) \
$(shell pkg-config --cflags fftw3)
Expand Down
25 changes: 21 additions & 4 deletions v4l2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,13 @@ static bool sort_on_device_name(const std::string &s1, const std::string &s2)



void V4l2::list_devices(std::vector<std::string> &files) {
void V4l2::list_devices(std::map<std::string, std::string> &cards) {
DIR *dp;
struct dirent *ep;
dev_map links;
dev_map cards;
struct v4l2_format fmt;

struct v4l2_capability vcap;
std::vector<std::string> files;
dp = opendir("/dev");
if (dp == NULL) {
perror ("Couldn't open the directory");
Expand Down Expand Up @@ -443,6 +443,7 @@ void V4l2::list_devices(std::vector<std::string> &files) {
char link[64+1];
int link_len;
std::string target;
;

link_len = readlink(iter->c_str(), link, 64);
if (link_len < 0) { /* Not a link or error */
Expand All @@ -465,9 +466,25 @@ void V4l2::list_devices(std::vector<std::string> &files) {
links[target] = *iter;
else
links[target] += ", " + *iter;
iter = files.erase(iter);
}

std::sort(files.begin(), files.end(), sort_on_device_name);
for (dev_vec::iterator iter = files.begin();
iter != files.end(); ++iter) {
int fd = open(iter->c_str(), O_RDWR);
std::string bus_info;

if (fd < 0)
continue;
int err = ioctl(fd, VIDIOC_QUERYCAP, &vcap);
close(fd);
if (err)
continue;
bus_info = (const char *)vcap.bus_info;
if (cards[*iter].empty())
cards[*iter] += *iter + " -- " +
std::string((char *)vcap.card) + " (" + bus_info + ")";
if (!(links[*iter].empty()))
cards[*iter] += " <- " + links[*iter];
}
}
3 changes: 2 additions & 1 deletion v4l2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <poll.h>
#include <assert.h>
#include <map>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
Expand Down Expand Up @@ -50,7 +51,7 @@ class V4l2 : public Drawable {
void init_mmap();
bool render(std::vector<cairo_t *> &contexts);
static void get_dimensions(std::string device, std::vector<std::pair<int, int> > &w_h);
static void list_devices(std::vector<std::string> &files);
static void list_devices(std::map<std::string, std::string> &files);
private:
static void* thread(void *v);
static int xioctl(int fh, int request, void *arg);
Expand Down
17 changes: 7 additions & 10 deletions v4l2_wayland.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ static gboolean set_modes_cb(GtkWidget *widget, gpointer data) {
GtkComboBoxText *resolution_combo = (GtkComboBoxText *) data;
gtk_combo_box_text_remove_all(resolution_combo);
std::vector<std::pair<int, int>> width_height;
gchar *name = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widget));
const gchar *name = gtk_combo_box_get_active_id(GTK_COMBO_BOX(widget));
V4l2::get_dimensions(name, width_height);
int index = 0;

Expand Down Expand Up @@ -1461,23 +1461,20 @@ static gboolean camera_cb(GtkWidget *, gpointer data) {
GtkWidget *combo;
GtkWidget *resolution_combo;
int res;
int index;
GtkDialogFlags flags = (GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT);
dialog = gtk_dialog_new_with_buttons("Open Camera", GTK_WINDOW(dd->ctl_window),
flags, "Open", GTK_RESPONSE_ACCEPT, "Cancel", GTK_RESPONSE_REJECT, NULL);
dialog_content = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
combo = gtk_combo_box_text_new();

std::vector<std::string> files;
std::map<std::string, std::string> files;
V4l2::list_devices(files);
index = 0;
for (std::vector<std::string>::iterator it = files.begin();
for (std::map<std::string, std::string>::iterator it = files.begin();
it != files.end(); ++it) {
char index_str[64];
memset(index_str, '\0', sizeof(index_str));
snprintf(index_str, 63, "%d", index);
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo), index_str, (*it).c_str());
++index;
snprintf(index_str, 63,"%s", it->first.c_str());
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(combo), index_str, it->second.c_str());
}
resolution_combo = gtk_combo_box_text_new();
gtk_container_add(GTK_CONTAINER(dialog_content), combo);
Expand All @@ -1490,14 +1487,14 @@ static gboolean camera_cb(GtkWidget *, gpointer data) {
gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
res = gtk_dialog_run(GTK_DIALOG(dialog));
if (res == GTK_RESPONSE_ACCEPT) {
gchar *name = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(combo));
const gchar *name = gtk_combo_box_get_active_id(GTK_COMBO_BOX(combo));
gchar *res_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(resolution_combo));
gchar *w, *h;
w = strsep(&res_str, "x");
h = strsep(&res_str, "x");
for(int i = 0; i < MAX_NUM_V4L2; i++) {
if (!dd->v4l2[i].allocated) {
dd->v4l2[i].create(dd, name, atof(w), atof(h), dd->next_z++);
dd->v4l2[i].create(dd, (char *)name, atof(w), atof(h), dd->next_z++);
break;
}
}
Expand Down

0 comments on commit 03e969e

Please sign in to comment.