diff mbox series

[v4l-utils] ir-ctl: allow for different gaps to be specified

Message ID 20211206180643.2690-1-sean@mess.org (mailing list archive)
State New, archived
Headers show
Series [v4l-utils] ir-ctl: allow for different gaps to be specified | expand

Commit Message

Sean Young Dec. 6, 2021, 6:06 p.m. UTC
For example:

ir-ctl --send=part1 --gap=10000 --send=part2 --gap=20000 --send=part3

This would introduce a gap of 10000 microseconds between part1 and
part2, and 20000 microseconds between part2 and part3.

Signed-off-by: Sean Young <sean@mess.org>
---
 utils/ir-ctl/ir-ctl.c | 122 +++++++++++++++++++++---------------------
 1 file changed, 60 insertions(+), 62 deletions(-)
diff mbox series

Patch

diff --git a/utils/ir-ctl/ir-ctl.c b/utils/ir-ctl/ir-ctl.c
index 34cde11e..c3a34d3f 100644
--- a/utils/ir-ctl/ir-ctl.c
+++ b/utils/ir-ctl/ir-ctl.c
@@ -66,14 +66,19 @@ 
 const char *argp_program_version = "IR ctl version " V4L_UTILS_VERSION;
 const char *argp_program_bug_address = "Sean Young <sean@mess.org>";
 
+enum send_ty {
+	SEND_RAW,
+	SEND_SCANCODE,
+	SEND_KEYCODE,
+	SEND_GAP,
+};
 /*
  * Since this program drives the lirc interface, use the same terminology
  */
 struct send {
 	struct send *next;
 	const char *fname;
-	bool is_scancode;
-	bool is_keycode;
+	enum send_ty ty;
 	union {
 		struct {
 			unsigned carrier;
@@ -85,6 +90,7 @@  struct send {
 			unsigned protocol;
 		};
 		char	keycode[1];
+		int	gap;
 	};
 };
 
@@ -223,8 +229,7 @@  static struct send *read_file_pulse_space(struct arguments *args, const char *fn
 		fprintf(stderr, _("Failed to allocate memory\n"));
 		return NULL;
 	}
-	f->is_scancode = false;
-	f->is_keycode = false;
+	f->ty = SEND_RAW;
 	f->carrier = UNSET;
 	f->fname = fname;
 
@@ -393,8 +398,7 @@  static struct send *read_file_raw(struct arguments *args, const char *fname, FIL
 		fclose(input);
 		return NULL;
 	}
-	f->is_scancode = false;
-	f->is_keycode = false;
+	f->ty = SEND_RAW;
 	f->carrier = UNSET;
 	f->fname = fname;
 
@@ -549,14 +553,32 @@  static struct send *read_scancode(const char *name)
 		return NULL;
 	}
 
-	f->is_scancode = true;
-	f->is_keycode = false;
+	f->ty = SEND_SCANCODE;
 	f->scancode = scancode;
 	f->protocol = proto;
 
 	return f;
 }
 
+static void add_to_send_list(struct arguments *arguments, struct send *send)
+{
+	send->next = NULL;
+
+	if (arguments->send == NULL)
+		arguments->send = send;
+	else {
+		// introduce gap
+		struct send *gap = malloc(sizeof(*gap));
+		gap->ty = SEND_GAP;
+		gap->gap = arguments->gap;
+		gap->next = send;
+
+		struct send *p = arguments->send;
+		while (p->next) p = p->next;
+		p->next = gap;
+	}
+}
+
 static error_t parse_opt(int k, char *arg, struct argp_state *state)
 {
 	struct arguments *arguments = state->input;
@@ -664,14 +686,7 @@  static error_t parse_opt(int k, char *arg, struct argp_state *state)
 		if (s == NULL)
 			exit(EX_DATAERR);
 
-		s->next = NULL;
-		if (arguments->send == NULL)
-			arguments->send = s;
-		else {
-			struct send *p = arguments->send;
-			while (p->next) p = p->next;
-			p->next = s;
-		}
+		add_to_send_list(arguments, s);
 		break;
 	case 'S':
 		if (arguments->receive || arguments->features)
@@ -680,14 +695,7 @@  static error_t parse_opt(int k, char *arg, struct argp_state *state)
 		if (s == NULL)
 			exit(EX_DATAERR);
 
-		s->next = NULL;
-		if (arguments->send == NULL)
-			arguments->send = s;
-		else {
-			struct send *p = arguments->send;
-			while (p->next) p = p->next;
-			p->next = s;
-		}
+		add_to_send_list(arguments, s);
 		break;
 
 	case 'K':
@@ -696,18 +704,10 @@  static error_t parse_opt(int k, char *arg, struct argp_state *state)
 		s = malloc(sizeof(*s) + strlen(arg));
 		if (s == NULL)
 			exit(EX_DATAERR);
-
-		s->next = NULL;
 		strcpy(s->keycode, arg);
-		s->is_scancode = false;
-		s->is_keycode = true;
-		if (arguments->send == NULL)
-			arguments->send = s;
-		else {
-			struct send *p = arguments->send;
-			while (p->next) p = p->next;
-			p->next = s;
-		}
+		s->ty = SEND_KEYCODE;
+
+		add_to_send_list(arguments, s);
 		break;
 
 	case 'k':
@@ -757,8 +757,7 @@  static struct send* convert_keycode(struct keymap *map, const char *keycode)
 				s = malloc(sizeof(*s) + re->raw_length * sizeof(int));
 				s->len = re->raw_length;
 				memcpy(s->buf, re->raw, s->len * sizeof(int));
-				s->is_scancode = false;
-				s->is_keycode = false;
+				s->ty = SEND_RAW;
 				s->carrier = keymap_param(map, "carrier", 0);
 				s->next = NULL;
 			}
