src/platform/psp2/main.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "psp2-context.h"
7
8#include <mgba/internal/gba/gba.h>
9#include "feature/gui/gui-runner.h"
10#include <mgba-util/gui.h>
11#include <mgba-util/gui/font.h>
12#include <mgba-util/gui/file-select.h>
13#include <mgba-util/gui/menu.h>
14
15#include <psp2/apputil.h>
16#include <psp2/ctrl.h>
17#include <psp2/display.h>
18#include <psp2/kernel/processmgr.h>
19#include <psp2/kernel/threadmgr.h>
20#include <psp2/power.h>
21#include <psp2/sysmodule.h>
22#include <psp2/system_param.h>
23#include <psp2/touch.h>
24
25#include <vita2d.h>
26
27static void _drawStart(void) {
28 static int vcount = 0;
29 extern bool frameLimiter;
30 int oldVCount = vcount;
31 vcount = sceDisplayGetVcount();
32 vita2d_set_vblank_wait(frameLimiter && vcount + 1 >= oldVCount);
33 vita2d_start_drawing();
34 vita2d_clear_screen();
35}
36
37static void _drawEnd(void) {
38 vita2d_end_drawing();
39 vita2d_wait_rendering_done();
40 vita2d_swap_buffers();
41}
42
43static uint32_t _pollInput(const struct mInputMap* map) {
44 SceCtrlData pad;
45 sceCtrlPeekBufferPositive(0, &pad, 1);
46 int input = mInputMapKeyBits(map, PSP2_INPUT, pad.buttons, 0);
47
48 if (pad.buttons & SCE_CTRL_UP || pad.ly < 64) {
49 input |= 1 << GUI_INPUT_UP;
50 }
51 if (pad.buttons & SCE_CTRL_DOWN || pad.ly >= 192) {
52 input |= 1 << GUI_INPUT_DOWN;
53 }
54 if (pad.buttons & SCE_CTRL_LEFT || pad.lx < 64) {
55 input |= 1 << GUI_INPUT_LEFT;
56 }
57 if (pad.buttons & SCE_CTRL_RIGHT || pad.lx >= 192) {
58 input |= 1 << GUI_INPUT_RIGHT;
59 }
60
61 return input;
62}
63
64static enum GUICursorState _pollCursor(unsigned* x, unsigned* y) {
65 SceTouchData touch;
66 sceTouchPeek(SCE_TOUCH_PORT_FRONT, &touch, 1);
67 if (touch.reportNum < 1) {
68 return GUI_CURSOR_NOT_PRESENT;
69 }
70 *x = touch.report[0].x / 2;
71 *y = touch.report[0].y / 2;
72 return GUI_CURSOR_DOWN;
73}
74
75static int _batteryState(void) {
76 int charge = scePowerGetBatteryLifePercent();
77 int adapter = scePowerIsPowerOnline();
78 int state = 0;
79 if (adapter) {
80 state |= BATTERY_CHARGING;
81 }
82 charge /= 25;
83 return state | charge;
84}
85
86int main() {
87 vita2d_init();
88 struct GUIFont* font = GUIFontCreate();
89 struct mGUIRunner runner = {
90 .params = {
91 PSP2_HORIZONTAL_PIXELS, PSP2_VERTICAL_PIXELS,
92 font, "",
93 _drawStart, _drawEnd,
94 _pollInput, _pollCursor,
95 _batteryState,
96 0, 0,
97 },
98 .configExtra = (struct GUIMenuItem[]) {
99 {
100 .title = "Screen mode",
101 .data = "screenMode",
102 .submenu = 0,
103 .state = 0,
104 .validStates = (const char*[]) {
105 "With Background",
106 "Without Background",
107 "Stretched",
108 "Fit Aspect Ratio",
109 },
110 .nStates = 4
111 },
112 {
113 .title = "Camera",
114 .data = "camera",
115 .submenu = 0,
116 .state = 1,
117 .validStates = (const char*[]) {
118 "None",
119 "Front",
120 "Back",
121 },
122 .nStates = 3
123 }
124 },
125 .keySources = (struct GUIInputKeys[]) {
126 {
127 .name = "Vita Input",
128 .id = PSP2_INPUT,
129 .keyNames = (const char*[]) {
130 "Select",
131 0,
132 0,
133 "Start",
134 "Up",
135 "Right",
136 "Down",
137 "Left",
138 "L",
139 "R",
140 0, // L2?
141 0, // R2?
142 "\1\xC",
143 "\1\xA",
144 "\1\xB",
145 "\1\xD"
146 },
147 .nKeys = 16
148 },
149 { .id = 0 }
150 },
151 .nConfigExtra = 2,
152 .setup = mPSP2Setup,
153 .teardown = mPSP2Teardown,
154 .gameLoaded = mPSP2LoadROM,
155 .gameUnloaded = mPSP2UnloadROM,
156 .prepareForFrame = NULL,
157 .drawFrame = mPSP2Draw,
158 .drawScreenshot = mPSP2DrawScreenshot,
159 .paused = mPSP2Paused,
160 .unpaused = mPSP2Unpaused,
161 .incrementScreenMode = mPSP2IncrementScreenMode,
162 .setFrameLimiter = mPSP2SetFrameLimiter,
163 .pollGameInput = mPSP2PollInput
164 };
165
166 sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START);
167 sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG_WIDE);
168 sceSysmoduleLoadModule(SCE_SYSMODULE_PHOTO_EXPORT);
169 sceSysmoduleLoadModule(SCE_SYSMODULE_APPUTIL);
170
171 mGUIInit(&runner, "psvita");
172
173 int enterButton;
174 SceAppUtilInitParam initParam;
175 SceAppUtilBootParam bootParam;
176 memset(&initParam, 0, sizeof(SceAppUtilInitParam));
177 memset(&bootParam, 0, sizeof(SceAppUtilBootParam));
178 sceAppUtilInit(&initParam, &bootParam);
179 sceAppUtilSystemParamGetInt(SCE_SYSTEM_PARAM_ID_ENTER_BUTTON, &enterButton);
180 sceAppUtilShutdown();
181
182 if (enterButton == SCE_SYSTEM_PARAM_ENTER_BUTTON_CIRCLE) {
183 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_CROSS, GUI_INPUT_BACK);
184 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_CIRCLE, GUI_INPUT_SELECT);
185 } else {
186 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_CROSS, GUI_INPUT_SELECT);
187 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_CIRCLE, GUI_INPUT_BACK);
188 }
189 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_TRIANGLE, GUI_INPUT_CANCEL);
190 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_UP, GUI_INPUT_UP);
191 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_DOWN, GUI_INPUT_DOWN);
192 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_LEFT, GUI_INPUT_LEFT);
193 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_RIGHT, GUI_INPUT_RIGHT);
194 mPSP2MapKey(&runner.params.keyMap, SCE_CTRL_SQUARE, mGUI_INPUT_SCREEN_MODE);
195
196 scePowerSetArmClockFrequency(444);
197 mGUIRunloop(&runner);
198
199 vita2d_fini();
200 mGUIDeinit(&runner);
201
202 int pgfLoaded = sceSysmoduleIsLoaded(SCE_SYSMODULE_PGF);
203 if (pgfLoaded != SCE_SYSMODULE_LOADED) {
204 sceSysmoduleLoadModule(SCE_SYSMODULE_PGF);
205 }
206 GUIFontDestroy(font);
207 sceSysmoduleUnloadModule(SCE_SYSMODULE_PGF);
208
209 sceKernelExitProcess(0);
210 return 0;
211}