AB Shutter 3 でコンソール・アプリをリモート操作するためのプログラムをHDMI-CEC対応しTVリモコンでも操作できるように改良してみた。
AB Shutter 3 を使って Raspberry PI コンソール・アプリをリモート操作する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# # remocon.conf - Application Remote Controller Configuration # # <hdmi-action-names> # # poweron # standby # activate # inactivate # volumeup # volumedown # muteaudio # [program] cmdline=command-line [control] quit=<"q"> ledpwroff=echo 0 > /sys/class/leds/PWR/brightness ledpwron=echo default-on > /sys/class/leds/PWR/trigger ledpwrblink=echo timer > /sys/class/leds/PWR/trigger ledactoff=echo mmc0 > /sys/class/leds/ACT/trigger ledacton=echo default-on > /sys/class/leds/ACT/trigger ledactblink=echo timer > /sys/class/leds/ACT/trigger [keyboard] volumeup=<"l"><activate>./left.sh volumedown=<"r"><activate>./right.sh [keyboard.long-pressed] volumeup<poweron> volumedown=<standby> [tv] name=TVの入力切替時に表示されるデバイス名 select= up=<"u">./up.sh down=<"d">./down.sh left=<"l">./left.sh right=<"r">./right.sh f1_blue=<ledpwroff> f2_red=<ledpwron> f3_green=<ledpwrblink> f4_yellow= [device] AB Shutter=Keyboard BOXPUT=Keyboard |
各イベントは下記のように、標準入力へ出力する文字列、HDMI-CEC制御アクション名、コマンドラインの3種類のパラメタが複数同時に設定可能となっている。
1 |
event=<"標準入力へ出力する文字列"><HDMI-CEC制御アクション名>コマンドライン |
AB Shutter 3 ではTVの電源を入れたり入力切替も可能であるがTVリモコンの場合は入力切替後にのみ操作可能となる。
【HDMI-CEC制御】
HDMI-CEC制御はlibCECを利用しており下記ファイルについてはlibCECのファイルの一部であるが、version.hのみMakeでコンパイルできるよう若干の修正を入れている。インストールは下記のように行う。
sudo apt-get install cec-utils -y
libcec/cec.h
libcec/cecloader.h
libcec/cectypes.h
libcec/version.h
【修正履歴】
2023-05-09
BOXPUTリモコンのOKボタン(select=)に対応。AB Shutter 3 は不安定なのでお勧めではないがBOXPUTリモコンは動作も安定していてお勧めだ。
2023-05-06
AB Shutter 3 の小さいボタンのコードの規定値をKEY_ENTERからKEY_VOLUMEDOWNに変更。あとHDMIアクション名からremote.confの[control]の設定を参照&実行できるように改良。
2023-05-05
対応デバイス名をコードで固定設定していたのをremocon.confで定義できるよう変更。これでプログラムを変更することなくlinuxで扱える様々なinputデバイスに対応することが出来るようになった。
remocon.confのデバイス定義は次のように行う。
1 2 3 |
[device] AB Shutter=Keyboard BOXPUT=Keyboard |
設定は、デバイスを接続した後、cat /proc/bus/input/devicesにより表示される N: Name=… の先頭文字と最終文字を’=’で分けて指定する。
例えば、Name=”AB Shutter3 Keyboard”の場合は、
1 |
N: Name="AB Shutter3 Keyboard" --> AB Shutter=Keyboard |
のように設定すればOKだ。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
pi@rp3ap:~ $ cat /proc/bus/input/devices I: Bus=001e Vendor=0000 Product=0000 Version=0001 N: Name="vc4-hdmi" P: Phys=vc4-hdmi/input0 S: Sysfs=/devices/platform/soc/3f902000.hdmi/rc/rc0/input0 U: Uniq= H: Handlers=kbd event0 B: PROP=20 B: EV=100017 B: KEY=ffffc000000000 3ff 0 400000320fc200 40830c900000000 0 210300 49d2c040ec00 1e378000000000 8010000010000000 B: REL=3 B: MSC=10 I: Bus=0003 Vendor=1234 Product=5678 Version=0000 N: Name="Example device" P: Phys= S: Sysfs=/devices/virtual/input/input1 U: Uniq= H: Handlers=sysrq kbd event1 rfkill B: PROP=0 B: EV=3 B: KEY=7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffff fffffffffffffffe I: Bus=0005 Vendor=248a Product=8266 Version=0001 N: Name="AB Shutter3 Consumer Control" P: Phys=b8:27:eb:35:24:9c S: Sysfs=/devices/virtual/misc/uhid/0005:248A:8266.0002/input/input2 U: Uniq=ff:ff:7f:f2:30:75 H: Handlers=kbd event2 B: PROP=0 B: EV=13 B: KEY=1e000000000000 0 B: MSC=10 I: Bus=0005 Vendor=248a Product=8266 Version=0001 N: Name="AB Shutter3 Keyboard" <-- ★★★この名前を登録する★★★ P: Phys=b8:27:eb:35:24:9c S: Sysfs=/devices/virtual/misc/uhid/0005:248A:8266.0002/input/input3 U: Uniq=ff:ff:7f:f2:30:75 H: Handlers=sysrq kbd event3 B: PROP=0 B: EV=100013 B: KEY=1000000000007 ff98007ac04007ff febeffdfffefffff fffffffffffffffe B: MSC=10 |
2023-05-04
Aliexpressで購入可能なBOXPUTというbluetoothリモコンに対応。
ワイヤレスリモコン,5.0 GHz,2.4インチ,ジャイロスコープ付き,Android TV,ボックス,PC用
この対応に伴いremocon.confをより汎用的な記述方法へ変更。
2023-05-01
TVの入力切替時に表示される名前が設定できるように改良。
2023-04-30
libCECの使い方が正しくなかったため修正。
2023-04-25
Bluetoothデバイスの検索を一定間隔でポーリング処理していたがCPU使用率が若干高めであったためinotify-apiを使って効率よく処理できるように改良してみた。ちなみにダイソーのAB Shutter3は2個のデバイスが作成されるがinotify-apiではそのうちの1個の名前しか返さないためinotify-apiは同期をとるためだけに利用している。またスレッドの挙動によるタイミング・バグがあったため修正。
【プログラム】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
TARGET = remocon OBJS = $(TARGET).o DEPFILES = $(OBJS:%.o=%.d) CC = gcc CFLAGS = CPPFLAGS = CXXFLAGS = -Wall -MMD -I./libcec LDLIBS = -L. -lstdc++ -lpthread -ldl $(TARGET): $(OBJS) $(CC) -o $@ $^ $(LDLIBS) clean: rm -f $(TARGET) $(OBJS) $(DEPFILES) -include $(DEPFILES) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#!/bin/bash CWD=$(cd $(dirname $0) && pwd) SCAN=${CWD}/ble-scan PAIRING=${CWD}/ble-pairing bluetoothctl power off bluetoothctl power on bluetoothctl agent NoInputNoOutput if [ -f ${PAIRING} ]; then MAC=$(cat ${PAIRING}) if [ "${MAC}" != "" ]; then bluetoothctl disconnect ${MAC} bluetoothctl untrust ${MAC} bluetoothctl remove ${MAC} fi rm ${PAIRING} > /dev/null 2>&1 fi stdbuf -o0 bluetoothctl scan on 1> ${SCAN} & while true do sleep 1 MAC=$(sed -n -r "s/\[.*NEW.*\] Device (.*) AB Shutter.*/\1/p" ${SCAN} | head -n 1) if [ "${MAC}" != "" ]; then break; fi MAC=$(sed -n -r "s/\[.*NEW.*\] Device (.*) BOXPUT.*/\1/p" ${SCAN} | head -n 1) if [ "${MAC}" != "" ]; then break; fi done echo ${MAC} > ${PAIRING} bluetoothctl pair ${MAC} bluetoothctl trust ${MAC} bluetoothctl connect ${MAC} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
/* remocon.cpp - Application Remote Controller Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <signal.h> #include "ceclib.h" #include "remocon.h" #include "fileprops.h" #define LONGPRESS_TIME 3000000 // us typedef struct { int code; const char *name; } keyinfo_t; const keyinfo_t INPUT_KEYS[] = { { KEY_RESERVED, "reserved" }, { KEY_ESC, "esc" }, { KEY_1, "1" }, { KEY_2, "2" }, { KEY_3, "3" }, { KEY_4, "4" }, { KEY_5, "5" }, { KEY_6, "6" }, { KEY_7, "7" }, { KEY_8, "8" }, { KEY_9, "9" }, { KEY_0, "0" }, { KEY_MINUS, "minus" }, { KEY_EQUAL, "equal" }, { KEY_BACKSPACE, "backspace" }, { KEY_TAB, "tab" }, { KEY_Q, "q" }, { KEY_W, "w" }, { KEY_E, "e" }, { KEY_R, "r" }, { KEY_T, "t" }, { KEY_Y, "y" }, { KEY_U, "u" }, { KEY_I, "i" }, { KEY_O, "o" }, { KEY_P, "p" }, { KEY_LEFTBRACE, "leftbrace" }, { KEY_RIGHTBRACE, "rightbrace" }, { KEY_ENTER, "enter" }, { KEY_LEFTCTRL, "leftctrl" }, { KEY_A, "a" }, { KEY_S, "s" }, { KEY_D, "d" }, { KEY_F, "f" }, { KEY_G, "g" }, { KEY_H, "h" }, { KEY_J, "j" }, { KEY_K, "k" }, { KEY_L, "l" }, { KEY_SEMICOLON, "semicolon" }, { KEY_APOSTROPHE, "apostrophe" }, { KEY_GRAVE, "grave" }, { KEY_LEFTSHIFT, "leftshift" }, { KEY_BACKSLASH, "backslash" }, { KEY_Z, "z" }, { KEY_X, "x" }, { KEY_C, "c" }, { KEY_V, "v" }, { KEY_B, "b" }, { KEY_N, "n" }, { KEY_M, "m" }, { KEY_COMMA, "comma" }, { KEY_DOT, "dot" }, { KEY_SLASH, "slash" }, { KEY_RIGHTSHIFT, "rightshift" }, { KEY_KPASTERISK, "kpasterisk" }, { KEY_LEFTALT, "leftalt" }, { KEY_SPACE, "space" }, { KEY_CAPSLOCK, "capslock" }, { KEY_F1, "f1" }, { KEY_F2, "f2" }, { KEY_F3, "f3" }, { KEY_F4, "f4" }, { KEY_F5, "f5" }, { KEY_F6, "f6" }, { KEY_F7, "f7" }, { KEY_F8, "f8" }, { KEY_F9, "f9" }, { KEY_F10, "f10" }, { KEY_NUMLOCK, "numlock" }, { KEY_SCROLLLOCK, "scrolllock" }, { KEY_KP7, "kp7" }, { KEY_KP8, "kp8" }, { KEY_KP9, "kp9" }, { KEY_KPMINUS, "kpminus" }, { KEY_KP4, "kp4" }, { KEY_KP5, "kp5" }, { KEY_KP6, "kp6" }, { KEY_KPPLUS, "kpplus" }, { KEY_KP1, "kp1" }, { KEY_KP2, "kp2" }, { KEY_KP3, "kp3" }, { KEY_KP0, "kp0" }, { KEY_KPDOT, "kpdot" }, { KEY_ZENKAKUHANKAKU, "zenkakuhankaku" }, { KEY_102ND, "102nd" }, { KEY_F11, "f11" }, { KEY_F12, "f12" }, { KEY_RO, "ro" }, { KEY_KATAKANA, "katakana" }, { KEY_HIRAGANA, "hiragana" }, { KEY_HENKAN, "henkan" }, { KEY_KATAKANAHIRAGANA, "katakanahiragana" }, { KEY_MUHENKAN, "muhenkan" }, { KEY_KPJPCOMMA, "kpjpcomma" }, { KEY_KPENTER, "kpenter" }, { KEY_RIGHTCTRL, "rightctrl" }, { KEY_KPSLASH, "kpslash" }, { KEY_SYSRQ, "sysrq" }, { KEY_RIGHTALT, "rightalt" }, { KEY_LINEFEED, "linefeed" }, { KEY_HOME, "home" }, { KEY_UP, "up" }, { KEY_PAGEUP, "pageup" }, { KEY_LEFT, "left" }, { KEY_RIGHT, "right" }, { KEY_END, "end" }, { KEY_DOWN, "down" }, { KEY_PAGEDOWN, "pagedown" }, { KEY_INSERT, "insert" }, { KEY_DELETE, "delete" }, { KEY_MACRO, "macro" }, { KEY_MUTE, "mute" }, { KEY_VOLUMEDOWN, "volumedown" }, { KEY_VOLUMEUP, "volumeup" }, { KEY_POWER, "power" }, { KEY_KPEQUAL, "kpequal" }, { KEY_KPPLUSMINUS, "kpplusminus" }, { KEY_PAUSE, "pause" }, { KEY_SCALE, "scale" }, { KEY_KPCOMMA, "kpcomma" }, { KEY_HANGEUL, "hangeul" }, { KEY_HANGUEL, "hanguel" }, { KEY_HANJA, "hanja" }, { KEY_YEN, "yen" }, { KEY_LEFTMETA, "leftmeta" }, { KEY_RIGHTMETA, "rightmeta" }, { KEY_COMPOSE, "compose" }, { KEY_STOP, "stop" }, { KEY_AGAIN, "again" }, { KEY_PROPS, "props" }, { KEY_UNDO, "undo" }, { KEY_FRONT, "front" }, { KEY_COPY, "copy" }, { KEY_OPEN, "open" }, { KEY_PASTE, "paste" }, { KEY_FIND, "find" }, { KEY_CUT, "cut" }, { KEY_HELP, "help" }, { KEY_MENU, "menu" }, { KEY_CALC, "calc" }, { KEY_SETUP, "setup" }, { KEY_SLEEP, "sleep" }, { KEY_WAKEUP, "wakeup" }, { KEY_FILE, "file" }, { KEY_SENDFILE, "sendfile" }, { KEY_DELETEFILE, "deletefile" }, { KEY_XFER, "xfer" }, { KEY_PROG1, "prog1" }, { KEY_PROG2, "prog2" }, { KEY_WWW, "www" }, { KEY_MSDOS, "msdos" }, { KEY_COFFEE, "coffee" }, { KEY_SCREENLOCK, "screenlock" }, { KEY_ROTATE_DISPLAY, "rotate_display" }, { KEY_DIRECTION, "direction" }, { KEY_CYCLEWINDOWS, "cyclewindows" }, { KEY_MAIL, "mail" }, { KEY_BOOKMARKS, "bookmarks" }, { KEY_COMPUTER, "computer" }, { KEY_BACK, "back" }, { KEY_FORWARD, "forward" }, { KEY_CLOSECD, "closecd" }, { KEY_EJECTCD, "ejectcd" }, { KEY_EJECTCLOSECD, "ejectclosecd" }, { KEY_NEXTSONG, "nextsong" }, { KEY_PLAYPAUSE, "playpause" }, { KEY_PREVIOUSSONG, "previoussong" }, { KEY_STOPCD, "stopcd" }, { KEY_RECORD, "record" }, { KEY_REWIND, "rewind" }, { KEY_PHONE, "phone" }, { KEY_ISO, "iso" }, { KEY_CONFIG, "config" }, { KEY_HOMEPAGE, "homepage" }, { KEY_REFRESH, "refresh" }, { KEY_EXIT, "exit" }, { KEY_MOVE, "move" }, { KEY_EDIT, "edit" }, { KEY_SCROLLUP, "scrollup" }, { KEY_SCROLLDOWN, "scrolldown" }, { KEY_KPLEFTPAREN, "kpleftparen" }, { KEY_KPRIGHTPAREN, "kprightparen" }, { KEY_NEW, "new" }, { KEY_REDO, "redo" }, { KEY_F13, "f13" }, { KEY_F14, "f14" }, { KEY_F15, "f15" }, { KEY_F16, "f16" }, { KEY_F17, "f17" }, { KEY_F18, "f18" }, { KEY_F19, "f19" }, { KEY_F20, "f20" }, { KEY_F21, "f21" }, { KEY_F22, "f22" }, { KEY_F23, "f23" }, { KEY_F24, "f24" }, { KEY_PLAYCD, "playcd" }, { KEY_PAUSECD, "pausecd" }, { KEY_PROG3, "prog3" }, { KEY_PROG4, "prog4" }, { KEY_ALL_APPLICATIONS, "all_applications" }, { KEY_DASHBOARD, "dashboard" }, { KEY_SUSPEND, "suspend" }, { KEY_CLOSE, "close" }, { KEY_PLAY, "play" }, { KEY_FASTFORWARD, "fastforward" }, { KEY_BASSBOOST, "bassboost" }, { KEY_PRINT, "print" }, { KEY_HP, "hp" }, { KEY_CAMERA, "camera" }, { KEY_SOUND, "sound" }, { KEY_QUESTION, "question" }, { KEY_EMAIL, "email" }, { KEY_CHAT, "chat" }, { KEY_SEARCH, "search" }, { KEY_CONNECT, "connect" }, { KEY_FINANCE, "finance" }, { KEY_SPORT, "sport" }, { KEY_SHOP, "shop" }, { KEY_ALTERASE, "alterase" }, { KEY_CANCEL, "cancel" }, { KEY_BRIGHTNESSDOWN, "brightnessdown" }, { KEY_BRIGHTNESSUP, "brightnessup" }, { KEY_MEDIA, "media" }, { KEY_SWITCHVIDEOMODE, "switchvideomode" }, { KEY_KBDILLUMTOGGLE, "kbdillumtoggle" }, { KEY_KBDILLUMDOWN, "kbdillumdown" }, { KEY_KBDILLUMUP, "kbdillumup" }, { KEY_SEND, "send" }, { KEY_REPLY, "reply" }, { KEY_FORWARDMAIL, "forwardmail" }, { KEY_SAVE, "save" }, { KEY_DOCUMENTS, "documents" }, { KEY_BATTERY, "battery" }, { KEY_BLUETOOTH, "bluetooth" }, { KEY_WLAN, "wlan" }, { KEY_UWB, "uwb" }, { KEY_UNKNOWN, "unknown" }, { KEY_VIDEO_NEXT, "video_next" }, { KEY_VIDEO_PREV, "video_prev" }, { KEY_BRIGHTNESS_CYCLE, "brightness_cycle" }, { KEY_BRIGHTNESS_AUTO, "brightness_auto" }, { KEY_BRIGHTNESS_ZERO, "brightness_zero" }, { KEY_DISPLAY_OFF, "display_off" }, { KEY_WWAN, "wwan" }, { KEY_WIMAX, "wimax" }, { KEY_RFKILL, "rfkill" }, { KEY_MICMUTE, "micmute" }, { 353, "select" }, // BOXPUT { 0, nullptr }, }; const keyinfo_t HDMI_CEC_KEYS[] = { { CEC_USER_CONTROL_CODE_SELECT , "select" }, { CEC_USER_CONTROL_CODE_UP , "up" }, { CEC_USER_CONTROL_CODE_DOWN , "down" }, { CEC_USER_CONTROL_CODE_LEFT , "left" }, { CEC_USER_CONTROL_CODE_RIGHT , "right" }, { CEC_USER_CONTROL_CODE_RIGHT_UP , "right_up" }, { CEC_USER_CONTROL_CODE_RIGHT_DOWN , "right_down" }, { CEC_USER_CONTROL_CODE_LEFT_UP , "left_up" }, { CEC_USER_CONTROL_CODE_LEFT_DOWN , "left_down" }, { CEC_USER_CONTROL_CODE_ROOT_MENU , "root_menu" }, { CEC_USER_CONTROL_CODE_SETUP_MENU , "setup_menu" }, { CEC_USER_CONTROL_CODE_CONTENTS_MENU , "contents_menu" }, { CEC_USER_CONTROL_CODE_FAVORITE_MENU , "favorite_menu" }, { CEC_USER_CONTROL_CODE_EXIT , "exit" }, { CEC_USER_CONTROL_CODE_TOP_MENU , "top_menu" }, { CEC_USER_CONTROL_CODE_DVD_MENU , "dvd_menu" }, { CEC_USER_CONTROL_CODE_NUMBER_ENTRY_MODE , "number_entry_mode" }, { CEC_USER_CONTROL_CODE_NUMBER11 , "number11" }, { CEC_USER_CONTROL_CODE_NUMBER12 , "number12" }, { CEC_USER_CONTROL_CODE_NUMBER0 , "number0" }, { CEC_USER_CONTROL_CODE_NUMBER1 , "number1" }, { CEC_USER_CONTROL_CODE_NUMBER2 , "number2" }, { CEC_USER_CONTROL_CODE_NUMBER3 , "number3" }, { CEC_USER_CONTROL_CODE_NUMBER4 , "number4" }, { CEC_USER_CONTROL_CODE_NUMBER5 , "number5" }, { CEC_USER_CONTROL_CODE_NUMBER6 , "number6" }, { CEC_USER_CONTROL_CODE_NUMBER7 , "number7" }, { CEC_USER_CONTROL_CODE_NUMBER8 , "number8" }, { CEC_USER_CONTROL_CODE_NUMBER9 , "number9" }, { CEC_USER_CONTROL_CODE_DOT , "dot" }, { CEC_USER_CONTROL_CODE_ENTER , "enter" }, { CEC_USER_CONTROL_CODE_CLEAR , "clear" }, { CEC_USER_CONTROL_CODE_NEXT_FAVORITE , "next_favorite" }, { CEC_USER_CONTROL_CODE_CHANNEL_UP , "channel_up" }, { CEC_USER_CONTROL_CODE_CHANNEL_DOWN , "channel_down" }, { CEC_USER_CONTROL_CODE_PREVIOUS_CHANNEL , "previous_channel" }, { CEC_USER_CONTROL_CODE_SOUND_SELECT , "sound_select" }, { CEC_USER_CONTROL_CODE_INPUT_SELECT , "input_select" }, { CEC_USER_CONTROL_CODE_DISPLAY_INFORMATION , "display_information" }, { CEC_USER_CONTROL_CODE_HELP , "help" }, { CEC_USER_CONTROL_CODE_PAGE_UP , "page_up" }, { CEC_USER_CONTROL_CODE_PAGE_DOWN , "page_down" }, { CEC_USER_CONTROL_CODE_POWER , "power" }, { CEC_USER_CONTROL_CODE_VOLUME_UP , "volume_up" }, { CEC_USER_CONTROL_CODE_VOLUME_DOWN , "volume_down" }, { CEC_USER_CONTROL_CODE_MUTE , "mute" }, { CEC_USER_CONTROL_CODE_PLAY , "play" }, { CEC_USER_CONTROL_CODE_STOP , "stop" }, { CEC_USER_CONTROL_CODE_PAUSE , "pause" }, { CEC_USER_CONTROL_CODE_RECORD , "record" }, { CEC_USER_CONTROL_CODE_REWIND , "rewind" }, { CEC_USER_CONTROL_CODE_FAST_FORWARD , "fast_forward" }, { CEC_USER_CONTROL_CODE_EJECT , "eject" }, { CEC_USER_CONTROL_CODE_FORWARD , "forward" }, { CEC_USER_CONTROL_CODE_BACKWARD , "backward" }, { CEC_USER_CONTROL_CODE_STOP_RECORD , "stop_record" }, { CEC_USER_CONTROL_CODE_PAUSE_RECORD , "pause_record" }, { CEC_USER_CONTROL_CODE_ANGLE , "angle" }, { CEC_USER_CONTROL_CODE_SUB_PICTURE , "sub_picture" }, { CEC_USER_CONTROL_CODE_VIDEO_ON_DEMAND , "video_on_demand" }, { CEC_USER_CONTROL_CODE_ELECTRONIC_PROGRAM_GUIDE , "electronic_program_guide" }, { CEC_USER_CONTROL_CODE_TIMER_PROGRAMMING , "timer_programming" }, { CEC_USER_CONTROL_CODE_INITIAL_CONFIGURATION , "initial_configuration" }, { CEC_USER_CONTROL_CODE_SELECT_BROADCAST_TYPE , "select_broadcast_type" }, { CEC_USER_CONTROL_CODE_SELECT_SOUND_PRESENTATION , "select_sound_presentation" }, { CEC_USER_CONTROL_CODE_PLAY_FUNCTION , "play_function" }, { CEC_USER_CONTROL_CODE_PAUSE_PLAY_FUNCTION , "pause_play_function" }, { CEC_USER_CONTROL_CODE_RECORD_FUNCTION , "record_function" }, { CEC_USER_CONTROL_CODE_PAUSE_RECORD_FUNCTION , "pause_record_function" }, { CEC_USER_CONTROL_CODE_STOP_FUNCTION , "stop_function" }, { CEC_USER_CONTROL_CODE_MUTE_FUNCTION , "mute_function" }, { CEC_USER_CONTROL_CODE_RESTORE_VOLUME_FUNCTION , "restore_volume_function" }, { CEC_USER_CONTROL_CODE_TUNE_FUNCTION , "tune_function" }, { CEC_USER_CONTROL_CODE_SELECT_MEDIA_FUNCTION , "select_media_function" }, { CEC_USER_CONTROL_CODE_SELECT_AV_INPUT_FUNCTION , "select_av_input_function" }, { CEC_USER_CONTROL_CODE_SELECT_AUDIO_INPUT_FUNCTION, "select_audio_input_function" }, { CEC_USER_CONTROL_CODE_POWER_TOGGLE_FUNCTION , "power_toggle_function" }, { CEC_USER_CONTROL_CODE_POWER_OFF_FUNCTION , "power_off_function" }, { CEC_USER_CONTROL_CODE_POWER_ON_FUNCTION , "power_on_function" }, { CEC_USER_CONTROL_CODE_F1_BLUE , "f1_blue" }, { CEC_USER_CONTROL_CODE_F2_RED , "f2_red" }, { CEC_USER_CONTROL_CODE_F3_GREEN , "f3_green" }, { CEC_USER_CONTROL_CODE_F4_YELLOW , "f4_yellow" }, { CEC_USER_CONTROL_CODE_F5 , "f5" }, { CEC_USER_CONTROL_CODE_DATA , "data" }, { CEC_USER_CONTROL_CODE_AN_RETURN , "an_return" }, { CEC_USER_CONTROL_CODE_AN_CHANNELS_LIST , "an_channels_list" }, { 0, nullptr }, }; FileProps props; CECLib ceclib; Remocon remocon; UInput uinput; int exitcode; FILE *redirect; bool cecready; bool debugmode; char config[PATH_MAX]; void command(const char *str); size_t escape(char *buf, const char *str, size_t len) { int cnt = 0; while (len-- > 0) { char c = *str++; if (c == '\\') { switch (c = *str++) { case 0 : case '0': c = 0; break; case 'a': c = '\a'; break; /* 07 */ case 'b': c = '\b'; break; /* 08 */ case 't': c = '\t'; break; /* 09 */ case 'n': c = '\n'; break; /* 0A */ case 'v': c = '\v'; break; /* 0B */ case 'f': c = '\f'; break; /* 0C */ case 'r': c = '\r'; break; /* 0D */ } } if ((*buf++ = c) == 0) break; cnt++; } return cnt; } void stdin_output(const char *str, size_t len) { char temp[256]; if (debugmode) printf("stdin-output(\"%.*s\")\n", (int)len, str); if ((len = escape(temp, str, len > sizeof(temp) ? sizeof(temp) : len))) { if (redirect) { fwrite(temp, 1, len, redirect); fflush(redirect); } else uinput.keystr(temp, len); } } void hdmi_control(const char *str, size_t len) { char cmd[16]; snprintf(cmd, sizeof(cmd), "%.*s", (int)len, str); if (debugmode) printf("hdmi-control(%s)\n", cmd); if (cecready) { if (strcmp(cmd, "power") == 0) { if (ceclib.GetDevicePowerStatus(CECDEVICE_TV) == CEC_POWER_STATUS_ON) ceclib.StandbyDevices(); else ceclib.PowerOnDevices(); } else if (strcmp(cmd, "poweron") == 0) ceclib.PowerOnDevices(); else if (strcmp(cmd, "standby") == 0) ceclib.StandbyDevices(); else if (strcmp(cmd, "activate") == 0) ceclib.SetActiveSource(); else if (strcmp(cmd, "inactivate") == 0) ceclib.SetInactiveView(); else if (strcmp(cmd, "volumeup") == 0) ceclib.VolumeUp(); else if (strcmp(cmd, "volumedown") == 0) ceclib.VolumeDown(); else if (strcmp(cmd, "muteaudio") == 0) ceclib.MuteAudio(); } /* extended control commands */ if (*(str = props.getProperty("control", cmd))) command(str); } void command(const char *str) { char temp[PATH_MAX]; snprintf(temp, sizeof(temp), "%s", str); str = temp; if (debugmode) printf("command(\"%s\")\n", str); while (*str == '<') { size_t i, len = strlen(str); for (i = 1; i < len; ) { char c = str[i++]; if (c == '\\') i++; else if (c == '>') { if (i > 2) { if ((i > 3) && (str[1] == '"') && (str[i - 2] == '"')) stdin_output(str + 2, i - 4); else if (cecready) hdmi_control(str + 1, i - 2); } break; } } str += i; } if (*str) { if (debugmode) printf("system(\"%s\")\n", str); system(str); } } long subtime(struct timeval &a, struct timeval &b) { return ((long long)a.tv_sec * 1000000 + a.tv_usec) - ((long long)b.tv_sec * 1000000 + b.tv_usec); } void keyboard_input(struct input_event *event, void *arg) { struct input_event pushed; const keyinfo_t *keyinfo; keyinfo_t unknown; char keyno[8]; for (keyinfo = INPUT_KEYS; keyinfo->name && (keyinfo->code != event->code); ++keyinfo) continue; if (!keyinfo->name) { snprintf(keyno, sizeof(keyno), "%u", event->code); unknown.name = keyno; unknown.code = event->code; keyinfo = &unknown; } if (debugmode) printf("keyboard_input { name=\"%s\", code=%d, value=%d }\n", keyinfo->name, event->code, event->value); if (keyinfo->name) { if (event->value) { pushed = *event; command(props.getProperty("keyboard", keyinfo->name)); } else if (subtime(event->time, pushed.time) >= LONGPRESS_TIME) { command(props.getProperty("keyboard.long-pressed", keyinfo->name)); } } } void hdmi_cec_keypress(void *cbParam, const cec_keypress* key) { const keyinfo_t *keyinfo; keyinfo_t unknown; char keyno[8]; if (key->duration) return; for (keyinfo = HDMI_CEC_KEYS; keyinfo->name && (keyinfo->code != key->keycode); ++keyinfo) continue; if (!keyinfo->name) { snprintf(keyno, sizeof(keyno), "%u", key->keycode); unknown.name = keyno; unknown.code = key->keycode; keyinfo = &unknown; } if (debugmode) printf("hdmi_cec_keypress { name=\"%s\", code=%d, duration=%d }\n", keyinfo->name, key->keycode, key->duration); if (keyinfo->name) command(props.getProperty("tv", keyinfo->name)); } void hdmi_cec_alert(void *cbParam, const libcec_alert type, const libcec_parameter param) { if (type == CEC_ALERT_CONNECTION_LOST) ceclib.Reconnect(); } void sighandler(int signum) { exitcode = 1; } void usage(void) { printf("Application Remote Controller, Version 1.00\n"); printf("Copyright (C) 2023 Sasapea's Lab. All right reserved.\n"); printf("\n"); printf("Usage: remocon [options]\n"); printf("\n"); printf(" options:\n"); printf(" -p ... redirect stdin\n"); printf(" -c ... command string\n"); printf(" -d ... debug mode\n"); printf(" -h ... this help\n"); printf("\n"); } int main(int argc, char **argv) { bool optpipe = false; // // set current directory to running process directory // snprintf(config, sizeof(config), "%s", argv[0]); char *p = strrchr(config, '/'); if (p) *p = 0; chdir(config); // // load properties // snprintf(config, sizeof(config), "%s.conf", argv[0]); props.load(config); // // command line option // int opt; while ((opt = getopt(argc, argv, "dpc:h")) != -1) { switch (opt) { case 'd': debugmode = true; break; case 'p': optpipe = true; break; case 'c': uinput.start(); command(props.getProperty("control", optarg)); uinput.stop(); exit(0); case 'h': usage(); /* fall through */ default: exit(1); } } // // setup signal handler // struct sigaction act; sigemptyset(&act.sa_mask); act.sa_handler = sighandler; act.sa_flags = SA_RESTART; sigaction(SIGINT , &act, NULL); sigaction(SIGTERM, &act, NULL); // // start Input Capture // std::vector<std::string> names; props.getPropertyNames(names, "device"); for (auto name = names.begin(); name != names.end(); ++name) remocon.add(name->c_str(), props.getProperty("device", name->c_str())); remocon.callback(keyboard_input); remocon.start(); uinput.start(); // // start HDMI-CEC Adapter // ICECCallbacks callbacks; callbacks.Clear(); callbacks.logMessage = nullptr; callbacks.keyPress = hdmi_cec_keypress; callbacks.commandReceived = nullptr; callbacks.alert = hdmi_cec_alert; cecready = ceclib.Start(callbacks, props.getProperty("tv", "name", nullptr)); // // run program // while (exitcode == 0) { // // reload properties // props.load(config); const char *cmd = props.getProperty("program", "cmdline"); if (*cmd && !debugmode) { if (!optpipe) system(cmd); else if ((redirect = popen(cmd, "w"))) pclose(redirect); } sleep(3); } // // stop HDMI-CEC Adapter // if (cecready) ceclib.Stop(); // // stop Input Capture // uinput.stop(); remocon.stop(); return exitcode; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
/* remocon.h - Remocon Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <dirent.h> #include <pthread.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/inotify.h> #include <linux/input.h> #include <linux/uinput.h> #include <string> #include <vector> #ifndef DEBUG #define DEBUG 0 #endif typedef void (*input_cb_t)(struct input_event *, void *arg); class UInput { public: UInput(void) : _fd(-1) { } virtual ~UInput(void) { stop(); } void start(void) { if ((_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK)) != -1) { struct uinput_setup usetup; memset(&usetup, 0, sizeof(usetup)); usetup.id.bustype = BUS_USB; usetup.id.vendor = 0x1234; /* sample vendor */ usetup.id.product = 0x5678; /* sample product */ strcpy(usetup.name, "Example device"); ioctl(_fd, UI_SET_EVBIT, EV_KEY); for (int i = 0; i < KEY_MAX; ++i) ioctl(_fd, UI_SET_KEYBIT, i); ioctl(_fd, UI_DEV_SETUP, &usetup); ioctl(_fd, UI_DEV_CREATE); } } void stop(void) { if (_fd != -1) { ioctl(_fd, UI_DEV_DESTROY); close(_fd); _fd = -1; } } void keystr(const char *str) { keystr(str, strlen(str)); } void keystr(const char *str, size_t len) { while (len-- > 0) keychar(*str++); } void keychar(char c) { static const unsigned char KEYCODES[] = { /* 00 */ KEY_RESERVED, /* 01 */ KEY_RESERVED, /* 02 */ KEY_RESERVED, /* 03 */ KEY_RESERVED, /* 04 */ KEY_RESERVED, /* 05 */ KEY_RESERVED, /* 06 */ KEY_RESERVED, /* 07 */ KEY_RESERVED, /* 08 */ KEY_RESERVED, /* 09 */ KEY_TAB, /* 0A */ KEY_LINEFEED, /* 0B */ KEY_RESERVED, /* 0C */ KEY_RESERVED, /* 0D */ KEY_ENTER, /* 0E */ KEY_RESERVED, /* 0F */ KEY_RESERVED, /* 10 */ KEY_RESERVED, /* 11 */ KEY_RESERVED, /* 12 */ KEY_RESERVED, /* 13 */ KEY_RESERVED, /* 14 */ KEY_RESERVED, /* 15 */ KEY_RESERVED, /* 16 */ KEY_RESERVED, /* 17 */ KEY_RESERVED, /* 18 */ KEY_RESERVED, /* 19 */ KEY_RESERVED, /* 1A */ KEY_RESERVED, /* 1B */ KEY_ESC, /* 1C */ KEY_RESERVED, /* 1D */ KEY_RESERVED, /* 1E */ KEY_RESERVED, /* 1F */ KEY_RESERVED, /* */ KEY_SPACE, /* ! */ KEY_1 | 0x80, /* " */ KEY_APOSTROPHE | 0x80, /* # */ KEY_3 | 0x80, /* $ */ KEY_4 | 0x80, /* % */ KEY_5 | 0x80, /* & */ KEY_7 | 0x80, /* ' */ KEY_APOSTROPHE, /* ( */ KEY_9 | 0x80, /* ) */ KEY_0 | 0x80, /* * */ KEY_8 | 0x80, /* + */ KEY_EQUAL | 0x80, /* , */ KEY_COMMA, /* - */ KEY_MINUS, /* . */ KEY_DOT, /* / */ KEY_SLASH, /* 0 */ KEY_0, /* 1 */ KEY_1, /* 2 */ KEY_2, /* 3 */ KEY_3, /* 4 */ KEY_4, /* 5 */ KEY_5, /* 6 */ KEY_6, /* 7 */ KEY_7, /* 8 */ KEY_8, /* 9 */ KEY_9, /* : */ KEY_SEMICOLON | 0x80, /* ; */ KEY_SEMICOLON, /* < */ KEY_COMMA | 0x80, /* = */ KEY_EQUAL, /* > */ KEY_DOT | 0x80, /* ? */ KEY_SLASH | 0x80, /* @ */ KEY_2 | 0x80, /* A */ KEY_A | 0x80, /* B */ KEY_B | 0x80, /* C */ KEY_C | 0x80, /* D */ KEY_D | 0x80, /* E */ KEY_E | 0x80, /* F */ KEY_F | 0x80, /* G */ KEY_G | 0x80, /* H */ KEY_H | 0x80, /* I */ KEY_I | 0x80, /* J */ KEY_J | 0x80, /* K */ KEY_K | 0x80, /* L */ KEY_L | 0x80, /* M */ KEY_M | 0x80, /* N */ KEY_N | 0x80, /* O */ KEY_O | 0x80, /* P */ KEY_P | 0x80, /* Q */ KEY_Q | 0x80, /* R */ KEY_R | 0x80, /* S */ KEY_S | 0x80, /* T */ KEY_T | 0x80, /* U */ KEY_U | 0x80, /* V */ KEY_V | 0x80, /* W */ KEY_W | 0x80, /* X */ KEY_X | 0x80, /* Y */ KEY_Y | 0x80, /* Z */ KEY_Z | 0x80, /* [ */ KEY_LEFTBRACE, /* \ */ KEY_BACKSLASH, /* ] */ KEY_RIGHTBRACE, /* ^ */ KEY_6 | 0x80, /* _ */ KEY_MINUS | 0x80, /* ` */ KEY_GRAVE, /* a */ KEY_A, /* b */ KEY_B, /* c */ KEY_C, /* d */ KEY_D, /* e */ KEY_E, /* f */ KEY_F, /* g */ KEY_G, /* h */ KEY_H, /* i */ KEY_I, /* j */ KEY_J, /* k */ KEY_K, /* l */ KEY_L, /* m */ KEY_M, /* n */ KEY_N, /* o */ KEY_O, /* p */ KEY_P, /* q */ KEY_Q, /* r */ KEY_R, /* s */ KEY_S, /* t */ KEY_T, /* u */ KEY_U, /* v */ KEY_V, /* w */ KEY_W, /* x */ KEY_X, /* y */ KEY_Y, /* z */ KEY_Z, /* { */ KEY_LEFTBRACE | 0x80, /* | */ KEY_BACKSLASH | 0x80, /* } */ KEY_RIGHTBRACE | 0x80, /* ~ */ KEY_GRAVE | 0x80, }; if ((unsigned char)c < sizeof(KEYCODES) / sizeof(KEYCODES[0])) { c = KEYCODES[(unsigned char)c]; if (c & 0x80) { keyctl(KEY_LEFTSHIFT, 1); keycode(c & ~0x80); keyctl(KEY_LEFTSHIFT, 0); } else if (c) keycode(c); } } void keycode(int code) { keyctl(code, 1); keyctl(code, 0); } void keyctl(int code, int value) { emit(EV_KEY, code, value); emit(EV_SYN, SYN_REPORT, 0); } private: void emit(int type, int code, int val) { struct input_event ie; ie.type = type; ie.code = code; ie.value = val; /* timestamp values below are ignored */ ie.time.tv_sec = 0; ie.time.tv_usec = 0; write(_fd, &ie, sizeof(ie)); } int _fd; }; class Input { public: typedef struct { uint16_t code; uint16_t value; } remap_t; Input(const char *name1, const char *name2, const remap_t *remap = nullptr) : _thid(0) , _fd(-1) , _enable(false) , _callback(nullptr) , _argument(nullptr) , _remap(nullptr) { _name1 = name1; _name2 = name2; _remap = remap; } virtual ~Input(void) { stop(); } void callback(input_cb_t cb, void *arg = nullptr) { _callback = cb; _argument = arg; } bool start(void) { return _enable = (pthread_create(&_thid, NULL, run0, this) == 0); } void stop(void) { stop0(); _enable = false; } private: void stop0(void) { if (_fd != -1) { ioctl(_fd, EVIOCGRAB, 0); close(_fd); _fd = -1; } } void run(void) { bool first = true; int nd, wd; if ((nd = inotify_init1(IN_CLOEXEC)) == -1) printf("[ERROR] inotify_init1(IN_CLOEXEC): %s\n", strerror(errno)); else { if ((wd = inotify_add_watch(nd, "/dev/input", IN_CREATE)) == -1) printf("[ERROR] inotify_add_watch(\"/dev/input\", IN_CREATE): %s\n", strerror(errno)); else { while (_enable) { ssize_t n; if (_fd != -1) { struct input_event input; while ((n = read(_fd, &input, sizeof(input))) == sizeof(input)) { if (_callback) { if (_remap) { for (const remap_t *map = _remap; map->code; ++map) { if (input.code == map->code) { input.code = map->value; break; } } } _callback(&input, _argument); } } if (n <= 0) stop0(); } else { struct inotify_event inotify[16]; if (first || read(nd, inotify, sizeof(inotify)) > 0) { DIR *dir; first = false; if ((dir = opendir("/dev/input"))) { struct dirent *entry; while ((entry = readdir(dir))) { if (strncmp(entry->d_name, "event", 5) == 0) { char path[PATH_MAX]; int fd; snprintf(path, sizeof(path), "/sys/class/input/%s/device/name", entry->d_name); if ((fd = open(path, O_RDONLY)) == -1) printf("[ERROR] open(\"%s\"): %s\n", path, strerror(errno)); else { n = read(fd, path, sizeof(path)); close(fd); if (n > 0) { path[n] = 0; if (strstr(path, _name1.c_str()) && strstr(path, _name2.c_str())) { snprintf(path, sizeof(path), "/dev/input/%s", entry->d_name); if ((_fd = open(path, O_RDONLY)) == -1) printf("[ERROR] open(\"%s\"): %s\n", path, strerror(errno)); else ioctl(_fd, EVIOCGRAB, 1); break; } } } } } closedir(dir); } } } } inotify_rm_watch(nd, wd); } close(nd); } } static void *run0(void *arg) { ((Input *)arg)->run(); return NULL; } std::string _name1; std::string _name2; pthread_t _thid; int _fd; bool _enable; input_cb_t _callback; void *_argument; const remap_t *_remap; }; class Remocon { public: static const int BUTTON_A = KEY_VOLUMEUP; static const int BUTTON_B = KEY_VOLUMEDOWN; Remocon(void) : _callback(nullptr) , _argument(nullptr) , _buttun_b(0) { } virtual ~Remocon(void) { stop(); for (auto input = _inputs.begin(); input != _inputs.end(); ++input) delete *input; _inputs.clear(); } void add(const char *name1, const char *name2, const Input::remap_t *remap = nullptr) { /* AB Shutter 3 */ if ((strcmp(name1, "AB Shutter") == 0) && (strcmp(name2, "Keyboard") == 0)) { /* support DAISO AB Shutter 3 */ add(name1, "Control"); /* remap KEY_ENTER to KEY_VOLUMEDOWN */ if (remap == nullptr) { static const Input::remap_t ABS3[] = { { KEY_ENTER, KEY_VOLUMEDOWN }, { 0, 0 }, }; remap = ABS3; } } Input *input = new Input(name1, name2, remap); input->callback(input0, this); _inputs.push_back(input); } void callback(input_cb_t cb, void *arg = nullptr) { _callback = cb; _argument = arg; } void start(void) { for (auto input = _inputs.begin(); input != _inputs.end(); ++input) (*input)->start(); } void stop(void) { for (auto input = _inputs.begin(); input != _inputs.end(); ++input) (*input)->stop(); } private: void input(struct input_event *event) { if ((event->type == EV_KEY) && (event->value <= 1)) { #if DEBUG printf("code=%d, value=%d\n", event->code, event->value); #endif /* support DAISO AB Shutter 3 */ if (event->code == BUTTON_B) _buttun_b = event->value; if ((event->code == BUTTON_B) || (_buttun_b == 0)) { if (_callback) _callback(event, _argument); } } } static void input0(struct input_event *event, void *arg) { ((Remocon *)arg)->input(event); } std::vector<Input *> _inputs; input_cb_t _callback; void *_argument; int _buttun_b; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/* filepros.h - File Properties Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <stdio.h> #include "properties.h" class FileProps : public Properties { public: FileProps(void) { } virtual ~FileProps(void) { } bool load(const char* path) override { bool rv = false; FILE *fp = fopen(path, "r"); if (fp) { char buf[256]; clear(); rv = true; while (rv) { size_t len = fread(buf, 1, sizeof(buf), fp); if (len == 0) break; rv = parse(buf, len); } finish(); fclose(fp); } return rv; } bool save(const char* path, const char* header = nullptr) override { bool rv = false; FILE *fp = fopen(path, "w"); if (fp) { std::string str; toStringWithHeader(str, header); rv = fwrite(str.c_str(), 1, str.length(), fp) == str.length(); fclose(fp); } return rv; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
/* properties.h - Properties Library Copyright (c) 2023 Sasapea's Lab. All right reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include <vector> #include <string> #include <string.h> #include <stdlib.h> #include <stdint.h> class Properties { private: typedef struct { std::string name; std::string value; } property_t; typedef struct { std::string name; std::vector<property_t> properties; } section_t; std::vector<section_t> _sections; section_t _current; std::string _linebuf; protected: std::string& trim(std::string& str) { std::string::size_type pos; pos = str.find_first_not_of(' '); if (pos != std::string::npos) str.erase(0, pos); pos = str.find_last_not_of(' '); if (pos != std::string::npos) str.erase(pos + 1); return str; } section_t* getSection(const char* name) { if (name) { for (auto sec = _sections.begin(); sec != _sections.end(); ++sec) { if (strcasecmp(sec->name.c_str(), name) == 0) return &*sec; } } return nullptr; } void setSection(section_t &newSection) { section_t* sec = getSection(newSection.name.c_str()); if (!sec) _sections.push_back(newSection); else { for (auto prop = newSection.properties.begin(); prop != newSection.properties.end(); ++prop) setProperty(*sec, *prop); } } property_t* getProperty(section_t* section, const char* name) { if (section && name) { for (auto prop = section->properties.begin(); prop != section->properties.end(); ++prop) { if (strcasecmp(prop->name.c_str(), name) == 0) return &*prop; } } return nullptr; } void setProperty(section_t §ion, property_t &prop) { trim(prop.name); trim(prop.value); property_t* exists = getProperty(§ion, prop.name.c_str()); if (exists) exists->value = prop.value; else section.properties.push_back(prop); } bool parse(const char* buf, size_t len) { while (len--) { char c = *buf++; if (c >= ' ') _linebuf += c; else if (c == 0x0A) { trim(_linebuf); if (_linebuf.length()) { c = _linebuf[0]; if (c == '[') { if (_linebuf[_linebuf.length() - 1] != ']') return false; // format error. if (_current.properties.size() || _current.name.length()) setSection(_current); _current.name = _linebuf.substr(1, _linebuf.length() - 2); _current.properties.clear(); trim(_current.name); } else if ((c != ';') && (c != '#')) { int sep = _linebuf.find('='); if (sep < 0) sep = _linebuf.length(); property_t prop = {_linebuf.substr(0, sep), _linebuf.substr(sep + 1)}; setProperty(_current, prop); } _linebuf.clear(); } } } return true; } void finish(void) { if (_current.properties.size() || _current.name.length()) setSection(_current); _current.name.clear(); _current.properties.clear(); _linebuf.clear(); } public: Properties(void) { } virtual ~Properties(void) { } void clear(void) { _sections.clear(); _current.name.clear(); _current.properties.clear(); _linebuf.clear(); } std::vector<std::string>& getSectionNames(std::vector<std::string>& names) { names.clear(); for (auto sec = _sections.begin(); sec != _sections.end(); ++sec) names.push_back(sec->name); return names; } std::vector<std::string>& getPropertyNames(std::vector<std::string>& names, const char* section) { names.clear(); section_t* sec = getSection(section); if (sec) { for (auto prop = sec->properties.begin(); prop != sec->properties.end(); ++prop) names.push_back(prop->name); } return names; } void removeSection(const char* section) { if (section) { for (auto sec = _sections.begin(); sec != _sections.end(); ++sec) { if (strcasecmp(sec->name.c_str(), section) == 0) { _sections.erase(sec); break; } } } } void removeProperty(const char* section, const char* name) { if (name) { section_t* sec = getSection(section); if (sec) { for (auto prop = sec->properties.begin(); prop != sec->properties.end(); ++prop) { if (strcasecmp(prop->name.c_str(), name) == 0) { sec->properties.erase(prop); break; } } } } } void setProperty(const char* section, const char* name, const char* value) { if (section && name && value) { section_t newsec = {section, {{name, value}}}; setSection(newsec); } } void setPropertyInt(const char* section, const char* name, int value) { if (section && name) { char buf[16]; snprintf(buf, sizeof(buf), "%d", value); section_t newsec = {section, {{name, buf}}}; setSection(newsec); } } void setPropertyI64(const char* section, const char* name, int64_t value) { if (section && name) { char buf[128]; snprintf(buf, sizeof(buf), "%lld", (long long int)value); section_t newsec = {section, {{name, buf}}}; setSection(newsec); } } const char* getProperty(const char* section, const char* name, const char* defval = "") { property_t* prop = getProperty(getSection(section), name); return prop ? prop->value.c_str() : defval; } int getPropertyInt(const char* section, const char* name, int defval = 0) { const char* value = getProperty(section, name); return *value ? atoi(value) : defval; } int getPropertyI64(const char* section, const char* name, int64_t defval = 0) { const char* value = getProperty(section, name); return *value ? strtoll(value, NULL, 10) : defval; } std::string& toString(std::string &str) { std::vector<std::string> names; std::string props; str.clear(); getSectionNames(names); for (auto name = names.begin(); name != names.end(); ++name) str.append("[").append(*name).append("]\n").append(toString(props, name->c_str())); return str; } std::string& toString(std::string &str, const char* section) { if (section == nullptr) toString(str); else { std::vector<std::string> names; str.clear(); getPropertyNames(names, section); for (auto name = names.begin(); name != names.end(); ++name) str.append(*name).append("=").append(getProperty(section, name->c_str())).append("\n"); } return str; } std::string& toStringWithHeader(std::string &str, const char* header) { str.clear(); if (header && *header) { const char* p; const char* q; p = q = header; do { if ((*q == 0) || (*q == 0x0A)) { str.append("# ").append(p, (size_t)q - (size_t)p).append("\n"); p = q + 1; } } while (*q++); } std::string props; return str.append(toString(props)); } virtual bool load(const char* path) = 0; virtual bool save(const char* path, const char* header = nullptr) = 0; }; |