summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinWX/Config/AdvancedConfigPane.cpp
blob: b7957d75120ccf2963e0f4f4cd8f1108269ac061 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "DolphinWX/Config/AdvancedConfigPane.h"

#include <cmath>

#include <wx/checkbox.h>
#include <wx/datectrl.h>
#include <wx/dateevt.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/time.h>
#include <wx/timectrl.h>

#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/HW/SystemTimers.h"
#include "DolphinWX/DolphinSlider.h"
#include "DolphinWX/WxEventUtils.h"

AdvancedConfigPane::AdvancedConfigPane(wxWindow* parent, wxWindowID id) : wxPanel(parent, id)
{
  InitializeGUI();
  LoadGUIValues();
  BindEvents();
}

void AdvancedConfigPane::InitializeGUI()
{
  m_clock_override_checkbox = new wxCheckBox(this, wxID_ANY, _("Enable CPU Clock Override"));
  m_clock_override_slider =
      new DolphinSlider(this, wxID_ANY, 100, 0, 150, wxDefaultPosition, FromDIP(wxSize(200, -1)));
  m_clock_override_text = new wxStaticText(this, wxID_ANY, "");

  m_custom_rtc_checkbox = new wxCheckBox(this, wxID_ANY, _("Enable Custom RTC"));
  m_custom_rtc_date_picker = new wxDatePickerCtrl(this, wxID_ANY);
  // The Wii System Menu only allows configuring a year between 2000 and 2035.
  // However, the GameCube main menu (IPL) allows setting a year between 2000 and 2099,
  // which is why we use that range here. The Wii still deals OK with dates beyond 2035
  // and simply clamps them to 2035-12-31.
  m_custom_rtc_date_picker->SetRange(wxDateTime(1, wxDateTime::Jan, 2000),
                                     wxDateTime(31, wxDateTime::Dec, 2099));
  m_custom_rtc_time_picker = new wxTimePickerCtrl(this, wxID_ANY);

  wxStaticText* const clock_override_description =
      new wxStaticText(this, wxID_ANY, _("Higher values can make variable-framerate games "
                                         "run at a higher framerate, at the expense of CPU. "
                                         "Lower values can make variable-framerate games "
                                         "run at a lower framerate, saving CPU.\n\n"
                                         "WARNING: Changing this from the default (100%) "
                                         "can and will break games and cause glitches. "
                                         "Do so at your own risk. Please do not report "
                                         "bugs that occur with a non-default clock. "));

  wxStaticText* const custom_rtc_description = new wxStaticText(
      this, wxID_ANY,
      _("This setting allows you to set a custom real time clock (RTC) separate "
        "from your current system time.\n\nIf you're unsure, leave this disabled."));

#ifdef __APPLE__
  clock_override_description->Wrap(550);
  custom_rtc_description->Wrap(550);
#else
  clock_override_description->Wrap(FromDIP(400));
  custom_rtc_description->Wrap(FromDIP(400));
#endif

  const int space5 = FromDIP(5);

  wxBoxSizer* const clock_override_slider_sizer = new wxBoxSizer(wxHORIZONTAL);
  clock_override_slider_sizer->Add(m_clock_override_slider, 1);
  clock_override_slider_sizer->Add(m_clock_override_text, 1, wxLEFT, space5);

  wxStaticBoxSizer* const cpu_options_sizer =
      new wxStaticBoxSizer(wxVERTICAL, this, _("CPU Options"));
  cpu_options_sizer->AddSpacer(space5);
  cpu_options_sizer->Add(m_clock_override_checkbox, 0, wxEXPAND | wxLEFT | wxRIGHT, space5);
  cpu_options_sizer->AddSpacer(space5);
  cpu_options_sizer->Add(clock_override_slider_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT, space5);
  cpu_options_sizer->AddSpacer(space5);
  cpu_options_sizer->Add(clock_override_description, 0, wxEXPAND | wxLEFT | wxRIGHT, space5);
  cpu_options_sizer->AddSpacer(space5);

  wxFlexGridSizer* const custom_rtc_date_time_sizer =
      new wxFlexGridSizer(2, wxSize(space5, space5));
  custom_rtc_date_time_sizer->Add(m_custom_rtc_date_picker, 0, wxEXPAND);
  custom_rtc_date_time_sizer->Add(m_custom_rtc_time_picker, 0, wxEXPAND);

  wxStaticBoxSizer* const custom_rtc_sizer =
      new wxStaticBoxSizer(wxVERTICAL, this, _("Custom RTC Options"));
  custom_rtc_sizer->AddSpacer(space5);
  custom_rtc_sizer->Add(m_custom_rtc_checkbox, 0, wxLEFT | wxRIGHT, space5);
  custom_rtc_sizer->AddSpacer(space5);
  custom_rtc_sizer->Add(custom_rtc_date_time_sizer, 0, wxLEFT | wxRIGHT, space5);
  custom_rtc_sizer->AddSpacer(space5);
  custom_rtc_sizer->Add(custom_rtc_description, 0, wxLEFT | wxRIGHT, space5);
  custom_rtc_sizer->AddSpacer(space5);

  wxBoxSizer* const main_sizer = new wxBoxSizer(wxVERTICAL);
  main_sizer->AddSpacer(space5);
  main_sizer->Add(cpu_options_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT, space5);
  main_sizer->AddSpacer(space5);
  main_sizer->Add(custom_rtc_sizer, 0, wxEXPAND | wxLEFT | wxRIGHT, space5);
  main_sizer->AddSpacer(space5);

  SetSizer(main_sizer);
}

