diff mbox

[1/3] import acpi-fakekey tool

Message ID 1405066855-17918-1-git-send-email-kengyu@canonical.com
State Rejected
Headers show

Commit Message

Keng-Yu Lin July 11, 2014, 8:20 a.m. UTC
This tool is imported from acpi-support v0.141.
The tool can find the correct input event node of
the keyboard and send a keycode to it. The keycode
value is specified as an parameter of the command.

Signed-off-by: Keng-Yu Lin <kengyu@canonical.com>
---
 debian/copyright             |  8 +++++
 debian/fwts.install          |  1 +
 debian/rules                 |  3 ++
 src/Makefile.am              |  3 +-
 src/utilities/Makefile.am    |  4 ++-
 src/utilities/acpi_fakekey.c | 77 ++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 94 insertions(+), 2 deletions(-)
 create mode 100644 src/utilities/acpi_fakekey.c

Comments

Colin Ian King July 11, 2014, 9:49 a.m. UTC | #1
On 11/07/14 09:20, Keng-Yu Lin wrote:
> This tool is imported from acpi-support v0.141.
> The tool can find the correct input event node of
> the keyboard and send a keycode to it. The keycode
> value is specified as an parameter of the command.
> 
> Signed-off-by: Keng-Yu Lin <kengyu@canonical.com>
> ---
>  debian/copyright             |  8 +++++
>  debian/fwts.install          |  1 +
>  debian/rules                 |  3 ++
>  src/Makefile.am              |  3 +-
>  src/utilities/Makefile.am    |  4 ++-
>  src/utilities/acpi_fakekey.c | 77 ++++++++++++++++++++++++++++++++++++++++++++
>  6 files changed, 94 insertions(+), 2 deletions(-)
>  create mode 100644 src/utilities/acpi_fakekey.c
> 
> diff --git a/debian/copyright b/debian/copyright
> index eaefaee..a3f1a9c 100644
> --- a/debian/copyright
> +++ b/debian/copyright
> @@ -153,3 +153,11 @@ License: other
>     United States government or any agency thereof requires an export license,
>     other governmental approval, or letter of assurance, without first obtaining
>     such license, approval or letter.
> +
> +Files: src/utilities/acpi_fakekey.c
> +Copyright: 2004-2014, Canonical Ltd
> +           2004, Matthew Garrett <mjg59@srcf.ucam.org>
> +           2004-2014, Thom May <thom@canonical.com>
> +License: GPL-2
> + On Debian systems, the complete text of the GNU General Public
> + License can be found in `/usr/share/common-licenses/GPL-2'.
> diff --git a/debian/fwts.install b/debian/fwts.install
> index 2630f4e..8400417 100644
> --- a/debian/fwts.install
> +++ b/debian/fwts.install
> @@ -4,3 +4,4 @@ usr/bin/fwts usr/bin
>  usr/share/man/man1/fwts.1 usr/share/man/man1
>  scripts/fwts-collect usr/bin
>  usr/share/man/man1/fwts-collect.1 usr/share/man/man1
> +usr/lib/fwts/acpi_fakekey usr/lib/fwts
> diff --git a/debian/rules b/debian/rules
> index 0887c38..5011334 100755
> --- a/debian/rules
> +++ b/debian/rules
> @@ -21,6 +21,9 @@ override_dh_auto_install:
>  override_dh_dkms:
>  	dh_dkms -V $(VERSION)
>  
> +override_dh_auto_configure:
> +	dh_auto_configure -- --libexecdir=/usr/lib/fwts
> +
>  override_dh_auto_test:
>  ifneq ($(DEB_BUILD_ARCH),arm64)
>  	dh_auto_test $@
> diff --git a/src/Makefile.am b/src/Makefile.am
> index ace23d6..eff1e31 100644
> --- a/src/Makefile.am
> +++ b/src/Makefile.am
> @@ -11,7 +11,8 @@ AM_CPPFLAGS = \
>  	-I$(top_srcdir)/src/acpica/source/include \
>  	-I$(top_srcdir)/src/acpica/source/compiler \
>  	-I$(top_srcdir)/efi_runtime \
> -	-Wall -Werror -Wextra
> +	-Wall -Werror -Wextra \
> +	-DLIBEXECDIR='"$(libexecdir)"'
>  
>  bin_PROGRAMS = fwts
>  
> diff --git a/src/utilities/Makefile.am b/src/utilities/Makefile.am
> index 01b534a..ecededa 100644
> --- a/src/utilities/Makefile.am
> +++ b/src/utilities/Makefile.am
> @@ -1,6 +1,8 @@
>  AM_CPPFLAGS = -Wall -Werror -Wextra -DDATAROOTDIR=\"$(datarootdir)\"
>  
>  bin_PROGRAMS = kernelscan
> +libexec_PROGRAMS = acpi_fakekey
>  kernelscan_SOURCES = kernelscan.c
>  kernelscan_LDFLAGS = -lpcre
> -
> +acpi_fakekey_SOURCES = acpi_fakekey.c
> +acpi_fakekey_CFLAGS = -w
> diff --git a/src/utilities/acpi_fakekey.c b/src/utilities/acpi_fakekey.c
> new file mode 100644
> index 0000000..a2f3f85
> --- /dev/null
> +++ b/src/utilities/acpi_fakekey.c
> @@ -0,0 +1,77 @@
> +#include <unistd.h>
> +#include <fcntl.h>
> +#include <string.h>
> +#include <stdlib.h>
> +#include <stdio.h>
> +#include <linux/input.h>
> +
> +#define TestBit(bit, array) (array[(bit) / 8] & (1 << ((bit) % 8)))
> +
> +int find_keyboard() {
> +	int i, j;
> +        int fd;
> +        char filename[32];
> +        char key_bitmask[(KEY_MAX + 7) / 8];
> +
> +        for (i=0; i<32; i++) {
> +                snprintf(filename,sizeof(filename), "/dev/input/event%d", i);
> +
> +                fd = open(filename, O_RDWR);
> +		if (fd < 0) {
> +			perror("open");
> +			exit(EXIT_FAILURE);
> +		}
> +
> +                ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
> +
> +		/* We assume that anything that has an alphabetic key in the
> +		   QWERTYUIOP range in it is the main keyboard. */
> +		for (j = KEY_Q; j <= KEY_P; j++) {
> +			if (TestBit(j, key_bitmask))
> +				return fd;
> +		}
> +
> +                close (fd);
> +        }
> +        return 0;
> +}
> +
> +int main(int argc, char** argv) {
> +	int fd;
> +	int key;
> +	struct input_event event;
> +
> +	if (argc == 2) {
> +		key = atoi(argv[1]);
> +	} else {
> +		return 1;
> +	}
> +
> +	fd = find_keyboard();
> +
> +	if (!fd) {
> +		return 2;
> +	}
> +	event.type = EV_SYN;
> +	event.code = SYN_REPORT;
> +	event.value = 0;
> +	write(fd, &event, sizeof event);
> +
> +	event.type = EV_KEY;
> +	event.code = key;
> +	event.value = 1;
> +	write(fd, &event, sizeof event);
> +
> +	event.type = EV_KEY;
> +	event.code = key;
> +	event.value = 0;
> +	write(fd, &event, sizeof event);
> +
> +	event.type = EV_SYN;
> +	event.code = SYN_REPORT;
> +	event.value = 0;
> +	write(fd, &event, sizeof event);
> +
> +	return 0;
> +}
> +
> 

Rather than just pulling in this code as an external binrary and then
exec'ing it, why not put the functionality into libfwts and remove the
need to exec this small program.  I'm keen to remove the need to exec
anything unless absolutely necessary.

Colin
Keng-Yu Lin July 14, 2014, 5:09 a.m. UTC | #2
@Colin,
  I have no preference on either way. I am happy to work on another
patch to make the code in libfwts.

On Fri, Jul 11, 2014 at 5:49 PM, Colin Ian King
<colin.king@canonical.com> wrote:
> On 11/07/14 09:20, Keng-Yu Lin wrote:
>> This tool is imported from acpi-support v0.141.
>> The tool can find the correct input event node of
>> the keyboard and send a keycode to it. The keycode
>> value is specified as an parameter of the command.
>>
>> Signed-off-by: Keng-Yu Lin <kengyu@canonical.com>
>> ---
>>  debian/copyright             |  8 +++++
>>  debian/fwts.install          |  1 +
>>  debian/rules                 |  3 ++
>>  src/Makefile.am              |  3 +-
>>  src/utilities/Makefile.am    |  4 ++-
>>  src/utilities/acpi_fakekey.c | 77 ++++++++++++++++++++++++++++++++++++++++++++
>>  6 files changed, 94 insertions(+), 2 deletions(-)
>>  create mode 100644 src/utilities/acpi_fakekey.c
>>
>> diff --git a/debian/copyright b/debian/copyright
>> index eaefaee..a3f1a9c 100644
>> --- a/debian/copyright
>> +++ b/debian/copyright
>> @@ -153,3 +153,11 @@ License: other
>>     United States government or any agency thereof requires an export license,
>>     other governmental approval, or letter of assurance, without first obtaining
>>     such license, approval or letter.
>> +
>> +Files: src/utilities/acpi_fakekey.c
>> +Copyright: 2004-2014, Canonical Ltd
>> +           2004, Matthew Garrett <mjg59@srcf.ucam.org>
>> +           2004-2014, Thom May <thom@canonical.com>
>> +License: GPL-2
>> + On Debian systems, the complete text of the GNU General Public
>> + License can be found in `/usr/share/common-licenses/GPL-2'.
>> diff --git a/debian/fwts.install b/debian/fwts.install
>> index 2630f4e..8400417 100644
>> --- a/debian/fwts.install
>> +++ b/debian/fwts.install
>> @@ -4,3 +4,4 @@ usr/bin/fwts usr/bin
>>  usr/share/man/man1/fwts.1 usr/share/man/man1
>>  scripts/fwts-collect usr/bin
>>  usr/share/man/man1/fwts-collect.1 usr/share/man/man1
>> +usr/lib/fwts/acpi_fakekey usr/lib/fwts
>> diff --git a/debian/rules b/debian/rules
>> index 0887c38..5011334 100755
>> --- a/debian/rules
>> +++ b/debian/rules
>> @@ -21,6 +21,9 @@ override_dh_auto_install:
>>  override_dh_dkms:
>>       dh_dkms -V $(VERSION)
>>
>> +override_dh_auto_configure:
>> +     dh_auto_configure -- --libexecdir=/usr/lib/fwts
>> +
>>  override_dh_auto_test:
>>  ifneq ($(DEB_BUILD_ARCH),arm64)
>>       dh_auto_test $@
>> diff --git a/src/Makefile.am b/src/Makefile.am
>> index ace23d6..eff1e31 100644
>> --- a/src/Makefile.am
>> +++ b/src/Makefile.am
>> @@ -11,7 +11,8 @@ AM_CPPFLAGS = \
>>       -I$(top_srcdir)/src/acpica/source/include \
>>       -I$(top_srcdir)/src/acpica/source/compiler \
>>       -I$(top_srcdir)/efi_runtime \
>> -     -Wall -Werror -Wextra
>> +     -Wall -Werror -Wextra \
>> +     -DLIBEXECDIR='"$(libexecdir)"'
>>
>>  bin_PROGRAMS = fwts
>>
>> diff --git a/src/utilities/Makefile.am b/src/utilities/Makefile.am
>> index 01b534a..ecededa 100644
>> --- a/src/utilities/Makefile.am
>> +++ b/src/utilities/Makefile.am
>> @@ -1,6 +1,8 @@
>>  AM_CPPFLAGS = -Wall -Werror -Wextra -DDATAROOTDIR=\"$(datarootdir)\"
>>
>>  bin_PROGRAMS = kernelscan
>> +libexec_PROGRAMS = acpi_fakekey
>>  kernelscan_SOURCES = kernelscan.c
>>  kernelscan_LDFLAGS = -lpcre
>> -
>> +acpi_fakekey_SOURCES = acpi_fakekey.c
>> +acpi_fakekey_CFLAGS = -w
>> diff --git a/src/utilities/acpi_fakekey.c b/src/utilities/acpi_fakekey.c
>> new file mode 100644
>> index 0000000..a2f3f85
>> --- /dev/null
>> +++ b/src/utilities/acpi_fakekey.c
>> @@ -0,0 +1,77 @@
>> +#include <unistd.h>
>> +#include <fcntl.h>
>> +#include <string.h>
>> +#include <stdlib.h>
>> +#include <stdio.h>
>> +#include <linux/input.h>
>> +
>> +#define TestBit(bit, array) (array[(bit) / 8] & (1 << ((bit) % 8)))
>> +
>> +int find_keyboard() {
>> +     int i, j;
>> +        int fd;
>> +        char filename[32];
>> +        char key_bitmask[(KEY_MAX + 7) / 8];
>> +
>> +        for (i=0; i<32; i++) {
>> +                snprintf(filename,sizeof(filename), "/dev/input/event%d", i);
>> +
>> +                fd = open(filename, O_RDWR);
>> +             if (fd < 0) {
>> +                     perror("open");
>> +                     exit(EXIT_FAILURE);
>> +             }
>> +
>> +                ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
>> +
>> +             /* We assume that anything that has an alphabetic key in the
>> +                QWERTYUIOP range in it is the main keyboard. */
>> +             for (j = KEY_Q; j <= KEY_P; j++) {
>> +                     if (TestBit(j, key_bitmask))
>> +                             return fd;
>> +             }
>> +
>> +                close (fd);
>> +        }
>> +        return 0;
>> +}
>> +
>> +int main(int argc, char** argv) {
>> +     int fd;
>> +     int key;
>> +     struct input_event event;
>> +
>> +     if (argc == 2) {
>> +             key = atoi(argv[1]);
>> +     } else {
>> +             return 1;
>> +     }
>> +
>> +     fd = find_keyboard();
>> +
>> +     if (!fd) {
>> +             return 2;
>> +     }
>> +     event.type = EV_SYN;
>> +     event.code = SYN_REPORT;
>> +     event.value = 0;
>> +     write(fd, &event, sizeof event);
>> +
>> +     event.type = EV_KEY;
>> +     event.code = key;
>> +     event.value = 1;
>> +     write(fd, &event, sizeof event);
>> +
>> +     event.type = EV_KEY;
>> +     event.code = key;
>> +     event.value = 0;
>> +     write(fd, &event, sizeof event);
>> +
>> +     event.type = EV_SYN;
>> +     event.code = SYN_REPORT;
>> +     event.value = 0;
>> +     write(fd, &event, sizeof event);
>> +
>> +     return 0;
>> +}
>> +
>>
>
> Rather than just pulling in this code as an external binrary and then
> exec'ing it, why not put the functionality into libfwts and remove the
> need to exec this small program.  I'm keen to remove the need to exec
> anything unless absolutely necessary.
>
> Colin
>
> --
> fwts-devel mailing list
> fwts-devel@lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/fwts-devel
diff mbox

