PageRenderTime 13ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lirc_srv.h

http://bdremote-ng.googlecode.com/
C++ Header | 162 lines | 46 code | 37 blank | 79 comment | 0 complexity | 9e574af361edc9f86081d0c2e9b05143 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/** @defgroup LIRC LIRC
 25
 26 *  This group contains a function used to change the UID/GID of the
 27 *  application to one with less priviledges.
 28
 29 *  @{
 30 */
 31
 32/*! \file lirc_srv.h
 33  \brief LIRC server.
 34
 35  This file contains a LIRC server. It's purpose is to translate
 36  bluetooth events received using a queue from the bluetooth
 37  interface into key press events which are understood by LIRC.
 38
 39*/
 40
 41#ifndef BD_LIRC_SRV_H
 42#define BD_LIRC_SRV_H
 43
 44#include <globaldefs.h>
 45
 46#include <bdrcfg.h>
 47#include <stdint.h>
 48
 49#include <pthread.h>
 50#include <sys/time.h>
 51
 52#include <q.h>
 53
 54/** The number of LIRC clients that can connect at the same time. */
 55#define MAX_CLIENTS 16
 56
 57/** Information used by the LIRC server. */
 58typedef struct
 59{
 60#if BDREMOTE_DEBUG
 61  /** Magic value, used for asserting. */
 62  int magic0;
 63#endif /* BDREMOTE_DEBUG */
 64  /** Configuration. */
 65  const configuration* config;
 66  /** Socked used to accept new LIRC clients. */
 67  int sockinet;
 68
 69  /** Client sockets. */
 70  int clis[MAX_CLIENTS];
 71  /** Number of client sockets. */
 72  int clin;
 73
 74  /* State information. */
 75
 76  /** Battery charge in percent. */
 77  int charge_percent;
 78
 79  /** Indictes if a charge was previously set.*/
 80  int charge_percent_set;
 81
 82  /** Mutex used. */
 83  pthread_mutex_t dataMutex;
 84
 85  /** Queue used to communicate BT events to lirc clients.
 86   * Thread safe.
 87   */
 88  queue qu;
 89
 90  /** Thread used to receive bluetooth events (keypresses) and
 91      sending them to LIRC in a format it understands.. */
 92  pthread_t thread;
 93
 94} lirc_data;
 95
 96/** Init data used by the LIRC server part of this application. */
 97void initLircData(lirc_data* _ld, const configuration* _config);
 98
 99/** Start a thread used to receive bluetooth events (keypresses) and
100    sending them to LIRC in a format it understands. */
101void startLircThread(lirc_data* _ld);
102
103/** Wait for the LIRC thread to terminate. */
104void waitForLircThread(lirc_data* _ld);
105
106/** Run a LIRC server. Should be run from the main thread of an
107    application. Blocks. */
108int lirc_server(configuration* _config, lirc_data* _lircdata);
109
110/** Broadcast a message to all connected LIRC sockets. */
111void broadcast_message(lirc_data* _lircdata, const char* _message);
112
113/** Destroy LIRC data used by the LIRC server part of this application. */
114void destroyLircData(lirc_data* _ld);
115
116/** Write a message to a socket. */
117int write_socket(int _fd, const char* _buf, int _len);
118
119/** Close sockets gracefully. */
120void nolinger(int sock);
121
122/** Remote a LIRC client from the list of clients receiving our messages. */
123void remove_client(lirc_data* _lircdata, int fd);
124
125/** Struct used to keep track of pressed key and repeat state. */
126typedef struct
127{
128  /** Indicates if a key is down. */
129  int keyDown;
130  /** Last sent key. */
131  int lastKey;
132  /** Clock used to keep time. */
133  struct timeval cl0;
134  /** Clock used to keep time. */
135  struct timeval cl1;
136
137  /** Number of elapsed miliseconds. */
138  unsigned long elapsed;
139
140  /** Repeat counter. */
141  int repeat_count;
142
143  /** Indicates that the driver is done waiting to begin stending
144      repeated keypresses. */
145  int done_waiting;
146
147} keyState;
148
149/* The following two functions should be used to get elapsed time
150   under one second. */
151
152/** Start keeping track of time. */
153void initTime(keyState* _ks);
154
155/** Update time counter. To avoid overflows, reset the counter
156    often. */
157void updateTime(keyState* _ks);
158
159#endif /* BD_LIRC_SRV_H */
160
161/*@}*/
162