1
0
Fork 0
mirror of https://github.com/rfc2822/GfxTablet synced 2025-10-03 09:39:16 +02:00

Merge pull request #88 from akdor1154/uint16_t

Use unsigned shorts as for value range
This commit is contained in:
Ricki 2015-11-29 00:16:48 +01:00
commit f957287eac
6 changed files with 32 additions and 31 deletions

View file

@ -124,17 +124,18 @@ public class CanvasView extends View implements SharedPreferences.OnSharedPrefer
return false; return false;
} }
// these overflow and wrap around to negative short values, but thankfully Java will continue
// on regardless, so we can just ignore Java's interpretation of them and send them anyway.
short normalizeX(float x) { short normalizeX(float x) {
return (short)(Math.min(Math.max(0, x), maxX) * Short.MAX_VALUE/maxX); return (short)(Math.min(Math.max(0, x), maxX) * 2*Short.MAX_VALUE/maxX);
} }
short normalizeY(float x) { short normalizeY(float x) {
return (short)(Math.min(Math.max(0, x), maxY) * Short.MAX_VALUE/maxY); return (short)(Math.min(Math.max(0, x), maxY) * 2*Short.MAX_VALUE/maxY);
} }
short normalizePressure(float x) { short normalizePressure(float x) {
return (short)(Math.min(Math.max(0, x), 2.0) * Short.MAX_VALUE/2.0); return (short)(Math.min(Math.max(0, x), 2.0) * Short.MAX_VALUE);
} }
} }

View file

@ -15,7 +15,7 @@ public class NetEvent {
TYPE_DISCONNECT TYPE_DISCONNECT
} }
static final String signature = "GfxTablet"; static final String signature = "GfxTablet";
static final short protocol_version = 1; static final short protocol_version = 2;
final Type type; final Type type;
short x, y, pressure; short x, y, pressure;

View file

@ -2,7 +2,7 @@ Network protocol used by GfxTablet
Version 1 Version 2
--------- ---------
GfxTablet app sends UDP packets to port 40118 of the destination host. GfxTablet app sends UDP packets to port 40118 of the destination host.
@ -10,17 +10,17 @@ GfxTablet app sends UDP packets to port 40118 of the destination host.
Packet structure, uses network byte order (big endian): Packet structure, uses network byte order (big endian):
9 bytes "GfxTablet" 9 bytes "GfxTablet"
1 word version number 1 unsigned int16 version number
1 byte type: 1 byte event type:
0 motion event (hovering) 0: motion event (hovering)
1 button event (finger, pen etc. touches surface) 1: button event (finger, pen etc. touches surface)
1 word x (using full range: 0..65535) 1 unsigned int16 x (using full range: 0..65535)
1 word y (using full range: 0..65535) 1 unsigned int16 y (using full range: 0..65535)
1 word pressure (using full range 0..65535, 32768 == pressure 1.0f on Android device) 1 unsigned int16 pressure (accepting full range 0..65535, but will clip to 32768 == pressure 1.0f on Android device)
when type == button event: when type == button event:
1 byte number of button, starting with 0 1 signed int8 number of button, starting with 0
1 byte button status: 1 byte button status:
0 button is released ("up") 0 button is released ("up")
1 button is pressed ("down") 1 button is pressed ("down")

View file

@ -2,7 +2,7 @@ TARGET = networktablet
PREFIX = /usr/local PREFIX = /usr/local
BINDIR = $(PREFIX)/bin BINDIR = $(PREFIX)/bin
networktablet : networktablet.c protocol.h networktablet : networktablet.c
clean : clean :
rm -f networktablet rm -f networktablet

View file

@ -10,6 +10,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/uinput.h> #include <linux/uinput.h>
#include <stdint.h>
#include "protocol.h" #include "protocol.h"
#define die(str, args...) { \ #define die(str, args...) { \
@ -52,11 +53,11 @@ void init_device(int fd)
uidev.id.product = 0x1; uidev.id.product = 0x1;
uidev.id.version = 1; uidev.id.version = 1;
uidev.absmin[ABS_X] = 0; uidev.absmin[ABS_X] = 0;
uidev.absmax[ABS_X] = SHRT_MAX; uidev.absmax[ABS_X] = UINT16_MAX;
uidev.absmin[ABS_Y] = 0; uidev.absmin[ABS_Y] = 0;
uidev.absmax[ABS_Y] = SHRT_MAX; uidev.absmax[ABS_Y] = UINT16_MAX;
uidev.absmin[ABS_PRESSURE] = 0; uidev.absmin[ABS_PRESSURE] = 0;
uidev.absmax[ABS_PRESSURE] = SHRT_MAX/2; uidev.absmax[ABS_PRESSURE] = INT16_MAX;
if (write(fd, &uidev, sizeof(uidev)) < 0) if (write(fd, &uidev, sizeof(uidev)) < 0)
die("error: write"); die("error: write");
@ -132,7 +133,7 @@ int main(void)
ev_pkt.x = ntohs(ev_pkt.x); ev_pkt.x = ntohs(ev_pkt.x);
ev_pkt.y = ntohs(ev_pkt.y); ev_pkt.y = ntohs(ev_pkt.y);
ev_pkt.pressure = ntohs(ev_pkt.pressure); ev_pkt.pressure = ntohs(ev_pkt.pressure);
printf("x: %hi, y: %hi, pressure: %hi\n", ev_pkt.x, ev_pkt.y, ev_pkt.pressure); printf("x: %hu, y: %hu, pressure: %hu\n", ev_pkt.x, ev_pkt.y, ev_pkt.pressure);
send_event(device, EV_ABS, ABS_X, ev_pkt.x); send_event(device, EV_ABS, ABS_X, ev_pkt.x);
send_event(device, EV_ABS, ABS_Y, ev_pkt.y); send_event(device, EV_ABS, ABS_Y, ev_pkt.y);

View file

@ -1,7 +1,6 @@
#define GFXTABLET_PORT 40118 #define GFXTABLET_PORT 40118
#define PROTOCOL_VERSION 1 #define PROTOCOL_VERSION 2
#pragma pack(push) #pragma pack(push)
@ -13,16 +12,16 @@
struct event_packet struct event_packet
{ {
char signature[9]; char signature[9];
unsigned short version; uint16_t version;
char type; /* EVENT_TYPE_... */ uint8_t type; /* EVENT_TYPE_... */
struct { /* required */ struct { /* required */
short x, y; uint16_t x, y;
short pressure; uint16_t pressure;
}; };
struct { /* only required for EVENT_TYPE_BUTTON */ struct { /* only required for EVENT_TYPE_BUTTON */
char button; /* number of button, beginning with 1 */ int8_t button; /* number of button, beginning with 1 */
char down; /* 1 = button down, 0 = button up */ int8_t down; /* 1 = button down, 0 = button up */
}; };
}; };