Patch

diff --git a/debian/copyright b/debian/copyright
index eaefaee..a3f1a9c 100644
--- a/debian/copyright
+++ b/debian/copyright
@@ -153,3 +153,11 @@  License: other
    United States government or any agency thereof requires an export license,
    other governmental approval, or letter of assurance, without first obtaining
    such license, approval or letter.
+
+Files: src/utilities/acpi_fakekey.c
+Copyright: 2004-2014, Canonical Ltd
+           2004, Matthew Garrett <mjg59@srcf.ucam.org>
+           2004-2014, Thom May <thom@canonical.com>
+License: GPL-2
+ On Debian systems, the complete text of the GNU General Public
+ License can be found in `/usr/share/common-licenses/GPL-2'.
diff --git a/debian/fwts.install b/debian/fwts.install
index 2630f4e..8400417 100644
--- a/debian/fwts.install
+++ b/debian/fwts.install
@@ -4,3 +4,4 @@  usr/bin/fwts usr/bin
 usr/share/man/man1/fwts.1 usr/share/man/man1
 scripts/fwts-collect usr/bin
 usr/share/man/man1/fwts-collect.1 usr/share/man/man1
+usr/lib/fwts/acpi_fakekey usr/lib/fwts
diff --git a/debian/rules b/debian/rules
index 0887c38..5011334 100755
--- a/debian/rules
+++ b/debian/rules
@@ -21,6 +21,9 @@  override_dh_auto_install:
 override_dh_dkms:
 	dh_dkms -V $(VERSION)
 
+override_dh_auto_configure:
+	dh_auto_configure -- --libexecdir=/usr/lib/fwts
+
 override_dh_auto_test:
 ifneq ($(DEB_BUILD_ARCH),arm64)
 	dh_auto_test $@
diff --git a/src/Makefile.am b/src/Makefile.am
index ace23d6..eff1e31 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -11,7 +11,8 @@  AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/acpica/source/include \
 	-I$(top_srcdir)/src/acpica/source/compiler \
 	-I$(top_srcdir)/efi_runtime \
-	-Wall -Werror -Wextra
+	-Wall -Werror -Wextra \
+	-DLIBEXECDIR='"$(libexecdir)"'
 
 bin_PROGRAMS = fwts
 
diff --git a/src/utilities/Makefile.am b/src/utilities/Makefile.am
index 01b534a..ecededa 100644
--- a/src/utilities/Makefile.am
+++ b/src/utilities/Makefile.am
@@ -1,6 +1,8 @@ 
 AM_CPPFLAGS = -Wall -Werror -Wextra -DDATAROOTDIR=\"$(datarootdir)\"
 
 bin_PROGRAMS = kernelscan
+libexec_PROGRAMS = acpi_fakekey
 kernelscan_SOURCES = kernelscan.c
 kernelscan_LDFLAGS = -lpcre
-
+acpi_fakekey_SOURCES = acpi_fakekey.c
+acpi_fakekey_CFLAGS = -w
diff --git a/src/utilities/acpi_fakekey.c b/src/utilities/acpi_fakekey.c
new file mode 100644
index 0000000..a2f3f85
--- /dev/null
+++ b/src/utilities/acpi_fakekey.c
@@ -0,0 +1,77 @@ 
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <linux/input.h>
+
+#define TestBit(bit, array) (array[(bit) / 8] & (1 << ((bit) % 8)))
+
+int find_keyboard() {
+	int i, j;
+        int fd;
+        char filename[32];
+        char key_bitmask[(KEY_MAX + 7) / 8];
+
+        for (i=0; i<32; i++) {
+                snprintf(filename,sizeof(filename), "/dev/input/event%d", i);
+
+                fd = open(filename, O_RDWR);
+		if (fd < 0) {
+			perror("open");
+			exit(EXIT_FAILURE);
+		}
+
+                ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
+
+		/* We assume that anything that has an alphabetic key in the
+		   QWERTYUIOP range in it is the main keyboard. */
+		for (j = KEY_Q; j <= KEY_P; j++) {
+			if (TestBit(j, key_bitmask))
+				return fd;
+		}
+
+                close (fd);
+        }
+        return 0;
+}
+
+int main(int argc, char** argv) {
+	int fd;
+	int key;
+	struct input_event event;
+
+	if (argc == 2) {
+		key = atoi(argv[1]);
+	} else {
+		return 1;
+	}
+
+	fd = find_keyboard();
+
+	if (!fd) {
+		return 2;
+	}
+	event.type = EV_SYN;
+	event.code = SYN_REPORT;
+	event.value = 0;
+	write(fd, &event, sizeof event);
+
+	event.type = EV_KEY;
+	event.code = key;
+	event.value = 1;
+	write(fd, &event, sizeof event);
+
+	event.type = EV_KEY;
+	event.code = key;
+	event.value = 0;
+	write(fd, &event, sizeof event);
+
+	event.type = EV_SYN;
+	event.code = SYN_REPORT;
+	event.value = 0;
+	write(fd, &event, sizeof event);
+
+	return 0;
+}
+