This file is indexed.

/usr/include/xbmc/xbmcclient.h is in xbmc-eventclients-dev 2:12.3+dfsg1-3ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#ifndef __XBMC_CLIENT_H__
#define __XBMC_CLIENT_H__

/*
 *  Copyright (C) 2008-2009 Team XBMC http://www.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 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef _WIN32
#include <winsock.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>

#define STD_PORT       9777

#define MS_ABSOLUTE    0x01
//#define MS_RELATIVE    0x02

#define BTN_USE_NAME   0x01
#define BTN_DOWN       0x02
#define BTN_UP         0x04
#define BTN_USE_AMOUNT 0x08
#define BTN_QUEUE      0x10
#define BTN_NO_REPEAT  0x20
#define BTN_VKEY       0x40
#define BTN_AXIS       0x80

#define PT_HELO         0x01
#define PT_BYE          0x02
#define PT_BUTTON       0x03
#define PT_MOUSE        0x04
#define PT_PING         0x05
#define PT_BROADCAST    0x06
#define PT_NOTIFICATION 0x07
#define PT_BLOB         0x08
#define PT_LOG          0x09
#define PT_ACTION       0x0A
#define PT_DEBUG        0xFF

#define ICON_NONE       0x00
#define ICON_JPEG       0x01
#define ICON_PNG        0x02
#define ICON_GIF        0x03

#define MAX_PACKET_SIZE  1024
#define HEADER_SIZE      32
#define MAX_PAYLOAD_SIZE (MAX_PACKET_SIZE - HEADER_SIZE)

#define MAJOR_VERSION 2
#define MINOR_VERSION 0

#define LOGDEBUG   0
#define LOGINFO    1
#define LOGNOTICE  2
#define LOGWARNING 3
#define LOGERROR   4
#define LOGSEVERE  5
#define LOGFATAL   6
#define LOGNONE    7

#define ACTION_EXECBUILTIN 0x01
#define ACTION_BUTTON      0x02

class CAddress
{
private:
  struct sockaddr_in m_Addr;
public:
  CAddress(int Port = STD_PORT)
  {
    m_Addr.sin_family = AF_INET;
    m_Addr.sin_port = htons(Port);
    m_Addr.sin_addr.s_addr = INADDR_ANY;
    memset(m_Addr.sin_zero, '\0', sizeof m_Addr.sin_zero);
  }

  CAddress(const char *Address, int Port = STD_PORT)
  {
    m_Addr.sin_port = htons(Port);
    
    struct hostent *h;
    if (Address == NULL || (h=gethostbyname(Address)) == NULL)
    {
        if (Address != NULL)
			printf("Error: Get host by name\n");

        m_Addr.sin_addr.s_addr  = INADDR_ANY;
        m_Addr.sin_family       = AF_INET;
    }
    else
    {
      m_Addr.sin_family = h->h_addrtype;
      m_Addr.sin_addr = *((struct in_addr *)h->h_addr);
    }
    memset(m_Addr.sin_zero, '\0', sizeof m_Addr.sin_zero);
  }

  void SetPort(int port)
  {
    m_Addr.sin_port = htons(port);
  }

  const sockaddr *GetAddress()
  {
    return ((struct sockaddr *)&m_Addr);
  }

  bool Bind(int Sockfd)
  {
    return (bind(Sockfd, (struct sockaddr *)&m_Addr, sizeof m_Addr) == 0);
  }
};

class XBMCClientUtils
{
public:
  XBMCClientUtils() {}
  ~XBMCClientUtils() {}
  static unsigned int GetUniqueIdentifier()
  {
    static time_t id = time(NULL);
    return id;
  }

  static void Clean()
  {
  #ifdef _WIN32
    WSACleanup();
  #endif
  }

  static bool Initialize()
  {
  #ifdef _WIN32
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData))
      return false;
  #endif
    return true;
  }
};

class CPacket
{
/*   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

         -----------------------------
         | -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 Payload size          | - 1  x UNSIGNED SHORT      2B
         | -H7 Client's unique token | - 1  x UNSIGNED LONG       4B
         | -H8 Reserved              | - 10 x UNSIGNED CHAR      10B
         |---------------------------|
         | -P1 payload               | -
         -----------------------------
*/
public:
  CPacket()
  {
    m_PacketType = 0;
  }
  virtual ~CPacket()
  { }

  bool Send(int Socket, CAddress &Addr, unsigned int UID = XBMCClientUtils::GetUniqueIdentifier())
  {
    if (m_Payload.size() == 0)
      ConstructPayload();
    bool SendSuccessfull = true;
    int NbrOfPackages = (m_Payload.size() / MAX_PAYLOAD_SIZE) + 1;
    int Send = 0;
    int Sent = 0;
    int Left = m_Payload.size();
    for (int Package = 1; Package <= NbrOfPackages; Package++)
    {
      if (Left > MAX_PAYLOAD_SIZE)
      {
        Send = MAX_PAYLOAD_SIZE;
        Left -= Send;
      }
      else
      {
        Send = Left;
        Left = 0;
      }

      ConstructHeader(m_PacketType, NbrOfPackages, Package, Send, UID, m_Header);
      char t[MAX_PACKET_SIZE];
      int i, j;
      for (i = 0; i < 32; i++)
        t[i] = m_Header[i];

      for (j = 0; j < Send; j++)
        t[(32 + j)] = m_Payload[j + Sent];

      int rtn = sendto(Socket, t, (32 + Send), 0, Addr.GetAddress(), sizeof(struct sockaddr));

      if (rtn != (32 + Send))
        SendSuccessfull = false;

      Sent += Send;
    }
    return SendSuccessfull;
  }
