* virtual rfkill button
@ 2009-05-05 0:12 Johannes Berg
2009-05-05 21:48 ` Johannes Berg
0 siblings, 1 reply; 2+ messages in thread
From: Johannes Berg @ 2009-05-05 0:12 UTC (permalink / raw)
To: linux-wireless
Just so I don't lose the code again ...
johannes
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <linux/uinput.h>
int main(int argc, char **argv)
{
struct uinput_user_dev dev;
struct input_event ev;
int fd, ret;
fd = open("/dev/input/uinput", O_WRONLY);
if (fd < 0) {
perror("open");
if (errno == ENOENT)
fprintf(stderr, "Try to \"modprobe uinput\".\n");
return 1;
}
/* set up virtual device */
memset(&dev, 0, sizeof(dev));
strcpy(dev.name, "virt-rfkill");
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_KEYBIT, KEY_WLAN);
ret = write(fd, &dev, sizeof(dev));
if (ret != sizeof(dev)) {
perror("write setup");
return 1;
}
/* register device */
ret = ioctl(fd, UI_DEV_CREATE);
if (ret) {
perror("create");
return 1;
}
/* write event */
memset(&ev, 0, sizeof(ev));
ev.type = EV_KEY;
ev.code = KEY_WLAN;
ret = write(fd, &ev, sizeof(ev));
if (ret != sizeof(ev)) {
perror("write event");
return 1;
}
return 0;
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: virtual rfkill button
2009-05-05 0:12 virtual rfkill button Johannes Berg
@ 2009-05-05 21:48 ` Johannes Berg
0 siblings, 0 replies; 2+ messages in thread
From: Johannes Berg @ 2009-05-05 21:48 UTC (permalink / raw)
To: linux-wireless
[-- Attachment #1.1: Type: text/plain, Size: 271 bytes --]
That didn't work, the attached one does... Also a module to show rfkill
working (for my new API rfkill).
The Makefile for the test module is a simple:
obj-m += fake-rfkill.o
and then you do
make -f /lib/modules/$(uname -r)/build/Makefile M=$(pwd)
johannes
[-- Attachment #1.2: fake-rfkill.c --]
[-- Type: text/x-csrc, Size: 1004 bytes --]
#include <linux/rfkill.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
static struct rfkill *rfk;
static void test_poll(struct rfkill *rfkill, void *data)
{
printk(KERN_DEBUG "poll test rfkill\n");
}
static void test_query(struct rfkill *rfkill, void *data)
{
printk(KERN_DEBUG "query test rfkill\n");
}
static int test_set_block(void *data, bool blocked)
{
printk(KERN_DEBUG "set test rfkill (%s)\n",
blocked ? "blocked" : "active");
return 0;
}
static struct rfkill_ops ops = {
.poll = test_poll,
.query = test_query,
.set_block = test_set_block,
};
int mod_init(void)
{
int err;
rfk = rfkill_alloc("fake", NULL, RFKILL_TYPE_WLAN, &ops, NULL);
if (!rfk)
return -ENOMEM;
err = rfkill_register(rfk);
if (err)
rfkill_destroy(rfk);
return err;
}
module_init(mod_init);
void mod_exit(void)
{
rfkill_unregister(rfk);
rfkill_destroy(rfk);
}
module_exit(mod_exit);
[-- Attachment #1.3: virt-rfkill.c --]
[-- Type: text/x-csrc, Size: 2633 bytes --]
/*
* Copyright (c) 2009 Johannes Berg
* Copyright (c) 2009 Luis R. Rodriguez
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/uinput.h>
#include <unistd.h>
/*
* You will need CONFIG_INPUT_UINPUT enabled, if
* enabled as a module modprobe uinput
*/
int main(int argc, char **argv)
{
struct uinput_user_dev dev;
struct input_event ev;
int fd, ret;
/* Tells the kernel to kmalloc() a uinput device for us */
fd = open("/dev/input/uinput", O_WRONLY | O_NDELAY);
if (fd < 0) {
perror("open uinput");
if (errno == ENOENT)
fprintf(stderr, "Try \"modprobe uinput\".\n");
return 1;
}
memset(&dev, 0, sizeof(dev));
strcpy(dev.name, "virt-rfkill");
/* sets up uinput device, all we need is a name */
ret = write(fd, &dev, sizeof(dev));
if (ret != sizeof(dev)) {
perror("write setup");
return 1;
}
/* sets "EV_KEY" bit on the the uinput dev's evbit */
if (ioctl(fd, UI_SET_EVBIT, EV_KEY)) {
perror("set key event");
return 1;
}
/* sets "KEY_WLAN" bit on the the uinput dev's keybit */
if (ioctl(fd, UI_SET_KEYBIT, KEY_WLAN)) {
perror("set wlan key");
return 1;
}
/* create an input device for uinput device */
ret = ioctl(fd, UI_DEV_CREATE);
if (ret) {
perror("create input device");
return 1;
}
/*
* write events -- once a uinput device has been set up writes
* inject events which show up in the virtual input device.
*/
memset(&ev, 0, sizeof(ev));
ev.type = EV_KEY;
ev.code = KEY_WLAN;
/* button down */
ev.value = 1;
ret = write(fd, &ev, sizeof(ev));
if (ret != sizeof(ev)) {
perror("write event");
return 1;
}
/* and up again - that's a press */
ev.value = 0;
ret = write(fd, &ev, sizeof(ev));
if (ret != sizeof(ev)) {
perror("write event");
return 1;
}
return 0;
}
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 836 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-05-05 21:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-05-05 0:12 virtual rfkill button Johannes Berg
2009-05-05 21:48 ` Johannes Berg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox