Skip to content

Commit

Permalink
Add option to set link network port.
Browse files Browse the repository at this point in the history
Add field for the network port to the start link dialog, default is 5738
as before.

The port is stored in the user's options config.

Add TextCtrl support to UIntValidator for this.

Related: #594.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
  • Loading branch information
rkitover committed Feb 4, 2020
1 parent 98f3948 commit 136c094
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/gba/GBALink.cpp
Expand Up @@ -98,7 +98,10 @@ bool gba_link_enabled = false;
bool speedhack = true;

#define LOCAL_LINK_NAME "VBA link memory"
#define IP_LINK_PORT 5738

#include <stdint.h>

uint32_t IP_LINK_PORT = 5738;

#include "../common/Port.h"
#include "GBA.h"
Expand Down
4 changes: 4 additions & 0 deletions src/gba/GBALink.h
@@ -1,6 +1,10 @@
#ifndef GBA_GBALINK_H
#define GBA_GBALINK_H

#include <stdint.h>

extern uint32_t IP_LINK_PORT;

/**
* Link modes to be passed to InitLink
*/
Expand Down
8 changes: 8 additions & 0 deletions src/wx/guiinit.cpp
Expand Up @@ -89,6 +89,8 @@ static class NetLink_t : public wxEvtHandler {
if (!dlg->Validate() || !dlg->TransferDataFromWindow())
return;

IP_LINK_PORT = gopts.link_port;

if (!server) {
bool valid = SetLinkServerHost(gopts.link_host.utf8_str());

Expand Down Expand Up @@ -3181,6 +3183,11 @@ bool MainFrame::BindControls()
tc = SafeXRCCTRL<wxTextCtrl>(d, n); \
tc->SetValidator(wxPositiveDoubleValidator(&o)); \
} while (0)
#define getutc(n, o) \
do { \
tc = SafeXRCCTRL<wxTextCtrl>(d, n); \
tc->SetValidator(wxUIntValidator(&o)); \
} while (0)
#ifndef NO_LINK
{
net_link_handler.dlg = d;
Expand All @@ -3199,6 +3206,7 @@ bool MainFrame::BindControls()
addrber(lab, true);
gettc("ServerIP", gopts.link_host);
addrber(tc, true);
getutc("ServerPort", gopts.link_port);
wxWindow* okb = d->FindWindow(wxID_OK);

if (okb) // may be gone if style guidlines removed it
Expand Down
2 changes: 2 additions & 0 deletions src/wx/opts.cpp
Expand Up @@ -223,6 +223,7 @@ opt_desc opts[] = {
BOOLOPT("GBA/LinkAuto", "LinkAuto", wxTRANSLATE("Enable link at boot"), gopts.link_auto),
INTOPT("GBA/LinkFast", "SpeedOn", wxTRANSLATE("Enable faster network protocol by default"), linkHacks, 0, 1),
STROPT("GBA/LinkHost", "", wxTRANSLATE("Default network link client host"), gopts.link_host),
UINTOPT("GBA/LinkPort", "", wxTRANSLATE("Default network link port (server and client)"), gopts.link_port, 0, 65535),
INTOPT("GBA/LinkProto", "LinkProto", wxTRANSLATE("Default network protocol"), gopts.link_proto, 0, 1),
INTOPT("GBA/LinkTimeout", "LinkTimeout", wxTRANSLATE("Link timeout (ms)"), linkTimeout, 0, 9999999),
INTOPT("GBA/LinkType", "LinkType", wxTRANSLATE("Link cable type"), gopts.gba_link_type, 0, 5),
Expand Down Expand Up @@ -359,6 +360,7 @@ opts_t::opts_t()
autoPatch = true;
// quick fix for issues #48 and #445
link_host = "127.0.0.1";
link_port = 5738;
}

// for binary_search() and friends
Expand Down
1 change: 1 addition & 0 deletions src/wx/opts.h
Expand Up @@ -38,6 +38,7 @@ extern struct opts_t {
wxString gba_bios;
int gba_link_type;
wxString link_host;
uint32_t link_port;
int link_proto;
bool link_auto;
wxString gba_rom_dir;
Expand Down
53 changes: 42 additions & 11 deletions src/wx/widgets/wxmisc.cpp
@@ -1,4 +1,9 @@
// utility widgets

#include <cctype>
#include <string>
#include <algorithm>

#include "wx/wxmisc.h"
#include <wx/wx.h>
#include <wx/spinctrl.h>
Expand Down Expand Up @@ -443,36 +448,62 @@ wxUIntValidator::wxUIntValidator(uint32_t* _val)

bool wxUIntValidator::TransferToWindow()
{
wxSpinCtrl* ctrl = wxDynamicCast(GetWindow(), wxSpinCtrl);
if (ctrl && uint_val) {
ctrl->SetValue(*uint_val);
return true;
if (uint_val) {
wxSpinCtrl* spin = wxDynamicCast(GetWindow(), wxSpinCtrl);
if (spin) {
spin->SetValue(*uint_val);
return true;
}

wxTextCtrl* txt = wxDynamicCast(GetWindow(), wxTextCtrl);
if (txt) {
txt->SetValue(wxString::Format(wxT("%d"), *uint_val));
return true;
}
}

return false;
}

bool wxUIntValidator::TransferFromWindow()
{
wxSpinCtrl* ctrl = wxDynamicCast(GetWindow(), wxSpinCtrl);
if (ctrl && uint_val) {
*uint_val = ctrl->GetValue();
return true;
if (uint_val) {
wxSpinCtrl* spin = wxDynamicCast(GetWindow(), wxSpinCtrl);
if (spin) {
*uint_val = spin->GetValue();
return true;
}

wxTextCtrl* txt = wxDynamicCast(GetWindow(), wxTextCtrl);
if (txt) {
*uint_val = wxAtoi(txt->GetValue());
return true;
}
}

return false;
}

bool wxUIntValidator::Validate(wxWindow* parent)
{
(void)parent; // unused params
wxSpinCtrl* ctrl = wxDynamicCast(GetWindow(), wxSpinCtrl);

if (ctrl) {
if (ctrl->GetValue() >= 0) {
wxSpinCtrl* spin = wxDynamicCast(GetWindow(), wxSpinCtrl);
if (spin) {
if (spin->GetValue() >= 0) {
return true;
}
return false;
}

wxTextCtrl* txt = wxDynamicCast(GetWindow(), wxTextCtrl);
if (txt) {
std::string val = std::string(txt->GetValue().mb_str());

return !val.empty()
&& std::find_if(val.begin(), val.end(), [](unsigned char c) { return !std::isdigit(c); }) == val.end();
}

return false;
}

Expand Down
13 changes: 13 additions & 0 deletions src/wx/xrc/NetLink.xrc
Expand Up @@ -76,6 +76,19 @@
<flag>wxALL|wxEXPAND</flag>
<border>5</border>
</object>
<object class="sizeritem">
<object class="wxStaticText" name="ServerPortLab">
<label>Port:</label>
</object>
<flag>wxALL|wxALIGN_CENTRE_VERTICAL</flag>
<border>5</border>
</object>
<object class="sizeritem">
<object class="wxTextCtrl" name="ServerPort"/>
<option>1</option>
<flag>wxALL|wxEXPAND</flag>
<border>5</border>
</object>
</object>
<flag>wxEXPAND</flag>
</object>
Expand Down

0 comments on commit 136c094

Please sign in to comment.