@@ -783,16 +782,14 @@  static struct send* convert_keycode(struct keymap *map, const char *keycode)
 				s = malloc(sizeof(*s));
 				s->protocol = proto;
 				s->scancode = se->scancode;
-				s->is_scancode = true;
-				s->is_keycode = false;
+				s->ty = SEND_SCANCODE;
 				s->next = NULL;
 			} else if (encode_bpf_protocol(map, se->scancode,
 						       buf, &length)) {
 				s = malloc(sizeof(*s) + sizeof(int) * length);
 				s->len = length;
 				memcpy(s->buf, buf, length * sizeof(int));
-				s->is_scancode = false;
-				s->is_keycode = false;
+				s->ty = SEND_RAW;
 				s->carrier = keymap_param(map, "carrier", 0);
 				s->next = NULL;
 			} else {
@@ -1049,7 +1046,7 @@  static int lirc_send(struct arguments *args, int fd, unsigned features, struct s
 		return EX_UNAVAILABLE;
 	}
 
-	if (f->is_scancode) {
+	if (f->ty == SEND_SCANCODE) {
 		if (args->verbose)
 			printf("Sending to kernel encoder protocol:%s scancode:0x%x\n",
 			       protocol_name(f->protocol), f->scancode);
@@ -1075,7 +1072,7 @@  static int lirc_send(struct arguments *args, int fd, unsigned features, struct s
 		return EX_UNAVAILABLE;
 	}
 
-	if (f->is_scancode) {
+	if (f->ty == SEND_SCANCODE) {
 		// encode scancode
 		enum rc_proto proto = f->protocol;
 		if (!protocol_encoder_available(proto)) {
@@ -1265,29 +1262,30 @@  int main(int argc, char *argv[])
 	struct send *s = args.send;
 	while (s) {
 		struct send *next = s->next;
-		if (s != args.send)
-			usleep(args.gap);
-
-		if (s->is_keycode) {
-			struct send *k;
+		if (s->ty == SEND_GAP) {
+			usleep(s->gap);
+		} else {
+			if (s->ty == SEND_KEYCODE) {
+				struct send *k;
 
-			if (!args.keymap) {
-				fprintf(stderr, _("error: no keymap specified\n"));
-				exit(EX_DATAERR);
-			}
+				if (!args.keymap) {
+					fprintf(stderr, _("error: no keymap specified\n"));
+					exit(EX_DATAERR);
+				}
 
-			k = convert_keycode(args.keymap, s->keycode);
-			if (!k)
-				exit(EX_DATAERR);
+				k = convert_keycode(args.keymap, s->keycode);
+				if (!k)
+					exit(EX_DATAERR);
 
-			free(s);
-			s = k;
-		}
+				free(s);
+				s = k;
+			}
 
-		rc = lirc_send(&args, fd, features, s);
-		if (rc) {
-			close(fd);
-			exit(rc);
+			rc = lirc_send(&args, fd, features, s);
+			if (rc) {
+				close(fd);
+				exit(rc);
+			}
 		}
 
 		free(s);