From f164606b754c74f6a7137a4ed02dd7a753bac187 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 8 Aug 2012 20:00:48 -0400 Subject: [PATCH] Fixed reality to match docs: (ManyMouse_Init() == 0) still means initialized. --- README.txt | 3 ++- example/detect_mice.c | 11 ++++---- example/manymousepong.c | 32 +++++++++++++++++------ example/mmpong.c | 48 +++++++++++++++++++++++----------- example/test_manymouse_sdl.c | 44 ++++++++++++++++++++++--------- example/test_manymouse_stdio.c | 21 ++++++++------- manymouse.c | 2 +- 7 files changed, 109 insertions(+), 52 deletions(-) diff --git a/README.txt b/README.txt index 3771dea..d7025ec 100644 --- a/README.txt +++ b/README.txt @@ -49,7 +49,8 @@ Basic usage: you poll for other system GUI events...once per iteration of your program's main loop. - When you are done processing mice, call ManyMouse_Quit() once, usually at - program termination. + program termination. You should call this even if ManyMouse_Init() returned + zero. There are examples of complete usage in the "example" directory. The simplest is test_manymouse_stdio.c ... diff --git a/example/detect_mice.c b/example/detect_mice.c index 55b82f2..40610ba 100644 --- a/example/detect_mice.c +++ b/example/detect_mice.c @@ -14,12 +14,12 @@ int main(int argc, char **argv) { - int available_mice = ManyMouse_Init(); - if (available_mice <= 0) - { + const int available_mice = ManyMouse_Init(); + + if (available_mice < 0) + printf("ManyMouse failed to initialize!\n"); + else if (available_mice == 0) printf("No mice detected!\n"); - return 1; - } else { int i; @@ -27,6 +27,7 @@ int main(int argc, char **argv) for (i = 0; i < available_mice; i++) printf("#%d: %s\n", i, ManyMouse_DeviceName(i)); } + ManyMouse_Quit(); return 0; } /* main */ diff --git a/example/manymousepong.c b/example/manymousepong.c index fc072dc..810e97b 100644 --- a/example/manymousepong.c +++ b/example/manymousepong.c @@ -169,26 +169,42 @@ static void setSidesExists(void) static int initMice(void) { + int i; + available_mice = ManyMouse_Init(); - if (available_mice > MAX_PADDLES) - available_mice = MAX_PADDLES; + + if (available_mice < 0) + { + printf("Error initializing ManyMouse!\n"); + return 0; + } + + printf("ManyMouse driver: %s\n", ManyMouse_DriverName()); if (available_mice == 0) printf("No mice detected!\n"); else { - int i; - printf("ManyMouse driver: %s\n", ManyMouse_DriverName()); for (i = 0; i < available_mice; i++) { const char *name = ManyMouse_DeviceName(i); - paddles[i].exists = 1; printf("#%d: %s\n", i, name); - } /* for */ - } /* else */ + } - setSidesExists(); + if (available_mice > MAX_PADDLES) + { + printf("Clamping to first %d mice.\n"); + available_mice = MAX_PADDLES; + } + + for (i = 0; i < available_mice; i++) + { + const char *name = ManyMouse_DeviceName(i); + paddles[i].exists = 1; + } + } + setSidesExists(); return 1; } /* initMice */ diff --git a/example/mmpong.c b/example/mmpong.c index c582bef..cd6d473 100644 --- a/example/mmpong.c +++ b/example/mmpong.c @@ -103,29 +103,47 @@ static void initial_setup(int screen_w, int screen_h) } + static void init_mice(void) { + int i; + available_mice = ManyMouse_Init(); - if (available_mice > MAX_MICE) - available_mice = MAX_MICE; - + + if (available_mice < 0) + { + printf("Error initializing ManyMouse!\n"); + return; + } + + printf("ManyMouse driver: %s\n", ManyMouse_DriverName()); + if (available_mice == 0) + { printf("No mice detected!\n"); - else + return; + } + + for (i = 0; i < available_mice; i++) { - int i; - printf("ManyMouse driver: %s\n", ManyMouse_DriverName()); - for (i = 0; i < available_mice; i++) - { - const char *name = ManyMouse_DeviceName(i); - strncpy(mice[i].name, name, sizeof (mice[i].name)); - mice[i].name[sizeof (mice[i].name) - 1] = '\0'; - mice[i].connected = 1; - printf("#%d: %s\n", i, mice[i].name); - } + const char *name = ManyMouse_DeviceName(i); + printf("#%d: %s\n", i, name); } -} + if (available_mice > MAX_MICE) + { + printf("Clamping to first %d mice.\n"); + available_mice = MAX_MICE; + } + + for (i = 0; i < available_mice; i++) + { + const char *name = ManyMouse_DeviceName(i); + strncpy(mice[i].name, name, sizeof (mice[i].name)); + mice[i].name[sizeof (mice[i].name) - 1] = '\0'; + mice[i].connected = 1; + } +} static void update_mice(int screen_w, int screen_h) { diff --git a/example/test_manymouse_sdl.c b/example/test_manymouse_sdl.c index f118573..126554f 100644 --- a/example/test_manymouse_sdl.c +++ b/example/test_manymouse_sdl.c @@ -187,24 +187,42 @@ static void initial_setup(int screen_w, int screen_h) static void init_mice(void) { + int i; + available_mice = ManyMouse_Init(); - if (available_mice > MAX_MICE) - available_mice = MAX_MICE; + + if (available_mice < 0) + { + printf("Error initializing ManyMouse!\n"); + return; + } + + printf("ManyMouse driver: %s\n", ManyMouse_DriverName()); if (available_mice == 0) + { printf("No mice detected!\n"); - else + return; + } + + for (i = 0; i < available_mice; i++) { - int i; - printf("ManyMouse driver: %s\n", ManyMouse_DriverName()); - for (i = 0; i < available_mice; i++) - { - const char *name = ManyMouse_DeviceName(i); - strncpy(mice[i].name, name, sizeof (mice[i].name)); - mice[i].name[sizeof (mice[i].name) - 1] = '\0'; - mice[i].connected = 1; - printf("#%d: %s\n", i, mice[i].name); - } + const char *name = ManyMouse_DeviceName(i); + printf("#%d: %s\n", i, name); + } + + if (available_mice > MAX_MICE) + { + printf("Clamping to first %d mice.\n"); + available_mice = MAX_MICE; + } + + for (i = 0; i < available_mice; i++) + { + const char *name = ManyMouse_DeviceName(i); + strncpy(mice[i].name, name, sizeof (mice[i].name)); + mice[i].name[sizeof (mice[i].name) - 1] = '\0'; + mice[i].connected = 1; } } diff --git a/example/test_manymouse_stdio.c b/example/test_manymouse_stdio.c index 2f2428a..8ce3b70 100644 --- a/example/test_manymouse_stdio.c +++ b/example/test_manymouse_stdio.c @@ -15,24 +15,27 @@ int main(int argc, char **argv) { ManyMouseEvent event; - int available_mice = ManyMouse_Init(); + const int available_mice = ManyMouse_Init(); + int i; + if (available_mice < 0) { printf("Error initializing ManyMouse!\n"); + ManyMouse_Quit(); return 2; } - else if (available_mice == 0) + + printf("ManyMouse driver: %s\n", ManyMouse_DriverName()); + + if (available_mice == 0) { printf("No mice detected!\n"); + ManyMouse_Quit(); return 1; } - else - { - int i; - printf("ManyMouse driver: %s\n", ManyMouse_DriverName()); - for (i = 0; i < available_mice; i++) - printf("#%d: %s\n", i, ManyMouse_DeviceName(i)); - } + + for (i = 0; i < available_mice; i++) + printf("#%d: %s\n", i, ManyMouse_DeviceName(i)); printf("\n"); printf("Use your mice, CTRL-C to exit.\n"); diff --git a/manymouse.c b/manymouse.c index ea1c988..f0948df 100644 --- a/manymouse.c +++ b/manymouse.c @@ -63,7 +63,7 @@ int ManyMouse_Init(void) if (mice > retval) retval = mice; /* may move from "error" to "no mice found". */ - if (mice > 0) + if (mice >= 0) driver = this_driver; } /* if */ } /* for */