void AdvancedConfigPane::LoadGUIValues()
{
  int ocFactor = (int)(std::log2f(SConfig::GetInstance().m_OCFactor) * 25.f + 100.f + 0.5f);
  bool oc_enabled = SConfig::GetInstance().m_OCEnable;
  m_clock_override_checkbox->SetValue(oc_enabled);
  m_clock_override_slider->SetValue(ocFactor);
  m_clock_override_slider->Enable(oc_enabled);
  UpdateCPUClock();
  LoadCustomRTC();
}

void AdvancedConfigPane::BindEvents()
{
  m_clock_override_checkbox->Bind(wxEVT_CHECKBOX,
                                  &AdvancedConfigPane::OnClockOverrideCheckBoxChanged, this);
  m_clock_override_checkbox->Bind(wxEVT_UPDATE_UI, &AdvancedConfigPane::OnUpdateCPUClockControls,
                                  this);

  m_clock_override_slider->Bind(wxEVT_SLIDER, &AdvancedConfigPane::OnClockOverrideSliderChanged,
                                this);
  m_clock_override_slider->Bind(wxEVT_UPDATE_UI, &AdvancedConfigPane::OnUpdateCPUClockControls,
                                this);

  m_custom_rtc_checkbox->Bind(wxEVT_CHECKBOX, &AdvancedConfigPane::OnCustomRTCCheckBoxChanged,
                              this);
  m_custom_rtc_checkbox->Bind(wxEVT_UPDATE_UI, &WxEventUtils::OnEnableIfCoreNotRunning);

  m_custom_rtc_date_picker->Bind(wxEVT_DATE_CHANGED, &AdvancedConfigPane::OnCustomRTCDateChanged,
                                 this);
  m_custom_rtc_date_picker->Bind(wxEVT_UPDATE_UI, &AdvancedConfigPane::OnUpdateRTCDateTimeEntries,
                                 this);

  m_custom_rtc_time_picker->Bind(wxEVT_TIME_CHANGED, &AdvancedConfigPane::OnCustomRTCTimeChanged,
                                 this);
  m_custom_rtc_time_picker->Bind(wxEVT_UPDATE_UI, &AdvancedConfigPane::OnUpdateRTCDateTimeEntries,
                                 this);
}

void AdvancedConfigPane::OnClockOverrideCheckBoxChanged(wxCommandEvent& event)
{
  SConfig::GetInstance().m_OCEnable = m_clock_override_checkbox->IsChecked();
  m_clock_override_slider->Enable(SConfig::GetInstance().m_OCEnable);
  UpdateCPUClock();
}

