]> git.itanic.dy.fi Git - glucose/blob - main.c
Add copyright notices
[glucose] / main.c
1 /*
2  * Copyright (C) 2012 Timo Kokkonen <timo.t.kokkonen@iki.fi>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <linux/types.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include "hiddev.h"
31 #include "utils.h"
32 #include "options.h"
33 #include "contour-protocol.h"
34
35 int main(int argc, char *argv[])
36 {
37         FILE *outf;
38         struct user_options opts;
39         struct msg msg;
40         int fd, usage_code, ret, error;
41         int entries = 0;
42
43         read_args(argc, argv, &opts);
44
45         trace_level = opts.trace_level;
46
47         if (opts.output_path) {
48                 outf = fopen(opts.output_path, "w");
49                 if (outf == NULL) {
50                         error = errno;
51                         trace(0, "Failed to open output file %s: %s\n",
52                                 opts.output_path, strerror(error));
53                         return 1;
54                 }
55         } else {
56                 outf = stdout;
57         }
58
59         if (opts.usbdev == NULL)
60                 fd = wait_for_device(CONTOUR_USB_VENDOR_ID,
61                                 CONTOUR_USB_PRODUCT_ID, &usage_code);
62         else
63                 fd = hiddev_open(opts.usbdev, &usage_code);
64         if (fd < 0)
65                 return 1;
66
67         trace(0, "Initializing\n");
68         contour_initialize(fd, usage_code);
69
70         trace(0, "Done! Reading data\n");
71         while (1) {
72                 ret = contour_read_entry(fd, usage_code, &msg);
73                 sanitize_ascii(msg.data, ret);
74
75                 if (ret < 45)
76                         break;
77
78                 fprintf(outf, "%s\n", msg.data);
79
80                 entries++;
81                 if (outf != stdout) {
82                         trace(0, "\r%d", entries);
83                         fflush(stdout);
84                 }
85         }
86         trace(0, "\n");
87
88         return 0;
89 }