CGR Localization
 All Classes Namespaces Files Functions Variables Macros Pages
core.c
1 /*
2  * This file is part of the OpenKinect Project. http://www.openkinect.org
3  *
4  * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
5  * for details.
6  *
7  * This code is licensed to you under the terms of the Apache License, version
8  * 2.0, or, at your option, the terms of the GNU General Public License,
9  * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10  * or the following URLs:
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * http://www.gnu.org/licenses/gpl-2.0.txt
13  *
14  * If you redistribute this file in source form, modified or unmodified, you
15  * may:
16  * 1) Leave this header intact and distribute it under the same terms,
17  * accompanying it with the APACHE20 and GPL20 files, or
18  * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19  * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20  * In all cases you must keep the copyright notice intact and include a copy
21  * of the CONTRIB file.
22  *
23  * Binary distributions must follow the binary distribution requirements of
24  * either License.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 #include "freenect_internal.h"
33 
34 int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx)
35 {
36  *ctx = malloc(sizeof(freenect_context));
37  if (!ctx)
38  return -1;
39 
40  memset(*ctx, 0, sizeof(freenect_context));
41 
42  return fnusb_init(&(*ctx)->usb, usb_ctx);
43 }
44 
45 int freenect_shutdown(freenect_context *ctx)
46 {
47  printf("%s NOT IMPLEMENTED YET\n", __FUNCTION__);
48  return 0;
49 }
50 
51 int freenect_process_events(freenect_context *ctx)
52 {
53  return fnusb_process_events(&ctx->usb);
54 }
55 
56 int freenect_num_devices(freenect_context *ctx)
57 {
58  libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
59  ssize_t cnt = libusb_get_device_list (ctx->usb.ctx, &devs); //get the list of devices
60  if (cnt < 0)
61  return (-1);
62 
63  int nr = 0, i = 0;
64  struct libusb_device_descriptor desc;
65  for (i = 0; i < cnt; ++i)
66  {
67  int r = libusb_get_device_descriptor (devs[i], &desc);
68  if (r < 0)
69  continue;
70  if (desc.idVendor == MS_MAGIC_VENDOR && desc.idProduct == MS_MAGIC_CAMERA_PRODUCT)
71  nr++;
72  }
73 
74  libusb_free_device_list (devs, 1); // free the list, unref the devices in it
75 
76  return (nr);
77 }
78 
79 int freenect_open_device(freenect_context *ctx, freenect_device **dev, int index)
80 {
81  int res;
82  freenect_device *pdev = malloc(sizeof(freenect_device));
83  if (!pdev)
84  return -1;
85 
86  memset(pdev, 0, sizeof(*pdev));
87 
88  pdev->parent = ctx;
89 
90  res = fnusb_open_subdevices(pdev, index);
91 
92  if (res < 0) {
93  free(pdev);
94  return res;
95  } else {
96  *dev = pdev;
97  return 0;
98  }
99 }
100 
101 int freenect_close_device(freenect_device *dev)
102 {
103  printf("%s NOT IMPLEMENTED YET\n", __FUNCTION__);
104  return 0;
105 }
106 
107 void freenect_set_user(freenect_device *dev, void *user)
108 {
109  dev->user_data = user;
110 }
111 void *freenect_get_user(freenect_device *dev)
112 {
113  return dev->user_data;
114 }
115