void AdvancedConfigPane::OnClockOverrideSliderChanged(wxCommandEvent& event)
{
  // Vaguely exponential scaling?
  SConfig::GetInstance().m_OCFactor =
      std::exp2f((m_clock_override_slider->GetValue() - 100.f) / 25.f);
  UpdateCPUClock();
}

static wxDateTime GetCustomRTCDateTime()
{
  time_t timestamp = SConfig::GetInstance().m_customRTCValue;
  return wxDateTime(timestamp).ToUTC();
}

static wxDateTime CombineDateAndTime(const wxDateTime& date, const wxDateTime& time)
{
  wxDateTime datetime = date;
  datetime.SetHour(time.GetHour());
  datetime.SetMinute(time.GetMinute());
  datetime.SetSecond(time.GetSecond());

  return datetime;
}

void AdvancedConfigPane::OnCustomRTCCheckBoxChanged(wxCommandEvent& event)
{
  const bool checked = m_custom_rtc_checkbox->IsChecked();
  SConfig::GetInstance().bEnableCustomRTC = checked;
  m_custom_rtc_date_picker->Enable(checked);
  m_custom_rtc_time_picker->Enable(checked);
}

void AdvancedConfigPane::OnCustomRTCDateChanged(wxDateEvent& event)
{
  wxDateTime datetime = CombineDateAndTime(event.GetDate(), GetCustomRTCDateTime());
  UpdateCustomRTC(datetime);
}

void AdvancedConfigPane::OnCustomRTCTimeChanged(wxDateEvent& event)
{
  wxDateTime datetime = CombineDateAndTime(GetCustomRTCDateTime(), event.GetDate());
  UpdateCustomRTC(datetime);
}

void AdvancedConfigPane::UpdateCPUClock()
{
  int core_clock = SystemTimers::GetTicksPerSecond() / pow(10, 6);
  int percent = static_cast<int>(std::round(SConfig::GetInstance().m_OCFactor * 100.f));
  int clock = static_cast<int>(std::round(SConfig::GetInstance().m_OCFactor * core_clock));

  m_clock_override_text->SetLabel(
      SConfig::GetInstance().m_OCEnable ? wxString::Format("%d %% (%d MHz)", percent, clock) : "");
}

void AdvancedConfigPane::LoadCustomRTC()
{
  bool custom_rtc_enabled = SConfig::GetInstance().bEnableCustomRTC;
  m_custom_rtc_checkbox->SetValue(custom_rtc_enabled);

  wxDateTime datetime = GetCustomRTCDateTime();
  if (datetime.IsValid())
  {
    m_custom_rtc_date_picker->SetValue(datetime);
    m_custom_rtc_time_picker->SetValue(datetime);
  }

  // Make sure we have a valid custom RTC date and time
  // both when it was out of range as well as when it was invalid to begin with.
  datetime = CombineDateAndTime(m_custom_rtc_date_picker->GetValue(),
                                m_custom_rtc_time_picker->GetValue());
  UpdateCustomRTC(datetime);
}

void AdvancedConfigPane::UpdateCustomRTC(const wxDateTime& datetime)
{
  // We need GetValue() as GetTicks() only works up to 0x7ffffffe, which is in 2038.
  u32 timestamp = datetime.FromUTC().GetValue().GetValue() / 1000;
  SConfig::GetInstance().m_customRTCValue = timestamp;
}

void AdvancedConfigPane::OnUpdateCPUClockControls(wxUpdateUIEvent& event)
{
  if (!Core::IsRunning())
  {
    event.Enable(true);
    return;
  }

  event.Enable(!Core::WantsDeterminism());
}

void AdvancedConfigPane::OnUpdateRTCDateTimeEntries(wxUpdateUIEvent& event)
{
  event.Enable(!Core::IsRunning() && m_custom_rtc_checkbox->IsChecked());
}