1 //
2 //   Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
3 //   Free Software Foundation, Inc.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 
20 #include <string>
21 #include <iostream>
22 
23 #include "GnashKey.h" // key::code
24 #include "network.h"
25 #include "log.h"
26 #include "lirc.h"
27 
28 using std::string;
29 
30 namespace gnash {
31 
32 // this number camne from the lirc irw program. If this size works for
33 // them, it should work for us.
34 const int LIRC_PACKET_SIZE = 128;
35 const int TIMEOUT = 10;
36 const int BUTTONSIZE = 10;
37 
Lirc()38 Lirc::Lirc()
39     : _sockname("/tmp/lircd"), _button(nullptr)
40 {
41 //    GNASH_REPORT_FUNCTION;
42     _button = new char[BUTTONSIZE];
43 }
44 
~Lirc()45 Lirc::~Lirc()
46 {
47 //    GNASH_REPORT_FUNCTION;
48     if (_button != nullptr) {
49 	delete _button;
50     }
51     closeNet();
52 }
53 
54 bool
init()55 Lirc::init()
56 {
57 //    GNASH_REPORT_FUNCTION;
58     return connectSocket(_sockname);
59 }
60 
61 bool
init(const char * sockpath)62 Lirc::init(const char *sockpath)
63 {
64 //    GNASH_REPORT_FUNCTION;
65     _connected = connectSocket(sockpath);
66     return _connected;
67 }
68 
69 // Whenever lircd receives a IR signal it will broadcast the
70 // following string to each client:
71 // <code> <repeat count> <button name> <remote control name>
72 // 0000000000000003 1 PREV LIRCEMU
73 // 0000000000000006 1 NEXT LIRCEMU
74 // 0000000000000012 1 A LIRCEMU
75 
76 gnash::key::code
getKey()77 Lirc::getKey()
78 {
79 //    GNASH_REPORT_FUNCTION;
80     key::code key = gnash::key::INVALID;
81 
82     byte_t buf[LIRC_PACKET_SIZE];
83     memset(buf, 0, LIRC_PACKET_SIZE);
84 
85     // read the data if there is any
86     readNet(buf, LIRC_PACKET_SIZE, TIMEOUT);
87 
88     string packet = reinterpret_cast<char *>(buf);
89     string::size_type space1 = packet.find(" ") +1;
90     string::size_type space2 = packet.find(" ", space1) + 1;
91     string::size_type space3 = packet.find(" ", space2) +1;
92 
93     string code_str = packet.substr(0, space1);
94     string count_str = packet.substr(space1, space2-space1);
95     string button_str = packet.substr(space2,space3-space2);
96     string control_str = packet.substr(space3);
97 
98     if (button_str[0] > 'A' && button_str[0] < 'Z') {
99         std::cerr << "Character: " << button_str << std::endl;
100         key = (gnash::key::code)button_str[0];
101     }
102 
103     return key;
104 }
105 
106 const char *
getButton()107 Lirc::getButton()
108 {
109 //    GNASH_REPORT_FUNCTION;
110 
111     byte_t buf[LIRC_PACKET_SIZE];
112     memset(buf, 0, LIRC_PACKET_SIZE);
113 
114     // read the data if there is any
115     readNet(buf, LIRC_PACKET_SIZE, TIMEOUT);
116 
117     string packet = reinterpret_cast<char *>(buf);
118     string::size_type space1 = packet.find(" ") + 1;
119     string::size_type space2 = packet.find(" ", space1) + 1;
120     string::size_type space3 = packet.find(" ", space2) + 1;
121 
122     string button_str = packet.substr(space2, space3-space2-1);
123 
124     memset(_button, 0, BUTTONSIZE);
125     strncpy(_button, button_str.c_str(), BUTTONSIZE);
126     return _button;
127 }
128 
129 } // end of gnash namespace
130 
131 // Local Variables:
132 // mode: C++
133 // indent-tabs-mode: t
134 // End:
135