Source code: com/tuneology/irremote/LircComponent.java
1 /*
2 LircComponent.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: LircComponent.java,v 1.2 2002/11/05 08:33:09 xnarf Exp $
21
22 */
23
24 package com.tuneology.irremote;
25
26 import java.io.*;
27 import java.util.*;
28 import java.awt.event.*;
29 import javax.swing.*;
30
31 /**
32 * A high-level interface to Linux Infrared Remote Control. The
33 * application instantiates a LircComponent object and registers
34 * listeners with it.
35 */
36
37 public class LircComponent implements IRComponent {
38 /**
39 * Create a component that listens for IR events and dispatches actions.
40 * @param prog The pathname to the irx executable.
41 */
42 public LircComponent(String arg) throws IOException {
43 client = new LircClient(arg);
44 init();
45 }
46 /**
47 * Add a callback for an IR command. The command corresponding to the
48 * action is stored in the IR_BINDING value of the listener.
49 */
50 public void addActionListener(AbstractAction listener) {
51 String action = (String) listener.getValue(IR_COMMAND);
52 if (action == null) {
53 allEventListeners.add(listener);
54 return;
55 }
56 actionMap.put(action, listener);
57 String cmd = (String) listener.getValue(IR_BINDING);
58 if ((cmd != null) && !cmd.equals("")) {
59 StringTokenizer tok = new StringTokenizer(cmd, ",");
60 while(tok.hasMoreTokens())
61 cmdMap.put(tok.nextToken(), listener);
62 }
63 listeners.add(listener);
64 }
65 /**
66 *
67 * @param listener
68 */
69 public void removeActionListener(AbstractAction listener) {
70 String cmd = (String) listener.getValue(IR_BINDING);
71 if (cmd == null) {
72 allEventListeners.remove(listener);
73 return;
74 }
75 listeners.remove(listener);
76 String action = (String) listener.getValue(IR_COMMAND);
77 if (action != null)
78 actionMap.remove(action);
79 if (!cmd.equals("")) {
80 StringTokenizer tok = new StringTokenizer(cmd, ",");
81 while(tok.hasMoreTokens())
82 cmdMap.remove(tok.nextToken());
83 }
84
85 }
86 /**
87 *
88 * @param action
89 * @return the listener corresponding to the action.
90 */
91 public AbstractAction getListener(String action) {
92 return (AbstractAction) actionMap.get(action);
93 }
94 /**
95 *
96 * @return a vector containing all the defined listeners.
97 */
98 public Vector getListeners() {
99 Vector v = (Vector) listeners.clone();
100 return v;
101 }
102 /**
103 *
104 * @return a vector containing the defined listeners to all events.
105 */
106 public Vector getAllEventListeners() {
107 Vector v = (Vector) allEventListeners.clone();
108 return v;
109 }
110 /**
111 *
112 * @return an array of all the defined IR commands.
113 */
114 public String[] getCommands() { return client.getCommands(); }
115 /**
116 *
117 * @param listener
118 * @return true if the listener is defined.
119 */
120 public boolean contains(AbstractAction listener) {
121 return listeners.contains(listener) || allEventListeners.contains(listener);
122 }
123 /**
124 *
125 * @return the command used to invoke irw
126 */
127 public String getIrwCmd() { return client.getIrwCmd(); }
128 /**
129 *
130 * @return the path to lircd.conf
131 */
132 public String getConfigPath() { return client.getConfigPath(); }
133 /**
134 *
135 * @param p
136 */
137 public void setConfigPath(String p) { client.setConfigPath(p); }
138
139 private void scanEvents() throws IOException {
140 String str;
141 while((str = client.readLine()) != null) {
142 StringTokenizer tok = new StringTokenizer(str, " ");
143 if (tok.countTokens() < 3) continue;
144 tok.nextToken(); tok.nextToken();
145 String evt = tok.nextToken();
146 final String nstr = tok.nextToken() + "-" + evt;
147 SwingUtilities.invokeLater(new Runnable() {
148 public void run() {
149 notifyActionListeners(nstr);
150 }
151 } );
152 }
153 client.close();
154 }
155
156 private void init() {
157 listeners = new Vector();
158 allEventListeners = new Vector();
159 cmdMap = new HashMap();
160 actionMap = new HashMap();
161 lircThread = new Thread() {
162 public void run() {
163 try {
164 scanEvents();
165 } catch (IOException e) {
166 }
167 }
168 };
169 lircThread.start();
170 }
171 private void notifyActionListeners(String evt) {
172 ActionEvent ev = new ActionEvent(this, 0, evt);
173 AbstractAction listener = (AbstractAction) cmdMap.get(evt);
174 if (listener != null)
175 listener.actionPerformed(ev);
176 for(int i = 0; i < allEventListeners.size(); i++)
177 ((AbstractAction)(allEventListeners.get(i))).actionPerformed(ev);
178 }
179 private Thread lircThread;
180 private LircClient client;
181 private HashMap cmdMap, actionMap;
182 private Vector listeners;
183 private Vector allEventListeners;
184 }
185 /*
186 Local Variables:
187 mode:java
188 indent-tabs-mode:nil
189 c-basic-offset:4
190 c-indent-level:4
191 c-continued-statement-offset:4
192 c-brace-offset:-4
193 c-brace-imaginary-offset:-4
194 c-argdecl-indent:0
195 c-label-offset:0
196 End:
197 */
198