protected:
  char            m_Header[HEADER_SIZE];
  unsigned short  m_PacketType;

  std::vector<char> m_Payload;

  static void ConstructHeader(int PacketType, int NumberOfPackets, int CurrentPacket, unsigned short PayloadSize, unsigned int UniqueToken, char *Header)
  {
    sprintf(Header, "XBMC");
    for (int i = 4; i < HEADER_SIZE; i++)
      Header[i] = 0;
    Header[4]  = MAJOR_VERSION;
    Header[5]  = MINOR_VERSION;
    if (CurrentPacket == 1)
    {
      Header[6]  = ((PacketType & 0xff00) >> 8);
      Header[7]  =  (PacketType & 0x00ff);
    }
    else
    {
      Header[6]  = ((PT_BLOB & 0xff00) >> 8);
      Header[7]  =  (PT_BLOB & 0x00ff);
    }
    Header[8]  = ((CurrentPacket & 0xff000000) >> 24);
    Header[9]  = ((CurrentPacket & 0x00ff0000) >> 16);
    Header[10] = ((CurrentPacket & 0x0000ff00) >> 8);
    Header[11] =  (CurrentPacket & 0x000000ff);

    Header[12] = ((NumberOfPackets & 0xff000000) >> 24);
    Header[13] = ((NumberOfPackets & 0x00ff0000) >> 16);
    Header[14] = ((NumberOfPackets & 0x0000ff00) >> 8);
    Header[15] =  (NumberOfPackets & 0x000000ff);

    Header[16] = ((PayloadSize & 0xff00) >> 8);
    Header[17] =  (PayloadSize & 0x00ff);

    Header[18] = ((UniqueToken & 0xff000000) >> 24);
    Header[19] = ((UniqueToken & 0x00ff0000) >> 16);
    Header[20] = ((UniqueToken & 0x0000ff00) >> 8);
    Header[21] =  (UniqueToken & 0x000000ff);
  }

  virtual void ConstructPayload()
  { }
};

