LIRC libraries
LinuxInfraredRemoteControl
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
release.c
Go to the documentation of this file.
1 /****************************************************************************
2 ** release.c ***************************************************************
3 ****************************************************************************
4 *
5 * Copyright (C) 2007 Christoph Bartelmus (lirc@bartelmus.de)
6 *
7 */
8 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sys/time.h>
24 
25 #include "include/media/lirc.h"
26 #include "lirc/release.h"
27 #include "lirc/receive.h"
28 #include "lirc/lirc_log.h"
29 
30 static struct timeval release_time;
31 static struct ir_remote* release_remote;
32 static struct ir_ncode* release_ncode;
33 static ir_code release_code;
34 static int release_reps;
35 static lirc_t release_gap;
36 
37 static struct ir_remote* release_remote2;
38 static struct ir_ncode* release_ncode2;
39 static ir_code release_code2;
40 static const char* release_suffix = LIRC_RELEASE_SUFFIX;
41 static char message[PACKET_SIZE + 1];
42 
43 void register_input(void)
44 {
45  struct timeval gap;
46 
47  if (release_remote == NULL)
48  return;
49 
50  timerclear(&gap);
51  gap.tv_usec = release_gap;
52 
53  gettimeofday(&release_time, NULL);
54  timeradd(&release_time, &gap, &release_time);
55 }
56 
57 void register_button_press(struct ir_remote* remote, struct ir_ncode* ncode, ir_code code, int reps)
58 {
59  if (reps == 0 && release_remote != NULL) {
60  release_remote2 = release_remote;
61  release_ncode2 = release_ncode;
62  release_code2 = release_code;
63  }
64 
65  release_remote = remote;
66  release_ncode = ncode;
67  release_code = code;
68  release_reps = reps;
69  /* some additional safety margin */
70  release_gap = upper_limit(remote,
71  remote->max_total_signal_length - remote->min_gap_length)
72  + receive_timeout(upper_limit(remote, remote->min_gap_length)) + 10000;
73 
74  LOGPRINTF(1, "release_gap: %lu", release_gap);
75 
76  register_input();
77 }
78 
79 void get_release_data(const char** remote_name, const char** button_name, int* reps)
80 {
81  if (release_remote != NULL) {
82  *remote_name = release_remote->name;
83  *button_name = release_ncode->name;
84  *reps = release_reps;
85  } else {
86  *remote_name = *button_name = "(NULL)";
87  *reps = 0;
88  }
89 }
90 
91 void set_release_suffix(const char* s)
92 {
93  release_suffix = s;
94 }
95 
96 void get_release_time(struct timeval* tv)
97 {
98  *tv = release_time;
99 }
100 
101 const char* check_release_event(const char** remote_name, const char** button_name)
102 {
103  int len = 0;
104 
105  if (release_remote2 != NULL) {
106  *remote_name = release_remote2->name;
107  *button_name = release_ncode2->name;
108  len = write_message(message,
109  PACKET_SIZE + 1,
110  release_remote2->name,
111  release_ncode2->name,
112  release_suffix,
113  release_code2,
114  0);
115  release_remote2 = NULL;
116  release_ncode2 = NULL;
117  release_code2 = 0;
118 
119  if (len >= PACKET_SIZE + 1) {
120  logprintf(LIRC_ERROR, "message buffer overflow");
121  return NULL;
122  }
123 
124  LOGPRINTF(3, "check");
125  return message;
126  }
127  return NULL;
128 }
129 
130 const char* trigger_release_event(const char** remote_name, const char** button_name)
131 {
132  int len = 0;
133 
134  if (release_remote != NULL) {
135  release_remote->release_detected = 1;
136  *remote_name = release_remote->name;
137  *button_name = release_ncode->name;
138  len = write_message(message,
139  PACKET_SIZE + 1,
140  release_remote->name,
141  release_ncode->name,
142  release_suffix,
143  release_code,
144  0);
145  timerclear(&release_time);
146  release_remote = NULL;
147  release_ncode = NULL;
148  release_code = 0;
149 
150  if (len >= PACKET_SIZE + 1) {
151  logprintf(LIRC_ERROR, "message buffer overflow");
152  return NULL;
153  }
154  LOGPRINTF(3, "trigger");
155  return message;
156  }
157  return NULL;
158 }
159 
160 const char* release_map_remotes(struct ir_remote* old, struct ir_remote* new, const char** remote_name,
161  const char** button_name)
162 {
163  struct ir_remote* remote;
164  struct ir_ncode* ncode = NULL;
165 
166  if (release_remote2 != NULL) {
167  /* should not happen */
168  logprintf(LIRC_ERROR, "release_remote2 still in use");
169  release_remote2 = NULL;
170  }
171  if (release_remote && is_in_remotes(old, release_remote)) {
172  remote = get_ir_remote(new, release_remote->name);
173  if (remote)
174  ncode = get_code_by_name(remote, release_ncode->name);
175  if (remote && ncode) {
176  release_remote = remote;
177  release_ncode = ncode;
178  } else {
179  return trigger_release_event(remote_name, button_name);
180  }
181  }
182  return NULL;
183 }
lirc_t max_total_signal_length
const char * name
struct ir_ncode * get_code_by_name(const struct ir_remote *remote, const char *name)
Definition: ir_remote.c:389
struct ir_remote * get_ir_remote(const struct ir_remote *remotes, const char *name)
Definition: ir_remote.c:243
__u64 ir_code
lirc_t min_gap_length
#define PACKET_SIZE
Definition: lirc_config.h:98
char * name
int write_message(char *buffer, size_t size, const char *remote_name, const char *button_name, const char *button_suffix, ir_code code, int reps)
Definition: ir_remote.c:705
const struct ir_remote * is_in_remotes(const struct ir_remote *remotes, const struct ir_remote *remote)
Definition: ir_remote.c:231
#define LIRC_RELEASE_SUFFIX
Definition: lirc_config.h:77
#define LOGPRINTF(level, fmt, args...)
Definition: lirc_log.h:75
int release_detected