linuxtrack-sys 1.0.1

Bindings to the linuxtrack.c client library for head tracking.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
The MIT License

Copyright (c) 2009 Tulthix, uglyDwarf

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/





#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <dlfcn.h>
#include <unistd.h>
#include <string.h>
#include "linuxtrack.h"

#ifdef HAVE_CONFIG_H
  #include <config.h>
#endif

#define BLOB_ELEMENTS 3

typedef linuxtrack_state_type (*ltr_gp_t)(void);
typedef linuxtrack_state_type (*ltr_init_t)(const char *cust_section);
typedef int (*ltr_get_pose_t)(float *heading,
                         float *pitch,
                         float *roll,
                         float *tx,
                         float *ty,
                         float *tz,
                         uint32_t *counter);
typedef int (*ltr_get_pose_full_t)(linuxtrack_pose_t *pose, float blobs[], int num_blobs, int *blobs_read);
typedef linuxtrack_state_type (*ltr_get_tracking_state_t)(void);
typedef const char *(*ltr_explain_t)(linuxtrack_state_type);
typedef int (*ltr_get_frame_t)(int *req_width, int *req_height, size_t buf_size, uint8_t *buffer);
typedef int (*ltr_get_notify_pipe_t)(void);
typedef int (*ltr_wait_t)(int timeout);


static ltr_init_t ltr_init_fun = NULL;
static ltr_gp_t ltr_shutdown_fun = NULL;
static ltr_gp_t ltr_suspend_fun = NULL;
static ltr_gp_t ltr_wakeup_fun = NULL;
static ltr_gp_t ltr_recenter_fun = NULL;
static ltr_get_pose_t ltr_get_pose_fun = NULL;
static ltr_get_pose_full_t ltr_get_pose_full_fun = NULL;
static ltr_get_tracking_state_t ltr_get_tracking_state_fun = NULL;
static ltr_explain_t ltr_explain_fun = NULL;
static ltr_get_pose_t ltr_get_abs_pose_fun = NULL;
static ltr_gp_t ltr_request_frames_fun = NULL;
static ltr_get_frame_t ltr_get_frame_fun = NULL;
static ltr_gp_t ltr_notification_on_fun = NULL;
static ltr_get_notify_pipe_t ltr_get_notify_pipe_fun = NULL;
static ltr_wait_t ltr_wait_fun = NULL;

static void *lib_handle = NULL;

struct func_defs_t{
  char *name;
  void *ref;
  int mandatory;
};

static struct func_defs_t functions[] =
{
  {(char*)"ltr_init", (void*)&ltr_init_fun, 1},
  {(char*)"ltr_shutdown", (void*)&ltr_shutdown_fun, 1},
  {(char*)"ltr_suspend", (void *)&ltr_suspend_fun, 1},
  {(char*)"ltr_wakeup", (void *)&ltr_wakeup_fun, 1},
  {(char*)"ltr_recenter", (void *)&ltr_recenter_fun, 1},
  {(char*)"ltr_get_pose", (void *)&ltr_get_pose_fun, 1},
  {(char*)"ltr_get_pose_full", (void *)&ltr_get_pose_full_fun, 1},
  {(char*)"ltr_get_tracking_state", (void *)&ltr_get_tracking_state_fun, 1},
  {(char*)"ltr_explain", (void *)&ltr_explain_fun, 0},
  {(char*)"ltr_get_abs_pose", (void *)&ltr_get_abs_pose_fun, 1},
  {(char*)"ltr_request_frames", (void *)&ltr_request_frames_fun, 0},
  {(char*)"ltr_get_frame", (void *)&ltr_get_frame_fun, 0},
  {(char*)"ltr_notification_on", (void *)&ltr_notification_on_fun, 0},
  {(char*)"ltr_get_notify_pipe", (void *)&ltr_get_notify_pipe_fun, 0},
  {(char*)"ltr_wait", (void *)&ltr_wait_fun, 0},
  {(char*)NULL, NULL, 0}
};

static const char *lib_locations[] = {
"/Frameworks/liblinuxtrack.0.dylib",
"/lib/linuxtrack/liblinuxtrack.so.0",
"/lib32/linuxtrack/liblinuxtrack32.so.0",
"/lib/i386-linux-gnu/linuxtrack/liblinuxtrack.so.0",
"/lib/i386-linux-gnu/linuxtrack/liblinuxtrack32.so.0",
"/lib/x86_64-linux-gnu/linuxtrack/liblinuxtrack.so.0",
/* old paths */
"/lib/liblinuxtrack.so.0",
"/lib32/liblinuxtrack.so.0",
"/lib32/liblinuxtrack32.so.0",
"/lib/i386-linux-gnu/liblinuxtrack.so.0",
"/lib/x86_64-linux-gnu/liblinuxtrack.so.0",
NULL
};

static FILE *log_f = NULL;
static char logfname[] = "/tmp/linuxtrack.logXXXXXX";

