]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCDialogOld.cxx
Expose a radio function (receiveBeacon) to the Nasal subsystem
[flightgear.git] / src / ATCDCL / ATCDialogOld.cxx
1 // ATCDialog.cxx - Functions and classes to handle the pop-up ATC dialog
2 //
3 // Written by Alexander Kappes and David Luff, started February 2003.
4 //
5 // Copyright (C) 2003  Alexander Kappes and David Luff
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <simgear/compiler.h>
26
27 #include <simgear/structure/commands.hxx>
28 #include <simgear/props/props_io.hxx>
29
30 #include <Main/globals.hxx>
31 #include <GUI/gui.h>            // mkDialog
32 #include <GUI/new_gui.hxx>
33 #include <Main/fg_props.hxx>
34
35 #include "ATCDialogOld.hxx"
36 #include "ATC.hxx"
37 #include "ATCmgr.hxx"
38 #include "ATCutils.hxx"
39 #include <Airports/simple.hxx>
40 #include <ATC/CommStation.hxx>
41
42 #include <sstream>
43
44 using std::ostringstream;
45 using std::cerr;
46 using std::endl;
47
48 FGATCDialog *current_atcdialog;
49
50 // For the command manager - maybe eventually this should go in the built in command list
51 static bool do_ATC_dialog(const SGPropertyNode* arg) {
52         cerr << "Running ATCDCL do_ATC_dialog" << endl;
53         current_atcdialog->PopupDialog();
54         return(true);
55 }
56
57 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
58         current_atcdialog->FreqDialog();
59         return(true);
60 }
61
62 ATCMenuEntry::ATCMenuEntry() {
63   stationid    = "";
64   //stationfr    = 0;
65   transmission = "";
66   menuentry    = "";
67   callback_code = 0;
68 }
69
70 ATCMenuEntry::~ATCMenuEntry() {
71 }
72
73 void atcUppercase(string &s) {
74         for(unsigned int i=0; i<s.size(); ++i) {
75                 s[i] = toupper(s[i]);
76         }
77 }
78
79 // find child whose <name>...</name> entry matches 'name'
80 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
81         SGPropertyNode* p;
82
83         for (int i = 0; i < prop->nChildren(); i++)
84                 if ((p = getNamedNode(prop->getChild(i), name)))
85                         return p;
86
87         if (!strcmp(prop->getStringValue("name"), name))
88                 return prop;
89
90         return 0;
91 }
92
93
94 FGATCDialog::FGATCDialog() {
95         _callbackPending = false;
96         _callbackTimer = 0.0;
97         _callbackWait = 0.0;
98         _callbackPtr = NULL;
99         _callbackCode = 0;
100         _gui = (NewGUI *)globals->get_subsystem("gui");
101 }
102
103 FGATCDialog::~FGATCDialog() {
104 }
105
106 void FGATCDialog::Init() {
107         // Add ATC-dialog to the command list
108         //globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
109         // Add ATC-freq-search to the command list
110         //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
111
112         // initialize properties polled in Update()
113         //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
114         //globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
115 }
116
117 void FGATCDialog::Update(double dt) {
118         //static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
119         //string s = airport->getStringValue();
120         //if (!s.empty()) {
121         //      airport->setStringValue("");
122         //      FreqDisplay(s);
123         //}
124
125         //static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
126         //int n = trans_num->getIntValue();
127         //if (n >= 0) {
128         //      trans_num->setIntValue(-1);
129         //      PopupCallback(n);
130         //}
131
132         //if(_callbackPending) {
133         //      if(_callbackTimer > _callbackWait) {
134         //              _callbackPtr->ReceiveUserCallback(_callbackCode);
135         //              _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
136         //              _callbackPending = false;
137         //      } else {
138         //              _callbackTimer += dt;
139         //      }
140         //}
141 }
142
143 // Add an entry
144 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
145
146   ATCMenuEntry a;
147
148   a.stationid = station;
149   a.transmission = transmission;
150   a.menuentry = menutext;
151   a.callback_code = code;
152
153   (available_dialog[type])[station.c_str()].push_back(a);
154
155 }
156
157 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
158   atcmentry_vec_type* p = &((available_dialog[type])[station]);
159   atcmentry_vec_iterator current = p->begin();
160   while(current != p->end()) {
161     if(current->transmission == trans) current = p->erase(current);
162         else ++current;
163   }
164 }
165
166 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
167   atcmentry_vec_type* p = &((available_dialog[type])[station]);
168   atcmentry_vec_iterator current = p->begin();
169   while(current != p->end()) {
170     if(current->callback_code == code) current = p->erase(current);
171         else ++current;
172   }
173 }
174
175 // query the database whether the transmission is already registered;
176 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
177   atcmentry_vec_type* p = &((available_dialog[type])[station]);
178   atcmentry_vec_iterator current = p->begin();
179   for ( ; current != p->end() ; ++current ) {
180     if ( current->transmission == trans ) return true;
181   }
182   return false;
183 }
184
185 // query the database whether the transmission is already registered;
186 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
187   atcmentry_vec_type* p = &((available_dialog[type])[station]);
188   atcmentry_vec_iterator current = p->begin();
189   for ( ; current != p->end() ; ++current ) {
190     if ( current->callback_code == code ) return true;
191   }
192   return false;
193 }
194
195 // Display the ATC popup dialog box with options relevant to the users current situation.
196 void FGATCDialog::PopupDialog() {
197         const char *dialog_name = "atc-dialog";
198         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
199         if (!dlg)
200                 return;
201
202         _gui->closeDialog(dialog_name);
203
204         SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
205         // remove all transmission buttons
206         button_group->removeChildren("button", false);
207
208         string label;
209         FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
210         if (!pAtcMgr)
211         {
212             SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
213             return;
214         }
215         FGATC* atcptr = pAtcMgr->GetComm1ATCPointer();  // Hardwired to comm1 at the moment
216
217         if (!atcptr) {
218                 label = "Not currently tuned to any ATC service";
219                 mkDialog(label.c_str());
220                 return;
221         }
222
223         if(atcptr->GetType() == ATIS) {
224                 label = "Tuned to ATIS - no communication possible";
225                 mkDialog(label.c_str());
226                 return;
227         }
228
229         atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
230         atcmentry_vec_iterator current = atcmlist.begin();
231         atcmentry_vec_iterator last = atcmlist.end();
232         
233         if(!atcmlist.size()) {
234                 label = "No transmission available";
235                 mkDialog(label.c_str());
236                 return;
237         }
238
239         const int bufsize = 32;
240         char buf[bufsize];
241         // loop over all entries in atcmentrylist
242         for (int n = 0; n < 10; ++n) {
243                 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
244                 fgSetBool(buf, false);
245
246                 if (current == last)
247                         continue;
248
249                 // add transmission button (modified copy of <button-template>)
250                 SGPropertyNode *entry = button_group->getNode("button", n, true);
251                 copyProperties(button_group->getNode("button-template", true), entry);
252                 entry->removeChildren("enabled", true);
253                 entry->setStringValue("property", buf);
254                 entry->setIntValue("keynum", '1' + n);
255                 if (n == 0)
256                         entry->setBoolValue("default", true);
257
258                 snprintf(buf, bufsize, "%d", n + 1);
259                 string legend = string(buf) + ". " + current->menuentry;
260                 entry->setStringValue("legend", legend.c_str());
261                 entry->setIntValue("binding/value", n);
262                 current++;
263         }
264
265         _gui->showDialog(dialog_name);
266         return;
267 }
268
269 void FGATCDialog::PopupCallback(int num) {
270     FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
271     if (!pAtcMgr)
272     {
273         SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
274         return;
275     }
276         FGATC* atcptr = pAtcMgr->GetComm1ATCPointer();  // FIXME - Hardwired to comm1 at the moment
277
278         if (!atcptr)
279                 return;
280
281         if (atcptr->GetType() == TOWER) {
282                 //cout << "TOWER " << endl;
283                 //cout << "ident is " << atcptr->get_ident() << endl;
284                 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
285                 int size = atcmlist.size();
286                 if(size && num < size) {
287                         //cout << "Doing callback...\n";
288                         ATCMenuEntry a = atcmlist[num];
289                         atcptr->SetFreqInUse();
290                         string pilot = atcptr->GenText(a.transmission, a.callback_code);
291                         fgSetString("/sim/messages/pilot", pilot.c_str());
292                         // This is the user's speech getting displayed.
293                         _callbackPending = true;
294                         _callbackTimer = 0.0;
295                         _callbackWait = 5.0;
296                         _callbackPtr = atcptr;
297                         _callbackCode = a.callback_code;
298                 } else {
299                         //cout << "No options available...\n";
300                 }
301                 //cout << "Donded" << endl;
302         }
303 }
304
305 class AirportsWithATC : public FGAirport::AirportFilter
306 {
307 public:
308     virtual FGPositioned::Type maxType() const {
309       return FGPositioned::SEAPORT;
310     }
311   
312     virtual bool passAirport(FGAirport* aApt) const
313     {
314       return (!aApt->commStations().empty());
315     }
316 };
317
318 void FGATCDialog::FreqDialog() {
319         const char *dialog_name = "atc-freq-search";
320         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
321         if (!dlg)
322                 return;
323
324         _gui->closeDialog(dialog_name);
325
326         SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
327         // remove all dynamic airport/ATC buttons
328         button_group->removeChildren("button", false);
329
330
331   SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
332     fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
333
334     AirportsWithATC filt;
335     FGPositioned::List results = FGPositioned::findWithinRange(geod, 50.0, &filt);
336     FGPositioned::sortByRange(results, geod);
337     for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
338       
339         SGPropertyNode *entry = button_group->getNode("button", r, true);
340                 copyProperties(button_group->getNode("button-template", true), entry);
341                 entry->removeChildren("enabled", true);
342                 entry->setStringValue("legend", results[r]->ident());
343                 entry->setStringValue("binding[0]/value", results[r]->ident());
344     }
345     
346         // (un)hide message saying no things in range
347         SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
348         range_error->setBoolValue("enabled", !results.empty());
349
350         _gui->showDialog(dialog_name);
351 }
352
353 void FGATCDialog::FreqDisplay(string& ident) {
354         const char *dialog_name = "atc-freq-display";
355         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
356         if (!dlg)
357                 return;
358
359         _gui->closeDialog(dialog_name);
360
361         SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
362         // remove all frequency entries
363         freq_group->removeChildren("group", false);
364
365         atcUppercase(ident);
366         string label;
367
368         const FGAirport *a = fgFindAirportID(ident);
369         if (!a) {
370                 label = "Airport " + ident + " not found in database.";
371                 mkDialog(label.c_str());
372                 return;
373         }
374
375         // set title
376         label = ident + " Frequencies";
377         dlg->setStringValue("text/label", label.c_str());
378
379     const flightgear::CommStationList& comms(a->commStations());
380     if (comms.empty()) {
381         label = "No frequencies found for airport " + ident;
382                 mkDialog(label.c_str());
383                 return;
384     }
385     
386     int n = 0;
387     for (unsigned int c=0; c < comms.size(); ++c) {
388         flightgear::CommStation* comm = comms[c];
389         
390         // add frequency line (modified copy of <group-template>)
391                 SGPropertyNode *entry = freq_group->getNode("group", n, true);
392                 copyProperties(freq_group->getNode("group-template", true), entry);
393                 entry->removeChildren("enabled", true);
394
395         entry->setStringValue("text[0]/label", comm->ident());
396
397                 char buf[8];
398                 snprintf(buf, 8, "%.2f", comm->freqMHz());
399                 if(buf[5] == '3') buf[5] = '2';
400                 if(buf[5] == '8') buf[5] = '7';
401                 buf[7] = '\0';
402
403                 entry->setStringValue("text[1]/label", buf);
404         ++n;
405     }
406
407         _gui->showDialog(dialog_name);
408 }
409