1
0
Fork 0
mirror of https://github.com/rfc2822/GfxTablet synced 2025-10-03 01:29:17 +02:00

* app refactoring

* uinput driver
This commit is contained in:
Richard Hirner 2013-01-26 21:24:36 +01:00
parent 1b93a87ed0
commit cef17da6d2
34 changed files with 251 additions and 103 deletions

View file

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.bitfire.gfxtablet"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="at.bitfire.gfxtablet.CanvasActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="at.bitfire.gfxtablet.SettingsActivity"
android:label="@string/title_activity_settings" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="at.bitfire.gfxtablet.CanvasActivity" />
</activity>
</application>
</manifest>

43
app-android/README.md Normal file
View file

@ -0,0 +1,43 @@
XorgTablet
==========
XorgTablet is an Android app that sends motion and touch events
via UDP to a specified host on port 40117.
It is especially useful in combination with the xf86-networktablet
X.org input driver (https://github.com/rfc2822/xf86-networktablet)
that allows these touch events to be processed by the X server. So,
you can use your Android tablet or smartphone to control the X
server and, for instance *use GIMP with your Android tablet
as a graphics tablet* (even pressure-sensitive, if your hardware
supports it).
Requirements
------------
Any device with Android 4.0+ and touch screen
Download
--------
You can find the latest binary on SourceForge:
https://sourceforge.net/projects/xorgtablet/files/XorgTablet/
Features
--------
* Pressure sensitive
* Size of canvas will be detected and sent to the client
* Option for ignoring events that are not triggered by a stylus pen:
so you can lay your hand on the tablet and draw with the pen.
Technical details
-----------------
The used protocol:
https://github.com/rfc2822/xf86-networktablet/blob/master/protocol.h

Binary file not shown.

View file

@ -0,0 +1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

View file

@ -0,0 +1,14 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
target=android-17

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -0,0 +1,17 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CanvasActivity" >
<LinearLayout
android:id="@+id/canvas_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>

View file

@ -0,0 +1,13 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="@string/menu_settings" android:onClick="showSettings"/>
<item
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_about" android:onClick="showAbout"/>
</menu>

View file

@ -0,0 +1,11 @@
<resources>
<!--
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<!-- API 11 theme customizations can go here. -->
</style>
</resources>

View file

@ -0,0 +1,12 @@
<resources>
<!--
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
</resources>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">GfxTablet</string>
<string name="menu_settings">Settings</string>
<string name="preferences_host_title">Network host</string>
<string name="stylus_preference">Sense stylus only</string>
<string name="menu_about">About / Help</string>
</resources>

View file

@ -0,0 +1,14 @@
<resources>
<string name="title_activity_settings">Settings</string>
<!-- Strings related to Settings -->
<!-- Example General settings -->
<!-- Example settings for Data & Sync -->
<!-- Example settings for Notifications -->
</resources>

View file

@ -0,0 +1,20 @@
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference android:title="@string/stylus_preference" android:key="stylus_only_preference" android:defaultValue="false"/>
</PreferenceScreen>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference android:title="@string/preferences_host_title" android:key="host_preference" android:defaultValue="please-replace.invalid" android:singleLine="true"/>
</PreferenceScreen>

View file

@ -0,0 +1,9 @@
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android">
<header android:fragment="at.bitfire.gfxtablet.SettingsActivity$NetworkPrefsFragment"
android:title="Networking" />
<header android:fragment="at.bitfire.gfxtablet.SettingsActivity$DrawingPrefsFragment"
android:title="Drawing" />
</preference-headers>

View 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));
}
}

View 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();
}
}
}
}

View 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();
}
}

View file

@ -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();
}
}

View 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;
}
}

View 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();
}

View 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();
}
}

View 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());
}
}
}

View 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);
}
}
}