1 /*
2  * ManyMouse foundation code; apps talks to this and it talks to the lowlevel
3  *  code for various platforms.
4  *
5  * Please see the file LICENSE.txt in the source's root directory.
6  *
7  *  This file written by Ryan C. Gordon.
8  */
9 
10 #include <stdlib.h>
11 #include "manymouse.h"
12 
13 static const char *manymouse_copyright =
14     "ManyMouse " MANYMOUSE_VERSION " copyright (c) 2005-2012 Ryan C. Gordon.";
15 
16 extern const ManyMouseDriver *ManyMouseDriver_windows;
17 extern const ManyMouseDriver *ManyMouseDriver_evdev;
18 extern const ManyMouseDriver *ManyMouseDriver_hidmanager;
19 extern const ManyMouseDriver *ManyMouseDriver_hidutilities;
20 extern const ManyMouseDriver *ManyMouseDriver_xinput2;
21 
22 /*
23  * These have to be in the favored order...obviously it doesn't matter if the
24  *  Linux driver comes before or after the Windows one, since one won't
25  *  exist on a given platform, but things like Mac OS X's hidmanager (which
26  *  only works on OS X 10.5 and later) should come before Mac OS X's
27  *  hidutilities (which works on older systems, but may stop working in 10.6
28  *  and later). In the Mac OS X case, you want to try the newer tech, and if
29  *  it's not available (on 10.4 or earlier), fall back to trying the legacy
30  *  code.
31  */
32 static const ManyMouseDriver **mice_drivers[] =
33 {
34     &ManyMouseDriver_evdev,
35     &ManyMouseDriver_xinput2,
36     &ManyMouseDriver_windows,
37     &ManyMouseDriver_hidmanager,
38     &ManyMouseDriver_hidutilities,
39 };
40 
41 
42 static const ManyMouseDriver *driver = NULL;
43 
ManyMouse_Init(void)44 int ManyMouse_Init(void)
45 {
46     const int upper = (sizeof (mice_drivers) / sizeof (mice_drivers[0]));
47     int i;
48     int retval = -1;
49 
50     /* impossible test to keep manymouse_copyright linked into the binary. */
51     if (manymouse_copyright == NULL)
52         return -1;
53 
54     if (driver != NULL)
55         return -1;
56 
57     for (i = 0; (i < upper) && (driver == NULL); i++)
58     {
59         const ManyMouseDriver *this_driver = *(mice_drivers[i]);
60         if (this_driver != NULL) /* if not built for this platform, skip it. */
61         {
62             const int mice = this_driver->init();
63             if (mice > retval)
64                 retval = mice; /* may move from "error" to "no mice found". */
65 
66             if (mice >= 0)
67                 driver = this_driver;
68         } /* if */
69     } /* for */
70 
71     return retval;
72 } /* ManyMouse_Init */
73 
74 
ManyMouse_Quit(void)75 void ManyMouse_Quit(void)
76 {
77     if (driver != NULL)
78     {
79         driver->quit();
80         driver = NULL;
81     } /* if */
82 } /* ManyMouse_Quit */
83 
ManyMouse_DriverName(void)84 const char *ManyMouse_DriverName(void)
85 {
86     return (driver) ? driver->driver_name : NULL;
87 } /* ManyMouse_DriverName */
88 
ManyMouse_DeviceName(unsigned int index)89 const char *ManyMouse_DeviceName(unsigned int index)
90 {
91     return (driver) ? driver->name(index) : NULL;
92 } /* ManyMouse_DeviceName */
93 
ManyMouse_PollEvent(ManyMouseEvent * event)94 int ManyMouse_PollEvent(ManyMouseEvent *event)
95 {
96     return (driver) ? driver->poll(event) : 0;
97 } /* ManyMouse_PollEvent */
98 
99 /* end of manymouse.c ... */
100 
101