mirror of
https://github.com/rfc2822/GfxTablet
synced 2025-10-06 02:39:56 +02:00
* app refactoring
* uinput driver
This commit is contained in:
parent
1b93a87ed0
commit
cef17da6d2
34 changed files with 251 additions and 103 deletions
55
app-android/src/at/bitfire/gfxtablet/CanvasActivity.java
Normal file
55
app-android/src/at/bitfire/gfxtablet/CanvasActivity.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
import at.bitfire.gfxtablet.R;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
public class CanvasActivity extends Activity {
|
||||
CanvasView canvas;
|
||||
SharedPreferences prefs;
|
||||
NetworkClient netClient;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
PreferenceManager.setDefaultValues(this, R.xml.network_preferences, false);
|
||||
PreferenceManager.setDefaultValues(this, R.xml.drawing_preferences, false);
|
||||
|
||||
setContentView(R.layout.activity_canvas);
|
||||
LinearLayout layout = (LinearLayout)findViewById(R.id.canvas_layout);
|
||||
|
||||
new Thread(netClient = new NetworkClient(PreferenceManager.getDefaultSharedPreferences(this))).start();
|
||||
|
||||
canvas = new CanvasView(this, netClient);
|
||||
layout.addView(canvas);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
netClient.getQueue().add(new NetDisconnectEvent());
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.activity_canvas, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void showAbout(MenuItem item) {
|
||||
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(("http://rfc2822.github.com/GfxTablet"))));
|
||||
}
|
||||
|
||||
public void showSettings(MenuItem item) {
|
||||
startActivity(new Intent(CanvasActivity.this, SettingsActivity.class));
|
||||
}
|
||||
}
|
110
app-android/src/at/bitfire/gfxtablet/CanvasView.java
Normal file
110
app-android/src/at/bitfire/gfxtablet/CanvasView.java
Normal file
|
@ -0,0 +1,110 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.os.AsyncTask;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
@SuppressLint("ViewConstructor")
|
||||
public class CanvasView extends View implements OnSharedPreferenceChangeListener {
|
||||
final static int PRESSURE_RESOLUTION = 10000;
|
||||
|
||||
NetworkClient netClient;
|
||||
SharedPreferences settings;
|
||||
boolean acceptStylusOnly;
|
||||
|
||||
public CanvasView(Context context, NetworkClient netClient) {
|
||||
super(context);
|
||||
|
||||
// disable until networking has been configured
|
||||
setEnabled(false);
|
||||
setBackgroundColor(0xFFD0D0D0);
|
||||
|
||||
settings = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
settings.registerOnSharedPreferenceChangeListener(this);
|
||||
reconfigureAcceptedInputDevices();
|
||||
|
||||
this.netClient = netClient;
|
||||
new ConfigureNetworkingTask().execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences pref, String key) {
|
||||
if (key.equals(SettingsActivity.KEY_PREF_HOST))
|
||||
new ConfigureNetworkingTask().execute();
|
||||
else if (key.equals(SettingsActivity.KEY_PREF_STYLUS_ONLY))
|
||||
reconfigureAcceptedInputDevices();
|
||||
}
|
||||
|
||||
void reconfigureAcceptedInputDevices() {
|
||||
acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
|
||||
Toast.makeText(getContext(), String.format("%dx%d", w, h), Toast.LENGTH_SHORT).show();
|
||||
netClient.getQueue().add(new NetConfigurationEvent(w, h, PRESSURE_RESOLUTION));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onGenericMotionEvent(MotionEvent event) {
|
||||
if (isEnabled()) {
|
||||
for (int ptr = 0; ptr < event.getPointerCount(); ptr++)
|
||||
if (!acceptStylusOnly || (event.getToolType(ptr) == MotionEvent.TOOL_TYPE_STYLUS)) {
|
||||
//Log.i("XorgTablet", String.format("Generic motion event logged: %f|%f, pressure %f", event.getX(ptr), event.getY(ptr), event.getPressure(ptr)));
|
||||
if (event.getActionMasked() == MotionEvent.ACTION_HOVER_MOVE)
|
||||
netClient.getQueue().add(new NetMotionEvent((int)event.getX(ptr), (int)event.getY(ptr), (int)(event.getPressure(ptr)*PRESSURE_RESOLUTION)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
if (isEnabled()) {
|
||||
for (int ptr = 0; ptr < event.getPointerCount(); ptr++)
|
||||
if (!acceptStylusOnly || (event.getToolType(ptr) == MotionEvent.TOOL_TYPE_STYLUS)) {
|
||||
//Log.i("XorgTablet", String.format("Touch event logged: %f|%f, pressure %f", event.getX(ptr), event.getY(ptr), event.getPressure(ptr)));
|
||||
switch (event.getActionMasked()) {
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
netClient.getQueue().add(new NetMotionEvent((int)event.getX(ptr), (int)event.getY(ptr), (int)(event.getPressure(ptr)*PRESSURE_RESOLUTION)));
|
||||
break;
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
netClient.getQueue().add(new NetButtonEvent((int)event.getX(ptr), (int)event.getY(ptr), (int)(event.getPressure(ptr)*PRESSURE_RESOLUTION), true));
|
||||
break;
|
||||
case MotionEvent.ACTION_UP:
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
netClient.getQueue().add(new NetButtonEvent((int)event.getX(ptr), (int)event.getY(ptr), (int)(event.getPressure(ptr)*PRESSURE_RESOLUTION), false));
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private class ConfigureNetworkingTask extends AsyncTask<Void, Void, Boolean> {
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... params) {
|
||||
return netClient.configureNetworking();
|
||||
}
|
||||
|
||||
protected void onPostExecute(Boolean success) {
|
||||
if (success)
|
||||
setEnabled(true);
|
||||
else {
|
||||
setEnabled(false);
|
||||
Toast.makeText(getContext(), "Unknown host name, network tablet disabled!", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
app-android/src/at/bitfire/gfxtablet/NetButtonEvent.java
Normal file
34
app-android/src/at/bitfire/gfxtablet/NetButtonEvent.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class NetButtonEvent extends NetEvent {
|
||||
boolean down;
|
||||
|
||||
public NetButtonEvent(int x, int y, int pressure, boolean down) {
|
||||
super(x, y, pressure);
|
||||
this.down = down;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos);
|
||||
|
||||
try {
|
||||
dos.write(1); /* EVENT_TYPE_BUTTON */
|
||||
dos.writeShort(x);
|
||||
dos.writeShort(y);
|
||||
dos.writeShort(pressure);
|
||||
dos.write(1);
|
||||
dos.write(down ? 1 : 0);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class NetConfigurationEvent extends NetEvent {
|
||||
public NetConfigurationEvent(int x, int y, int pressure) {
|
||||
super(x, y, pressure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos);
|
||||
|
||||
try {
|
||||
dos.write(2); /* EVENT_TYPE_SET_RESOLUTION */
|
||||
dos.writeShort(x);
|
||||
dos.writeShort(y);
|
||||
dos.writeShort(pressure);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
12
app-android/src/at/bitfire/gfxtablet/NetDisconnectEvent.java
Normal file
12
app-android/src/at/bitfire/gfxtablet/NetDisconnectEvent.java
Normal file
|
@ -0,0 +1,12 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
public class NetDisconnectEvent extends NetEvent {
|
||||
public NetDisconnectEvent() {
|
||||
super(0, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
return null;
|
||||
}
|
||||
}
|
17
app-android/src/at/bitfire/gfxtablet/NetEvent.java
Normal file
17
app-android/src/at/bitfire/gfxtablet/NetEvent.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
public abstract class NetEvent {
|
||||
int x, y, pressure;
|
||||
|
||||
public int getX() { return x; }
|
||||
public int getY() { return y; }
|
||||
public int getPressure() { return pressure; }
|
||||
|
||||
public NetEvent(int x, int y, int pressure) {
|
||||
this.x = Math.max(x, 0);
|
||||
this.y = Math.max(y, 0);
|
||||
this.pressure = pressure;
|
||||
}
|
||||
|
||||
public abstract byte[] toByteArray();
|
||||
}
|
28
app-android/src/at/bitfire/gfxtablet/NetMotionEvent.java
Normal file
28
app-android/src/at/bitfire/gfxtablet/NetMotionEvent.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class NetMotionEvent extends NetEvent {
|
||||
public NetMotionEvent(int x, int y, int pressure) {
|
||||
super(x, y, pressure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toByteArray() {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
DataOutputStream dos = new DataOutputStream(baos);
|
||||
|
||||
try {
|
||||
dos.write(0); /* EVENT_TYPE_MOTION */
|
||||
dos.writeShort(x);
|
||||
dos.writeShort(y);
|
||||
dos.writeShort(pressure);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
}
|
68
app-android/src/at/bitfire/gfxtablet/NetworkClient.java
Normal file
68
app-android/src/at/bitfire/gfxtablet/NetworkClient.java
Normal file
|
@ -0,0 +1,68 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
// see "drivers" directory in Github repository for details about the protocol
|
||||
|
||||
|
||||
public class NetworkClient implements Runnable {
|
||||
LinkedBlockingQueue<NetEvent> motionQueue = new LinkedBlockingQueue<NetEvent>();
|
||||
LinkedBlockingQueue<NetEvent> getQueue() { return motionQueue; }
|
||||
|
||||
InetAddress destAddress;
|
||||
SharedPreferences preferences;
|
||||
NetConfigurationEvent lastConfiguration = null;
|
||||
|
||||
NetworkClient(SharedPreferences preferences) {
|
||||
this.preferences = preferences;
|
||||
}
|
||||
|
||||
boolean configureNetworking() {
|
||||
try {
|
||||
String hostName = preferences.getString(SettingsActivity.KEY_PREF_HOST, "unknown.invalid");
|
||||
destAddress = InetAddress.getByName(hostName);
|
||||
|
||||
if (lastConfiguration != null)
|
||||
motionQueue.add(lastConfiguration);
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
destAddress = null;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
DatagramSocket socket = new DatagramSocket();
|
||||
|
||||
while (true) {
|
||||
NetEvent event = motionQueue.take();
|
||||
|
||||
// save resolution, even if not sending it
|
||||
if (event.getClass() == NetConfigurationEvent.class)
|
||||
lastConfiguration = (NetConfigurationEvent)event;
|
||||
// graceful shutdown
|
||||
else if (event.getClass() == NetDisconnectEvent.class)
|
||||
break;
|
||||
|
||||
if (destAddress == null) // no valid destination host
|
||||
continue;
|
||||
|
||||
byte[] data = event.toByteArray();
|
||||
DatagramPacket pkt = new DatagramPacket(data, data.length, destAddress, 40117);
|
||||
socket.send(pkt);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("GfxTablet", "motionQueue failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
52
app-android/src/at/bitfire/gfxtablet/SettingsActivity.java
Normal file
52
app-android/src/at/bitfire/gfxtablet/SettingsActivity.java
Normal file
|
@ -0,0 +1,52 @@
|
|||
package at.bitfire.gfxtablet;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceFragment;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import at.bitfire.gfxtablet.R;
|
||||
|
||||
public class SettingsActivity extends PreferenceActivity {
|
||||
public static final String
|
||||
KEY_PREF_HOST = "host_preference",
|
||||
KEY_PREF_STYLUS_ONLY = "stylus_only_preference";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBuildHeaders(List<Header> target) {
|
||||
loadHeadersFromResource(R.xml.preference_headers, target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (item.getItemId() == android.R.id.home)
|
||||
finish();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static class NetworkPrefsFragment extends PreferenceFragment {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.network_preferences);
|
||||
}
|
||||
}
|
||||
|
||||
public static class DrawingPrefsFragment extends PreferenceFragment {
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.drawing_preferences);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue