Drop udev dependency for frecon-lite

This patch expands the existing frecon-lite concept that is currently
only managed by ebuilds (and the DBUS=0 command line flag) to be known
within the frecon Makefiles. Code that is different between the two
binaries is split into separate files with _full.c/_lite.c suffix.

Also decouple frecon-lite from udev so that we can drop another big
dependency from the recovery initramfs. Instead frecon-lite will simply
listen to all /dev/input/eventX nodes that exist on startup. We don't
really care about hotplugging for recovery, and the extra power
consumption from listening to irrelevant inputs doesn't really matter
for that use case.

BUG=chromium:984814
TEST=Ran recovery on Kevin, confirmed I could still switch VTs with
Ctrl+Alt+F2.

Cq-Depend: chromium:1737847
Change-Id: I352ac2e6aa2dbe558d251bfe9fcd49a81c9d5b6f
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1709749
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Dominik Behr <dbehr@chromium.org>
(cherry picked from commit aa279299deec851ef4cc29eb99f38426afc92d12)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/frecon/+/1737725
diff --git a/Makefile b/Makefile
index c00ec4a..68cf300 100644
--- a/Makefile
+++ b/Makefile
@@ -4,14 +4,19 @@
 
 include common.mk
 
-DBUS ?= 1
+FRECON_LITE ?= 0
 
-PC_DEPS = libdrm libudev libpng libtsm
-ifeq ($(DBUS),1)
-PC_DEPS += dbus-1
-CPPFLAGS += -DDBUS=1
-endif
+PC_DEPS = libdrm libpng libtsm
+ifeq ($(FRECON_LITE),1)
+FRECON_OBJECTS = $(filter-out %_full.o,$(C_OBJECTS))
+CPPFLAGS += -DFRECON_LITE=1
+TARGET ?= frecon-lite
+else
+FRECON_OBJECTS = $(filter-out %_lite.o,$(C_OBJECTS))
+PC_DEPS += dbus-1 libudev
+CPPFLAGS += -DFRECON_LITE=0
 TARGET ?= frecon
+endif
 
 PC_CFLAGS := $(shell $(PKG_CONFIG) --cflags $(PC_DEPS))
 PC_LIBS := $(shell $(PKG_CONFIG) --libs $(PC_DEPS))
@@ -27,7 +32,7 @@
 
 font.o.depends: $(OUT)glyphs.h
 
-CC_BINARY($(TARGET)): $(C_OBJECTS)
+CC_BINARY($(TARGET)): $(FRECON_OBJECTS)
 
 all: CC_BINARY($(TARGET))
 
diff --git a/dbus.c b/dbus_full.c
similarity index 93%
rename from dbus.c
rename to dbus_full.c
index d854d51..d2a89d9 100644
--- a/dbus.c
+++ b/dbus_full.c
@@ -4,7 +4,6 @@
  * found in the LICENSE file.
  */
 
-#if DBUS
 #include <dbus/dbus.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -427,60 +426,3 @@
 	suspend_done_callback = callback;
 	suspend_done_callback_userptr = userptr;
 }
-
-#else
-
-#include <stdlib.h>
-#include <stdbool.h>
-
-bool dbus_init()
-{
-	return true;
-}
-
-bool dbus_init_wait()
-{
-	return true;
-}
-
-void dbus_destroy(void)
-{
-}
-
-void dbus_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd)
-{
-}
-
-void dbus_dispatch_io(void)
-{
-}
-
-void dbus_report_user_activity(int activity_type)
-{
-}
-
-bool dbus_take_display_ownership(void)
-{
-	return true;
-}
-
-bool dbus_release_display_ownership(void)
-{
-	return true;
-}
-
-bool dbus_is_initialized(void)
-{
-	return true;
-}
-
-void dbus_set_login_prompt_visible_callback(void (*callback)(void))
-{
-}
-
-void dbus_set_suspend_done_callback(void (*callback)(void*),
-				    void* userptr)
-{
-}
-
-#endif
diff --git a/dbus_lite.c b/dbus_lite.c
new file mode 100644
index 0000000..2574d69
--- /dev/null
+++ b/dbus_lite.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2019 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <stdlib.h>
+#include <stdbool.h>
+
+bool dbus_init()
+{
+	return true;
+}
+
+bool dbus_init_wait()
+{
+	return true;
+}
+
+void dbus_destroy(void)
+{
+}
+
+void dbus_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd)
+{
+}
+
+void dbus_dispatch_io(void)
+{
+}
+
+void dbus_report_user_activity(int activity_type)
+{
+}
+
+bool dbus_take_display_ownership(void)
+{
+	return true;
+}
+
+bool dbus_release_display_ownership(void)
+{
+	return true;
+}
+
+bool dbus_is_initialized(void)
+{
+	return true;
+}
+
+void dbus_set_login_prompt_visible_callback(void (*callback)(void))
+{
+}
+
+void dbus_set_suspend_done_callback(void (*callback)(void*),
+				    void* userptr)
+{
+}
diff --git a/dev.h b/dev.h
index 503ae81..b1b73c3 100644
--- a/dev.h
+++ b/dev.h
@@ -7,9 +7,10 @@
 #ifndef DEV_H
 #define DEV_H
 
