#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
typedef enum {
PD_READ = 0,
PD_WRITE = 1
} PipeDirection;
static pid_t create_child_process(const char* path, char * const argv[], int* stdout_pipe, int* stderr_pipe) {
int child_stdout_pipe[2];
int child_stderr_pipe[2];
if (pipe(child_stdout_pipe) == -1) {
perror("child_stdout_pipe");
exit(EXIT_FAILURE);
}
if (pipe(child_stderr_pipe) == -1) {
perror("child_stderr_pipe");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
dup2(child_stdout_pipe[PD_WRITE], STDOUT_FILENO);
dup2(child_stderr_pipe[PD_WRITE], STDERR_FILENO);
close(child_stdout_pipe[PD_READ]);
close(child_stderr_pipe[PD_READ]);
execvp(path, argv);
perror(path);
} else {
close(child_stdout_pipe[PD_WRITE]);
close(child_stderr_pipe[PD_WRITE]);
*stdout_pipe = child_stdout_pipe[PD_READ];
*stderr_pipe = child_stderr_pipe[PD_READ];
}
return pid;
}
static void printf_lines_starting_with(FILE* f, const char* line_start) {
char *line = NULL;
size_t len = 0;
ssize_t read;
size_t line_start_len = strlen(line_start);
do {
read = getline(&line, &len, f);
if (read != -1 && !strncmp(line, line_start, line_start_len)) {
printf("%s", line);
}
}
while(read != -1);
free(line);
}
#if __APPLE__ && __MACH__
#define ETM_PATH "/Applications/TobiiProEyeTrackerManager.app/Contents/MacOS/TobiiProEyeTrackerManager"
#else
#define ETM_PATH "TobiiProEyeTrackerManager"
#endif
int main() {
char* device_address = NULL;
char device_address_arg[128];
int child_stdout_pipe = 0;
int child_stderr_pipe = 0;
pid_t pid;
int wstatus;
int w;
printf("ERROR: %d when trying to get eyetracker!\n", status);
exit(EXIT_FAILURE);
}
printf("ERROR: %d when trying to get address!\n", status);
exit(EXIT_FAILURE);
}
snprintf(device_address_arg, sizeof(device_address_arg)/sizeof(char), "--device-address=%s", device_address);
#define MODE "displayarea"
char *etm_argv[] = { ETM_PATH,
device_address_arg,
"--mode=" MODE,
NULL};
pid = create_child_process(ETM_PATH, etm_argv, &child_stdout_pipe, &child_stderr_pipe);
w = waitpid(pid, &wstatus, 0);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WEXITSTATUS(wstatus) == 0) {
printf("Eye Tracker Manager was called successfully!\n");
} else {
printf("Eye Tracker Manager call returned the error code: %i\n", WEXITSTATUS(wstatus));
FILE* child_stderr = fdopen(child_stderr_pipe, "r");
printf_lines_starting_with(child_stderr, "ETM Error:");
fclose(child_stderr);
}
}