├── .gitignore ├── AUTHORS ├── COPYING ├── Makefile ├── README ├── cmdsocket.c ├── cmdsocket.h ├── command.h ├── devices.c ├── devices.h ├── devtag.h ├── eventnames.c ├── eventnames.h ├── eventtable.h ├── ignore.c ├── ignore.h ├── keystate.c ├── keystate.h ├── obey.c ├── obey.h ├── systemd ├── triggerhappy.service └── triggerhappy.socket ├── th-cmd.c ├── th-cmd.pod ├── thd.c ├── thd.h ├── thd.pod ├── trigger.c ├── trigger.h ├── triggerhappy.conf.examples ├── triggerparser.c ├── triggerparser.h ├── udev └── triggerhappy-udev.rules ├── uinput.c ├── uinput.h └── version.inc /.gitignore: -------------------------------------------------------------------------------- 1 | thd 2 | thd.1 3 | th-cmd 4 | th-cmd.1 5 | *.o 6 | /*.d 7 | evtable_*.inc 8 | linux_input_defs_gen.inc 9 | version.h 10 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Stefan Tomanek 2 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PREFIX:=/usr/ 2 | DESTDIR:=/ 3 | BINDIR:=$(DESTDIR)/$(PREFIX)/sbin/ 4 | MANDIR:=$(DESTDIR)/$(PREFIX)/share/man/man1/ 5 | 6 | PKGCONFIG = pkg-config 7 | HAVE_PKGCONFIG = $(shell $(PKGCONFIG) --version 2>/dev/null || echo no) 8 | ifneq ($(HAVE_PKGCONFIG),no) 9 | HAVE_SYSTEMD = $(shell $(PKGCONFIG) --exists libsystemd && echo 1 || echo 0) 10 | ifeq ($(HAVE_SYSTEMD),1) 11 | CPPFLAGS += -DHAVE_SYSTEMD=1 12 | CFLAGS += $(shell $(PKGCONFIG) --cflags libsystemd) 13 | LDLIBS += $(shell $(PKGCONFIG) --libs libsystemd) 14 | endif 15 | endif 16 | 17 | VERSION:=$(shell cat version.inc) 18 | 19 | THD_COMPS := thd keystate trigger eventnames devices cmdsocket obey ignore uinput triggerparser 20 | THCMD_COMPS := th-cmd cmdsocket 21 | 22 | MAKEDEPEND = $(CC) -M -MG $(CPPFLAGS) -o $*.d $< 23 | 24 | all: thd th-cmd man 25 | 26 | man: thd.1 th-cmd.1 27 | 28 | thd: $(THD_COMPS:%=%.o) 29 | 30 | th-cmd: $(THCMD_COMPS:%=%.o) 31 | 32 | %.1: %.pod 33 | pod2man \ 34 | --center="Triggerhappy daemon" \ 35 | --section=1 \ 36 | --release="$(VERSION)" \ 37 | $< > $@ 38 | 39 | linux_input_defs_gen.inc: 40 | echo "#include " | $(CC) $(CPPFLAGS) -dM -E - > $@ 41 | 42 | evtable_%.inc: linux_input_defs_gen.inc 43 | awk '/^#define $*_/ && $$2 !~ /_(MIN_INTERESTING|MAX|CNT|VERSION)$$/ {print "EV_MAP("$$2"),"}' $< > $@ 44 | 45 | version.h: version.inc 46 | sed -r 's!(.*)!#define TH_VERSION "\1"!' $< > $@ 47 | 48 | clean: 49 | rm -f *.d 50 | rm -f *.o 51 | rm -f linux_input_defs_gen.inc 52 | rm -f evtable_*.inc 53 | rm -f version.h 54 | rm -f thd th-cmd 55 | rm -f thd.1 th-cmd.1 56 | 57 | install: all 58 | install -D thd $(BINDIR)/thd 59 | install -D th-cmd $(BINDIR)/th-cmd 60 | install -D thd.1 $(MANDIR)/thd.1 61 | install -D th-cmd.1 $(MANDIR)/th-cmd.1 62 | 63 | %.d : %.c 64 | $(MAKEDEPEND) 65 | 66 | -include $(THD_COMPS:%=%.d) $(THCMD_COMPS:%=%.d) 67 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Triggerhappy 2 | - a lightweight hotkey daemon - 3 | 4 | by Stefan Tomanek 5 | http://github.com/wertarbyte/triggerhappy/ 6 | 7 | Triggerhappy is a hotkey daemon developed with small and embedded systems in 8 | mind, e.g. linux based routers. It attaches to the input device files and 9 | interprets the event data received and executes scripts configured in its 10 | configuration. 11 | 12 | Examples: 13 | 14 | Dump all events received through two device files: 15 | 16 | thd --dump /dev/input/event0 /dev/input/event1 17 | 18 | Event handlers can be defined in configuration files (specified by --triggers). 19 | If a directory is specified, all configuration files within matching the 20 | pattern *.conf are loaded. The files use the following format: 21 | 22 | 23 | 24 | e.g. 25 | 26 | KEY_VOLUMEUP 1 /usr/bin/amixer set Master 5%+ 27 | KEY_VOLUMEUP 2 /usr/bin/amixer set Master 5%+ 28 | KEY_VOLUMEDOWN 1 /usr/bin/amixer set Master 5%- 29 | KEY_VOLUMEDOWN 2 /usr/bin/amixer set Master 5%- 30 | 31 | A value of 1 corresponds to pressing a key, while 2 is generated by 32 | holding it; releasing it yields a value of 0. 33 | 34 | It is also possible to append up to 5 modifiers to each event definition by 35 | appending their names to the event name: 36 | 37 | KEY_VOLUMEUP+KEY_LEFTSHIFT 1 /usr/bin/amixer set Master 15%+ 38 | 39 | Devices can be added dynamically by specifying a command socket: 40 | 41 | thd --socket /var/run/triggerhappy.socket --triggers /etc/triggerhappy.d/ 42 | 43 | The program th-cmd is used to add new devices to the daemon: 44 | 45 | th-cmd --socket /var/run/triggerhappy.socket --add /dev/input/event0 46 | th-cmd --socket /var/run/triggerhappy.socket --remove /dev/input/event3 47 | 48 | An example udev script is also included to facilitate this hotplug 49 | support - for this use, th-cmd can handle the parameter "udev" to 50 | deduce the device name and action from the udev environment. 51 | 52 | For more information, consult the manual pages of "thd" and "th-cmd". 53 | -------------------------------------------------------------------------------- /cmdsocket.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #ifdef HAVE_SYSTEMD 19 | #include 20 | #endif 21 | 22 | #include "devtag.h" 23 | #include "devices.h" 24 | #include "command.h" 25 | #include "cmdsocket.h" 26 | 27 | int bind_cmdsocket( char *name ) { 28 | int cmd_fd; 29 | 30 | #ifdef HAVE_SYSTEMD 31 | if (sd_listen_fds(0) == 1) { 32 | fprintf(stderr, SD_DEBUG "Found socket passed from systemd\n"); 33 | return SD_LISTEN_FDS_START + 0; 34 | } 35 | #endif 36 | 37 | struct sockaddr_un addr; 38 | /* remove any stale files */ 39 | struct stat sb; 40 | int serr = stat(name, &sb); 41 | if ( !serr && S_ISSOCK(sb.st_mode)) { 42 | unlink(name); 43 | } 44 | cmd_fd = socket(AF_UNIX, SOCK_DGRAM, 0); 45 | strcpy(addr.sun_path, name); 46 | addr.sun_family = AF_UNIX; 47 | bind (cmd_fd, (struct sockaddr *) &addr, 48 | strlen(addr.sun_path) + sizeof (addr.sun_family)); 49 | return cmd_fd; 50 | } 51 | 52 | int connect_cmdsocket( char *name ) { 53 | int fd; 54 | struct sockaddr_un server; 55 | fd = socket(AF_UNIX, SOCK_DGRAM, 0); 56 | strcpy(server.sun_path, name); 57 | server.sun_family = AF_UNIX; 58 | connect(fd, (struct sockaddr *) &server, 59 | strlen(server.sun_path) + sizeof (server.sun_family)); 60 | return fd; 61 | } 62 | 63 | struct command *read_command( int cmd_fd ) { 64 | struct command *cmd = malloc(sizeof(struct command)); 65 | if (! cmd) { 66 | fprintf(stderr, "Unable to allocate memory for command!\n"); 67 | return NULL; 68 | } 69 | 70 | int fd[1] = {-1}; 71 | char buffer[CMSG_SPACE(sizeof fd)]; 72 | 73 | struct iovec v = { 74 | .iov_base = cmd, 75 | .iov_len = sizeof(*cmd) 76 | }; 77 | struct msghdr msg = { 78 | .msg_iov = &v, 79 | .msg_iovlen = 1, 80 | .msg_control = buffer, 81 | .msg_controllen = sizeof(buffer) 82 | }; 83 | 84 | int done = recvmsg( cmd_fd, &msg, 0 ); 85 | 86 | if (done == -1) { 87 | fprintf(stderr, "Error reading command."); 88 | free(cmd); 89 | return NULL; 90 | } 91 | struct cmsghdr *cmessage = CMSG_FIRSTHDR(&msg); 92 | if (cmessage) { 93 | memcpy(fd, CMSG_DATA(cmessage), sizeof fd); 94 | /* place FD back in the command message */ 95 | cmd->fd = (int) fd[0]; 96 | } 97 | return cmd; 98 | } 99 | 100 | int send_command( int cmd_fd, enum command_type type, char *param, int passfd, int exclusive, char *tag ) { 101 | if (type == CMD_ADD && passfd == 1) { 102 | type = CMD_PASSFD; 103 | } 104 | struct command cmd = { 105 | .fd = -1, 106 | .exclusive = exclusive, 107 | .type = type, 108 | .param = {0}, 109 | .tag = {0} 110 | }; 111 | if (param != NULL) { 112 | strncpy(cmd.param, param, TH_COMMAND_PARAM_LENGTH); 113 | cmd.param[TH_COMMAND_PARAM_LENGTH-1] = '\0'; 114 | } 115 | if (tag != NULL) { 116 | strncpy(cmd.tag, tag, TH_DEVICE_TAG_LENGTH); 117 | cmd.tag[TH_DEVICE_TAG_LENGTH-1] = '\0'; 118 | } 119 | 120 | struct iovec v = { 121 | .iov_base = &cmd, 122 | .iov_len = sizeof(cmd) 123 | }; 124 | struct msghdr m = { 125 | .msg_iov = &v, 126 | .msg_iovlen = 1 127 | }; 128 | 129 | /* add FD */ 130 | int dev_fd[1] = { -1 }; 131 | char buffer[CMSG_SPACE(sizeof(dev_fd))]; 132 | if (passfd) { 133 | int fd = open( param, O_RDONLY ); 134 | if (fd < 0) { 135 | perror("open"); 136 | return 1; 137 | } 138 | dev_fd[0] = fd ; 139 | m.msg_control = buffer; 140 | m.msg_controllen = sizeof(buffer); 141 | 142 | struct cmsghdr *cmessage = CMSG_FIRSTHDR(&m); 143 | cmessage->cmsg_level = SOL_SOCKET; 144 | cmessage->cmsg_type = SCM_RIGHTS; 145 | cmessage->cmsg_len = CMSG_LEN(sizeof(dev_fd)); 146 | 147 | m.msg_controllen = cmessage->cmsg_len; 148 | memcpy(CMSG_DATA(cmessage), dev_fd, sizeof dev_fd); 149 | } 150 | int done = sendmsg( cmd_fd, &m, 0 ); 151 | return (done == -1); 152 | } 153 | -------------------------------------------------------------------------------- /cmdsocket.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | int bind_cmdsocket(char *name); 8 | struct command *read_command(int cmd_fd); 9 | int send_command(int cmd_fd, enum command_type type, char *param, int passfd, int exclusive, char *tag); 10 | int connect_cmdsocket(char *name); 11 | -------------------------------------------------------------------------------- /command.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include "devtag.h" 8 | #define TH_COMMAND_PARAM_LENGTH 256 9 | 10 | enum command_type { 11 | CMD_NOP, 12 | CMD_ADD, 13 | CMD_REMOVE, 14 | CMD_QUIT, 15 | CMD_DISABLE, 16 | CMD_ENABLE, 17 | CMD_PASSFD, 18 | CMD_CLEARDEVS, 19 | CMD_CHANGEMODE 20 | }; 21 | 22 | struct command { 23 | enum command_type type; 24 | /* udev pathnames are long, but not that long */ 25 | char param[TH_COMMAND_PARAM_LENGTH]; 26 | int fd; 27 | int exclusive; 28 | char tag[TH_DEVICE_TAG_LENGTH]; 29 | }; 30 | -------------------------------------------------------------------------------- /devices.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "devices.h" 21 | #include "eventnames.h" 22 | 23 | #define BITFIELD uint32_t 24 | 25 | static device *device_list = NULL; 26 | 27 | char *get_device_description(int fd) { 28 | char descr[256] = "Unknown"; 29 | if(ioctl(fd, EVIOCGNAME(sizeof(descr)), descr) < 0) { 30 | perror("evdev ioctl"); 31 | } 32 | return strdup(descr); 33 | } 34 | 35 | static __inline__ int test_bit(int nr, BITFIELD * addr) { 36 | BITFIELD mask; 37 | 38 | addr += nr >> 5; 39 | mask = 1 << (nr & 0x1f); 40 | return ((mask & *addr) != 0); 41 | } 42 | 43 | int device_is_suitable(int fd) { 44 | BITFIELD bits[32]; 45 | int rc = ioctl(fd, EVIOCGBIT(0,sizeof(bits)), bits); 46 | return rc > 0 && ( 47 | /* we only consider devices with keys or switches suitable */ 48 | test_bit(EV_KEY, bits) || test_bit(EV_SW, bits) 49 | ); 50 | } 51 | 52 | void add_device(char *dev, int fd, int excl, char *tag) { 53 | device **p = &device_list; 54 | // find end of list 55 | while (*p != NULL) { 56 | p = &( (*p)->next ); 57 | } 58 | if (fd < 0) { 59 | fd = open( dev, O_RDONLY ); 60 | } 61 | if (fd >= 0) { 62 | if (! device_is_suitable(fd) ) { 63 | fprintf(stderr, "Device %s not suitable.\n", dev); 64 | close(fd); 65 | return; 66 | } 67 | /* grab device exclusively */ 68 | if (excl) { 69 | if (ioctl(fd, EVIOCGRAB, 1) == -1) { 70 | fprintf(stderr, "Device %s cannot be grabbed exclusively.\n", dev); 71 | close(fd); 72 | return; 73 | } 74 | } 75 | *p = malloc(sizeof(device)); 76 | if (*p == NULL) goto nomem_cleanup; 77 | memset(*p, 0, sizeof(device)); 78 | (*p)->devname = strdup(dev); 79 | if ((*p)->devname == NULL) goto nomem_cleanup; 80 | (*p)->fd = fd; 81 | (*p)->exclusive = excl; 82 | if (tag) { 83 | strncpy((*p)->tag, tag, TH_DEVICE_TAG_LENGTH); 84 | (*p)->tag[TH_DEVICE_TAG_LENGTH-1] = '\0'; 85 | } 86 | (*p)->next = NULL; 87 | } else { 88 | fprintf( stderr, "Error opening '%s': %s\n", dev, strerror(errno) ); 89 | } 90 | return; 91 | nomem_cleanup: 92 | fprintf(stderr, "Unable to allocate memory for new device!\n"); 93 | if (*p) { 94 | free( (*p)->devname ); 95 | } 96 | free(*p); 97 | *p = NULL; 98 | } 99 | 100 | int remove_device(char *dev) { 101 | device **p = &device_list; 102 | while (*p != NULL) { 103 | if ( strcmp( (*p)->devname, dev ) == 0 ) { 104 | device *copy = *p; 105 | /* bypass the list item */ 106 | *p = copy->next; 107 | close(copy->fd); 108 | free(copy->devname); 109 | free(copy); 110 | return 1; 111 | } 112 | /* advance to the next node */ 113 | p = &( (*p)->next ); 114 | } 115 | /* reached the end of the list */ 116 | return 0; 117 | } 118 | 119 | void clear_devices(void) { 120 | device *p = device_list; 121 | while (p != NULL) { 122 | device *next = p->next; 123 | close(p->fd); 124 | free(p->devname); 125 | free(p); 126 | p = next; 127 | } 128 | device_list = NULL; 129 | } 130 | 131 | int count_devices(void) { 132 | int n = 0; 133 | device *p = device_list; 134 | while (p != NULL) { 135 | n++; 136 | p = p->next; 137 | } 138 | return n; 139 | } 140 | 141 | void for_each_device( void(*func)(device*) ) { 142 | device *p = device_list; 143 | while (p != NULL) { 144 | func( p ); 145 | p = p->next; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /devices.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include "devtag.h" 8 | 9 | typedef struct device { 10 | int fd; 11 | char *devname; 12 | int exclusive; 13 | char tag[TH_DEVICE_TAG_LENGTH]; 14 | struct device *next; 15 | } device; 16 | 17 | void add_device(char *dev, int fd, int excl, char *tag); 18 | int remove_device(char *dev); 19 | void clear_devices(void); 20 | int count_devices(void); 21 | 22 | void for_each_device( void(*func)(device*) ); 23 | 24 | char *get_device_description(int fd); 25 | -------------------------------------------------------------------------------- /devtag.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #define TH_DEVICE_TAG_LENGTH 256 8 | -------------------------------------------------------------------------------- /eventnames.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include "eventnames.h" 11 | #include "eventtable.h" 12 | 13 | const char *lookup_event_name_i( const int evtype, const int evcode ) { 14 | switch(evtype) { 15 | case EV_KEY: 16 | return (KEY_MAX >= evcode ? KEY_NAME[ evcode ] : NULL); 17 | case EV_SW: 18 | return (SW_MAX >= evcode ? SW_NAME[ evcode ] : NULL); 19 | case EV_REL: 20 | return (REL_MAX >= evcode ? REL_NAME[ evcode ] : NULL); 21 | case EV_ABS: 22 | return (ABS_MAX >= evcode ? ABS_NAME[ evcode ] : NULL); 23 | default: 24 | return NULL; 25 | } 26 | } 27 | 28 | const char *lookup_event_name( const struct input_event ev ) { 29 | return lookup_event_name_i( ev.type, ev.code ); 30 | } 31 | 32 | const char *lookup_type_name_i( const int evtype ) { 33 | return (EV_MAX >= evtype ? EV_NAME[ evtype ] : NULL); 34 | } 35 | 36 | const char *lookup_type_name( const struct input_event ev ) { 37 | return lookup_type_name_i( ev.type ); 38 | } 39 | 40 | static int lookup_index(const char **map, const char *value, int max) { 41 | int i = 0; 42 | while (i <= max) { 43 | if (map[i] != NULL && 44 | strcmp(value, map[i]) == 0) { 45 | return i; 46 | } 47 | i++; 48 | } 49 | return -1; 50 | } 51 | 52 | int lookup_event_code( const char *eventname ) { 53 | int res = -1; 54 | if (res == -1) res = lookup_index(KEY_NAME, eventname, KEY_MAX); 55 | if (res == -1) res = lookup_index(SW_NAME, eventname, SW_MAX); 56 | if (res == -1) res = lookup_index(REL_NAME, eventname, REL_MAX); 57 | if (res == -1) res = lookup_index(ABS_NAME, eventname, ABS_MAX); 58 | return res; 59 | } 60 | 61 | int lookup_event_type( const char *eventname ) { 62 | if (lookup_index(KEY_NAME, eventname, KEY_MAX) != -1) { 63 | return EV_KEY; 64 | } 65 | if (lookup_index(SW_NAME, eventname, SW_MAX) != -1) { 66 | return EV_SW; 67 | } 68 | if (lookup_index(REL_NAME, eventname, REL_MAX) != -1) { 69 | return EV_REL; 70 | } 71 | if (lookup_index(ABS_NAME, eventname, ABS_MAX) != -1) { 72 | return EV_ABS; 73 | } 74 | return -1; 75 | } 76 | -------------------------------------------------------------------------------- /eventnames.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | 9 | const char *lookup_event_name_i(const int evtype, const int evcode); 10 | const char *lookup_event_name(const struct input_event ev); 11 | 12 | const char *lookup_type_name_i(const int evtype); 13 | const char *lookup_type_name(const struct input_event ev); 14 | 15 | int lookup_event_code(const char *eventname); 16 | int lookup_event_type(const char *eventname); 17 | -------------------------------------------------------------------------------- /eventtable.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | 9 | #define EV_MAP( N ) [ N ] = #N 10 | 11 | static const char *EV_NAME[EV_CNT] = { 12 | #include "evtable_EV.inc" 13 | }; 14 | 15 | static const char *KEY_NAME[KEY_CNT] = { 16 | #include "evtable_KEY.inc" 17 | #include "evtable_BTN.inc" 18 | }; 19 | 20 | static const char *REL_NAME[REL_CNT] = { 21 | #include "evtable_REL.inc" 22 | }; 23 | 24 | static const char *ABS_NAME[ABS_CNT] = { 25 | #include "evtable_ABS.inc" 26 | }; 27 | 28 | static const char *SW_NAME[SW_CNT] = { 29 | #include "evtable_SW.inc" 30 | }; 31 | -------------------------------------------------------------------------------- /ignore.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include "ignore.h" 10 | 11 | void ignore_key( int code, ignore **list ) { 12 | ignore **p = list; 13 | while ( *p != NULL ) { 14 | p = &( (*p)->next ); 15 | } 16 | *p = malloc( sizeof(ignore) ); 17 | if (*p) { 18 | (*p)->next = NULL; 19 | (*p)->code = code; 20 | } else { 21 | fprintf(stderr, "Unable to allocate memory for ignored key!\n"); 22 | } 23 | } 24 | 25 | void print_ignores( ignore *list ) { 26 | ignore *p = list; 27 | while ( p != NULL ) { 28 | p = p->next; 29 | } 30 | } 31 | 32 | int is_ignored( int code, ignore *list ) { 33 | ignore *p = list; 34 | while ( p != NULL ) { 35 | if (p->code == code) { 36 | return 1; 37 | } 38 | p = p->next; 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /ignore.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | typedef struct ignore { 8 | int code; 9 | struct ignore *next; 10 | } ignore; 11 | 12 | void ignore_key( int code, ignore **list ); 13 | int is_ignored( int code, ignore *list ); 14 | void clear_ignore_list( ignore **list ); 15 | -------------------------------------------------------------------------------- /keystate.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "eventnames.h" 12 | #include "keystate.h" 13 | 14 | void init_keystate_holder(keystate_holder **ksh) { 15 | if (*ksh == NULL) { 16 | *ksh = malloc(sizeof(keystate_holder)); 17 | } 18 | if (*ksh == NULL) { 19 | fprintf(stderr, "Unable to allocate memory for keystate table!\n"); 20 | return; 21 | } 22 | int i; 23 | for (i=0; i<=KEY_MAX; i++) { 24 | (**ksh)[i] = 0; 25 | } 26 | } 27 | 28 | /* 29 | * Keep track of a pressed or released key 30 | */ 31 | void change_keystate( keystate_holder ksh, struct input_event ev ) { 32 | if (ev.type != EV_KEY) 33 | return; 34 | if (ev.code > KEY_MAX) 35 | return; 36 | switch(ev.value) { 37 | case 1: // pressed 38 | ksh[ev.code]++; 39 | break; 40 | case 0: // released 41 | if (ksh[ev.code] > 0) { 42 | ksh[ev.code]--; 43 | } 44 | break; 45 | } 46 | } 47 | 48 | char *get_keystate(keystate_holder ksh) { 49 | return get_keystate_ignore_key( ksh, -1 ); 50 | } 51 | 52 | char *get_keystate_ignore_key(keystate_holder ksh, int ignore_key) { 53 | const int bsize = 1024; 54 | char *KS = "+"; 55 | 56 | char buf[bsize]; 57 | buf[0] = '\0'; 58 | 59 | int i; 60 | int n = 0; 61 | for (i=0; i<=KEY_MAX; i++) { 62 | if (i != ignore_key && ksh[i] > 0) { 63 | if (n>0) { 64 | strncat( buf, KS, bsize-1-strlen(KS) ); 65 | } 66 | const char *name = lookup_event_name_i(EV_KEY, i); 67 | strncat( &(buf[0]), name, bsize-1-strlen(buf) ); 68 | n++; 69 | } 70 | } 71 | char *res = malloc( sizeof(char) * (1+strlen(buf)) ); 72 | if (res) { 73 | strcpy( res, buf ); 74 | } 75 | return res; 76 | } 77 | -------------------------------------------------------------------------------- /keystate.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | typedef unsigned short keystate_holder[KEY_MAX+1]; 9 | 10 | void init_keystate_holder(keystate_holder**); 11 | void change_keystate(keystate_holder, struct input_event); 12 | char *get_keystate(keystate_holder); 13 | char *get_keystate_ignore_key(keystate_holder, int ignore_key); 14 | -------------------------------------------------------------------------------- /obey.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include "devices.h" 9 | #include "command.h" 10 | #include "keystate.h" 11 | #include "trigger.h" 12 | #include "thd.h" 13 | 14 | int obey_command( struct command *cmd ) { 15 | if (cmd->type == CMD_ADD) { 16 | add_device( cmd->param, -1, cmd->exclusive, cmd->tag ); 17 | return 0; 18 | } 19 | if (cmd->type == CMD_PASSFD) { 20 | add_device( cmd->param, cmd->fd, cmd->exclusive, cmd->tag ); 21 | return 0; 22 | } 23 | if (cmd->type == CMD_REMOVE) { 24 | remove_device( cmd->param ); 25 | return 0; 26 | } 27 | if (cmd->type == CMD_QUIT) { 28 | cleanup(); 29 | return 0; 30 | } 31 | if (cmd->type == CMD_ENABLE) { 32 | triggers_enabled(1); 33 | return 0; 34 | } 35 | if (cmd->type == CMD_DISABLE) { 36 | triggers_enabled(0); 37 | return 0; 38 | } 39 | if (cmd->type == CMD_CLEARDEVS) { 40 | clear_devices(); 41 | return 0; 42 | } 43 | if (cmd->type == CMD_CHANGEMODE) { 44 | change_trigger_mode( cmd->param ); 45 | return 0; 46 | } 47 | return 1; 48 | } 49 | -------------------------------------------------------------------------------- /obey.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | int obey_command( struct command *cmd ); 8 | -------------------------------------------------------------------------------- /systemd/triggerhappy.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=triggerhappy global hotkey daemon 3 | After=local-fs.target 4 | 5 | [Service] 6 | Type=notify 7 | ExecStart=/usr/sbin/thd --triggers /etc/triggerhappy/triggers.d/ --socket /run/thd.socket --user nobody --deviceglob /dev/input/event* 8 | 9 | [Install] 10 | WantedBy=multi-user.target 11 | -------------------------------------------------------------------------------- /systemd/triggerhappy.socket: -------------------------------------------------------------------------------- 1 | [Socket] 2 | ListenStream=/run/thd.socket 3 | 4 | [Install] 5 | WantedBy=sockets.target 6 | -------------------------------------------------------------------------------- /th-cmd.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "devices.h" 18 | #include "command.h" 19 | #include "cmdsocket.h" 20 | 21 | #include "version.h" 22 | 23 | void show_help(void) { 24 | fprintf( stderr, "Triggerhappy command " TH_VERSION "\n"); 25 | fprintf( stderr, "Use:\n"); 26 | fprintf( stderr, " th-cmd --socket [--grab] [--passfd] --add \n"); 27 | fprintf( stderr, " th-cmd --socket --remove \n"); 28 | fprintf( stderr, " th-cmd --socket --clear\n"); 29 | fprintf( stderr, " th-cmd --socket --enable\n"); 30 | fprintf( stderr, " th-cmd --socket --disable\n"); 31 | fprintf( stderr, " th-cmd --socket --mode \n"); 32 | fprintf( stderr, " th-cmd --socket --quit\n"); 33 | fprintf( stderr, " th-cmd --socket --help\n"); 34 | } 35 | 36 | int main(int argc, char *argv[]) { 37 | char *socket = NULL; 38 | static int passfd = 0; 39 | static int grab_dev = 0; 40 | static int op_add = 0; 41 | static int op_rem = 0; 42 | static int op_clear = 0; 43 | static int op_udev = 0; 44 | static int op_en = 0; 45 | static int op_dis = 0; 46 | static int op_mode = 0; 47 | static int op_quit = 0; 48 | enum command_type ctype = CMD_NOP; 49 | 50 | char *tag = NULL; 51 | 52 | static struct option long_options[] = { 53 | { "socket", 1, 0, 's' }, 54 | { "add", 0, &op_add, 1 }, 55 | { "remove", 0, &op_rem, 1 }, 56 | { "clear", 0, &op_clear, 1 }, 57 | { "udev", 0, &op_udev, 1 }, 58 | { "passfd", 0, &passfd, 1 }, 59 | { "grab", 0, &grab_dev, 1 }, 60 | { "enable", 0, &op_en, 1 }, 61 | { "disable", 0, &op_dis, 1 }, 62 | { "mode", 0, &op_mode, 1 }, 63 | { "quit", 0, &op_quit, 1 }, 64 | { "help", 0, 0, 'h' }, 65 | { "tag", required_argument, 0, 't' }, 66 | { 0, 0, 0, 0 } 67 | }; 68 | 69 | int c; 70 | while (1) { 71 | int option_index = 0; 72 | c = getopt_long(argc, argv, "s:t:", long_options, &option_index); 73 | if (c == -1) { 74 | break; 75 | } 76 | switch(c) { 77 | case 's': 78 | socket = optarg; 79 | break; 80 | case 't': 81 | tag = optarg; 82 | break; 83 | case 'h': 84 | case '?': 85 | case -1: 86 | show_help(); 87 | return 1; 88 | } 89 | } 90 | 91 | if ( op_add + op_rem + op_clear + op_udev + op_en + op_dis + op_mode + op_quit != 1 ) { 92 | fprintf(stderr, "A single command must be specified!\n"); 93 | show_help(); 94 | return 1; 95 | } 96 | 97 | if (! socket ) { 98 | fprintf(stderr, "No socket specified!\n"); 99 | show_help(); 100 | return 1; 101 | } 102 | int s = connect_cmdsocket( socket ); 103 | if (s < 0) { 104 | perror("connect()"); 105 | return 1; 106 | } 107 | 108 | int err = 0; 109 | if (op_udev) { 110 | if (strcasecmp("add", getenv("ACTION")) == 0) { 111 | ctype = CMD_ADD; 112 | } else if (strcasecmp("remove", getenv("ACTION")) == 0) { 113 | ctype = CMD_REMOVE; 114 | } 115 | char *dev = getenv("DEVNAME"); 116 | if ( ctype && dev ) { 117 | err = send_command( s, ctype, dev, passfd, grab_dev, tag ); 118 | } 119 | } else { 120 | /* get devices from command line */ 121 | if (op_add) ctype = CMD_ADD; 122 | else if (op_rem) ctype = CMD_REMOVE; 123 | else if (op_clear) ctype = CMD_CLEARDEVS; 124 | else if (op_en) ctype = CMD_ENABLE; 125 | else if (op_dis) ctype = CMD_DISABLE; 126 | else if (op_mode) ctype = CMD_CHANGEMODE; 127 | else if (op_quit) ctype = CMD_QUIT; 128 | 129 | if ( ctype == CMD_NOP) { 130 | show_help(); 131 | } 132 | switch (ctype) { 133 | case CMD_CLEARDEVS: 134 | case CMD_ENABLE: 135 | case CMD_DISABLE: 136 | case CMD_QUIT: 137 | err = send_command( s, ctype, "", 0, 0, tag); 138 | break; 139 | case CMD_CHANGEMODE: 140 | err = send_command( s, ctype, 141 | (optind < argc) ? argv[optind] : "", 142 | 0, 0, tag ); 143 | break; 144 | case CMD_ADD: 145 | case CMD_REMOVE: 146 | while (optind < argc && err == 0) { 147 | err = send_command( s, ctype, argv[optind++], passfd, grab_dev, tag ); 148 | } 149 | break; 150 | default: 151 | err = 1; 152 | } 153 | if (err != 0) { 154 | perror("send_command"); 155 | } 156 | } 157 | close(s); 158 | return err; 159 | } 160 | -------------------------------------------------------------------------------- /th-cmd.pod: -------------------------------------------------------------------------------- 1 | =pod 2 | 3 | =head1 NAME 4 | 5 | th-cmd -- triggerhappy commander 6 | 7 | =head1 SYNOPSIS 8 | 9 | B --socket B<< >> [B<--passfd>] [B<--tag> I<< >>] [B<--clear>|B<--enable>|B<--disable>|B<--mode> I<< >>|B<--quit>|B<--udev>|B<--add> F<< >>|B<--remove> F<< >>] 10 | 11 | =head1 DESCRIPTION 12 | 13 | This program is used to issue commands to a running triggerhappy daemon. It utilizes an unix domain socket bound by the daemon. 14 | 15 | =head1 OPTIONS 16 | 17 | =over 18 | 19 | =item B<--socket> F 20 | 21 | The socket file opened by the running triggerhappy daemon instance. 22 | 23 | =item B<--add> F<< >> 24 | 25 | Instruct the running daemon to open the input devices specified on the command line 26 | 27 | =item B<--remove> F<< >> 28 | 29 | Remove specified devices from the running daemon. 30 | 31 | =item B<--clear> 32 | 33 | Remove all devices from the running daemon. 34 | 35 | =item B<--udev> 36 | 37 | Deduce operation and device name from udev environment (for use in udev rules). 38 | 39 | =item B<--disable> 40 | 41 | Disable the execution of triggers. 42 | 43 | =item B<--enable> 44 | 45 | Re-enable the execution of triggers. 46 | 47 | =item B<--mode> I<< >> 48 | 49 | Change the mode of the triggerhappy daemon to I<< >>. If no new mode is specified, the daemon switches to default mode. 50 | 51 | =item B<--quit> 52 | 53 | Terminate the triggerhappy daemon. 54 | 55 | =item B<--passfd> 56 | 57 | Instead of instructing the daemon to open the device, open the device and pass the file descriptor to the daemon. This allows the adding of new devices to a daemon having dropped its privileges, however the th-cmd process must have access to the device file. 58 | 59 | =item B<--grab> 60 | 61 | Grab the device; the triggerhappy daemon will try to get exclusive access to the device, other applications will not receive the events emitted by it. 62 | 63 | =item B<--tag> F 64 | 65 | Label the added device with the tag I<< >>; this can be used to limit the scope of event 66 | handlers to a subset of input devices. 67 | 68 | =back 69 | 70 | =head1 AUTHOR 71 | 72 | Stefan Tomanek Estefan.tomanek+th@wertarbyte.deE 73 | 74 | -------------------------------------------------------------------------------- /thd.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #ifdef HAVE_SYSTEMD 29 | #include 30 | #endif 31 | 32 | #include "thd.h" 33 | #include "eventnames.h" 34 | #include "devices.h" 35 | #include "keystate.h" 36 | #include "trigger.h" 37 | #include "command.h" 38 | #include "cmdsocket.h" 39 | #include "obey.h" 40 | #include "ignore.h" 41 | #include "uinput.h" 42 | 43 | /* version information */ 44 | #include "version.h" 45 | 46 | /* command channel & FD */ 47 | static char *cmd_file = NULL; 48 | static int cmd_fd = -1; 49 | 50 | static char *uinput_dev = NULL; 51 | 52 | static int dump_events = 0; 53 | static int run_as_daemon = 0; 54 | 55 | static char *triggerfile = NULL; 56 | static char *pidfile = NULL; 57 | 58 | static keystate_holder *keystate = NULL; 59 | 60 | static ignore *ignored_keys = NULL; 61 | 62 | static int normalize_events = 0; 63 | 64 | static char *user = NULL; 65 | 66 | static int exiting = 0; 67 | static int reload_conf = 0; 68 | 69 | /* forward declarations */ 70 | static int reload_triggerfile(); 71 | 72 | /* FDs to watch */ 73 | static fd_set rfds; 74 | static int max_fd = -1; 75 | 76 | /* 77 | * Look up event and key names and print them to STDOUT 78 | */ 79 | static void print_event(char* dev, struct input_event ev) { 80 | const char *typename = lookup_type_name( ev ); 81 | const char *evname = lookup_event_name( ev ); 82 | if ( evname != NULL ) { 83 | printf( "%s\t%s\t%d\t%s\n", typename, evname, ev.value, dev ); 84 | } else { 85 | fprintf( stderr, "Unknown %s event id on %s : %d (value %d)\n", typename, dev, ev.code, ev.value ); 86 | } 87 | fflush(stdout); 88 | } 89 | 90 | static void print_triggerline(struct input_event ev, keystate_holder ksh) { 91 | const char *evname = lookup_event_name( ev ); 92 | char *state = ( ev.type == EV_KEY ? get_keystate_ignore_key( ksh, ev.code ) : NULL); 93 | const int d = (state && strlen(state)>0); 94 | if ( evname != NULL ) { 95 | printf( "# %s%s%s\t%d\tcommand\n", state?state:"", d?"+":"", evname, ev.value ); 96 | fflush(stdout); 97 | } 98 | free(state); 99 | } 100 | 101 | /* 102 | * Read event from fd, decode it and pass it to handlers 103 | */ 104 | static int read_event( device *dev ) { 105 | int fd = dev->fd; 106 | char *devname = dev->devname; 107 | struct input_event ev; 108 | int n = read( fd, &ev, sizeof(ev) ); 109 | if ( n != sizeof(ev) ) { 110 | fprintf(stderr, "Error reading device '%s'\n", dev->devname); 111 | return 1; 112 | } 113 | /* ignore all events except KEY, SW and REL*/ 114 | if (ev.type == EV_KEY || ev.type == EV_SW || ev.type == EV_REL || ev.type == EV_ABS) { 115 | if (ev.type == EV_KEY && is_ignored( ev.code, ignored_keys)) { 116 | return 0; 117 | } 118 | if ((ev.type == EV_REL || ev.type == EV_ABS) && normalize_events) { 119 | if (ev.value > 0) { 120 | ev.value = 1; 121 | } else if (ev.value < 0) { 122 | ev.value = -1; 123 | } 124 | } 125 | if (dump_events) { 126 | print_event( devname, ev ); 127 | print_triggerline( ev, *keystate ); 128 | } 129 | run_triggers( ev.type, ev.code, ev.value, *keystate, dev ); 130 | change_keystate( *keystate, ev ); 131 | } 132 | return 0; 133 | } 134 | 135 | static void check_device( device *d ) { 136 | int fd = d->fd; 137 | if (FD_ISSET( fd, &rfds )) { 138 | if (read_event( d )) { 139 | /* read error? Remove the device! */ 140 | remove_device( d->devname ); 141 | } 142 | } 143 | } 144 | 145 | static void process_devices(void) { 146 | for_each_device( &check_device ); 147 | } 148 | 149 | static void add_device_to_fdset( device *d ) { 150 | int fd = d->fd; 151 | if (fd > max_fd) { 152 | max_fd = fd; 153 | } 154 | FD_SET( fd, &rfds ); 155 | } 156 | 157 | 158 | static void process_events(void) { 159 | struct timeval tv; 160 | int retval; 161 | /* loop as long as we have at least one device or 162 | * the command channel 163 | */ 164 | while ( count_devices() > 0 || cmd_fd != -1 ) { 165 | FD_ZERO( &rfds ); 166 | max_fd = -1; 167 | /* add device fds */ 168 | for_each_device( &add_device_to_fdset ); 169 | /* add command channel */ 170 | if (cmd_fd != -1) { 171 | FD_SET( cmd_fd, &rfds ); 172 | max_fd = cmd_fd > max_fd ? cmd_fd : max_fd; 173 | } 174 | tv.tv_sec = 5; 175 | tv.tv_usec = 0; 176 | retval = select(max_fd+1, &rfds, NULL, NULL, &tv); 177 | if (retval == -1 && errno != EINTR) { 178 | perror("select()"); 179 | continue; 180 | } else if (exiting) { 181 | break; 182 | } else if (reload_conf) { 183 | reload_conf = 0; 184 | reload_triggerfile(); 185 | continue; 186 | } else if (retval) { 187 | process_devices(); 188 | if ( cmd_fd != -1 && FD_ISSET( cmd_fd, &rfds ) ) { 189 | struct command *cmd = read_command( cmd_fd ); 190 | obey_command( cmd ); 191 | free(cmd); 192 | } 193 | } 194 | } 195 | } 196 | 197 | static int write_pidfile( char *pidfile ) { 198 | FILE *pid = fopen( pidfile, "w+" ); 199 | if (pid == NULL) { 200 | return 1; 201 | } 202 | fprintf(pid, "%u\n", (unsigned int) getpid()); 203 | return fclose(pid); 204 | } 205 | 206 | static struct option long_options[] = { 207 | {"dump", no_argument, &dump_events, 1}, 208 | {"daemon", no_argument, &run_as_daemon, 1}, 209 | {"user", required_argument, 0, 'u'}, 210 | {"pidfile", required_argument, 0, 'p'}, 211 | {"triggers", required_argument, 0, 't'}, 212 | {"socket", required_argument, 0, 's'}, 213 | {"ignore", required_argument, 0, 'i'}, 214 | {"normalize", no_argument, &normalize_events, 1}, 215 | {"help", no_argument, 0, 'h'}, 216 | {"uinput", required_argument, 0, '<'}, 217 | {"listevents", no_argument, 0, 'l'}, 218 | {"deviceglob", required_argument, 0, 'g'}, 219 | {0,0,0,0} /* end of list */ 220 | }; 221 | 222 | void show_help(void) { 223 | printf( "Triggerhappy event daemon " TH_VERSION "\n\n" ); 224 | printf( "Usage:\n" ); 225 | printf( " thd [switches] [devices]\n\n" ); 226 | printf( "Command line switches:\n" ); 227 | printf( " --help Display this help message\n" ); 228 | printf( " --daemon Run as daemon process\n" ); 229 | printf( " --dump Dump events to console\n" ); 230 | printf( " --listevents Print a list of all known events\n" ); 231 | printf( " --triggers Load trigger definitions from \n" ); 232 | printf( " --socket Read commands from socket\n" ); 233 | printf( " --ignore Ignore key events with name \n" ); 234 | printf( " --normalize Normalize relative movement events\n" ); 235 | printf( " --user Drop privileges to after opening devices\n" ); 236 | printf( " --deviceglob

