int ia_play(t_player *player, t_map map) { t_msg msg; int ret; lock(); if (check_dead(player, map) == FAILURE) ret = FAILURE; else if (read_msg(CASE_DEAD_MSG_ID + player->pos.x + player->pos.y * MAP_SIZE, &msg, IPC_NOWAIT) == SUCCESS) ret = ia_exit(player, map); else if (read_msg(TEAM_MSG_ID + player->id_team, &msg, IPC_NOWAIT) == SUCCESS) { if (msg.type == MSG_GETOUT) ret = ia_exit(player, map); else if (msg.type == MSG_GO_TO_ALLY || msg.type == MSG_GO_TO_ENNEMY) ret = ia_path_finding(player, map, msg.type); } else { what_msg_send(player, map); ret = ia_path_finding(player, map, MSG_GO_TO_ENNEMY); } if (count_nb_ennemy(map, player, &ret) == FAILURE) printf("Team [%i] WON!\n", player->id_team); return (unlock() == SUCCESS && ret); }
static void open_device (ia_v4l_t* v) { struct stat st; char* dev_name = v->dev_name; int fd; if (-1 == ia_stat (dev_name, &st)) { ia_fprintf (stderr, "Cannot identify '%s': %d, %s\n", dev_name, errno, ia_strerror (errno)); ia_exit (EXIT_FAILURE); } if (!S_ISCHR (st.st_mode)) { ia_fprintf (stderr, "%s is no device\n", dev_name); ia_exit (EXIT_FAILURE); } fd = ia_open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0); if (-1 == fd) { ia_fprintf (stderr, "Cannot open '%s': %d, %s\n", dev_name, errno, ia_strerror (errno)); ia_exit (EXIT_FAILURE); } v->fd = fd; }
static void errno_exit (const char * s) { ia_fprintf (stderr, "%s error %d, %s\n", s, errno, ia_strerror (errno)); ia_exit (EXIT_FAILURE); }
static void init_device (ia_v4l_t* v) { struct video_capability vcap; struct video_window vwind; struct video_picture vpict; unsigned int i; int fd = v->fd; char* dev_name = v->dev_name; int width = v->width; int height = v->height; io_method io = v->io; /* query the device for its capabilities */ CLEAR (vcap); if (-1 == xioctl (fd, VIDIOCGCAP, &vcap)) { errno_exit ("VIDIOCGCAP"); } /* make sure it supports capture to memory * and has at least one video channel */ if (VID_TYPE_CAPTURE != vcap.type) { ia_fprintf (stderr, "%s does not support " "capture to memory\n", dev_name); ia_exit (EXIT_FAILURE); } /* make sure it has at least one video channel */ if (vcap.channels < 1) { ia_fprintf (stderr, "%s does not have any video channels\n", dev_name); ia_exit (EXIT_FAILURE); } /* if no width was provided then choose the maximum */ if (width <= 0) { width = vcap.maxwidth; /* make sure any provided width is within bounds */ } else if (vcap.maxwidth < width || width < vcap.minwidth) { ia_fprintf (stderr, "%s supports a maximum resolution of %dx%d\n", dev_name, vcap.maxwidth, vcap.maxheight); ia_exit (EXIT_FAILURE); } /* if no height was provided then choose the maximum */ if (height <= 0) { height = vcap.maxheight; /* make sure any provided height is within bounds */ } else if (vcap.maxheight < height || height < vcap.minheight) { ia_fprintf (stderr, "%s supports a maximum resolution of %dx%d\n", dev_name, vcap.maxwidth, vcap.maxheight); ia_exit (EXIT_FAILURE); } /* set capture window */ CLEAR (vwind); vwind.x = 0; vwind.y = 0; vwind.width = width; vwind.height = height; if (-1 == xioctl (fd, VIDIOCSWIN, &vwind)) { errno_exit ("VIDIOCSWIN"); } /* make sure the device took our settings */ CLEAR (vwind); if (-1 == xioctl (fd, VIDIOCGWIN, &vwind)) { errno_exit ("VIDIOCGWIN"); } /* if the settings didnt stick then exit with failure */ if (0 != vwind.x || 0 != vwind.y || width != vwind.width || height != vwind.height) { ia_fprintf (stderr, "%s did not hold our settings\n", dev_name); ia_exit (EXIT_FAILURE); } /* query image properties */ CLEAR (vpict); if (-1 == xioctl (fd, VIDIOCGPICT, &vpict)) { errno_exit ("VIDIOCGPICT"); } /* set the picture format */ i = 7; vpict.palette = video_formats[i].palette; vpict.depth = video_formats[i].depth; if (-1 == xioctl (fd, VIDIOCSPICT, &vpict)) { for (i = 0; i < vformat_num; i++) { vpict.palette = video_formats[i].palette; vpict.depth = video_formats[i].depth; if (-1 != xioctl (fd, VIDIOCSPICT, &vpict)) { break; } } if (vformat_num <= i) { fprintf (stderr, "%s does not provide" "any formats that I support\n", dev_name); ia_exit (EXIT_FAILURE); } } v->width = width; v->height = height; v->pix_fmt = i; v->ia_pix_fmt = video_formats[i].pix_fmt; switch (io) { case IO_METHOD_MMAP: init_mmap (v); break; case IO_METHOD_READ: v->n_buffers = 1; default: break; } }
static void init_mmap (ia_v4l_t* v) { struct video_mbuf vmbuf; int fd = v->fd; char* dev_name = v->dev_name; int width = v->width; int height = v->height; int pix_fmt = v->pix_fmt; struct buffer* buffers; unsigned int n_buffers; void* buffer; CLEAR (vmbuf); /* query device for buffer parameters */ if (-1 == xioctl (fd, VIDIOCGMBUF, &vmbuf)) { switch (errno) { case EINVAL: ia_fprintf (stderr, "%s does not support " "memory mapping\n", dev_name); ia_exit (EXIT_FAILURE); default: errno_exit ("VIDIOCGMBUF"); } } if (vmbuf.frames < 1) { ia_fprintf (stderr, "Insufficient buffer memory on %s\n", dev_name); ia_exit (EXIT_FAILURE); } if (NULL == (buffers = ia_calloc (vmbuf.frames, sizeof (struct buffer)))) { ia_fprintf (stderr, "Out of memory\n"); ia_exit (EXIT_FAILURE); } /* mmap a buffer from the device */ if (MAP_FAILED == (buffer = ia_mmap (NULL, vmbuf.size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, vmbuf.offsets[0]))) { /* if MAP_SHARED is not supported */ if (MAP_FAILED == (buffer = ia_mmap (NULL, vmbuf.size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, vmbuf.offsets[0]))) { errno_exit ("MMAP"); } } /* try to do things the v4l2 way */ for (n_buffers = 0; n_buffers < vmbuf.frames; ++n_buffers) { buffers[n_buffers].length = width * height * video_formats[pix_fmt].depth / 8; buffers[n_buffers].start = buffer + vmbuf.offsets[n_buffers]; } v->buffers = buffers; v->n_buffers = n_buffers; v->mmap_buffer = buffer; v->mmap_length = vmbuf.size; }