]> git.mxchange.org Git - flightgear.git/blob - utils/js_server/js_server.cxx
Merge branch 'jmt/acinclude'
[flightgear.git] / utils / js_server / js_server.cxx
1 /*
2      Plib based joystick server based on PLIBs js_demo.cxx
3
4      js_server is Copyright (C) 2003 
5      by Stephen Lowry and Manuel Bessler
6
7      PLIB - A Suite of Portable Game Libraries
8      Copyright (C) 2001  Steve Baker
9
10      This program is free software; you can redistribute it and/or modify
11      it under the terms of the GNU General Public License as published by
12      the Free Software Foundation; either version 2 of the License, or
13      (at your option) any later version.
14
15      This program is distributed in the hope that it will be useful,
16      but WITHOUT ANY WARRANTY; without even the implied warranty of
17      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18      GNU General Public License for more details.
19
20      You should have received a copy of the GNU General Public License
21      along with this program; if not, write to the Free Software
22      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23
24      For further information visit http://plib.sourceforge.net
25
26 */
27
28 #include <math.h>
29 #include <stdio.h>
30 #include <plib/netSocket.h>
31 #include <plib/js.h>
32
33 void usage(char * progname)
34 {
35     printf("This is an UDP based remote joystick server.\n");
36     printf("usage: %s <hostname> <port>\n", progname);
37 }
38
39 int main ( int argc, char ** argv )
40 {
41   jsJoystick * js ;
42   float      * ax;
43   int port = 16759;
44   char * host; /* = "192.168.1.7"; */
45   int activeaxes = 4;
46
47   if( argc != 3 )
48   {
49     usage(argv[0]); 
50     exit(1);
51   }
52   host = argv[1];
53   port = atoi(argv[2]);
54
55   jsInit () ;
56
57   js = new jsJoystick ( 0 ) ;
58
59   if ( js->notWorking () )
60   {
61     printf ( "no Joystick detected... exitting\n" ) ;
62     exit(1);     
63   }
64   printf ( "Joystick is \"%s\"\n", js->getName() ) ;
65
66   int numaxes = js->getNumAxes();
67   ax = new float [ numaxes ] ;
68   activeaxes = numaxes;
69   
70   if( numaxes > 4 )
71   {
72     printf("max 4 axes joysticks supported at the moment, however %i axes were detected\nWill only use the first 4 axes!\n", numaxes);
73     activeaxes = 4;
74   }
75
76   // Must call this before any other net stuff
77   netInit( &argc,argv );
78
79   netSocket c;
80
81   if ( ! c.open( false ) ) {    // open a UDP socket
82         printf("error opening socket\n");
83         return -1;
84     }
85
86     c.setBlocking( false );
87
88     if ( c.connect( host, port ) == -1 ) {
89         printf("error connecting to %s:%d\n", host, port);
90         return -1;
91     }
92
93
94   char packet[256] = "Hello world!";
95   while(1)
96   {
97         int b;
98         int len = 0;
99         int axis = 0;
100
101         js->read( &b, ax );
102         for ( axis = 0 ; axis < activeaxes ; axis++ )
103         {
104           int32_t axisvalue = (int32_t)(ax[axis]*2147483647.0);
105           printf("axisval=%li\n", (long)axisvalue);
106           memcpy(packet+len, &axisvalue, sizeof(axisvalue));
107           len+=sizeof(axisvalue);
108         }
109         // fill emtpy values into packes when less than 4 axes
110         for( ; axis < 4; axis++ )
111         {
112           int32_t axisvalue = 0;
113           memcpy(packet+len, &axisvalue, sizeof(axisvalue));
114           len+=sizeof(axisvalue);
115         }
116
117         int32_t b_l = b;
118         memcpy(packet+len, &b_l, sizeof(b_l));
119         len+=sizeof(b_l);
120
121         const char * termstr = "\0\0\r\n";
122         memcpy(packet+len, termstr, 4);
123         len += 4;
124
125         c.send( packet, len, 0 );
126
127     /* give other processes a chance */
128
129 #ifdef _WIN32
130     Sleep ( 1 ) ;
131 #elif defined(sgi)
132     sginap ( 1 ) ;
133 #else
134     usleep ( 200 ) ;
135 #endif
136     printf(".");
137     fflush(stdout);
138   }
139
140   return 0 ;
141 }