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