static void linuxtrack_log(const char *format, ...)
{
  if(log_f == NULL){
    FILE *tmpf;
    int tmpfd = mkstemp(logfname);
    if(tmpfd != -1){
      tmpf = fdopen(tmpfd, "a");
      if(tmpf != NULL){
        log_f = tmpf;
      }
    }
  }
  va_list ap;
  va_start(ap,format);
  vfprintf(log_f, format, ap);
  fflush(log_f);
  va_end(ap);
}

linuxtrack_state_type linuxtrack_shutdown(void)
{
  linuxtrack_state_type res;
  if(ltr_shutdown_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  res = ltr_shutdown_fun();

  if(lib_handle != NULL){
    void *handle = lib_handle;
    lib_handle = NULL;
    ltr_init_fun = NULL;
    ltr_shutdown_fun = NULL;
    ltr_suspend_fun = NULL;
    ltr_wakeup_fun = NULL;
    ltr_recenter_fun = NULL;
    ltr_get_pose_fun = NULL;
    ltr_get_pose_full_fun = NULL;
    ltr_get_tracking_state_fun = NULL;
    ltr_explain_fun = NULL;
    dlclose(handle);
  }
  return res;
}

linuxtrack_state_type linuxtrack_suspend(void)
{
  if(ltr_suspend_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_suspend_fun();
}

linuxtrack_state_type linuxtrack_wakeup(void)
{
  if(ltr_wakeup_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_wakeup_fun();
}

linuxtrack_state_type linuxtrack_recenter(void)
{
  if(ltr_recenter_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_recenter_fun();
}


//RetVal 0 means no new data
int linuxtrack_get_pose(float *heading,
                         float *pitch,
                         float *roll,
                         float *tx,
                         float *ty,
                         float *tz,
                         uint32_t *counter)
{
  if(ltr_get_pose_fun == NULL){
    *heading = *pitch = *roll = *tx = *ty = *tz = 0.0f;
    *counter = 0;
    return 0;
  }
  return ltr_get_pose_fun(heading, pitch, roll, tx, ty, tz, counter);
}

//RetVal 0 means no new data
int linuxtrack_get_abs_pose(float *heading,
                            float *pitch,
                            float *roll,
                            float *tx,
                            float *ty,
                            float *tz,
                            uint32_t *counter)
{
  if(ltr_get_abs_pose_fun == NULL){
    *heading = *pitch = *roll = *tx = *ty = *tz = 0.0f;
    *counter = 0;
    return 0;
  }
  return ltr_get_abs_pose_fun(heading, pitch, roll, tx, ty, tz, counter);
}

//RetVal 0 means no new data
int linuxtrack_get_pose_full(linuxtrack_pose_t *pose, float blobs[], int num_blobs, int *blobs_read)
{
  if(ltr_get_pose_full_fun == NULL){
    memset(pose, 0, sizeof(linuxtrack_pose_t));
    int i;
    for(i = 0; i < num_blobs * BLOB_ELEMENTS; ++i){
      blobs[i] = 0.0f;
    }
    return 0;
  }
  return ltr_get_pose_full_fun(pose, blobs, num_blobs, blobs_read);
}


linuxtrack_state_type linuxtrack_get_tracking_state(void)
{
  if(ltr_get_tracking_state_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_get_tracking_state_fun();
}


static int linuxtrack_load_functions(void *handle)
{
  int i = 0;
  void *symbol;
  while((functions[i]).name != NULL){
    dlerror();
    if((symbol = dlsym(handle, (functions[i]).name)) == NULL){
      linuxtrack_log("Couldn't load symbol '%s': %s\n", (functions[i]).name, dlerror());
      if((functions[i]).mandatory){
        return err_SYMBOL_LOOKUP;
      }
    }
    *((void **)(functions[i]).ref) = symbol;
    ++i;
  }
  return LINUXTRACK_OK;
}


static char *construct_name(const char *path, const char *sep, const char *name)
{
  size_t len = strlen(path) + strlen(sep) + strlen(name) + 1;
  char *res = (char *)malloc(len);
  snprintf(res, len, "%s%s%s", path, sep, name);
  return res;
}


static void* linuxtrack_try_library(const char *path)
{
  void *handle = NULL;
  linuxtrack_log("Trying to load '%s'... ", path);
  if(access(path, F_OK) != 0){
    linuxtrack_log("Not found.\n");
    return NULL;
  }
  dlerror();
  handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
  if(handle != NULL){
    linuxtrack_log("Loaded OK.\n");
    return handle;
  }
  linuxtrack_log("Couldn't load library - %s!\n", dlerror());
  return NULL;
}

char *linuxtrack_get_prefix()
{
  char *prefix = NULL;
  char *home = getenv("HOME");
  char *cfg = (char*)"/.config/linuxtrack/linuxtrack1.conf";
  char *fname;
  FILE *f;
  char *line;
  char *val, *key;

  if(home == NULL){
    linuxtrack_log("Please set HOME variable!\n");
    return NULL;
  }
  fname = construct_name(home, cfg, "");
  if((f = fopen(fname, "r")) == NULL){
    free(fname);
    return NULL;
  }
  free(fname);
  line = (char *)malloc(4096);
  val = (char *)malloc(4096);
  key = (char *)malloc(4096);
  while(!feof(f)){
    if(fgets(line, 4095, f) != NULL){
      if((sscanf(line, "%s = \"%[^\"\n]", key, val) == 2) &&
        strcasecmp(key, "prefix") == 0){
	prefix = strdup(val);
	break;
      }
    }
  }
  fclose(f);
  free(line);
  free(val);
  free(key);
  return prefix;
}



static void* linuxtrack_find_library(linuxtrack_state_type *problem)
{
  /*
  //search order:
  //  1. LINUXTRACK_LIBS
  //       development, backward compatibility and weird locations handling
  //  2. prefix from config file
  //  3. plain libname
  //       worth in Linux only, since on Mac we never install to system libraries
  */
  void *handle = NULL;
  char *name = NULL;
  char *prefix;
  /*Look for LINUXTRACK_LIBS*/
  char *lp = getenv("LINUXTRACK_LIBS");
  if(lp != NULL){
    char *path = strdup(lp);
    char *part = path;
    while(1){
      part = strtok(part, ":");
      if((part == NULL) || ((handle = linuxtrack_try_library(part)) != NULL)){
        break;
      }
      part = NULL;
    }
    free(path);
    if(handle != NULL){
      return handle;
    }
  }

  prefix = linuxtrack_get_prefix();
  if(prefix == NULL){
    *problem = err_NO_CONFIG;
    return NULL;
  }
  int i = 0;
  while(lib_locations[i] != NULL){
    name = construct_name(prefix, "/../", lib_locations[i++]);
    if((handle = linuxtrack_try_library(name)) != NULL){
      free(name);
      free(prefix);
      return handle;
    }
    free(name);
  }
  free(prefix);
  *problem = err_NOT_FOUND;
  return NULL;
}



static linuxtrack_state_type linuxtrack_load_library()
{
  linuxtrack_state_type problem;
  lib_handle = linuxtrack_find_library(&problem);
  if(lib_handle == NULL){
    linuxtrack_log("Couldn't load liblinuxtrack, headtracking will not be available!\n");
    return problem;
  }
  dlerror(); /*clear any existing error...*/
  if(linuxtrack_load_functions(lib_handle) != LINUXTRACK_OK){
    linuxtrack_log("Couldn't load liblinuxtrack functions, headtracking will not be available!\n");
    return err_SYMBOL_LOOKUP;
  }
  return LINUXTRACK_OK;
}

linuxtrack_state_type linuxtrack_init(const char *cust_section)
{
  linuxtrack_state_type res = linuxtrack_load_library();
  if(res < LINUXTRACK_OK){
    return res;
  }
  return ltr_init_fun(cust_section);
}

const char *linuxtrack_explain(linuxtrack_state_type status)
{
  if(ltr_explain_fun != NULL){
    return ltr_explain_fun(status);
  }
  const char *res = NULL;
  switch(status){
    case INITIALIZING:
      res = "Linuxtrack is initializing.";
      break;
    case RUNNING:
      res = "Linuxtrack is running.";
      break;
    case PAUSED:
      res = "Linuxtrack is paused.";
      break;
    case STOPPED:
      res = "Linuxtrack is stopped.";
      break;
    case err_NOT_INITIALIZED:
      res = "Linuxtrack function was called without proper initialization.";
      break;
    case err_SYMBOL_LOOKUP:
      res = "Internal error (symbol lookup). Please file an issue at http://linuxtrack.eu";
      break;
    case err_NO_CONFIG:
      res = "Linuxtrack config not found. If you have installed Linuxtrack,\n"
            "run ltr_gui and set it up first.";
      break;
    case err_NOT_FOUND:
      res = "Linuxtrack was removed or relocated. If you relocated it,\n"
            "run ltr_gui from the new location, save preferences and try again.";
      break;
    case err_PROCESSING_FRAME:
      res = "Internal error (frame processing). Please file an issue at http://linuxtrack.eu";
      break;
    default:
      printf("UNKNOWN status code. Please file an issue at http://linuxtrack.eu\n");
     break;
  }
  return res;
}


linuxtrack_state_type linuxtrack_request_frames(void)
{
  if(ltr_request_frames_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_request_frames_fun();
}

int linuxtrack_get_frame(int *req_width, int *req_height, size_t buf_size, uint8_t *buffer)
{
  if(ltr_get_frame_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_get_frame_fun(req_width, req_height, buf_size, buffer);
}

linuxtrack_state_type linuxtrack_notification_on(void)
{
  if(ltr_notification_on_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_notification_on_fun();
}

int linuxtrack_get_notify_pipe(void)
{
  if(ltr_get_notify_pipe_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_get_notify_pipe_fun();
}

int linuxtrack_wait(int timeout)
{
  if(ltr_wait_fun == NULL){
    return err_NOT_INITIALIZED;
  }
  return ltr_wait_fun(timeout);
}