/src/lirc_callback.c

http://bdremote-ng.googlecode.com/ · C · 140 lines · 77 code · 25 blank · 38 comment · 10 complexity · d87a402f07dbe71425bc5a08f01a65ae MD5 · raw file

  1. /*
  2. * bdremoteng - helper daemon for Sony(R) BD Remote Control
  3. * Based on bdremoted, written by Anton Starikov <antst@mail.ru>.
  4. *
  5. * Copyright (C) 2009 Michael Wojciechowski <wojci@wojci.dk>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. /** \ingroup LIRC */
  24. /*@{*/
  25. /*! \file lirc_callback.c
  26. \brief Capture device, callback implementation.
  27. This file implements the callback mechanism specified by the capture
  28. interface. Look in the captureif.h header file.
  29. */
  30. #include "lirc_srv.h"
  31. #include <globaldefs.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <assert.h>
  35. #include <unistd.h>
  36. #include <stdlib.h>
  37. static const unsigned int moduleMask = MODULEMASK_LIRC_CB;
  38. /** If enabled, call: <script> <prev> <now>, where <prev> is the
  39. previous charge in percent, and <now> is the current charge in
  40. percent. */
  41. void CallBatteryChargeScript(const configuration* _config, int _prev, int _now);
  42. void RemoteConnected(void* _p)
  43. {
  44. lirc_data* lc = (lirc_data*)_p;
  45. BDREMOTE_DBG(lc->config->debug, "Remote connected.");
  46. }
  47. /* Received some data from the ps3 remote.
  48. * Forward it to LIRC clients.
  49. */
  50. void DataInd(void* _p, const char* _data, const int _size)
  51. {
  52. lirc_data* lc = (lirc_data*)_p;
  53. queueData* qd = 0;
  54. #if BDREMOTE_DEBUG
  55. assert(lc->magic0 == 0x15);
  56. #endif /* BDREMOTE_DEBUG */
  57. qd = queueDataInit(_data, _size);
  58. queueAdd(&lc->qu, qd);
  59. }
  60. void RemoteBatteryCharge(void* _p, int _val)
  61. {
  62. lirc_data* lc = (lirc_data*)_p;
  63. if (lc->charge_percent_set)
  64. {
  65. if (lc->charge_percent != _val)
  66. {
  67. fprintf(printStream, "Battery charge changed, from %d %% to %d %%.\n", lc->charge_percent, _val);
  68. CallBatteryChargeScript(lc->config, lc->charge_percent, _val);
  69. lc->charge_percent = _val;
  70. }
  71. }
  72. else
  73. {
  74. lc->charge_percent = _val;
  75. lc->charge_percent_set = 1;
  76. CallBatteryChargeScript(lc->config, _val, _val);
  77. fprintf(printStream, "Battery charge %d %%.\n", lc->charge_percent);
  78. }
  79. }
  80. void RemoteDisconnected(void* _p)
  81. {
  82. lirc_data* lc = (lirc_data*)_p;
  83. BDREMOTE_DBG(lc->config->debug, "Remote disconnected.");
  84. }
  85. #define cmd_len 512
  86. void CallBatteryChargeScript(const configuration* _config, int _prev, int _now)
  87. {
  88. int result = -1;
  89. char cmd[cmd_len];
  90. if (!_config->battery_script_set)
  91. {
  92. return;
  93. }
  94. result = access(_config->battery_script, R_OK|X_OK);
  95. if (result != 0)
  96. {
  97. BDREMOTE_DBG(_config->debug, "Unable to call battery script: call to access() failed.")
  98. return;
  99. }
  100. result = snprintf(&cmd[0], cmd_len, "%s '%d' '%d' &", _config->battery_script, _prev, _now);
  101. if (result <= 0)
  102. {
  103. BDREMOTE_DBG(_config->debug, "Unable to call battery script: call to sprintf() failed.")
  104. return;
  105. }
  106. result = system(cmd);
  107. if (result == -1)
  108. {
  109. BDREMOTE_DBG(_config->debug, "Unable to call battery script: call to system() failed.")
  110. }
  111. BDREMOTE_DBG(_config->debug, "Battery script called successfully.")
  112. }
  113. /*@}*/