Added infrastructure for sending packets to Kodi's EventServer

This commit is contained in:
Synced Synapse 2015-04-23 19:19:18 +01:00
parent 3d020ceccf
commit 9d13a71e8a
11 changed files with 1306 additions and 0 deletions

View File

@ -0,0 +1,180 @@
/*
* Copyright (C) 2005-2009 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC Remote; see the file license. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
package org.xbmc.kore.eventclient;
/**
* Remote control and keyboard strings, taken from xbmc/ButtonTranslator.cpp
*
* @author Team XBMC
*/
public final class ButtonCodes {
public static final String REMOTE_LEFT = "left";
public static final String REMOTE_RIGHT = "right";
public static final String REMOTE_UP = "up";
public static final String REMOTE_DOWN = "down";
public static final String REMOTE_SELECT = "select";
public static final String REMOTE_BACK = "back";
public static final String REMOTE_MENU = "menu";
public static final String REMOTE_INFO = "info";
public static final String REMOTE_DISPLAY = "display";
public static final String REMOTE_TITLE = "title";
public static final String REMOTE_PLAY = "play";
public static final String REMOTE_PAUSE = "pause";
public static final String REMOTE_REVERSE = "reverse";
public static final String REMOTE_FORWARD = "forward";
public static final String REMOTE_SKIP_PLUS = "skipplus";
public static final String REMOTE_SKIP_MINUS = "skipminus";
public static final String REMOTE_STOP = "stop";
public static final String REMOTE_0 = "zero";
public static final String REMOTE_1 = "one";
public static final String REMOTE_2 = "two";
public static final String REMOTE_3 = "three";
public static final String REMOTE_4 = "four";
public static final String REMOTE_5 = "five";
public static final String REMOTE_6 = "six";
public static final String REMOTE_7 = "seven";
public static final String REMOTE_8 = "eight";
public static final String REMOTE_9 = "nine";
// additional keys from the media center extender for xbox remote
public static final String REMOTE_POWER = "power";
public static final String REMOTE_MY_TV = "mytv";
public static final String REMOTE_MY_MUSIC = "mymusic";
public static final String REMOTE_MY_PICTURES = "mypictures";
public static final String REMOTE_MY_VIDEOS = "myvideo";
public static final String REMOTE_RECORD = "record";
public static final String REMOTE_START = "start";
public static final String REMOTE_VOLUME_PLUS = "volumeplus";
public static final String REMOTE_VOLUME_MINUS = "volumeminus";
public static final String REMOTE_CHANNEL_PLUS = "channelplus";
public static final String REMOTE_CHANNEL_MINUS = "channelminus";
public static final String REMOTE_PAGE_PLUS = "pageplus";
public static final String REMOTE_PAGE_MINUS = "pageminus";
public static final String REMOTE_MUTE = "mute";
public static final String REMOTE_RECORDED_TV = "recordedtv";
public static final String REMOTE_GUIDE = "guide";
public static final String REMOTE_LIVE_TV = "livetv";
public static final String REMOTE_STAR = "star";
public static final String REMOTE_HASH = "hash";
public static final String REMOTE_CLEAR = "clear";
public static final String REMOTE_ENTER = "enter";
public static final String REMOTE_XBOX = "xbox";
public static final String KEYBOARD_RETURN = "return";
public static final String KEYBOARD_ENTER = "enter";
public static final String KEYBOARD_ESCAPE = "escape";
public static final String KEYBOARD_ESC = "esc";
public static final String KEYBOARD_TAB = "tab";
public static final String KEYBOARD_SPACE = "space";
public static final String KEYBOARD_LEFT = "left";
public static final String KEYBOARD_RIGHT = "right";
public static final String KEYBOARD_UP = "up";
public static final String KEYBOARD_DOWN = "down";
public static final String KEYBOARD_INSERT = "insert";
public static final String KEYBOARD_DELETE = "delete";
public static final String KEYBOARD_HOME = "home";
public static final String KEYBOARD_END = "end";
public static final String KEYBOARD_F1 = "f1";
public static final String KEYBOARD_F2 = "f2";
public static final String KEYBOARD_F3 = "f3";
public static final String KEYBOARD_F4 = "f4";
public static final String KEYBOARD_F5 = "f5";
public static final String KEYBOARD_F6 = "f6";
public static final String KEYBOARD_F7 = "f7";
public static final String KEYBOARD_F8 = "f8";
public static final String KEYBOARD_F9 = "f9";
public static final String KEYBOARD_F10 = "f10";
public static final String KEYBOARD_F11 = "f11";
public static final String KEYBOARD_F12 = "f12";
public static final String KEYBOARD_NUMPAD_ZERO = "numpadzero";
public static final String KEYBOARD_NUMPAD_1 = "numpadone";
public static final String KEYBOARD_NUMPAD_2 = "numpadtwo";
public static final String KEYBOARD_NUMPAD_3 = "numpadthree";
public static final String KEYBOARD_NUMPAD_4 = "numpadfour";
public static final String KEYBOARD_NUMPAD_5 = "numpadfive";
public static final String KEYBOARD_NUMPAD_6 = "numpadsix";
public static final String KEYBOARD_NUMPAD_7 = "numpadseven";
public static final String KEYBOARD_NUMPAD_8 = "numpadeight";
public static final String KEYBOARD_NUMPAD_9 = "numpadnine";
public static final String KEYBOARD_NUMPAD_TIMES = "numpadtimes";
public static final String KEYBOARD_NUMPAD_PLUS = "numpadplus";
public static final String KEYBOARD_NUMPAD_MINUS = "numpadminus";
public static final String KEYBOARD_NUMPAD_PERIOD = "numpadperiod";
public static final String KEYBOARD_NUMPAD_DIVIDE = "numpaddivide";
public static final String KEYBOARD_PAGEUP = "pageup";
public static final String KEYBOARD_PAGEDOWN = "pagedown";
public static final String KEYBOARD_PRINTSCREEN = "printscreen";
public static final String KEYBOARD_BACKSPACE = "backspace";
public static final String KEYBOARD_MENU = "menu";
public static final String KEYBOARD_PAUSE = "pause";
public static final String KEYBOARD_LEFTSHIFT = "leftshift";
public static final String KEYBOARD_RIGHTSHIFT = "rightshift";
public static final String KEYBOARD_LEFTCTRL = "leftctrl";
public static final String KEYBOARD_RIGHTCTRL = "rightctrl";
public static final String KEYBOARD_LEFTALT = "leftalt";
public static final String KEYBOARD_RIGHTALT = "rightalt";
public static final String KEYBOARD_LEFTWINDOWS = "leftwindows";
public static final String KEYBOARD_RIGHTWINDOWS = "rightwindows";
public static final String KEYBOARD_CAPSLOCK = "capslock";
public static final String KEYBOARD_NUMLOCK = "numlock";
public static final String KEYBOARD_SCROLLLOCK = "scrolllock";
public static final String KEYBOARD_SEMICOLON = "semicolon";
public static final String KEYBOARD_COLON = "colon";
public static final String KEYBOARD_EQUALS = "equals";
public static final String KEYBOARD_PLUS = "plus";
public static final String KEYBOARD_COMMA = "comma";
public static final String KEYBOARD_LESSTHAN = "lessthan";
public static final String KEYBOARD_MINUS = "minus";
public static final String KEYBOARD_UNDERLINE = "underline";
public static final String KEYBOARD_PERIOD = "period";
public static final String KEYBOARD_GREATERTHAN = "greaterthan";
public static final String KEYBOARD_FORWARDSLASH = "forwardslash";
public static final String KEYBOARD_QUESTIONMARK = "questionmark";
public static final String KEYBOARD_LEFTQUOTE = "leftquote";
public static final String KEYBOARD_TILDE = "tilde";
public static final String KEYBOARD_OPENSQUAREBRACKET = "opensquarebracket";
public static final String KEYBOARD_OPENBRACE = "openbrace";
public static final String KEYBOARD_BACKSLASH = "backslash";
public static final String KEYBOARD_PIPE = "pipe";
public static final String KEYBOARD_CLOSESQUAREBRACKET = "closesquarebracket";
public static final String KEYBOARD_CLOSEBRACE = "closebrace";
public static final String KEYBOARD_QUOTE = "quote";
public static final String KEYBOARD_DOUBLEQUOTE = "doublequote";
public static final String KEYBOARD_LAUNCH_MAIL = "launch_mail";
public static final String KEYBOARD_BROWSER_HOME = "browser_home";
public static final String KEYBOARD_BROWSER_FAVORITES = "browser_favorites";
public static final String KEYBOARD_BROWSER_REFRESH = "browser_refresh";
public static final String KEYBOARD_BROWSER_SEARCH = "browser_search";
public static final String KEYBOARD_LAUNCH_APP1_PC_ICON = "launch_app1_pc_icon";
public static final String KEYBOARD_LAUNCH_MEDIA_SELECT = "launch_media_select";
public static final String KEYBOARD_PLAY_PAUSE = "play_pause";
public static final String KEYBOARD_STOP = "stop";
public static final String KEYBOARD_VOLUME_UP = "volume_up";
public static final String KEYBOARD_VOLUME_MUTE = "volume_mute";
public static final String KEYBOARD_VOLUME_DOWN = "volume_down";
public static final String KEYBOARD_PREV_TRACK = "prev_track";
public static final String KEYBOARD_NEXT_TRACK = "next_track";
public static final String GAMEPAD_LEFT_ANALOG_TRIGGER = "leftanalogtrigger";
public static final String GAMEPAD_RIGHT_ANALOG_TRIGGER = "rightanalogtrigger";
}

