root/branches/dev/lswm/lswm.c

Revision 120, 3.4 kB (checked in by dsmith, 2 years ago)

command-line rework (lswm, wminput) - Nick <nickishappy@gmail.com>

Line 
1 /* Copyright (C) 2007 L. Donnie Smith <cwiid@abstrakraft.org>
2  *
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation; either version 2 of the License, or
6  *  (at your option) any later version.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  *  ChangeLog:
18  *  2007-06-01 Nick <nickishappy@gmail.com>
19  *  * reworked command-line options (added standard options, long options)
20  *
21  *  2007-04-09 L. Donnie Smith <cwiid@abstrakraft.org>
22  *  * updated for libcwiid rename
23  *
24  *  2007-04-07 L. Donnie Smith <cwiid@abstrakraft.org>
25  *  * changed cwiid_info.class to btclass
26  *
27  *  2007-04-01 L. Donnie Smith <cwiid@abstrakraft.org>
28  *  * created file
29  */
30
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <bluetooth/bluetooth.h>
36 #include <cwiid.h>
37 #include <getopt.h>
38
39 void print_usage(void)
40 {
41         printf("lswm lists availiable wiimotes\n");
42         printf("Usage: %s [OPTIONS]...\n\n", "lswm");
43         printf("Options:\n");
44         printf("\t-h, --help\t\tPrints this output.\n");
45         printf("\t-v, --version\t\toutput version information and exit.\n");
46         printf("\t-l, --long\t\tlong format (device details).\n");
47         printf("\t-q, --quiet\t\tquiet mode (less verbose).\n");
48         printf("\t-a, --all\t\tlist all bluetooth devices (not just wiimotes).\n");
49 }
50
51 int main(int argc, char *argv[])
52 {
53         int c;
54         uint8_t flags = 0;
55         char long_format = 0;
56         char quiet = 0;
57         struct cwiid_bdinfo *bdinfo;
58         int bdinfo_count;
59         int i;
60         char ba_str[18];
61
62         /* Parse options */
63         while (1) {
64                 int option_index = 0;
65
66                 static struct option long_options[] = {
67                         {"help", 0, 0, 'h'},
68                         {"all", 0, 0, 'a'},
69                         {"long", 0, 0, 'l'},
70                         {"version", 0, 0, 'v'},
71                         {"quiet", 0, 0, 'q'},
72                         {0, 0, 0, 0}
73                 };
74
75                 c = getopt_long(argc, argv, "halvq", long_options, &option_index);
76
77                 if (c == -1) {
78                         break;
79                 }
80
81                 switch (c) {
82                 case 'h':
83                         print_usage();
84                         return 0;
85                         break;
86                 case 'a':
87                         flags |= BT_NO_WIIMOTE_FILTER;
88                         break;
89                 case 'l':
90                         long_format = 1;
91                         break;
92                 case 'v':
93                         printf("CWiid Version %s\n", CWIID_VERSION);
94                         return 0;
95                         break;
96                 case 'q':
97                         quiet = 1;
98                         break;
99                 case '?':
100                 default:
101                         fprintf(stderr, "Try '%s --help' for more information\n", argv[0]);
102                         return -1;
103                         break;
104                 }
105         }
106
107         /* Handle quiet mode */
108         if (quiet) {
109                 cwiid_set_err(NULL);
110         }
111         /* Print discoverable mode message */
112         else if (flags & BT_NO_WIIMOTE_FILTER) {
113                 fprintf(stderr, "Put Bluetooth devices in discoverable mode now...\n");
114         }
115         else {
116                 fprintf(stderr, "Put Wiimotes in discoverable mode now "
117                         "(press 1+2)...\n");
118         }
119
120         /* Get device info */
121         if ((bdinfo_count = cwiid_get_bdinfo_array(-1, 2, -1, &bdinfo, flags))
122           == -1) {
123                 return -1;
124         }
125
126         /* Print info */
127         for (i=0; i < bdinfo_count; i++) {
128                 ba2str(&bdinfo[i].bdaddr, ba_str);
129                 if (long_format) {
130                         printf("%s 0x%.2X%.2X%.2X %s\n", ba_str, bdinfo[i].btclass[2],
131                                bdinfo[i].btclass[1], bdinfo[i].btclass[0], bdinfo[i].name);
132                 }
133                 else {
134                         printf("%s\n", ba_str);
135                 }
136         }
137
138         return 0;
139 }
Note: See TracBrowser for help on using the browser.