summaryrefslogtreecommitdiff
path: root/qt/events-qt.h
blob: 43987b7c4e9cb5ae23ef34129d4b0d3829e19ec7 (plain)
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
/* -*- C++ -*- */
/*
 * Software Bluetooth Hands-Free Implementation
 *
 * Copyright (C) 2006-2008 Sam Revitch <samr7@cs.washington.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

/*
 * The purpose of this module is to glue together the Qt library and
 * the user interface components with the framework-agnostic libhfp.
 */

#if !defined(__EVENTS_QT_H__)
#define __EVENTS_QT_H__

#include <qobject.h>
#include <qsocketnotifier.h>
#include <qtimer.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>

#include <libhfp/events.h>

using namespace libhfp;

class QtEiTimerNotifier : public QObject, public TimerNotifier {
	Q_OBJECT;
	bool timer_set;
public:
	void timerEvent(QTimerEvent *e) {
		killTimer(e->timerId());
		timer_set = false;
		(*this)(this);
	}
	virtual void Set(int msec) {
		assert(Registered());
		if (timer_set)
			Cancel();
		startTimer(msec);
		timer_set = true;
	}
	virtual void Cancel(void) {
		if (timer_set) {
			killTimers();
			timer_set = false;
		}
	}
	QtEiTimerNotifier(void) : QObject(0, 0), timer_set(false) {}
	virtual ~QtEiTimerNotifier() {
		Cancel();
	}
};
class QtEiSocketNotifier : public QObject, public SocketNotifier {
	Q_OBJECT;

public slots:
	void SocketNotify(int fh) { (*this)(this, fh); }
public:
	QSocketNotifier			*m_sn;
	QtEiSocketNotifier(int fh, bool nwrite)
		: m_sn(new QSocketNotifier(fh, (nwrite
						? QSocketNotifier::Write
						: QSocketNotifier::Read))) {
		connect(m_sn, SIGNAL(activated(int)),
			this, SLOT(SocketNotify(int)));
	}
	virtual void SetEnabled(bool enable) {
		m_sn->setEnabled(enable);
	}
	virtual ~QtEiSocketNotifier() {
		if (m_sn) {
			delete m_sn;
			m_sn = NULL;
		}
	}
};

class QtEventDispatchInterface : public DispatchInterface {
public:

public:
	/*
	 * Socket event reporting
	 */
	virtual SocketNotifier *NewSocket(int fh, bool nwrite)
		{ return new QtEiSocketNotifier(fh, nwrite); }
	virtual TimerNotifier *NewTimer(void)
		{ return new QtEiTimerNotifier; }
	virtual void LogVa(DispatchInterface::logtype_t lt, const char *fmt,
			   va_list ap) {
		vfprintf(stderr, fmt, ap);
		fprintf(stderr, "\n");
	}
	QtEventDispatchInterface() {}
};

#endif /* !defined(__EVENTS_QT_H__) */