/* * 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 #include #include #include #include #include /* * 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; }