class CPacketHELO : public CPacket
{
    /************************************************************************/
    /* Payload format                                                       */
    /* %s -  device name (max 128 chars)                                    */
    /* %c -  icontype ( 0=>NOICON, 1=>JPEG , 2=>PNG , 3=>GIF )              */
    /* %s -  my port ( 0=>not listening )                                   */
    /* %d -  reserved1 ( 0 )                                                */
    /* %d -  reserved2 ( 0 )                                                */
    /* XX -  imagedata ( can span multiple packets )                        */
    /************************************************************************/
private:
  std::vector<char> m_DeviceName;
  unsigned short m_IconType;
  char *m_IconData;
  unsigned short m_IconSize;
public:
  virtual void ConstructPayload()
  {
    m_Payload.clear();

    for (unsigned int i = 0; i < m_DeviceName.size(); i++)
      m_Payload.push_back(m_DeviceName[i]);

    m_Payload.push_back('\0');

    m_Payload.push_back(m_IconType);

    m_Payload.push_back(0);
    m_Payload.push_back('\0');

    for (int j = 0; j < 8; j++)
      m_Payload.push_back(0);

    for (int ico = 0; ico < m_IconSize; ico++)
      m_Payload.push_back(m_IconData[ico]);
  }

  CPacketHELO(const char *DevName, unsigned short IconType, const char *IconFile = NULL) : CPacket()
  {
    m_PacketType = PT_HELO;

    unsigned int len = strlen(DevName);
    for (unsigned int i = 0; i < len; i++)
      m_DeviceName.push_back(DevName[i]);    

    m_IconType = IconType;

    if (IconType == ICON_NONE || IconFile == NULL)
    {
      m_IconData = NULL;
      m_IconSize = 0;
      return;
    }

    std::ifstream::pos_type size;

    std::ifstream file (IconFile, std::ios::in|std::ios::binary|std::ios::ate);
    if (file.is_open())
    {
      size = file.tellg();
      m_IconData = new char [size];
      file.seekg (0, std::ios::beg);
      file.read (m_IconData, size);
      file.close();
      m_IconSize = size;
    }
    else
    {
      m_IconType = ICON_NONE;
      m_IconSize = 0;
    }
  }

  virtual ~CPacketHELO()
  {
    m_DeviceName.clear();
    delete[] m_IconData;
  }
};

class CPacketNOTIFICATION : public CPacket
{
    /************************************************************************/
    /* Payload format:                                                      */
    /* %s - caption                                                         */
    /* %s - message                                                         */
    /* %c - icontype ( 0=>NOICON, 1=>JPEG , 2=>PNG , 3=>GIF )               */
    /* %d - reserved ( 0 )                                                  */
    /* XX - imagedata ( can span multiple packets )                         */
    /************************************************************************/
private:
  std::vector<char> m_Title;
  std::vector<char> m_Message;
  unsigned short m_IconType;
  char *m_IconData;
  unsigned short m_IconSize;
public:
  virtual void ConstructPayload()
  {
    m_Payload.clear();

    for (unsigned int i = 0; i < m_Title.size(); i++)
      m_Payload.push_back(m_Title[i]);

    m_Payload.push_back('\0');

    for (unsigned int i = 0; i < m_Message.size(); i++)
      m_Payload.push_back(m_Message[i]);

    m_Payload.push_back('\0');

    m_Payload.push_back(m_IconType);

    for (int i = 0; i < 4; i++)
      m_Payload.push_back(0);

    for (int ico = 0; ico < m_IconSize; ico++)
      m_Payload.push_back(m_IconData[ico]);
  }

  CPacketNOTIFICATION(const char *Title, const char *Message, unsigned short IconType, const char *IconFile = NULL) : CPacket()
  {
    m_PacketType = PT_NOTIFICATION;
    m_IconData = NULL;
    m_IconSize = 0;
    unsigned int len = 0;
    if (Title != NULL)
    {
      len = strlen(Title);
      for (unsigned int i = 0; i < len; i++)
        m_Title.push_back(Title[i]);
    }

    if (Message != NULL)
    {
      len = strlen(Message);
      for (unsigned int i = 0; i < len; i++)
        m_Message.push_back(Message[i]);
    }
    m_IconType = IconType;

    if (IconType == ICON_NONE || IconFile == NULL)
      return;

    std::ifstream::pos_type size;

    std::ifstream file (IconFile, std::ios::in|std::ios::binary|std::ios::ate);
    if (file.is_open())
    {
      size = file.tellg();
      m_IconData = new char [size];
      file.seekg (0, std::ios::beg);
      file.read (m_IconData, size);
      file.close();
      m_IconSize = size;
    }
    else
      m_IconType = ICON_NONE;
  }

  virtual ~CPacketNOTIFICATION()
  {
    m_Title.clear();
    m_Message.clear();
    delete[] m_IconData;
  }
};

class CPacketBUTTON : public CPacket
{
    /************************************************************************/
    /* Payload format                                                       */
    /* %i - button code                                                     */
    /* %i - flags 0x01 => use button map/name instead of code               */
    /*            0x02 => btn down                                          */
    /*            0x04 => btn up                                            */
    /*            0x08 => use amount                                        */
    /*            0x10 => queue event                                       */
    /*            0x20 => do not repeat                                     */
    /*            0x40 => virtual key                                       */
    /*            0x40 => axis key                                          */
    /* %i - amount ( 0 => 65k maps to -1 => 1 )                             */
    /* %s - device map (case sensitive and required if flags & 0x01)        */
    /*      "KB" - Standard keyboard map                                    */
    /*      "XG" - Xbox Gamepad                                             */
    /*      "R1" - Xbox Remote                                              */
    /*      "R2" - Xbox Universal Remote                                    */
    /*      "LI:devicename" -  valid LIRC device map where 'devicename'     */
    /*                         is the actual name of the LIRC device        */
    /*      "JS<num>:joyname" -  valid Joystick device map where            */
    /*                           'joyname'  is the name specified in        */
    /*                           the keymap. JS only supports button code   */
    /*                           and not button name currently (!0x01).     */
    /* %s - button name (required if flags & 0x01)                          */
    /************************************************************************/
private:
  std::vector<char> m_DeviceMap;
  std::vector<char> m_Button;
  unsigned short m_ButtonCode;
  unsigned short m_Amount;
  unsigned short m_Flags;
public:
  virtual void ConstructPayload()
  {
    m_Payload.clear();

    if (m_Button.size() != 0)
    {
      if (!(m_Flags & BTN_USE_NAME)) // If the BTN_USE_NAME isn't flagged for some reason
        m_Flags |= BTN_USE_NAME;
      m_ButtonCode = 0;
    }
    else
      m_Button.clear();

    if (m_Amount > 0)
    {
      if (!(m_Flags & BTN_USE_AMOUNT))
        m_Flags |= BTN_USE_AMOUNT;
    }
    if (!((m_Flags & BTN_DOWN) || (m_Flags & BTN_UP))) //If none of them are tagged.
      m_Flags |= BTN_DOWN;

    m_Payload.push_back(((m_ButtonCode & 0xff00) >> 8));
    m_Payload.push_back( (m_ButtonCode & 0x00ff));

    m_Payload.push_back(((m_Flags & 0xff00) >> 8) );
    m_Payload.push_back( (m_Flags & 0x00ff));

    m_Payload.push_back(((m_Amount & 0xff00) >> 8) );
    m_Payload.push_back( (m_Amount & 0x00ff));


    for (unsigned int i = 0; i < m_DeviceMap.size(); i++)
      m_Payload.push_back(m_DeviceMap[i]);

    m_Payload.push_back('\0');

    for (unsigned int i = 0; i < m_Button.size(); i++)
      m_Payload.push_back(m_Button[i]);

    m_Payload.push_back('\0');
  }

  CPacketBUTTON(const char *Button, const char *DeviceMap, unsigned short Flags, unsigned short Amount = 0) : CPacket()
  {
    m_PacketType = PT_BUTTON;
    m_Flags      = Flags;
    m_ButtonCode = 0;
    m_Amount     = Amount;

    unsigned int len = strlen(DeviceMap);
    for (unsigned int i = 0; i < len; i++)
      m_DeviceMap.push_back(DeviceMap[i]);

    len = strlen(Button);
    for (unsigned int i = 0; i < len; i++)
      m_Button.push_back(Button[i]);
  }

  CPacketBUTTON(unsigned short ButtonCode, const char *DeviceMap, unsigned short Flags, unsigned short Amount = 0) : CPacket()
  {
    m_PacketType = PT_BUTTON;
    m_Flags      = Flags;
    m_ButtonCode = ButtonCode;
    m_Amount     = Amount;

    unsigned int len = strlen(DeviceMap);
    for (unsigned int i = 0; i < len; i++)
      m_DeviceMap.push_back(DeviceMap[i]);
  }

  CPacketBUTTON(unsigned short ButtonCode, unsigned short Flags, unsigned short Amount = 0) : CPacket()
  {
    m_PacketType = PT_BUTTON;
    m_Flags      = Flags;
    m_ButtonCode = ButtonCode;
    m_Amount     = Amount;
  }

  // Used to send a release event
  CPacketBUTTON() : CPacket()
  {
    m_PacketType = PT_BUTTON;
    m_Flags = BTN_UP;
    m_Amount = 0;
    m_ButtonCode = 0;
  }

  virtual ~CPacketBUTTON()
  {
    m_DeviceMap.clear();
    m_Button.clear();
  }

  inline unsigned short GetFlags() { return m_Flags; }
  inline unsigned short GetButtonCode() { return m_ButtonCode; }
};

class CPacketPING : public CPacket
{
    /************************************************************************/
    /* no payload                                                           */
    /************************************************************************/
public:
  CPacketPING() : CPacket()
  {
    m_PacketType = PT_PING;
  }
  virtual ~CPacketPING()
  { }
};

class CPacketBYE : public CPacket
{
    /************************************************************************/
    /* no payload                                                           */
    /************************************************************************/
public:
  CPacketBYE() : CPacket()
  {
    m_PacketType = PT_BYE;
  }
  virtual ~CPacketBYE()
  { }
};

class CPacketMOUSE : public CPacket
{
    /************************************************************************/
    /* Payload format                                                       */
    /* %c - flags                                                           */
    /*    - 0x01 absolute position                                          */
    /* %i - mousex (0-65535 => maps to screen width)                        */
    /* %i - mousey (0-65535 => maps to screen height)                       */
    /************************************************************************/
private:
  unsigned short m_X;
  unsigned short m_Y;
  unsigned char  m_Flag;
public:
  CPacketMOUSE(int X, int Y, unsigned char Flag = MS_ABSOLUTE)
  {
    m_PacketType = PT_MOUSE;
    m_Flag = Flag;
    m_X = X;
    m_Y = Y;
  }

  virtual void ConstructPayload()
  {
    m_Payload.clear();

    m_Payload.push_back(m_Flag);

    m_Payload.push_back(((m_X & 0xff00) >> 8));
    m_Payload.push_back( (m_X & 0x00ff));

    m_Payload.push_back(((m_Y & 0xff00) >> 8));
    m_Payload.push_back( (m_Y & 0x00ff));
  }
  
  virtual ~CPacketMOUSE()
  { }
};

class CPacketLOG : public CPacket
{
    /************************************************************************/
    /* Payload format                                                       */
    /* %c - log type                                                        */
    /* %s - message                                                         */
    /************************************************************************/
private:
  std::vector<char> m_Message;
  unsigned char  m_LogLevel;
  bool m_AutoPrintf;
public:
  CPacketLOG(int LogLevel, const char *Message, bool AutoPrintf = true)
  {
    m_PacketType = PT_LOG;

    unsigned int len = strlen(Message);
    for (unsigned int i = 0; i < len; i++)
      m_Message.push_back(Message[i]);

    m_LogLevel = LogLevel;
    m_AutoPrintf = AutoPrintf;
  }

  virtual void ConstructPayload()
  {
    m_Payload.clear();

    m_Payload.push_back( (m_LogLevel & 0x00ff) );

    if (m_AutoPrintf)
    {
      char* str=&m_Message[0];
      printf("%s\n", str);
    }
    for (unsigned int i = 0; i < m_Message.size(); i++)
      m_Payload.push_back(m_Message[i]);

    m_Payload.push_back('\0');
  }
  
  virtual ~CPacketLOG()
  { }
};

class CPacketACTION : public CPacket
{
    /************************************************************************/
    /* Payload format                                                       */
    /* %c - action type                                                     */
    /* %s - action message                                                  */
    /************************************************************************/
private:
  unsigned char     m_ActionType;
  std::vector<char> m_Action;
public:
  CPacketACTION(const char *Action, unsigned char ActionType = ACTION_EXECBUILTIN)
  {
    m_PacketType = PT_ACTION;

    m_ActionType = ActionType;
    unsigned int len = strlen(Action);
    for (unsigned int i = 0; i < len; i++)
      m_Action.push_back(Action[i]);
  }

  virtual void ConstructPayload()
  {
    m_Payload.clear();

    m_Payload.push_back(m_ActionType);
    for (unsigned int i = 0; i < m_Action.size(); i++)
      m_Payload.push_back(m_Action[i]);

    m_Payload.push_back('\0');
  }
  
  virtual ~CPacketACTION()
  { }
};

class CXBMCClient
{
private:
  CAddress      m_Addr;
  int           m_Socket;
  unsigned int  m_UID;
public:
  CXBMCClient(const char *IP = "127.0.0.1", int Port = 9777, int Socket = -1, unsigned int UID = 0)
  {
    m_Addr = CAddress(IP, Port);
    if (Socket == -1)
      m_Socket = socket(AF_INET, SOCK_DGRAM, 0);
    else
      m_Socket = Socket;

    if (UID)
      m_UID = UID;
    else
      m_UID = XBMCClientUtils::GetUniqueIdentifier();
  }

  void SendNOTIFICATION(const char *Title, const char *Message, unsigned short IconType, const char *IconFile = NULL)
  {
    if (m_Socket < 0)
      return;

    CPacketNOTIFICATION notification(Title, Message, IconType, IconFile);
    notification.Send(m_Socket, m_Addr, m_UID);
  }

  void SendHELO(const char *DevName, unsigned short IconType, const char *IconFile = NULL)
  {
    if (m_Socket < 0)
      return;

    CPacketHELO helo(DevName, IconType, IconFile);
    helo.Send(m_Socket, m_Addr, m_UID);
  }

  void SendButton(const char *Button, const char *DeviceMap, unsigned short Flags, unsigned short Amount = 0)
  {
    if (m_Socket < 0)
      return;

    CPacketBUTTON button(Button, DeviceMap, Flags, Amount);
    button.Send(m_Socket, m_Addr, m_UID);
  }

  void SendButton(unsigned short ButtonCode, const char *DeviceMap, unsigned short Flags, unsigned short Amount = 0)
  {
    if (m_Socket < 0)
      return;

    CPacketBUTTON button(ButtonCode, DeviceMap, Flags, Amount);
    button.Send(m_Socket, m_Addr, m_UID);
  }

  void SendButton(unsigned short ButtonCode, unsigned Flags, unsigned short Amount = 0)
  {
    if (m_Socket < 0)
      return;

    CPacketBUTTON button(ButtonCode, Flags, Amount);
    button.Send(m_Socket, m_Addr, m_UID);
  }

  void SendMOUSE(int X, int Y, unsigned char Flag = MS_ABSOLUTE)
  {
    if (m_Socket < 0)
      return;

    CPacketMOUSE mouse(X, Y, Flag);
    mouse.Send(m_Socket, m_Addr, m_UID);
  }

  void SendLOG(int LogLevel, const char *Message, bool AutoPrintf = true)
  {
    if (m_Socket < 0)
      return;

    CPacketLOG log(LogLevel, Message, AutoPrintf);
    log.Send(m_Socket, m_Addr, m_UID);
  }

  void SendACTION(const char *ActionMessage, int ActionType = ACTION_EXECBUILTIN)
  {
    if (m_Socket < 0)
      return;

    CPacketACTION action(ActionMessage, ActionType);
    action.Send(m_Socket, m_Addr, m_UID);
  }
};

#endif