View File

@ -0,0 +1,317 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
/**
* Event Client Class
*
* Implements an XBMC-Client. This class can be used to implement your own application which
* should act as a Input device for XBMC. Also starts a Ping-Thread, which tells the XBMC EventServer
* that the client is alive. Therefore if you close your application you SHOULD call stopClient()!
* @author Stefan Agner
*
*/
public class EventClient
{
private boolean hasIcon = false;
private String deviceName;
private PingThread oPingThread;
private byte iconType = Packet.ICON_PNG;
private byte[] iconData;
private InetAddress hostAddress;
private int hostPort;
/**
* Starts a XBMC EventClient.
* @param hostAddress Address of the Host running XBMC
* @param hostPort Port of the Host running XBMC (default 9777)
* @param deviceName Name of the Device
* @param iconFile Path to the Iconfile (PNG, JPEG or GIF)
* @throws IOException
*/
public EventClient(InetAddress hostAddress, int hostPort, String deviceName, String iconFile) throws IOException
{
byte iconType = Packet.ICON_PNG;
// Assume png as icon type
if(iconFile.toLowerCase().endsWith(".jpeg"))
iconType = Packet.ICON_JPEG;
if(iconFile.toLowerCase().endsWith(".jpg"))
iconType = Packet.ICON_JPEG;
if(iconFile.toLowerCase().endsWith(".gif"))
iconType = Packet.ICON_GIF;
// Read the icon file to the byte array...
FileInputStream iconFileStream = new FileInputStream(iconFile);
byte[] iconData = new byte[iconFileStream.available()];
iconFileStream.read(iconData);
hasIcon = true;
// Call start-Method...
startClient(hostAddress, hostPort, deviceName, iconType, iconData);
}
/**
* Starts a XBMC EventClient.
* @param hostAddress Address of the Host running XBMC
* @param hostPort Port of the Host running XBMC (default 9777)
* @param deviceName Name of the Device
* @param iconType Type of the icon file (see Packet.ICON_PNG, Packet.ICON_JPEG or Packet.ICON_GIF)
* @param iconData The icon itself as a Byte-Array
* @throws IOException
*/
public EventClient(InetAddress hostAddress, int hostPort, String deviceName, byte iconType, byte[] iconData) throws IOException
{
hasIcon = true;
startClient(hostAddress, hostPort, deviceName, iconType, iconData);
}
/**
* Starts a XBMC EventClient without an icon.
* @param hostAddress Address of the Host running XBMC
* @param hostPort Port of the Host running XBMC (default 9777)
* @param deviceName Name of the Device
* @throws IOException
*/
public EventClient(InetAddress hostAddress, int hostPort, String deviceName) throws IOException
{
hasIcon = false;
byte iconType = Packet.ICON_NONE;
byte[] iconData = null;
startClient(hostAddress, hostPort, deviceName, iconType, iconData);
}
/**
* Starts a XBMC EventClient.
* @param hostAddress Address of the Host running XBMC
* @param hostPort Port of the Host running XBMC (default 9777)
* @param deviceName Name of the Device
* @param iconType Type of the icon file (see Packet.ICON_PNG, Packet.ICON_JPEG or Packet.ICON_GIF)
* @param iconData The icon itself as a Byte-Array
* @throws IOException
*/
private void startClient(InetAddress hostAddress, int hostPort, String deviceName, byte iconType, byte[] iconData) throws IOException
{
// Save host address and port
this.hostAddress = hostAddress;
this.hostPort = hostPort;
this.deviceName = deviceName;
this.iconType = iconType;
this.iconData = iconData;
// Send Hello Packet...
PacketHELO p;
if(hasIcon)
p = new PacketHELO(deviceName, iconType, iconData);
else
p = new PacketHELO(deviceName);
p.send(hostAddress, hostPort);
// Start Thread (for Ping packets...)
oPingThread = new PingThread(hostAddress, hostPort, 20000);
oPingThread.start();
}
/**
* Stops the XBMC EventClient (especially the Ping-Thread)
* @throws IOException
*/
public void stopClient() throws IOException
{
// Stop Ping-Thread...
oPingThread.giveup();
oPingThread.interrupt();
PacketBYE p = new PacketBYE();
p.send(hostAddress, hostPort);
}
/**
* Displays a notification window in XBMC.
* @param title Message title
* @param message The actual message
*/
public void sendNotification(String title, String message) throws IOException
{
PacketNOTIFICATION p;
if(hasIcon)
p = new PacketNOTIFICATION(title, message, iconType, iconData);
else
p = new PacketNOTIFICATION(title, message);
p.send(hostAddress, hostPort);
}
/**
* Sends a Button event
* @param code raw button code (default: 0)
* @param repeat this key press should repeat until released (default: 1)
* Note that queued pressed cannot repeat.
* @param down if this is 1, it implies a press event, 0 implies a release
* event. (default: 1)
* @param queue a queued key press means that the button event is
* executed just once after which the next key press is processed.
* It can be used for macros. Currently there is no support for
* time delays between queued presses. (default: 0)
* @param amount unimplemented for now; in the future it will be used for
* specifying magnitude of analog key press events
* @param axis
*/
public void sendButton(short code, boolean repeat, boolean down, boolean queue, short amount, byte axis) throws IOException
{
PacketBUTTON p = new PacketBUTTON(code, repeat, down, queue, amount, axis);
p.send(hostAddress, hostPort);
}
/**
* Sends a Button event
* @param map_name a combination of map_name and button_name refers to a
* mapping in the user's Keymap.xml or Lircmap.xml.
* map_name can be one of the following:
* <ul>
* <li>"KB" => standard keyboard map ( <keyboard> section )</li>
* <li>"XG" => xbox gamepad map ( <gamepad> section )</li>
* <li>"R1" => xbox remote map ( <remote> section )</li>
* <li>"R2" => xbox universal remote map ( <universalremote> section )</li>
* <li>"LI:devicename" => LIRC remote map where 'devicename' is the
* actual device's name</li></ul>
* @param button_name a button name defined in the map specified in map_name.
* For example, if map_name is "KB" refering to the <keyboard> section in Keymap.xml
* then, valid button_names include "printscreen", "minus", "x", etc.
* @param repeat this key press should repeat until released (default: 1)
* Note that queued pressed cannot repeat.
* @param down if this is 1, it implies a press event, 0 implies a release
* event. (default: 1)
* @param queue a queued key press means that the button event is
* executed just once after which the next key press is processed.
* It can be used for macros. Currently there is no support for
* time delays between queued presses. (default: 0)
* @param amount unimplemented for now; in the future it will be used for
* specifying magnitude of analog key press events
* @param axis
*/
public void sendButton(String map_name, String button_name, boolean repeat, boolean down, boolean queue, short amount, byte axis) throws IOException
{
PacketBUTTON p = new PacketBUTTON(map_name, button_name, repeat, down, queue, amount, axis);
p.send(hostAddress, hostPort);
}
/**
* Sets the mouse position in XBMC
* @param x horitontal position ranging from 0 to 65535
* @param y vertical position ranging from 0 to 65535
*/
public void sendMouse(int x, int y) throws IOException
{
PacketMOUSE p = new PacketMOUSE(x, y);
p.send(hostAddress, hostPort);
}
/**
* Sends a ping to the XBMC EventServer
* @throws IOException
*/
public void ping() throws IOException
{
PacketPING p = new PacketPING();
p.send(hostAddress, hostPort);
}
/**
* Tells XBMC to log the message to xbmc.log with the loglevel as specified.
* @param loglevel the loglevel, follows XBMC standard.
* <ul>
* <li>0 = DEBUG</li>
* <li>1 = INFO</li>
* <li>2 = NOTICE</li>
* <li>3 = WARNING</li>
* <li>4 = ERROR</li>
* <li>5 = SEVERE</li>
* </ul>
* @param logmessage the message to log
*/
public void sendLog(byte loglevel, String logmessage) throws IOException
{
PacketLOG p = new PacketLOG(loglevel, logmessage);
p.send(hostAddress, hostPort);
}
/**
* Tells XBMC to do the action specified, based on the type it knows were it needs to be sent.
* @param actionmessage Actionmessage (as in scripting/skinning)
*/
public void sendAction(String actionmessage) throws IOException
{
PacketACTION p = new PacketACTION(actionmessage);
p.send(hostAddress, hostPort);
}
/**
* Implements a PingThread which tells XBMC EventServer that the Client is alive (this should
* be done at least every 60 seconds!
* @author Stefan Agner
*
*/
class PingThread extends Thread
{
private InetAddress hostAddress;
private int hostPort;
private int sleepTime;
private boolean giveup = false;
public PingThread(InetAddress hostAddress, int hostPort, int sleepTime)
{
super("XBMC EventClient Ping-Thread");
this.hostAddress = hostAddress;
this.hostPort = hostPort;
this.sleepTime = sleepTime;
}
public void giveup()
{
giveup = true;
}
public void run()
{
while(!giveup)
{
try {
PacketPING p = new PacketPING();
p.send(hostAddress, hostPort);
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
}
}
}
}
}

