| 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-07-28 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 19 |
* * added config.h include |
|---|
| 20 |
* * use PACKAGE_VERSION from config.h instead of CWIID_VERSION |
|---|
| 21 |
* |
|---|
| 22 |
* 2007-06-01 Nick <nickishappy@gmail.com> |
|---|
| 23 |
* * reworked command-line options (added standard options, long options) |
|---|
| 24 |
* |
|---|
| 25 |
* 2007-04-09 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 26 |
* * updated for libcwiid rename |
|---|
| 27 |
* |
|---|
| 28 |
* 2007-04-07 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 29 |
* * changed cwiid_info.class to btclass |
|---|
| 30 |
* |
|---|
| 31 |
* 2007-04-01 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 32 |
* * created file |
|---|
| 33 |
*/ |
|---|
| 34 |
|
|---|
| 35 |
#ifdef HAVE_CONFIG_H |
|---|
| 36 |
#include "config.h" |
|---|
| 37 |
#endif |
|---|
| 38 |
|
|---|
| 39 |
#include <stdint.h> |
|---|
| 40 |
#include <stdio.h> |
|---|
| 41 |
#include <stdlib.h> |
|---|
| 42 |
#include <unistd.h> |
|---|
| 43 |
#include <bluetooth/bluetooth.h> |
|---|
| 44 |
#include <cwiid.h> |
|---|
| 45 |
#include <getopt.h> |
|---|
| 46 |
|
|---|
| 47 |
void print_usage(void) |
|---|
| 48 |
{ |
|---|
| 49 |
printf("lswm lists availiable wiimotes\n"); |
|---|
| 50 |
printf("Usage: %s [OPTIONS]...\n\n", "lswm"); |
|---|
| 51 |
printf("Options:\n"); |
|---|
| 52 |
printf("\t-h, --help\t\tPrints this output.\n"); |
|---|
| 53 |
printf("\t-v, --version\t\toutput version information and exit.\n"); |
|---|
| 54 |
printf("\t-l, --long\t\tlong format (device details).\n"); |
|---|
| 55 |
printf("\t-q, --quiet\t\tquiet mode (less verbose).\n"); |
|---|
| 56 |
printf("\t-a, --all\t\tlist all bluetooth devices (not just wiimotes).\n"); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
int main(int argc, char *argv[]) |
|---|
| 60 |
{ |
|---|
| 61 |
int c; |
|---|
| 62 |
uint8_t flags = 0; |
|---|
| 63 |
char long_format = 0; |
|---|
| 64 |
char quiet = 0; |
|---|
| 65 |
struct cwiid_bdinfo *bdinfo; |
|---|
| 66 |
int bdinfo_count; |
|---|
| 67 |
int i; |
|---|
| 68 |
char ba_str[18]; |
|---|
| 69 |
|
|---|
| 70 |
/* Parse options */ |
|---|
| 71 |
while (1) { |
|---|
| 72 |
int option_index = 0; |
|---|
| 73 |
|
|---|
| 74 |
static struct option long_options[] = { |
|---|
| 75 |
{"help", 0, 0, 'h'}, |
|---|
| 76 |
{"all", 0, 0, 'a'}, |
|---|
| 77 |
{"long", 0, 0, 'l'}, |
|---|
| 78 |
{"version", 0, 0, 'v'}, |
|---|
| 79 |
{"quiet", 0, 0, 'q'}, |
|---|
| 80 |
{0, 0, 0, 0} |
|---|
| 81 |
}; |
|---|
| 82 |
|
|---|
| 83 |
c = getopt_long(argc, argv, "halvq", long_options, &option_index); |
|---|
| 84 |
|
|---|
| 85 |
if (c == -1) { |
|---|
| 86 |
break; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
switch (c) { |
|---|
| 90 |
case 'h': |
|---|
| 91 |
print_usage(); |
|---|
| 92 |
return 0; |
|---|
| 93 |
break; |
|---|
| 94 |
case 'a': |
|---|
| 95 |
flags |= BT_NO_WIIMOTE_FILTER; |
|---|
| 96 |
break; |
|---|
| 97 |
case 'l': |
|---|
| 98 |
long_format = 1; |
|---|
| 99 |
break; |
|---|
| 100 |
case 'v': |
|---|
| 101 |
printf("CWiid Version %s\n", PACKAGE_VERSION); |
|---|
| 102 |
return 0; |
|---|
| 103 |
break; |
|---|
| 104 |
case 'q': |
|---|
| 105 |
quiet = 1; |
|---|
| 106 |
break; |
|---|
| 107 |
case '?': |
|---|
| 108 |
default: |
|---|
| 109 |
fprintf(stderr, "Try '%s --help' for more information\n", argv[0]); |
|---|
| 110 |
return -1; |
|---|
| 111 |
break; |
|---|
| 112 |
} |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
/* Handle quiet mode */ |
|---|
| 116 |
if (quiet) { |
|---|
| 117 |
cwiid_set_err(NULL); |
|---|
| 118 |
} |
|---|
| 119 |
/* Print discoverable mode message */ |
|---|
| 120 |
else if (flags & BT_NO_WIIMOTE_FILTER) { |
|---|
| 121 |
fprintf(stderr, "Put Bluetooth devices in discoverable mode now...\n"); |
|---|
| 122 |
} |
|---|
| 123 |
else { |
|---|
| 124 |
fprintf(stderr, "Put Wiimotes in discoverable mode now " |
|---|
| 125 |
"(press 1+2)...\n"); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
/* Get device info */ |
|---|
| 129 |
if ((bdinfo_count = cwiid_get_bdinfo_array(-1, 2, -1, &bdinfo, flags)) |
|---|
| 130 |
== -1) { |
|---|
| 131 |
return -1; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
/* Print info */ |
|---|
| 135 |
for (i=0; i < bdinfo_count; i++) { |
|---|
| 136 |
ba2str(&bdinfo[i].bdaddr, ba_str); |
|---|
| 137 |
if (long_format) { |
|---|
| 138 |
printf("%s 0x%.2X%.2X%.2X %s\n", ba_str, bdinfo[i].btclass[2], |
|---|
| 139 |
bdinfo[i].btclass[1], bdinfo[i].btclass[0], bdinfo[i].name); |
|---|
| 140 |
} |
|---|
| 141 |
else { |
|---|
| 142 |
printf("%s\n", ba_str); |
|---|
| 143 |
} |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
return 0; |
|---|
| 147 |
} |
|---|