]> git.vomp.tv Git - vompclient.git/blob - remote.cc
Connection failure detection
[vompclient.git] / remote.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "remote.h"
22
23 Remote* Remote::instance = NULL;
24
25 Remote::Remote()
26 {
27   if (instance) return;
28   instance = this;
29   initted = 0;
30   device = 0;
31   tv.tv_sec = 0;
32   tv.tv_usec = 0;
33   remoteType = OLDREMOTE;
34 }
35
36 Remote::~Remote()
37 {
38   instance = NULL;
39 }
40
41 Remote* Remote::getInstance()
42 {
43   return instance;
44 }
45
46 int Remote::init(char* devName)
47 {
48   if (initted) return 0;
49   initted = 1;
50
51   device = open(devName, O_RDONLY);
52   if (device < 0)
53   {
54     initted = 0;
55     return 0;
56   }
57
58   return 1;
59 }
60
61 int Remote::shutdown()
62 {
63   if (!initted) return 0;
64   initted = 0;
65   close(device);
66   device = 0;
67   return 1;
68 }
69
70 int Remote::getDevice()
71 {
72   if (!initted) return 0;
73   return device;
74 }
75
76 void Remote::setRemoteType(UCHAR newType)
77 {
78   if ((newType != OLDREMOTE) && (newType != NEWREMOTE)) return;
79   remoteType = newType;
80 }
81
82 UCHAR Remote::getButtonPress(int waitType)
83 {
84   /* how = 0 - block
85      how = 1 - start new wait
86      how = 2 - continue wait
87      how = 3 - no wait
88   */
89
90   unsigned long input;
91   struct timeval* passToSelect = NULL;
92   int retval;
93   fd_set readfds;
94
95   if (waitType == 0)
96   {
97     passToSelect = NULL;
98   }
99   else if (waitType == 1)
100   {
101     tv.tv_sec = 1;
102     tv.tv_usec = 000000;
103     passToSelect = &tv;
104   }
105   else if (waitType == 2)
106   {
107     if ((tv.tv_sec == 0) && (tv.tv_usec == 0))  // protection in case timer = 0
108     {
109       tv.tv_sec = 1;
110       tv.tv_usec = 000000;
111     }
112     passToSelect = &tv;
113   }
114   else if (waitType == 3)
115   {
116     tv.tv_sec = 0;
117     tv.tv_usec = 0;
118     passToSelect = &tv;
119   }
120   FD_ZERO(&readfds);
121   FD_SET(device, &readfds);
122
123   retval = select(device + 1, &readfds, NULL, NULL, &tv);
124   // 0 = nothing happened
125   // 1 = data arrived (actually num of descriptors that changed)
126   // other value = signal or error
127   if (retval == 0) return NA_NONE;
128   if (retval == -1) return NA_SIGNAL;
129
130   int count = read(device, &input, 4);
131   if (count == 4)
132   {
133     input = (0X00FF0000 & input) >> 16;
134     Log::getInstance()->log("Remote", Log::DEBUG, "Button %i", input);
135
136     if (remoteType == OLDREMOTE)
137     {
138       if (input == VOLUMEDOWN) return DF_LEFT;
139       if (input == VOLUMEUP) return DF_RIGHT;
140       if (input == CHANNELUP) return DF_UP;
141       if (input == CHANNELDOWN) return DF_DOWN;
142     }
143
144     return (UCHAR) input;
145   }
146   return NA_UNKNOWN;
147 }
148
149 void Remote::clearBuffer()
150 {
151   while(getButtonPress(3) != NA_NONE);
152 }