Plasma5Support

location_gps.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Petri Damstén <damu@iki.fi>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "location_gps.h"
8#include "geolocdebug.h"
9
10Gpsd::Gpsd(gps_data_t *gpsdata)
11 : m_gpsdata(gpsdata)
12 , m_abort(false)
13{
14}
15
16Gpsd::~Gpsd()
17{
18 m_abort = true;
19 m_condition.wakeOne();
20 wait();
21}
22
23void Gpsd::update()
24{
25 if (!isRunning()) {
26 start();
27 } else {
28 m_condition.wakeOne();
29 }
30}
31
32void Gpsd::run()
33{
34#if defined(GPSD_API_MAJOR_VERSION) && (GPSD_API_MAJOR_VERSION >= 3) && defined(WATCH_ENABLE)
35 gps_stream(m_gpsdata, WATCH_ENABLE, nullptr);
36#else
37 gps_query(m_gpsdata, "w+x\n");
38#endif
39
40 while (!m_abort) {
42
43#if GPSD_API_MAJOR_VERSION >= 7
44 if (gps_read(m_gpsdata, NULL, 0) != -1) {
45#elif GPSD_API_MAJOR_VERSION >= 5
46 if (gps_read(m_gpsdata) != -1) {
47#else
48 if (gps_poll(m_gpsdata) != -1) {
49#endif
50#if GPSD_API_MAJOR_VERSION >= 9
51 if (m_gpsdata->online.tv_sec || m_gpsdata->online.tv_nsec) {
52#else
53 if (m_gpsdata->online) {
54#endif
55#if defined(STATUS_UNK) // STATUS_NO_FIX was renamed to STATUS_UNK without bumping API version
56 if (m_gpsdata->fix.status != STATUS_UNK) {
57#elif GPSD_API_MAJOR_VERSION >= 10
58 if (m_gpsdata->fix.status != STATUS_NO_FIX) {
59#else
60 if (m_gpsdata->status != STATUS_NO_FIX) {
61#endif
62 d["accuracy"] = 30;
63 d["latitude"] = QString::number(m_gpsdata->fix.latitude);
64 d["longitude"] = QString::number(m_gpsdata->fix.longitude);
65 }
66 }
67 }
68
69 Q_EMIT dataReady(d);
70
71 m_condition.wait(&m_mutex);
72 }
73}
74
75Gps::Gps(QObject *parent)
76 : GeolocationProvider(parent)
77 , m_gpsd(nullptr)
78#if GPSD_API_MAJOR_VERSION >= 5
79 , m_gpsdata(nullptr)
80#endif
81{
82#if GPSD_API_MAJOR_VERSION >= 5
83 m_gpsdata = new gps_data_t;
84 if (gps_open("localhost", DEFAULT_GPSD_PORT, m_gpsdata) != -1) {
85#else
86 gps_data_t *m_gpsdata = gps_open("localhost", DEFAULT_GPSD_PORT);
87 if (m_gpsdata) {
88#endif
89 qCDebug(DATAENGINE_GEOLOCATION) << "gpsd found.";
90 m_gpsd = new Gpsd(m_gpsdata);
91 connect(m_gpsd, SIGNAL(dataReady(Plasma5Support::DataEngine::Data)), this, SLOT(setData(Plasma5Support::DataEngine::Data)));
92 } else {
93 qCWarning(DATAENGINE_GEOLOCATION) << "gpsd not found";
94 }
95
96 setIsAvailable(m_gpsd);
97}
98
99Gps::~Gps()
100{
101 delete m_gpsd;
102#if GPSD_API_MAJOR_VERSION >= 5
103 delete m_gpsdata;
104#endif
105}
106
107void Gps::update()
108{
109 if (m_gpsd) {
110 m_gpsd->update();
111 }
112}
113
115
116#include "location_gps.moc"
#define K_PLUGIN_CLASS(classname)
Q_SCRIPTABLE Q_NOREPLY void start()
Q_EMITQ_EMIT
QObject * parent() const const
QString number(double n, char format, int precision)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isRunning() const const
bool wait(QDeadlineTimer deadline)
bool wait(QMutex *lockedMutex, QDeadlineTimer deadline)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.