PageRenderTime 71ms CodeModel.GetById 62ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 24/** \ingroup LIRC */
 25/*@{*/
 26
 27/*! \file lirc_callback.c
 28  \brief Capture device, callback implementation.
 29
 30  This file implements the callback mechanism specified by the capture
 31  interface. Look in the captureif.h header file.
 32
 33*/
 34
 35#include "lirc_srv.h"
 36#include <globaldefs.h>
 37
 38#include <stdint.h>
 39#include <stdio.h>
 40#include <assert.h>
 41#include <unistd.h>
 42#include <stdlib.h>
 43
 44static const unsigned int moduleMask = MODULEMASK_LIRC_CB;
 45
 46/** If enabled, call: <script> <prev> <now>, where <prev> is the
 47    previous charge in percent, and <now> is the current charge in
 48    percent. */
 49void CallBatteryChargeScript(const configuration* _config, int _prev, int _now);
 50
 51void RemoteConnected(void* _p)
 52{
 53   lirc_data* lc = (lirc_data*)_p;
 54
 55   BDREMOTE_DBG(lc->config->debug, "Remote connected.");
 56}
 57
 58/* Received some data from the ps3 remote.
 59 * Forward it to LIRC clients.
 60 */
 61void DataInd(void* _p, const char* _data, const int _size)
 62{
 63   lirc_data* lc = (lirc_data*)_p;
 64   queueData* qd = 0;
 65#if BDREMOTE_DEBUG
 66   assert(lc->magic0 == 0x15);
 67#endif /* BDREMOTE_DEBUG */
 68
 69   qd = queueDataInit(_data, _size);
 70   queueAdd(&lc->qu, qd);
 71}
 72
 73void RemoteBatteryCharge(void* _p, int _val)
 74{
 75   lirc_data* lc = (lirc_data*)_p;
 76
 77   if (lc->charge_percent_set)
 78      {
 79         if (lc->charge_percent != _val)
 80            {
 81               fprintf(printStream, "Battery charge changed, from %d %% to %d %%.\n", lc->charge_percent, _val);
 82               CallBatteryChargeScript(lc->config, lc->charge_percent, _val);
 83               lc->charge_percent = _val;
 84            }
 85      }
 86   else
 87      {
 88         lc->charge_percent     = _val;
 89         lc->charge_percent_set = 1;
 90
 91         CallBatteryChargeScript(lc->config, _val, _val);
 92         fprintf(printStream, "Battery charge %d %%.\n", lc->charge_percent);
 93      }
 94
 95}
 96
 97void RemoteDisconnected(void* _p)
 98{
 99   lirc_data* lc = (lirc_data*)_p;
100
101   BDREMOTE_DBG(lc->config->debug, "Remote disconnected.");
102}
103
104#define cmd_len 512
105
106void CallBatteryChargeScript(const configuration* _config, int _prev, int _now)
107{
108   int result = -1;
109   char cmd[cmd_len];
110
111   if (!_config->battery_script_set)
112      {
113         return;
114      }
115   
116   result = access(_config->battery_script, R_OK|X_OK);
117   if (result != 0)
118      {
119         BDREMOTE_DBG(_config->debug, "Unable to call battery script: call to access() failed.")
120         return;
121      }
122
123   result = snprintf(&cmd[0], cmd_len, "%s '%d' '%d' &", _config->battery_script, _prev, _now);
124   if (result <= 0)
125      {
126         BDREMOTE_DBG(_config->debug, "Unable to call battery script: call to sprintf() failed.")
127         return;
128      }
129
130   result = system(cmd);
131   if (result == -1)
132      {
133         BDREMOTE_DBG(_config->debug, "Unable to call battery script: call to system() failed.")
134      }
135
136   BDREMOTE_DBG(_config->debug, "Battery script called successfully.")
137}
138
139/*@}*/
140