• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.input;
18 
19 import java.io.DataOutputStream;
20 import java.net.Socket;
21 
22 import com.badlogic.gdx.Gdx;
23 import com.badlogic.gdx.Input.Peripheral;
24 import com.badlogic.gdx.InputProcessor;
25 
26 /** Sends all inputs from touch, key, accelerometer and compass to a {@link RemoteInput} at the given ip/port. Instantiate this and
27  * call sendUpdate() periodically.
28  *
29  * @author mzechner */
30 public class RemoteSender implements InputProcessor {
31 	private DataOutputStream out;
32 	private boolean connected = false;
33 
34 	public static final int KEY_DOWN = 0;
35 	public static final int KEY_UP = 1;
36 	public static final int KEY_TYPED = 2;
37 
38 	public static final int TOUCH_DOWN = 3;
39 	public static final int TOUCH_UP = 4;
40 	public static final int TOUCH_DRAGGED = 5;
41 
42 	public static final int ACCEL = 6;
43 	public static final int COMPASS = 7;
44 	public static final int SIZE = 8;
45 	public static final int GYRO = 9;
46 
RemoteSender(String ip, int port)47 	public RemoteSender (String ip, int port) {
48 		try {
49 			Socket socket = new Socket(ip, port);
50 			socket.setTcpNoDelay(true);
51 			socket.setSoTimeout(3000);
52 			out = new DataOutputStream(socket.getOutputStream());
53 			out.writeBoolean(Gdx.input.isPeripheralAvailable(Peripheral.MultitouchScreen));
54 			connected = true;
55 			Gdx.input.setInputProcessor(this);
56 		} catch (Exception e) {
57 			Gdx.app.log("RemoteSender", "couldn't connect to " + ip + ":" + port);
58 		}
59 	}
60 
sendUpdate()61 	public void sendUpdate () {
62 		synchronized (this) {
63 			if (!connected) return;
64 		}
65 		try {
66 			out.writeInt(ACCEL);
67 			out.writeFloat(Gdx.input.getAccelerometerX());
68 			out.writeFloat(Gdx.input.getAccelerometerY());
69 			out.writeFloat(Gdx.input.getAccelerometerZ());
70 			out.writeInt(COMPASS);
71 			out.writeFloat(Gdx.input.getAzimuth());
72 			out.writeFloat(Gdx.input.getPitch());
73 			out.writeFloat(Gdx.input.getRoll());
74 			out.writeInt(SIZE);
75 			out.writeFloat(Gdx.graphics.getWidth());
76 			out.writeFloat(Gdx.graphics.getHeight());
77 			out.writeInt(GYRO);
78 			out.writeFloat(Gdx.input.getGyroscopeX());
79 			out.writeFloat(Gdx.input.getGyroscopeY());
80 			out.writeFloat(Gdx.input.getGyroscopeZ());
81 		} catch (Throwable t) {
82 			out = null;
83 			connected = false;
84 		}
85 	}
86 
87 	@Override
keyDown(int keycode)88 	public boolean keyDown (int keycode) {
89 		synchronized (this) {
90 			if (!connected) return false;
91 		}
92 
93 		try {
94 			out.writeInt(KEY_DOWN);
95 			out.writeInt(keycode);
96 		} catch (Throwable t) {
97 			synchronized (this) {
98 				connected = false;
99 			}
100 		}
101 		return false;
102 	}
103 
104 	@Override
keyUp(int keycode)105 	public boolean keyUp (int keycode) {
106 		synchronized (this) {
107 			if (!connected) return false;
108 		}
109 
110 		try {
111 			out.writeInt(KEY_UP);
112 			out.writeInt(keycode);
113 		} catch (Throwable t) {
114 			synchronized (this) {
115 				connected = false;
116 			}
117 		}
118 		return false;
119 	}
120 
121 	@Override
keyTyped(char character)122 	public boolean keyTyped (char character) {
123 		synchronized (this) {
124 			if (!connected) return false;
125 		}
126 
127 		try {
128 			out.writeInt(KEY_TYPED);
129 			out.writeChar(character);
130 		} catch (Throwable t) {
131 			synchronized (this) {
132 				connected = false;
133 			}
134 		}
135 		return false;
136 	}
137 
138 	@Override
touchDown(int x, int y, int pointer, int button)139 	public boolean touchDown (int x, int y, int pointer, int button) {
140 		synchronized (this) {
141 			if (!connected) return false;
142 		}
143 
144 		try {
145 			out.writeInt(TOUCH_DOWN);
146 			out.writeInt(x);
147 			out.writeInt(y);
148 			out.writeInt(pointer);
149 		} catch (Throwable t) {
150 			synchronized (this) {
151 				connected = false;
152 			}
153 		}
154 		return false;
155 	}
156 
157 	@Override
touchUp(int x, int y, int pointer, int button)158 	public boolean touchUp (int x, int y, int pointer, int button) {
159 		synchronized (this) {
160 			if (!connected) return false;
161 		}
162 
163 		try {
164 			out.writeInt(TOUCH_UP);
165 			out.writeInt(x);
166 			out.writeInt(y);
167 			out.writeInt(pointer);
168 		} catch (Throwable t) {
169 			synchronized (this) {
170 				connected = false;
171 			}
172 		}
173 		return false;
174 	}
175 
176 	@Override
touchDragged(int x, int y, int pointer)177 	public boolean touchDragged (int x, int y, int pointer) {
178 		synchronized (this) {
179 			if (!connected) return false;
180 		}
181 
182 		try {
183 			out.writeInt(TOUCH_DRAGGED);
184 			out.writeInt(x);
185 			out.writeInt(y);
186 			out.writeInt(pointer);
187 		} catch (Throwable t) {
188 			synchronized (this) {
189 				connected = false;
190 			}
191 		}
192 		return false;
193 	}
194 
195 	@Override
mouseMoved(int x, int y)196 	public boolean mouseMoved (int x, int y) {
197 		return false;
198 	}
199 
200 	@Override
scrolled(int amount)201 	public boolean scrolled (int amount) {
202 		return false;
203 	}
204 
isConnected()205 	public boolean isConnected () {
206 		synchronized (this) {
207 			return connected;
208 		}
209 	}
210 }
211