Open device files matching pattern

\n" ); 237 | } 238 | 239 | static void list_event_table(int type, int max) { 240 | int n = 0; 241 | for (n = 0; n <= max; n++) { 242 | const char *name = lookup_event_name_i(type, n); 243 | if (name) { 244 | printf("%s\n", name); 245 | } 246 | } 247 | } 248 | 249 | static void list_events(void) { 250 | list_event_table(EV_KEY, KEY_MAX); 251 | list_event_table(EV_SW, SW_MAX); 252 | list_event_table(EV_REL, REL_MAX); 253 | list_event_table(EV_ABS, REL_MAX); 254 | } 255 | 256 | void cleanup(void) { 257 | close_uinput(); 258 | if (cmd_file) { 259 | if (cmd_fd != -1) { 260 | close( cmd_fd ); 261 | } 262 | unlink(cmd_file); 263 | cmd_fd = -1; 264 | } 265 | clear_devices(); 266 | if (pidfile) { 267 | unlink(pidfile); 268 | } 269 | } 270 | 271 | static int reload_triggerfile(void) { 272 | clear_triggers(); 273 | if (triggerfile) { 274 | int err = read_triggers(triggerfile); 275 | if (err) { 276 | fprintf(stderr, "Error loading triggerfile '%s'\n", triggerfile); 277 | return 1; 278 | } 279 | } 280 | return 0; 281 | } 282 | 283 | static void handle_signal(int sig) { 284 | switch (sig) { 285 | case SIGINT: 286 | case SIGTERM: 287 | exiting = 1; 288 | break; 289 | case SIGHUP: 290 | reload_conf = 1; 291 | break; 292 | } 293 | } 294 | 295 | int start_readers(int argc, char *argv[], int start, char *dev_glob) { 296 | if (argc-start < 1 && cmd_file == NULL && dev_glob == NULL) { 297 | fprintf(stderr, "No input device files or command pipe specified.\n"); 298 | return 1; 299 | } 300 | /* open command pipe */ 301 | if (cmd_file) { 302 | cmd_fd = bind_cmdsocket(cmd_file); 303 | if (cmd_fd < 0) { 304 | return 1; 305 | } 306 | } 307 | 308 | /* add every device file supplied on command line */ 309 | int i; 310 | for (i=start; ipw_uid; 345 | int gid = pw->pw_gid; 346 | if ( initgroups(user, gid) ) { 347 | perror("initgroups"); 348 | return 1; 349 | } 350 | if ( setregid( gid, gid ) ) { 351 | perror("setregid"); 352 | return 1; 353 | } 354 | if ( setreuid( uid, uid ) ) { 355 | perror("setreuid"); 356 | return 1; 357 | } 358 | } else { 359 | fprintf(stderr, "Unable to lookup user '%s'.\n", user); 360 | return 1; 361 | } 362 | } 363 | 364 | #ifdef HAVE_SYSTEMD 365 | sd_notify(0, "READY=1"); 366 | #endif 367 | 368 | process_events(); 369 | 370 | return 0; 371 | } 372 | 373 | int main(int argc, char *argv[]) { 374 | signal(SIGCHLD, SIG_IGN); 375 | char *dev_glob = NULL; 376 | int option_index = 0; 377 | int c; 378 | while (1) { 379 | c = getopt_long (argc, argv, "t:s:dhpni:u:g:", long_options, &option_index); 380 | if (c == -1) { 381 | break; 382 | } 383 | switch (c) { 384 | case 0: 385 | if (long_options[option_index].flag != 0) { 386 | break; 387 | } 388 | printf ("option %s", long_options[option_index].name); 389 | if (optarg) 390 | printf (" with arg %s", optarg); 391 | printf ("\n"); 392 | break; 393 | case 'd': /* short for --dump */ 394 | dump_events = 1; 395 | break; 396 | case 'p': /* short for --pidfile */ 397 | pidfile = optarg; 398 | break; 399 | case 't': 400 | triggerfile = optarg; 401 | break; 402 | case 's': 403 | cmd_file = optarg; 404 | break; 405 | case 'i': 406 | if ( lookup_event_type(optarg) == EV_KEY ) { 407 | ignore_key( lookup_event_code(optarg), &ignored_keys ); 408 | } 409 | break; 410 | case 'h': 411 | show_help(); 412 | return 0; 413 | case 'l': 414 | list_events(); 415 | return 0; 416 | case 'u': 417 | user = optarg; 418 | break; 419 | case '<': 420 | uinput_dev = optarg; 421 | break; 422 | case 'g': 423 | dev_glob = optarg; 424 | break; 425 | case '?': 426 | default: 427 | return 1; 428 | } 429 | } 430 | /* load config file */ 431 | if (reload_triggerfile() != 0) { 432 | return 1; 433 | } 434 | /* init keystate holder */ 435 | init_keystate_holder(&keystate); 436 | /* set initial trigger mode */ 437 | change_trigger_mode(""); 438 | /* open uinput if requested */ 439 | if (uinput_dev != NULL && open_uinput(uinput_dev) == -1) { 440 | fprintf(stderr, "Error setting up uinput support\n"); 441 | return 1; 442 | } 443 | /* install signal handler */ 444 | struct sigaction handler; 445 | handler.sa_handler = handle_signal; 446 | sigfillset(&handler.sa_mask); 447 | handler.sa_flags=0; 448 | sigaction(SIGINT,&handler,0); 449 | sigaction(SIGTERM,&handler,0); 450 | sigaction(SIGHUP,&handler,0); 451 | 452 | int status = start_readers(argc, argv, optind, dev_glob); 453 | cleanup(); 454 | return status; 455 | } 456 | 457 | -------------------------------------------------------------------------------- /thd.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | void cleanup(void); 8 | -------------------------------------------------------------------------------- /thd.pod: -------------------------------------------------------------------------------- 1 | =pod 2 | 3 | =head1 NAME 4 | 5 | thd -- triggerhappy global hotkey daemon 6 | 7 | =head1 SYNOPSIS 8 | 9 | B [B<--help>] [B<--user> I] [B<--listevents>] [B<--dump>] [B<--socket> I] [B<--triggers> I] [B<--daemon>] [B<--pidfile> I] [B<--uinput> I][B<--ignore> I] [B<--deviceglob> I] [I] 10 | 11 | =head1 DESCRIPTION 12 | 13 | Triggerhappy is a hotkey daemon that operates on a system wide scale. It watches all configured input devices 14 | for key, switch or button events and can launch arbitrary commands specified by the administrator. In contrast 15 | to hotkey services provided by desktop environments, Triggerhappy is especially suited to hardware related switches 16 | like volume or wifi control; it works independently from a specific user being logged in and is also suitable for embedded systems that do not a graphical user interface. 17 | 18 | =head1 OPTIONS 19 | 20 | =over 21 | 22 | =item B<--help> 23 | 24 | Shows usage instructions 25 | 26 | =item B<--listevents> 27 | 28 | Prints a list of all known event names. 29 | 30 | =item B<--triggers> F 31 | 32 | Read trigger definitions from F, which can either be a file or a directory. If a directory is specified, all its files matching the pattern *.conf are loaded. 33 | 34 | =item B<--dump> 35 | 36 | Dump all recognized events to STDOUT. This can also be utilized to create a skeleton trigger configuration by redirecting the printed configuration lines to a configuration file, adding the desired command and activating the generated line by removing the comment mark "#" at its beginning: 37 | 38 | thd --dump /dev/input/event* | grep ^# > /etc/triggerhappy/triggers.d/skeleton.conf 39 | 40 | =item B<--socket> F 41 | 42 | Open a unix domain socket at F; this socket can be used to send commands to the running daemon (by using the program L), e.g. for adding or removing devices. 43 | 44 | =item B<--daemon> 45 | 46 | Run as a background daemon and detach from the terminal. 47 | 48 | =item B<--pidfile> F 49 | 50 | Write PID to F. 51 | 52 | =item B<--uinput> F 53 | 54 | Open uinput file (probably F) to generate synthetic events. 55 | 56 | =item B<--ignore> I 57 | 58 | Ignore key and switch event labeled I. This can be used to suppress the FN key on some notebooks which only generates events sometimes and might screw up key combinations. 59 | 60 | =item B<--normalize> 61 | 62 | Normalize REL and ABS events. If this option is enabled, the values of axis movement events are mapped to the three values -1, 0 and 1, depending on the sign of the value reported by the kernel. 63 | 64 | =item B<--user> I 65 | 66 | Change to user id I after opening files. This usually prevents thd from opening additional input devices, unless they are opened by the th-cmd program and their file descriptor are passed to the daemon. 67 | 68 | =item B<--deviceglob> I 69 | 70 | Open device files matching the glob I. 71 | 72 | =back 73 | 74 | Additional command line arguments are considered filenames of input devices. 75 | 76 | =head1 CONFIGURATION 77 | 78 | 79 | =head2 Configuring event handlers 80 | 81 | The hotkey bindings used by Triggerhappy are set in the configuration file specified by B<--triggers>. Each line consists of three segments: 82 | The symbolic name of the key or event name to react on, the value carried by the expected event, and of course the command to be launched. 83 | 84 | The event names can be identified by operating the desired key or switch while running the triggerhappy daemon with the option B<--dump>. 85 | 86 | Key events carry the value I<1> for a key being pressed and transmit the payload I<0> when it is released; holding the key down constantly yields events with a value of I<2>. 87 | 88 | The command can include any number of arguments. Please include the full path to avoid trouble through different $PATH settings for the daemon and your interactive session. 89 | 90 | The three fields are separated by an arbitrary number of whitespaces, while anything behind a # character is ignored and considered a comment. 91 | 92 | =head1 EXAMPLE 93 | 94 | =head2 Starting the daemon 95 | 96 | B 97 | 98 | Dump all events processable by thd to the console; this is useful to find out the correct event name for a specific key. 99 | 100 | B 101 | 102 | Read from all currently connected input devices and process events according to the files in F. 103 | 104 | B 105 | 106 | Do not open any input devices yet, but bind the socket F for L to connect to. 107 | 108 | =head2 Configuration files 109 | 110 | Any number of event handlers can be placed in the configuration file: 111 | 112 | # /etc/triggerhappy/triggers.d/suspend.conf 113 | # 114 | # Suspend the system 115 | KEY_SLEEP 1 /usr/sbin/hibernate-ram 116 | KEY_SLEEP+KEY_LEFTSHIFT 1 /usr/sbin/hibernate-disk 117 | 118 | # /etc/triggerhappy/triggers.d/audio.conf 119 | # Change mixer volume when pressing the appropriate keys (or holding them) 120 | KEY_VOLUMEUP 1 /usr/bin/amixer set Master 5%+ 121 | KEY_VOLUMEUP 2 /usr/bin/amixer set Master 5%+ 122 | KEY_VOLUMEDOWN 1 /usr/bin/amixer set Master 5%- 123 | KEY_VOLUMEDOWN 2 /usr/bin/amixer set Master 5%- 124 | 125 | In more complex situations, triggerhappy can provide multiple modes that map a 126 | single event to different keys; triggers are bound to a specific mode by appending 127 | its name to the event name: 128 | 129 | KEY_KPPLUS@media 1 /usr/bin/mpc next 130 | KEY_KPMINUS@media 1 /usr/bin/mpc prev 131 | 132 | These two lines will only cause mpc to be called if the triggerhappy daemon is in 133 | "media" mode. Changing the mode can be achieved by placing a special trigger inside 134 | the configuration: 135 | 136 | KEY_F12@ 1 @media 137 | KEY_F12@media 1 @ 138 | 139 | These two lines make the F12 key toggle between the (nameless) default mode and the 140 | newly defined media mode. 141 | 142 | Triggers with "@" appended are only executed if the specified mode is active; all other 143 | triggers are enabled in every mode. 144 | 145 | When launched with the option B<--uinput>, triggerhappy can be used to generate 146 | synthetic events. A virtual input device is created which emits specified key 147 | presses once a special trigger is reached: 148 | 149 | KEY_KPASTERISK 1 and supply it with a tag; this 158 | tag can then be used to limit the scope of a trigger to this device: 159 | 160 | KEY_ENTER 1 /usr/bin/mpc stop 161 | 162 | Defining the hotkey in this way will only trigger the handler if the event is received 163 | from a device tagged "remote". 164 | 165 | =head1 AUTHOR 166 | 167 | Stefan Tomanek Estefan.tomanek+th@wertarbyte.deE 168 | 169 | -------------------------------------------------------------------------------- /trigger.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "eventnames.h" 17 | #include "keystate.h" 18 | #include "devices.h" 19 | #include "trigger.h" 20 | #include "uinput.h" 21 | 22 | #include "triggerparser.h" 23 | 24 | static trigger *trigger_list = NULL; 25 | 26 | static int triggers_are_enabled = 1; 27 | 28 | static char *trigger_mode = NULL; 29 | 30 | char *get_trigger_mode() { 31 | return trigger_mode; 32 | } 33 | 34 | void change_trigger_mode(const char *mode) { 35 | if (trigger_mode != NULL) { 36 | free(trigger_mode); 37 | /* suppress the message on the first change to default mode */ 38 | fprintf(stderr, "Changing trigger mode to <%s>\n", mode); 39 | } 40 | trigger_mode = strdup(mode); 41 | } 42 | 43 | void triggers_enabled( int status ) { 44 | triggers_are_enabled = status; 45 | } 46 | 47 | void append_trigger(trigger *t) { 48 | trigger **p = &trigger_list; 49 | while (*p != NULL) { 50 | p = &( (*p)->next ); 51 | } 52 | *p = t; 53 | } 54 | 55 | static int read_triggerfile(const char *filename) { 56 | FILE *conf; 57 | size_t len = 0; 58 | char *line = NULL; 59 | ssize_t read; 60 | 61 | if(!filename) 62 | return 2; 63 | 64 | conf = fopen(filename, "r"); 65 | if (conf == NULL) { 66 | return 1; 67 | } 68 | while ((read = getline(&line, &len, conf)) != -1) { 69 | char *copy = strdup(line); 70 | if (!copy) { 71 | fprintf(stderr, "Error allocating memory for trigger parsing!\n"); 72 | break; 73 | } 74 | trigger *t = parse_trigger(copy); 75 | if (t) { 76 | append_trigger(t); 77 | } else if (strlen(copy) > 0) { 78 | /* perhaps it was just an empty line */ 79 | fprintf(stderr, "Unable to parse trigger line: %s\n", line); 80 | } 81 | free(copy); 82 | } 83 | fclose(conf); 84 | conf=NULL; 85 | free(line); 86 | line=NULL; 87 | return 0; 88 | } 89 | 90 | static int accept_triggerfile(const struct dirent *entry) { 91 | const char *suffix = ".conf"; 92 | const char *name = entry->d_name; 93 | char *end = strstr( name, suffix ); 94 | if ( end && end[ strlen(suffix) ] == '\0' ) { 95 | return 1; 96 | } else { 97 | return 0; 98 | } 99 | } 100 | 101 | int read_triggers(const char *path) { 102 | /* check whether filename is a directory */ 103 | struct stat sb; 104 | if (stat(path, &sb) == -1) { 105 | perror("stat"); 106 | return 1; 107 | } 108 | if (S_ISDIR(sb.st_mode)) { 109 | /* dive into it */ 110 | struct dirent **namelist; 111 | int n; 112 | n = scandir(path, &namelist, accept_triggerfile, alphasort); 113 | if ( n < 0) { 114 | perror("scandir"); 115 | return 1; 116 | } else { 117 | while (n--) { 118 | struct stat sf; 119 | char *file = namelist[n]->d_name; 120 | char *sep = "/"; 121 | char fpath[strlen(path)+strlen(sep)+strlen(file) + 1]; 122 | strcpy(fpath, path); 123 | strcat(fpath, sep); 124 | strcat(fpath, file); 125 | if (stat(fpath, &sf) == -1) { 126 | perror("stat"); 127 | } 128 | if (S_ISREG(sf.st_mode)) { 129 | read_triggerfile(fpath); 130 | } 131 | free(namelist[n]); 132 | } 133 | free(namelist); 134 | return 0; 135 | } 136 | } else { 137 | return read_triggerfile( path ); 138 | } 139 | } 140 | 141 | static int devtag_equal(const char *d_tag, const char *t_tag) { 142 | if (strlen(t_tag) == 0) { 143 | return 1; 144 | } 145 | if (strcmp(t_tag, d_tag) == 0) { 146 | return 1; 147 | } 148 | return 0; 149 | } 150 | 151 | static int mods_equal(keystate_holder ksh, trigger_modifier tm, int ignore_key) { 152 | int n = 0; 153 | while ( n < TRIGGER_MODIFIERS_MAX ) { 154 | int code = tm[n]; /* this key must be pressed */ 155 | if (code < 0) { 156 | break; 157 | } else if (ksh[code] <= 0) { 158 | return 0; 159 | } 160 | n++; 161 | } 162 | /* Now n is equal to the number of modifiers needed 163 | * we can check whether any additional keys are pressed 164 | * by counting the number of pressed keys instead of having 165 | * to cross-examine every key against the modifier list 166 | */ 167 | int x; 168 | for (x=0; x<=KEY_MAX; x++) { 169 | if (x != ignore_key && ksh[x] > 0) { 170 | n--; 171 | } 172 | } 173 | /* if n is zero, we have the exact number of needed modifiers pressed */ 174 | return (n == 0); 175 | } 176 | 177 | static int correct_mode( const char *tmode ) { 178 | /* not mode specified, valid in every mode */ 179 | return (tmode == NULL) || (strcmp( get_trigger_mode(), tmode ) == 0); 180 | } 181 | 182 | void run_triggers(int type, int code, int value, keystate_holder ksh, device *dev) { 183 | if (triggers_are_enabled == 0) { 184 | return; 185 | } 186 | if (dev && dev->tag) { 187 | } 188 | trigger *et = trigger_list; 189 | while (et != NULL) { 190 | if ( type == et->type && 191 | code == et->code && 192 | value == et->value && 193 | et->action && 194 | correct_mode( et->mode ) && 195 | devtag_equal(&(dev->tag[0]), &(et->devtag[0])) && 196 | mods_equal(ksh, et->modifiers, (type==EV_KEY?code:-1) )) { 197 | fprintf(stderr, "Executing trigger action: %s\n", et->action); 198 | /* switch trigger mode or execute program? */ 199 | if (et->action[0] == '@') { 200 | change_trigger_mode( &(et->action)[1] ); 201 | /* we now stop checking for additional triggers 202 | * to avoid changing back to the original mode 203 | */ 204 | break; 205 | } else if (et->action[0] == '<' ) { 206 | char *keyname = &(et->action[1]); 207 | fprintf(stderr, "Sending event: %s\n", keyname); 208 | int type = lookup_event_type( keyname ); 209 | int code = lookup_event_code( keyname ); 210 | if (type && code) { 211 | send_event( type, code, 1 ); 212 | send_event( type, code, 0 ); 213 | send_event( EV_SYN, 0, 0 ); 214 | } 215 | } else { 216 | int pid = fork(); 217 | if (pid == 0 ) { 218 | /* adjust environment */ 219 | setenv( "TH_DEVICE", dev->devname, 1 ); 220 | setenv( "TH_KEYSTATE", get_keystate(ksh), 1 ); 221 | const char *en = lookup_event_name_i( et->type, et->code ); 222 | setenv( "TH_EVENT", en, 1 ); 223 | char ev[8]; 224 | sprintf( &(ev[0]), "%d", et->value ); 225 | setenv( "TH_VALUE", &(ev[0]), 1 ); 226 | /* default SIGCHLD is not automatically 227 | restored and most applications would 228 | fail if this signal is ignored */ 229 | signal(SIGCHLD, SIG_DFL); 230 | system(et->action); 231 | exit(0); 232 | } else if (pid < 0) { 233 | fprintf(stderr, "Unable to fork!\n"); 234 | } 235 | } 236 | } 237 | et = et->next; 238 | } 239 | } 240 | 241 | int count_triggers( trigger **list ) { 242 | int n = 0; 243 | if(!list) 244 | return 0; 245 | trigger *p = *list; 246 | while ( p != NULL ) { 247 | n++; 248 | p = p->next; 249 | } 250 | return n; 251 | } 252 | 253 | void clear_triggers() { 254 | trigger *p = trigger_list; 255 | while (p != NULL) { 256 | trigger *next = p->next; 257 | free(p->action); 258 | free(p->mode); 259 | free(p); 260 | p = next; 261 | } 262 | trigger_list = NULL; 263 | } 264 | -------------------------------------------------------------------------------- /trigger.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include "devtag.h" 8 | #define TRIGGER_MODIFIERS_MAX 5 9 | 10 | typedef int trigger_modifier[TRIGGER_MODIFIERS_MAX]; 11 | 12 | typedef struct trigger { 13 | int type; 14 | int code; 15 | int value; 16 | trigger_modifier modifiers; 17 | char *mode; 18 | char devtag[TH_DEVICE_TAG_LENGTH]; 19 | char *action; 20 | struct trigger *next; 21 | } trigger; 22 | 23 | trigger* parse_trigger(char* line); 24 | void append_trigger(trigger *t); 25 | int read_triggers(const char *filename); 26 | void run_triggers(int type, int code, int value, keystate_holder ksh, device *dev); 27 | void clear_triggers(); 28 | 29 | void triggers_enabled( int status ); 30 | 31 | void change_trigger_mode( const char *tmode ); 32 | -------------------------------------------------------------------------------- /triggerhappy.conf.examples: -------------------------------------------------------------------------------- 1 | # adjust volume 2 | KEY_VOLUMEUP 1 /usr/bin/amixer set Master 5%+ 3 | KEY_VOLUMEUP+KEY_LEFTSHIFT 1 /usr/bin/amixer set Master 15%+ # with modifier 4 | KEY_VOLUMEUP 2 /usr/bin/amixer set Master 5%+ # even while holding 5 | KEY_VOLUMEDOWN 1 /usr/bin/amixer set Master 5%- 6 | KEY_VOLUMEDOWN+KEY_LEFTSHIFT 1 /usr/bin/amixer set Master 15%- 7 | KEY_VOLUMEDOWN 2 /usr/bin/amixer set Master 5%- 8 | # only trigger commands when in "media" mode 9 | KEY_KPPLUS@media 1 /usr/bin/mpc next # next song 10 | KEY_KPMINUS@media 1 /usr/bin/mpc prev # next song 11 | # only used if in the nameless default mode 12 | KEY_F12@ 1 @media # switch to media mode 13 | KEY_F12@media 1 @ # switch back to nameless default mode 14 | KEY_KPASTERISK 1 KEY_F11 1 id 17 | -------------------------------------------------------------------------------- /triggerparser.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "eventnames.h" 16 | #include "keystate.h" 17 | #include "devices.h" 18 | #include "trigger.h" 19 | #include "triggerparser.h" 20 | 21 | static int parse_evdef(char *evdef, trigger *t) { 22 | /* first we split the mode (if present) */ 23 | char *mode = strchr(evdef, '@'); 24 | if (mode) { 25 | *mode = '\0'; 26 | mode++; 27 | } 28 | /* place a copy of the mode string or NULL in the struct */ 29 | t->mode = (mode ? strdup(mode) : NULL); 30 | 31 | /* The evdef event now might still contain a device tag enclosed in < > */ 32 | t->devtag[0] = '\0'; 33 | char *tmp = strchr(evdef, '>'); 34 | if (tmp && evdef[0] == '<') { 35 | *tmp = '\0'; 36 | strncpy(t->devtag, &evdef[1], TH_DEVICE_TAG_LENGTH); 37 | t->devtag[TH_DEVICE_TAG_LENGTH-1] = '\0'; 38 | evdef = tmp+1; /* point evdef to the beginning of the event */ 39 | } 40 | 41 | /* now we can start to separate the triggering event from the modifiers */ 42 | char *sptr = NULL; 43 | char *s_trigger = strtok_r(evdef, "+", &sptr); 44 | 45 | int n = 0; 46 | while (n < TRIGGER_MODIFIERS_MAX) { 47 | char *s_mod = strtok_r(NULL, "+", &sptr); 48 | if (s_mod == NULL) { 49 | t->modifiers[n] = -1; 50 | } else { 51 | int type = lookup_event_type(s_mod); 52 | int c = lookup_event_code(s_mod); 53 | if (type == EV_KEY && c >= 0) { 54 | t->modifiers[n] = c; 55 | } else { 56 | /* we cannot process this key code */ 57 | return 1; 58 | } 59 | } 60 | n++; 61 | } 62 | 63 | int type = lookup_event_type( s_trigger ); 64 | int code = lookup_event_code( s_trigger ); 65 | 66 | if (type < 0 || code < 0) { 67 | return 1; 68 | } 69 | t->type = type; 70 | t->code = code; 71 | 72 | return 0; 73 | } 74 | 75 | trigger* parse_trigger(char* line) { 76 | /* ignore everything behind # */ 77 | char *comment = strchr(line, '#'); 78 | if ( comment != NULL ) { 79 | *comment = '\0'; 80 | } 81 | 82 | /* no we must have at least 3 tokens left */ 83 | char *delim = " \t\n"; 84 | char *sptr = NULL; 85 | /* event definition including modifiers and daemon mode */ 86 | char *s_evdef = strtok_r(line, delim, &sptr); 87 | /* value of the event (0, 1, 2) */ 88 | char *s_value = strtok_r(NULL, delim, &sptr); 89 | /* the action that should take place filling the rest of the line */ 90 | char *s_action = strtok_r(NULL, "", &sptr); 91 | 92 | /* if we do not have 3 tokens, we can abort */ 93 | if (! (s_evdef && s_value && s_action)) { 94 | return NULL; 95 | } 96 | trigger *t = malloc( sizeof(trigger) ); 97 | if (! t) { 98 | fprintf(stderr, "Unable to allocate memory for trigger definition!\n"); 99 | return NULL; 100 | } 101 | memset( t, 0, sizeof(*t) ); 102 | t->next = NULL; 103 | 104 | t->value = atoi(s_value); 105 | 106 | int err = parse_evdef( s_evdef, t ); 107 | if (!err) { 108 | /* remove trailing whitespaces from the end of the command line */ 109 | char *end = s_action + strlen(s_action) - 1; 110 | while (end >= s_action && (isspace(*end) || *end == '\n')) { 111 | end --; 112 | } 113 | *(end+1) = '\0'; 114 | /* remove leading whitespaces as well */ 115 | while (isspace(*s_action)) { 116 | s_action++; 117 | } 118 | /* now copy the strings (mode is already copied in parse_evdef) */ 119 | t->action = strdup(s_action); 120 | } else { 121 | /* free the allocated memory */ 122 | free(t->mode); 123 | free(t->action); 124 | free(t); 125 | t = NULL; 126 | } 127 | return t; 128 | } 129 | 130 | -------------------------------------------------------------------------------- /triggerparser.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | trigger* parse_trigger(char* line); 8 | -------------------------------------------------------------------------------- /udev/triggerhappy-udev.rules: -------------------------------------------------------------------------------- 1 | ## open new input devices and pass the file descriptor to the triggerhappy daemon 2 | ACTION=="add", SUBSYSTEM=="input", \ 3 | ATTRS{name}!="triggerhappy", \ 4 | RUN+="/usr/sbin/th-cmd --socket /var/run/thd.socket --passfd --udev" 5 | ## add device exclusively 6 | #ACTION=="add", SUBSYSTEM=="input", \ 7 | # ATTRS{name}=="Nintendo Wiimote", \ 8 | # RUN+="/usr/sbin/th-cmd --socket /var/run/thd.socket --passfd --udev --grab" 9 | -------------------------------------------------------------------------------- /uinput.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "uinput.h" 17 | 18 | static int uinput_fd = -1; 19 | 20 | int open_uinput(const char *path) { 21 | close_uinput(); 22 | struct uinput_user_dev device; 23 | uinput_fd = open(path, O_WRONLY); 24 | if (!uinput_fd) { 25 | return -1; 26 | } 27 | memset(&device, 0, sizeof device); 28 | strcpy(device.name,"triggerhappy"); 29 | device.id.bustype = BUS_USB; 30 | device.id.vendor = 1; 31 | device.id.product = 1; 32 | device.id.version = 1; 33 | if (write(uinput_fd,&device,sizeof(device)) != sizeof(device)) { 34 | close_uinput(); 35 | return -1; 36 | } 37 | 38 | if (ioctl(uinput_fd,UI_SET_EVBIT,EV_KEY) < 0) { 39 | close_uinput(); 40 | return -1; 41 | } 42 | 43 | /* we can generate _any_ key event */ 44 | int i; 45 | for (i=0; i= 0) { 62 | close(uinput_fd); 63 | uinput_fd = -1; 64 | } 65 | } 66 | 67 | int send_event(const int type, const int code, const int value) { 68 | if (!uinput_fd) { 69 | return -1; 70 | } 71 | struct input_event event; 72 | memset(&event, 0, sizeof event); 73 | event.type = type; 74 | event.code = code; 75 | event.value = value; 76 | if (write(uinput_fd, &event, sizeof(event)) != sizeof(event)) { 77 | fprintf(stderr, "Error on send_event\n"); 78 | return -1; 79 | } 80 | return 0; 81 | } 82 | 83 | -------------------------------------------------------------------------------- /uinput.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2010 Stefan Tomanek 2 | * You have permission to copy, modify, and redistribute under the 3 | * terms of the GPLv3 or any later version. 4 | * For full license terms, see COPYING. 5 | */ 6 | 7 | int open_uinput(const char *path); 8 | void close_uinput(); 9 | int send_event(const int type, const int code, const int value); 10 | -------------------------------------------------------------------------------- /version.inc: -------------------------------------------------------------------------------- 1 | 0.5.0 2 | --------------------------------------------------------------------------------