]> git.vomp.tv Git - vompclient-marten.git/blob - remote.cc
Fix for cant power down if server unavailable
[vompclient-marten.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 }
34
35 Remote::~Remote()
36 {
37   instance = NULL;
38 }
39
40 Remote* Remote::getInstance()
41 {
42   return instance;
43 }
44
45 int Remote::init(char* devName)
46 {
47   if (initted) return 0;
48   initted = 1;
49
50   device = open(devName, O_RDONLY);
51   if (device < 0)
52   {
53     initted = 0;
54     return 0;
55   }
56
57   return 1;
58 }
59
60 int Remote::shutdown()
61 {
62   if (!initted) return 0;
63   initted = 0;
64   close(device);
65   device = 0;
66   return 1;
67 }
68
69 int Remote::getDevice()
70 {
71   if (!initted) return 0;
72   return device;
73 }
74
75 UCHAR Remote::getButtonPress(int waitType)
76 {
77   /* how = 0 - block
78      how = 1 - start new wait
79      how = 2 - continue wait
80      how = 3 - no wait
81   */
82
83   unsigned long input;
84   struct timeval* passToSelect = NULL;
85   int retval;
86   fd_set readfds;
87
88   if (waitType == 0)
89   {
90     passToSelect = NULL;
91   }
92   else if (waitType == 1)
93   {
94     tv.tv_sec = 1;
95     tv.tv_usec = 000000;
96     passToSelect = &tv;
97   }
98   else if (waitType == 2)
99   {
100     if ((tv.tv_sec == 0) && (tv.tv_usec == 0))  // protection in case timer = 0
101     {
102       tv.tv_sec = 1;
103       tv.tv_usec = 000000;
104     }
105     passToSelect = &tv;
106   }
107   else if (waitType == 3)
108   {
109     tv.tv_sec = 0;
110     tv.tv_usec = 0;
111     passToSelect = &tv;
112   }
113   FD_ZERO(&readfds);
114   FD_SET(device, &readfds);
115
116   retval = select(device + 1, &readfds, NULL, NULL, &tv);
117   // 0 = nothing happened
118   // 1 = data arrived (actually num of descriptors that changed)
119   // other value = signal or error
120   if (retval == 0) return NONE;
121   if (retval == -1) return SIGNAL;
122
123   int count = read(device, &input, 4);
124   if (count == 4)
125   {
126     input = (0X00FF0000 & input) >> 16;
127     Log::getInstance()->log("Remote", Log::DEBUG, "Button %i", input);
128     return (UCHAR) input;
129   }
130   return UNKNOWN;
131 }