From 7c14f8cf16921c0cc40b9edcd1c12dca54ef6bd3 Mon Sep 17 00:00:00 2001 From: James Browning Date: Fri, 17 May 2024 07:30:42 -0700 Subject: [PATCH 5/5] xgps: Add manual {dis.re}connect code ... The disconnect code does not always interoperate well. The remote hangup handling code still ends xgps. --- clients/xgps.py.in | 68 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 8 deletions(-) diff --git a/clients/xgps.py.in b/clients/xgps.py.in index ad0f50082..7f2d0adbb 100644 --- a/clients/xgps.py.in +++ b/clients/xgps.py.in @@ -992,17 +992,17 @@ frame * { topmenu.set_submenu(submenu) menui = Gtk.MenuItem(label="Connect") - # key, mod = Gtk.accelerator_parse("Q") - # menui.add_accelerator("activate", agr, key, mod, - # Gtk.AccelFlags.VISIBLE) - # menui.connect("activate", Gtk.main_quit) + key, mod = Gtk.accelerator_parse("N") + menui.add_accelerator("activate", agr, key, mod, + Gtk.AccelFlags.VISIBLE) + menui.connect("activate", self.new_connect) submenu.append(menui) menui = Gtk.MenuItem(label="Disconnect") - # key, mod = Gtk.accelerator_parse("Q") - # menui.add_accelerator("activate", agr, key, mod, - # Gtk.AccelFlags.VISIBLE) - # menui.connect("activate", Gtk.main_quit) + key, mod = Gtk.accelerator_parse("W") + menui.add_accelerator("activate", agr, key, mod, + Gtk.AccelFlags.VISIBLE) + menui.connect("activate", self.disconnect) submenu.append(menui) menui = Gtk.MenuItem(label="Quit") @@ -1694,6 +1694,58 @@ frame * { GLib.io_add_watch(daem.sock, GLib.PRIORITY_DEFAULT, GLib.IO_HUP, self.handle_hangup) + def disconnect(self, _): + """Disconnect from the remote gpsd instance.""" + if self.daemon.sock: + self.daemon.sock.close() + self.daemon.sock = None + + def new_connect(self, _): + """Connect to a new remote gpsd instance.""" + self.disconnect(_) + win = Gtk.Window(type=Gtk.WindowType.TOPLEVEL, title="Connect?") + + box = Gtk.Grid() + win.add(box) + + lbl = Gtk.Label(label="host, port and optional device") + box.attach(lbl, 0, 0, 2, 1) + + lbl = Gtk.Label(label="host") + box.attach(lbl, 0, 1, 1, 1) + host = Gtk.Entry() + host.set_text(options.host) + box.attach(host, 1, 1, 1, 1) + + lbl = Gtk.Label(label="port") + box.attach(lbl, 0, 2, 1, 1) + port = Gtk.SpinButton.new_with_range(1, 65535, 1) + port.set_numeric(True) + port.set_value(int(options.port)) + box.attach(port, 1, 2, 1, 1) + + lbl = Gtk.Label(label="device") + box.attach(lbl, 0, 3, 1, 1) + device = Gtk.Entry() + device.set_text(self.device) + box.attach(device, 1, 3, 1, 1) + + def do_connect(_): + lister = [host.get_text(), port.get_value_as_int(), device.get_text()] + daemon = gps.gps(host=lister[0], + port=lister[1], + mode=(gps.WATCH_ENABLE | gps.WATCH_JSON | + gps.WATCH_SCALED), + verbose=options.debug) + self.watch(daemon, lister[2]) + win.destroy() + + button = Gtk.Button.new_with_mnemonic("_Open") + button.connect("clicked", do_connect) + box.attach(button, 0, 4, 2, 1) + + win.show_all() + def handle_response(self, source, condition): """Handle ordinary I/O ready condition from the daemon.""" if -1 == self.daemon.read(): -- 2.43.0