-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
EmulationFragment.java
266 lines (217 loc) · 6.17 KB
/
EmulationFragment.java
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package org.dolphinemu.dolphinemu.fragments;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import org.dolphinemu.dolphinemu.BuildConfig;
import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.overlay.InputOverlay;
import org.dolphinemu.dolphinemu.utils.Log;
public final class EmulationFragment extends Fragment implements SurfaceHolder.Callback
{
public static final String FRAGMENT_TAG = BuildConfig.APPLICATION_ID + ".emulation_fragment";
private static final String ARGUMENT_GAME_PATH = BuildConfig.APPLICATION_ID + ".game_path";
private SharedPreferences mPreferences;
private Surface mSurface;
private InputOverlay mInputOverlay;
private Thread mEmulationThread;
private boolean mEmulationStarted;
private boolean mEmulationRunning;
public static EmulationFragment newInstance(String path)
{
EmulationFragment fragment = new EmulationFragment();
Bundle arguments = new Bundle();
arguments.putString(ARGUMENT_GAME_PATH, path);
fragment.setArguments(arguments);
return fragment;
}
/**
* Initialize anything that doesn't depend on the layout / views in here.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// So this fragment doesn't restart on configuration changes; i.e. rotation.
setRetainInstance(true);
mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
}
/**
* Initialize the UI and start emulation in here.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
String path = getArguments().getString(ARGUMENT_GAME_PATH);
NativeLibrary.SetFilename(path);
View contents = inflater.inflate(R.layout.fragment_emulation, container, false);
SurfaceView surfaceView = (SurfaceView) contents.findViewById(R.id.surface_emulation);
mInputOverlay = (InputOverlay) contents.findViewById(R.id.surface_input_overlay);
surfaceView.getHolder().addCallback(this);
// If the input overlay was previously disabled, then don't show it.
if (mInputOverlay != null)
{
if (!mPreferences.getBoolean("showInputOverlay", true))
{
mInputOverlay.setVisibility(View.GONE);
}
}
if (savedInstanceState == null)
{
mEmulationThread = new Thread(mEmulationRunner);
}
else
{
// Likely a rotation occurred.
// TODO Pass native code the Surface, which will have been recreated, from surfaceChanged()
// TODO Also, write the native code that will get the video backend to accept the new Surface as one of its own.
}
return contents;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
Button doneButton = (Button) view.findViewById(R.id.done_control_config);
if (doneButton != null)
{
doneButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
stopConfiguringControls();
}
});
}
}
@Override
public void onStart()
{
super.onStart();
startEmulation();
}
@Override
public void onStop()
{
super.onStop();
}
@Override
public void onDestroyView()
{
super.onDestroyView();
if (getActivity().isFinishing() && mEmulationStarted)
{
NativeLibrary.StopEmulation();
}
}
public void toggleInputOverlayVisibility()
{
SharedPreferences.Editor editor = mPreferences.edit();
// If the overlay is currently set to INVISIBLE
if (!mPreferences.getBoolean("showInputOverlay", false))
{
// Set it to VISIBLE
mInputOverlay.setVisibility(View.VISIBLE);
editor.putBoolean("showInputOverlay", true);
}
else
{
// Set it to INVISIBLE
mInputOverlay.setVisibility(View.GONE);
editor.putBoolean("showInputOverlay", false);
}
editor.apply();
}
public void refreshInputOverlay()
{
mInputOverlay.refreshControls();
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
Log.debug("[EmulationFragment] Surface created.");
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Log.debug("[EmulationFragment] Surface changed. Resolution: " + width + "x" + height);
mSurface = holder.getSurface();
NativeLibrary.SurfaceChanged(mSurface);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
Log.debug("[EmulationFragment] Surface destroyed.");
NativeLibrary.SurfaceDestroyed();
if (mEmulationRunning)
{
pauseEmulation();
}
}
private void startEmulation()
{
if (!mEmulationStarted)
{
Log.debug("[EmulationFragment] Starting emulation thread.");
mEmulationThread.start();
}
else
{
Log.debug("[EmulationFragment] Resuming emulation.");
NativeLibrary.UnPauseEmulation();
}
mEmulationRunning = true;
}
private void pauseEmulation()
{
Log.debug("[EmulationFragment] Pausing emulation.");
NativeLibrary.PauseEmulation();
mEmulationRunning = false;
}
/**
* Called by containing activity to tell the Fragment emulation is already stopping,
* so it doesn't try to stop emulation on its way to the garbage collector.
*/
public void notifyEmulationStopped()
{
mEmulationStarted = false;
mEmulationRunning = false;
}
private Runnable mEmulationRunner = new Runnable()
{
@Override
public void run()
{
mEmulationRunning = true;
mEmulationStarted = true;
while (mSurface == null)
if (!mEmulationRunning)
return;
Log.info("[EmulationFragment] Starting emulation: " + mSurface);
// Start emulation using the provided Surface.
NativeLibrary.Run();
}
};
public void startConfiguringControls()
{
getView().findViewById(R.id.done_control_config).setVisibility(View.VISIBLE);
mInputOverlay.setIsInEditMode(true);
}
public void stopConfiguringControls()
{
getView().findViewById(R.id.done_control_config).setVisibility(View.GONE);
mInputOverlay.setIsInEditMode(false);
}
public boolean isConfiguringControls()
{
return mInputOverlay.isInEditMode();
}
}