View File

@ -0,0 +1,289 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* XBMC Event Client Class
* <p>
* Implementation of XBMC's UDP based input system.
* A set of classes that abstract the various packets that the event server
* currently supports. In addition, there's also a class, XBMCClient, that
* provides functions that sends the various packets. Use XBMCClient if you
* don't need complete control over packet structure.
* </p>
* <p>
* The basic workflow involves:
* <ol>
* <li>Send a HELO packet</li>
* <li>Send x number of valid packets</li>
* <li>Send a BYE packet</li>
* </ol>
* </p>
* <p>
* IMPORTANT NOTE ABOUT TIMEOUTS:
* A client is considered to be timed out if XBMC doesn't received a packet
* at least once every 60 seconds. To "ping" XBMC with an empty packet use
* PacketPING or XBMCClient.ping(). See the documentation for details.
* </p>
* <p>
* Base class that implements a single event packet.
* - Generic packet structure (maximum 1024 bytes per packet)
* - Header is 32 bytes long, so 992 bytes available for payload
* - large payloads can be split into multiple packets using H4 and H5
* H5 should contain total no. of packets in such a case
* - H6 contains length of P1, which is limited to 992 bytes
* - if H5 is 0 or 1, then H4 will be ignored (single packet msg)
* - H7 must be set to zeros for now
* </p>
* <pre>
* -----------------------------
* | -H1 Signature ("XBMC") | - 4 x CHAR 4B
* | -H2 Version (eg. 2.0) | - 2 x UNSIGNED CHAR 2B
* | -H3 PacketType | - 1 x UNSIGNED SHORT 2B
* | -H4 Sequence number | - 1 x UNSIGNED LONG 4B
* | -H5 No. of packets in msg | - 1 x UNSIGNED LONG 4B
* | -H6 Payloadsize of packet | - 1 x UNSIGNED SHORT 2B
* | -H7 Client's unique token | - 1 x UNSIGNED LONG 4B
* | -H8 Reserved | - 10 x UNSIGNED CHAR 10B
* |---------------------------|
* | -P1 payload | -
* -----------------------------
* </pre>
* @author Stefan Agner
*
*/
public abstract class Packet {
private byte[] sig;
private byte[] payload = new byte[0];
private byte minver;
private byte majver;
private short packettype;
private final static short MAX_PACKET_SIZE = 1024;
private final static short HEADER_SIZE = 32;
private final static short MAX_PAYLOAD_SIZE = MAX_PACKET_SIZE - HEADER_SIZE;
protected final static byte PT_HELO = 0x01;
protected final static byte PT_BYE = 0x02;
protected final static byte PT_BUTTON = 0x03;
protected final static byte PT_MOUSE = 0x04;
protected final static byte PT_PING = 0x05;
protected final static byte PT_BROADCAST = 0x06;
protected final static byte PT_NOTIFICATION = 0x07;
protected final static byte PT_BLOB = 0x08;
protected final static byte PT_LOG = 0x09;
protected final static byte PT_ACTION = 0x0A;
protected final static byte PT_DEBUG = (byte)0xFF;
public final static byte ICON_NONE = 0x00;
public final static byte ICON_JPEG = 0x01;
public final static byte ICON_PNG = 0x02;
public final static byte ICON_GIF = 0x03;
private static int uid = (int)(Math.random()*Integer.MAX_VALUE);
/**
* This is an Abstract class and cannot be instanced. Please use one of the Packet implementation Classes
* (PacketXXX).
*
* Implements an XBMC Event Client Packet. Type is to be specified at creation time, Payload can be added
* with the various appendPayload methods. Packet can be sent through UDP-Socket with method "send".
* @param packettype Type of Packet (PT_XXX)
*/
protected Packet(short packettype)
{
sig = new byte[] {'X', 'B', 'M', 'C' };
minver = 0;
majver = 2;
this.packettype = packettype;
}
/**
* Appends a String to the payload (terminated with 0x00)
* @param payload Payload as String
*/
protected void appendPayload(String payload)
{
byte[] payloadarr = payload.getBytes();
int oldpayloadsize = this.payload.length;
byte[] oldpayload = this.payload;
this.payload = new byte[oldpayloadsize+payloadarr.length+1]; // Create new Array with more place (+1 for string terminator)
System.arraycopy(oldpayload, 0, this.payload, 0, oldpayloadsize);
System.arraycopy(payloadarr, 0, this.payload, oldpayloadsize, payloadarr.length);
}
/**
* Appends a single Byte to the payload
* @param payload Payload
*/
protected void appendPayload(byte payload)
{
appendPayload(new byte[] { payload });
}
/**
* Appends a Byte-Array to the payload
* @param payloadarr Payload
*/
protected void appendPayload(byte[] payloadarr)
{
int oldpayloadsize = this.payload.length;
byte[] oldpayload = this.payload;
this.payload = new byte[oldpayloadsize+payloadarr.length];
System.arraycopy(oldpayload, 0, this.payload, 0, oldpayloadsize);
System.arraycopy(payloadarr, 0, this.payload, oldpayloadsize, payloadarr.length);
}
/**
* Appends an integer to the payload
* @param i Payload
*/
protected void appendPayload(int i) {
appendPayload(intToByteArray(i));
}
/**
* Appends a short to the payload
* @param s Payload
*/
protected void appendPayload(short s) {
appendPayload(shortToByteArray(s));
}
/**
* Get Number of Packets which will be sent with current Payload...
* @return Number of Packets
*/
public int getNumPackets()
{
return (int)((payload.length + (MAX_PAYLOAD_SIZE - 1)) / MAX_PAYLOAD_SIZE);
}
/**
* Get Header for a specific Packet in this sequence...
* @param seq Current sequence number
* @param maxseq Maximal sequence number
* @param actpayloadsize Payloadsize of this packet
* @return Byte-Array with Header information (currently 32-Byte long, see HEADER_SIZE)
*/
private byte[] getHeader(int seq, int maxseq, short actpayloadsize)
{
byte[] header = new byte[HEADER_SIZE];
System.arraycopy(sig, 0, header, 0, 4);
header[4] = majver;
header[5] = minver;
byte[] packettypearr = shortToByteArray(this.packettype);
System.arraycopy(packettypearr, 0, header, 6, 2);
byte[] seqarr = intToByteArray(seq);
System.arraycopy(seqarr, 0, header, 8, 4);
byte[] maxseqarr = intToByteArray(maxseq);
System.arraycopy(maxseqarr, 0, header, 12, 4);
byte[] payloadsize = shortToByteArray(actpayloadsize);
System.arraycopy(payloadsize, 0, header, 16, 2);
byte[] uid = intToByteArray(Packet.uid);
System.arraycopy(uid, 0, header, 18, 4);
byte[] reserved = new byte[10];
System.arraycopy(reserved, 0, header, 22, 10);
return header;
}
/**
* Generates the whole UDP-Message with Header and Payload of a specific Packet in sequence
* @param seq Current sequence number
* @return Byte-Array with UDP-Message
*/
private byte[] getUDPMessage(int seq)
{
int maxseq = (int)((payload.length + (MAX_PAYLOAD_SIZE - 1)) / MAX_PAYLOAD_SIZE);
if(seq > maxseq)
return null;
short actpayloadsize;
if(seq == maxseq)
actpayloadsize = (short)(payload.length%MAX_PAYLOAD_SIZE);
else
actpayloadsize = (short)MAX_PAYLOAD_SIZE;
byte[] pack = new byte[HEADER_SIZE+actpayloadsize];
System.arraycopy(getHeader(seq, maxseq, actpayloadsize), 0, pack, 0, HEADER_SIZE);
System.arraycopy(payload, (seq-1)*MAX_PAYLOAD_SIZE, pack, HEADER_SIZE, actpayloadsize);
return pack;
}
/**
* Sends this packet to the EventServer
* @param adr Address of the EventServer
* @param port Port of the EventServer
* @throws IOException
*/
public void send(InetAddress adr, int port) throws IOException
{
int maxseq = getNumPackets();
DatagramSocket s = new DatagramSocket();
// For each Packet in Sequence...
for(int seq=1;seq<=maxseq;seq++)
{
// Get Message and send them...
byte[] pack = getUDPMessage(seq);
DatagramPacket p = new DatagramPacket(pack, pack.length);
p.setAddress(adr);
p.setPort(port);
s.send(p);
}
}
/**
* Helper Method to convert an integer to a Byte array
* @param value
* @return Byte-Array
*/
private static final byte[] intToByteArray(int value) {
return new byte[] {
(byte)(value >>> 24),
(byte)(value >>> 16),
(byte)(value >>> 8),
(byte)value};
}
/**
* Helper Method to convert an short to a Byte array
* @param value
* @return Byte-Array
*/
private static final byte[] shortToByteArray(short value) {
return new byte[] {
(byte)(value >>> 8),
(byte)value};
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
/**
* XBMC Event Client Class
*
* An ACTION packet tells XBMC to do the action specified, based on the type it knows were it needs to be sent.
* The idea is that this will be as in scripting/skining and keymapping, just triggered from afar.
* @author Stefan Agner
*
*/
public class PacketACTION extends Packet {
public final static byte ACTION_EXECBUILTIN = 0x01;
public final static byte ACTION_BUTTON = 0x02;
/**
* An ACTION packet tells XBMC to do the action specified, based on the type it knows were it needs to be sent.
* @param actionmessage Actionmessage (as in scripting/skinning)
*/
public PacketACTION(String actionmessage)
{
super(PT_ACTION);
byte actiontype = ACTION_EXECBUILTIN;
appendPayload(actionmessage, actiontype);
}
/**
* An ACTION packet tells XBMC to do the action specified, based on the type it knows were it needs to be sent.
* @param actionmessage Actionmessage (as in scripting/skinning)
* @param actiontype Actiontype (ACTION_EXECBUILTIN or ACTION_BUTTON)
*/
public PacketACTION(String actionmessage, byte actiontype)
{
super(PT_ACTION);
appendPayload(actionmessage, actiontype);
}
private void appendPayload(String actionmessage, byte actiontype)
{
appendPayload(actiontype);
appendPayload(actionmessage);
}
}

View File

@ -0,0 +1,157 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
/**
* XBMC Event Client Class
*
* A button packet send a key press or release event to XBMC
* @author Stefan Agner
*
*/
public class PacketBUTTON extends Packet {
protected final static byte BT_USE_NAME = 0x01;
protected final static byte BT_DOWN = 0x02;
protected final static byte BT_UP = 0x04;
protected final static byte BT_USE_AMOUNT = 0x08;
protected final static byte BT_QUEUE = 0x10;
protected final static byte BT_NO_REPEAT = 0x20;
protected final static byte BT_VKEY = 0x40;
protected final static byte BT_AXIS = (byte)0x80;
protected final static byte BT_AXISSINGLE = (byte)0x100;
/**
* A button packet send a key press or release event to XBMC
* @param code raw button code (default: 0)
* @param repeat this key press should repeat until released (default: 1)
* Note that queued pressed cannot repeat.
* @param down if this is 1, it implies a press event, 0 implies a release
* event. (default: 1)
* @param queue a queued key press means that the button event is
* executed just once after which the next key press is processed.
* It can be used for macros. Currently there is no support for
* time delays between queued presses. (default: 0)
* @param amount unimplemented for now; in the future it will be used for
* specifying magnitude of analog key press events
* @param axis
*/
public PacketBUTTON(short code, boolean repeat, boolean down, boolean queue, short amount, byte axis)
{
super(PT_BUTTON);
String map_name = "";
String button_name = "";
short flags = 0;
appendPayload(code, map_name, button_name, repeat, down, queue, amount, axis, flags);
}
/**
* A button packet send a key press or release event to XBMC
* @param map_name a combination of map_name and button_name refers to a
* mapping in the user's Keymap.xml or Lircmap.xml.
* map_name can be one of the following:
* <ul>
* <li>"KB" => standard keyboard map ( <keyboard> section )</li>
* <li>"XG" => xbox gamepad map ( <gamepad> section )</li>
* <li>"R1" => xbox remote map ( <remote> section )</li>
* <li>"R2" => xbox universal remote map ( <universalremote> section )</li>
* <li>"LI:devicename" => LIRC remote map where 'devicename' is the
* actual device's name</li></ul>
* @param button_name a button name defined in the map specified in map_name.
* For example, if map_name is "KB" refering to the <keyboard> section in Keymap.xml
* then, valid button_names include "printscreen", "minus", "x", etc.
* @param repeat this key press should repeat until released (default: 1)
* Note that queued pressed cannot repeat.
* @param down if this is 1, it implies a press event, 0 implies a release
* event. (default: 1)
* @param queue a queued key press means that the button event is
* executed just once after which the next key press is processed.
* It can be used for macros. Currently there is no support for
* time delays between queued presses. (default: 0)
* @param amount unimplemented for now; in the future it will be used for
* specifying magnitude of analog key press events
* @param axis
*/
public PacketBUTTON(String map_name, String button_name, boolean repeat, boolean down, boolean queue, short amount, byte axis)
{
super(PT_BUTTON);
short code = 0;
short flags = BT_USE_NAME;
appendPayload(code, map_name, button_name, repeat, down, queue, amount, axis, flags);
}
/**
* Appends Payload for a Button Packet (this method is used by the different Constructors of this Packet)
* @param code raw button code (default: 0)
* @param map_name a combination of map_name and button_name refers to a
* mapping in the user's Keymap.xml or Lircmap.xml.
* map_name can be one of the following:
* <ul>
* <li>"KB" => standard keyboard map ( <keyboard> section )</li>
* <li>"XG" => xbox gamepad map ( <gamepad> section )</li>
* <li>"R1" => xbox remote map ( <remote> section )</li>
* <li>"R2" => xbox universal remote map ( <universalremote> section )</li>
* <li>"LI:devicename" => LIRC remote map where 'devicename' is the
* actual device's name</li></ul>
* @param button_name a button name defined in the map specified in map_name.
* For example, if map_name is "KB" refering to the <keyboard> section in Keymap.xml
* then, valid button_names include "printscreen", "minus", "x", etc.
* @param repeat this key press should repeat until released (default: 1)
* Note that queued pressed cannot repeat.
* @param down if this is 1, it implies a press event, 0 implies a release
* event. (default: 1)
* @param queue a queued key press means that the button event is
* executed just once after which the next key press is processed.
* It can be used for macros. Currently there is no support for
* time delays between queued presses. (default: 0)
* @param amount unimplemented for now; in the future it will be used for
* specifying magnitude of analog key press events
* @param axis
* @param flags Packet specific flags
*/
private void appendPayload(short code, String map_name, String button_name, boolean repeat, boolean down, boolean queue, short amount, byte axis, short flags)
{
if(amount>0)
flags |= BT_USE_AMOUNT;
else
amount = 0;
if(down)
flags |= BT_DOWN;
else
flags |= BT_UP;
if(!repeat)
flags |= BT_NO_REPEAT;
if(queue)
flags |= BT_QUEUE;
if(axis == 1)
flags |= BT_AXISSINGLE;
else if (axis == 2)
flags |= BT_AXIS;
appendPayload(code);
appendPayload(flags);
appendPayload(amount);
appendPayload(map_name);
appendPayload(button_name);
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
/**
* XBMC Event Client Class
*
* A BYE packet terminates the connection to XBMC.
* @author Stefan Agner
*
*/
public class PacketBYE extends Packet
{
/**
* A BYE packet terminates the connection to XBMC.
*/
public PacketBYE()
{
super(PT_BYE);
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
import java.nio.charset.Charset;
/**
* XBMC Event Client Class
*
* A HELO packet establishes a valid connection to XBMC. It is the
* first packet that should be sent.
* @author Stefan Agner
*
*/
public class PacketHELO extends Packet {
/**
* A HELO packet establishes a valid connection to XBMC.
* @param devicename Name of the device which connects to XBMC
*/
public PacketHELO(String devicename)
{
super(PT_HELO);
this.appendPayload(devicename);
this.appendPayload(ICON_NONE);
this.appendPayload((short)0); // port no
this.appendPayload(0); // reserved1
this.appendPayload(0); // reserved2
}
/**
* A HELO packet establishes a valid connection to XBMC.
* @param devicename Name of the device which connects to XBMC
* @param iconType Type of the icon (Packet.ICON_PNG, Packet.ICON_JPEG or Packet.ICON_GIF)
* @param iconData The icon as a Byte-Array
*/
public PacketHELO(String devicename, byte iconType, byte[] iconData)
{
super(PT_HELO);
this.appendPayload(devicename);
this.appendPayload(iconType);
this.appendPayload((short)0); // port no
this.appendPayload(0); // reserved1
this.appendPayload(0); // reserved2
this.appendPayload(iconData); // reserved2
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
/**
* XBMC Event Client Class
*
* A LOG packet tells XBMC to log the message to xbmc.log with the loglevel as specified.
* @author Stefan Agner
*
*/
public class PacketLOG extends Packet {
/**
* A LOG packet tells XBMC to log the message to xbmc.log with the loglevel as specified.
* @param loglevel the loglevel, follows XBMC standard.
* <ul>
* <li>0 = DEBUG</li>
* <li>1 = INFO</li>
* <li>2 = NOTICE</li>
* <li>3 = WARNING</li>
* <li>4 = ERROR</li>
* <li>5 = SEVERE</li>
* </ul>
* @param logmessage the message to log
*/
public PacketLOG(byte loglevel, String logmessage)
{
super(PT_LOG);
appendPayload(loglevel);
appendPayload(logmessage);
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
/**
* XBMC Event Client Class
*
* A MOUSE packets sets the mouse position in XBMC
* @author Stefan Agner
*
*/
public class PacketMOUSE extends Packet {
protected final static byte MS_ABSOLUTE = 0x01;
/**
* A MOUSE packets sets the mouse position in XBMC
* @param x horitontal position ranging from 0 to 65535
* @param y vertical position ranging from 0 to 65535
*/
public PacketMOUSE(int x, int y)
{
super(PT_MOUSE);
byte flags = 0;
flags |= MS_ABSOLUTE;
appendPayload(flags);
appendPayload((short)x);
appendPayload((short)y);
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
/**
* XBMC Event Client Class
*
* This packet displays a notification window in XBMC. It can contain
* a caption, a message and an icon.
* @author Stefan Agner
*
*/
public class PacketNOTIFICATION extends Packet {
/**
* This packet displays a notification window in XBMC.
* @param title Message title
* @param message The actual message
* @param iconType Type of the icon (Packet.ICON_PNG, Packet.ICON_JPEG or Packet.ICON_GIF)
* @param iconData The icon as a Byte-Array
*/
public PacketNOTIFICATION(String title, String message, byte iconType, byte[] iconData)
{
super(PT_NOTIFICATION);
appendPayload(title, message, iconType, iconData);
}
/**
* This packet displays a notification window in XBMC.
* @param title Message title
* @param message The actual message
*/
public PacketNOTIFICATION(String title, String message)
{
super(PT_NOTIFICATION);
appendPayload(title, message, Packet.ICON_NONE, null);
}
/**
* Appends the payload to the packet...
* @param title Message title
* @param message The actual message
* @param iconType Type of the icon (Packet.ICON_PNG, Packet.ICON_JPEG or Packet.ICON_GIF)
* @param iconData The icon as a Byte-Array
*/
private void appendPayload(String title, String message, byte iconType, byte[] iconData)
{
appendPayload(title);
appendPayload(message);
appendPayload(iconType);
appendPayload(0); // reserved
if(iconData!=null)
appendPayload(iconData);
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2008-2013 Team XBMC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.xbmc.kore.eventclient;
/**
* XBMC Event Client Class
*
* A PING packet tells XBMC that the client is still alive. All valid
* packets act as ping (not just this one). A client needs to ping
* XBMC at least once in 60 seconds or it will time
* @author Stefan Agner
*
*/
public class PacketPING extends Packet {
/**
* A PING packet tells XBMC that the client is still alive.
*/
public PacketPING()
{
super(PT_PING);
}
}