Skip to content

Instantly share code, notes, and snippets.

@dzeban
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzeban/a1ec00a75b15b3723e16 to your computer and use it in GitHub Desktop.
Save dzeban/a1ec00a75b15b3723e16 to your computer and use it in GitHub Desktop.
keynotifier
#if !defined(_KEYNOTIFIER_H) || defined(TRACE_HEADER_MULTI_READ)
#define _KEYNOTIFIER_H
#include <linux/tracepoint.h>
TRACE_EVENT(keynotifier_press,
TP_PROTO(int value),
TP_ARGS(value),
TP_STRUCT__entry(
__field( int, value )
),
TP_fast_assign(
__entry->value = value;
),
TP_printk("Key value %d", __entry->value)
);
#endif
#undef TRACE_INCLUDE_PATH
#undef TRACE_INCLUDE_FILE
#define TRACE_INCLUDE_PATH .
#define TRACE_INCLUDE_FILE keynotifier-trace
#include <trace/define_trace.h>
/* Keynotifier - keyboard press notifier
*
* Copyright (C) 2014 Alex Dzyoba <avd@reduct.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include "keynotifier.h"
#define CREATE_TRACE_POINTS
#include "keynotifier-trace.h"
// Keyboard notification callback
static int kb_nf_callback(struct notifier_block *nb, unsigned long code, void *_param)
{
struct keyboard_notifier_param *param = NULL;
param = (struct keyboard_notifier_param *)_param;
if (!param)
{
pr_err("Bad keyboard notification\n");
return NOTIFY_BAD;
}
if (param->down && code == KBD_KEYCODE)
{
pr_info("Key: %d\n", param->value);
trace_keynotifier_press(param->value);
}
return NOTIFY_DONE;
}
static void cleanup(void)
{
int rc = 0;
rc = unregister_keyboard_notifier(&kb_nf);
if (rc)
{
pr_err("Failed to unregister keyboard notifier. Error %d\n", rc);
}
return;
}
static void probe_tracepoint(void *ignore, int value)
{
pr_alert("Key value %d\n", value);
}
static int keynotifier_init(void)
{
int rc = 0;
rc = register_trace_keynotifier_press(probe_tracepoint, NULL);
rc = register_keyboard_notifier(&kb_nf);
if (rc)
{
pr_err("Failed to register keyboard notifier. Error %d\n", rc);
cleanup();
return -EFAULT;
}
return 0;
}
static void keynotifier_exit(void)
{
cleanup();
return;
}
module_init(keynotifier_init);
module_exit(keynotifier_exit);
MODULE_AUTHOR("Alex Dzyoba <avd@reduct.ru>");
MODULE_DESCRIPTION("Keyboard press notifier");
MODULE_LICENSE("GPL");
/*
* Keynotifier - keyboard press notifier
*
* Copyright (C) 2014 Alex Dzyoba <avd@reduct.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the init macros */
#include <linux/keyboard.h> /* Needed for */
#include <linux/notifier.h> /* keyboard notifier */
#include <linux/hardirq.h>
// -----------------------------------------------------------------------------
// Keyboard notifier
// -----------------------------------------------------------------------------
static int kb_nf_callback(struct notifier_block *nb, unsigned long code, void *_param);
static struct notifier_block kb_nf = {
.notifier_call = kb_nf_callback
};
CFLAGS_keynotifier.o := -I$(src)
obj-m := keynotifier.o
KDIR := /lib/modules/$(shell uname -r)/build
module:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment