Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/tuneology/irremote/LircClient.java


1   /*
2     LircClient.java
3   
4     Copyright (C) 2002 Fran Taylor
5   
6     This library is free software; you can redistribute it and/or modify
7     it under the terms of the GNU Lesser General Public License as
8     published by the Free Software Foundation; either version 2.1 of the
9     License, or (at your option) any later version.
10  
11    This library 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 GNU
14    Lesser General Public License for more details.
15  
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19    
20    $Id: LircClient.java,v 1.1 2002/09/23 14:29:16 xnarf Exp $
21  
22  */
23  
24  package com.tuneology.irremote;
25  
26  import java.io.*;
27  import java.util.*;
28  
29  /**
30   * Low-level API to LIRC.  Consists of a 
31   * reader connected to the irx process.  Outputs a line of text
32   * for each infrared event.
33   *
34   * @version $Id: LircClient.java,v 1.1 2002/09/23 14:29:16 xnarf Exp $
35   * 
36   * @author Fran Taylor
37   */
38  public class LircClient extends BufferedReader {
39      /**
40       * Constructs a Lirc Client object.  Starts up irx and attach this object to it.
41       * 
42       * @param arg The pathname to the irx executable.
43       * @throws IOException
44       */
45      public LircClient(String arg) throws IOException {
46          this(Runtime.getRuntime().exec(getIrwCmd(arg)));
47          this.prog = prog;
48      }
49      /**
50       * Close the connection to the remote control interface.
51       *
52       * @throws IOException
53       */
54      public void close() throws IOException {
55          proc.destroy();
56          super.close();
57      }
58      /**
59       * Sets the path to the irw command.  Default is /usr/local/bin/irw
60       * 
61       * @param cmd the pathname of the irw command.
62       */
63      public static void setIrwCmd(String cmd) { prog = cmd; }
64      /**
65       *
66       * @return the command used to invoke irw.
67       */
68      public static String getIrwCmd() { return prog; }
69      /**
70       * Returns a string containing version info for irw.
71       *
72       * @return The version string.
73       */
74      public static String getIrwVersion() {
75          String[] cmd = new String[2];
76          cmd[0] = prog;
77          cmd[1] = "--version";
78          try {
79              Process vproc = Runtime.getRuntime().exec(cmd);
80              BufferedReader rdr = new BufferedReader(new InputStreamReader(vproc.getInputStream()));
81              String str;
82              while((str = rdr.readLine()) != null) {
83                  if (!str.equals("")) {
84                      rdr.close();
85                      return str;
86                  }
87              }
88              rdr.close();
89          } catch (Exception ex) {
90              return "irw exception: " + ex.toString();
91          }
92          return "irw version";
93      }
94  
95      /**
96       * Sets the path to the lircd.conf file. Default is /etc/lircd.conf
97       *
98       * @param path
99       */
100     public static void setConfigPath(String path) { configPath = path; }
101     /**
102      * Returns the path to the lircd.conf file.
103      *
104      * @return the path to the lircd config file.
105      */
106     public static String getConfigPath() { return configPath; }
107     /**
108      * Returns a list of commands that can be generated by LIRC.
109      *
110      * @return list of commands.
111      */
112     public String[] getCommands() {
113         Vector v = new Vector();
114         try {
115             // read lircd.conf and make an array of all recognized remote commands
116             BufferedReader rdr = new BufferedReader(new FileReader(configPath));
117             String remote = null;
118             boolean incodes = false;
119             String str;
120             while((str = rdr.readLine()) != null) {
121                 StringTokenizer tok = new StringTokenizer(str, " \t");
122                 if (!tok.hasMoreTokens()) continue;
123                 String t1 = tok.nextToken();
124                 if (!incodes) {
125                     if (t1.equals("begin")) {
126                         if (!tok.hasMoreTokens()) continue;
127                         if (tok.nextToken().equals("codes")) incodes = true;
128                     } else if (t1.equals("name")) {
129                         if (!tok.hasMoreTokens()) continue;
130                         remote = tok.nextToken();
131                     }
132                 } else if (t1.equals("end")) {
133                     if (!tok.hasMoreTokens()) continue;
134                     if (tok.nextToken().equals("codes")) incodes = false; 
135                 } else if (remote != null) {
136                     v.add(remote + "-" + t1);
137                 }
138             }
139             rdr.close();
140         } catch (IOException e) {
141             return null;
142         }
143         String[] retval = new String[v.size()];
144         for(int i = 0; i < v.size(); i++) retval[i] = (String) v.get(i);
145         return retval;
146     }
147     // constructor trick so we can hang onto the process object
148     private LircClient(Process proc) throws IOException {
149         super(new InputStreamReader(proc.getInputStream()));
150         this.proc = proc;
151     }
152     // parse the 
153     private static String[] getIrwCmd(String str) {
154         String[] cmd = null;
155         if (str == null) {
156             cmd = new String[1];
157         } else {
158             cmd = new String[2];
159             cmd[1] = str;
160         }
161         cmd[0] = prog;
162         return cmd;
163     }
164     private Process proc;
165     private static String prog;
166     private static String configPath;
167     static {
168         prog = "/usr/local/bin/irw";
169         configPath = "/etc/lircd.conf";
170     }
171 }
172 /*
173    Local Variables:
174    mode:java
175    indent-tabs-mode:nil 
176    c-basic-offset:4 
177    c-indent-level:4 
178    c-continued-statement-offset:4 
179    c-brace-offset:-4 
180    c-brace-imaginary-offset:-4 
181    c-argdecl-indent:0
182    c-label-offset:0
183    End:
184 */