+#include <sys/select.h>
+
 int dev_init(void);
 void dev_close(void);
-void dev_add_existing_input_devs(void);
 void dev_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd);
 void dev_dispatch_io(fd_set* read_set, fd_set* exception_set);
 
diff --git a/dev.c b/dev_full.c
similarity index 98%
rename from dev.c
rename to dev_full.c
index 0bd5ac3..f8e71a9 100644
--- a/dev.c
+++ b/dev_full.c
@@ -17,6 +17,36 @@
 static struct udev_monitor* udev_monitor = NULL;
 static int udev_fd = -1;
 
+static bool dev_is_keyboard_device(struct udev_device* dev)
+{
+	const char *keyboard = udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD");
+
+	if (keyboard && !strcmp(keyboard, "1"))
+		return true;
+
+	return false;
+}
+
+static void dev_add_existing_input_devs(void)
+{
+	struct udev_enumerate* udev_enum;
+	struct udev_list_entry* devices, *deventry;
+	udev_enum = udev_enumerate_new(udev);
+	udev_enumerate_add_match_subsystem(udev_enum, "input");
+	udev_enumerate_scan_devices(udev_enum);
+	devices = udev_enumerate_get_list_entry(udev_enum);
+	udev_list_entry_foreach(deventry, devices) {
+		const char* syspath;
+		struct udev_device* dev;
+		syspath = udev_list_entry_get_name(deventry);
+		dev = udev_device_new_from_syspath(udev, syspath);
+		if (dev_is_keyboard_device(dev))
+			input_add(udev_device_get_devnode(dev));
+		udev_device_unref(dev);
+	}
+	udev_enumerate_unref(udev_enum);
+}
+
 int dev_init(void)
 {
 	udev = udev_new();
@@ -53,36 +83,6 @@
 	udev_fd = -1;
 }
 
-static bool dev_is_keyboard_device(struct udev_device* dev)
-{
-	const char *keyboard = udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD");
-
-	if (keyboard && !strcmp(keyboard, "1"))
-		return true;
-
-	return false;
-}
-
-void dev_add_existing_input_devs(void)
-{
-	struct udev_enumerate* udev_enum;
-	struct udev_list_entry* devices, *deventry;
-	udev_enum = udev_enumerate_new(udev);
-	udev_enumerate_add_match_subsystem(udev_enum, "input");
-	udev_enumerate_scan_devices(udev_enum);
-	devices = udev_enumerate_get_list_entry(udev_enum);
-	udev_list_entry_foreach(deventry, devices) {
-		const char* syspath;
-		struct udev_device* dev;
-		syspath = udev_list_entry_get_name(deventry);
-		dev = udev_device_new_from_syspath(udev, syspath);
-		if (dev_is_keyboard_device(dev))
-			input_add(udev_device_get_devnode(dev));
-		udev_device_unref(dev);
-	}
-	udev_enumerate_unref(udev_enum);
-}
-
 void dev_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd)
 {
 	FD_SET(udev_fd, read_set);
diff --git a/dev_lite.c b/dev_lite.c
new file mode 100644
index 0000000..ea58ae4
--- /dev/null
+++ b/dev_lite.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2019 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "dev.h"
+#include "input.h"
+#include "util.h"
+
+/*
+ * We believe that there's a limit of 32 /dev/input/eventX devices in the
+ * kernel. It's not quite clear. Anyway, we should probably never have more than
+ * that in practice on Chrome OS anyway, so our algorithm is to check for the
+ * first 32 nodes explicitly, then keep checking for any further ones until we
+ * find an entry missing. (This is way simpler than using actual POSIX globbing
+ * or directory entry APIs.)
+ */
+#define EVDEV_MAX 32
+
+int dev_init(void)
+{
+	char path[64];
+	int i;
+
+	for (i = 0;; i++) {
+		snprintf(path, sizeof(path), "/dev/input/event%d", i);
+		if (access(path, F_OK)) {
+			if (i >= EVDEV_MAX)
+				break;
+			else
+				continue;
+		}
+		input_add(path);
+	}
+
+	return 0;
+}
+
+void dev_close(void)
+{
+}
+
+void dev_add_fds(fd_set* read_set, fd_set* exception_set, int *maxfd)
+{
+}
+
+void dev_dispatch_io(fd_set* read_set, fd_set* exception_set)
+{
+}
diff --git a/input.c b/input.c
index d67db88..f0f36d7 100644
--- a/input.c
+++ b/input.c
@@ -7,7 +7,6 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <libudev.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>