forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
video_state_python.c
407 lines (334 loc) · 9.53 KB
/
video_state_python.c
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "video_state_python.h"
#include "../dynamic.h"
#include "../libretro.h"
#include "../general.h"
#include <compat/strl.h>
#include <compat/posix_string.h>
#include "../input/input_common.h"
#include "../file_ops.h"
static PyObject* py_read_wram(PyObject *self, PyObject *args)
{
unsigned addr;
size_t max;
const uint8_t *data = (const uint8_t*)
pretro_get_memory_data(RETRO_MEMORY_SYSTEM_RAM);
(void)self;
if (!data)
{
Py_INCREF(Py_None);
return Py_None;
}
max = pretro_get_memory_size(RETRO_MEMORY_SYSTEM_RAM);
if (!PyArg_ParseTuple(args, "I", &addr))
return NULL;
if (addr >= max)
{
Py_INCREF(Py_None);
return Py_None;
}
return PyLong_FromLong(data[addr]);
}
static PyObject* py_read_vram(PyObject *self, PyObject *args)
{
unsigned addr;
size_t max;
const uint8_t *data = (const uint8_t*)
pretro_get_memory_data(RETRO_MEMORY_VIDEO_RAM);
(void)self;
if (!data)
{
Py_INCREF(Py_None);
return Py_None;
}
max = pretro_get_memory_size(RETRO_MEMORY_VIDEO_RAM);
if (!PyArg_ParseTuple(args, "I", &addr))
return NULL;
if (addr >= max)
{
Py_INCREF(Py_None);
return Py_None;
}
return PyLong_FromLong(data[addr]);
}
static PyObject *py_read_input(PyObject *self, PyObject *args)
{
unsigned user, key, i;
const struct retro_keybind *py_binds[MAX_USERS];
int16_t res = 0;
driver_t *driver = driver_get_ptr();
settings_t *settings = config_get_ptr();
for (i = 0; i < MAX_USERS; i++)
py_binds[i] = settings->input.binds[i];
(void)self;
if (!driver->input_data)
return PyBool_FromLong(0);
if (!PyArg_ParseTuple(args, "II", &user, &key))
return NULL;
if (user > MAX_USERS || user < 1 || key >= RARCH_FIRST_META_KEY)
return NULL;
if (!driver->block_libretro_input)
res = input_driver_state(py_binds, user - 1, RETRO_DEVICE_JOYPAD, 0, key);
return PyBool_FromLong(res);
}
static PyObject *py_read_analog(PyObject *self, PyObject *args)
{
unsigned user, index, id, i;
int16_t res = 0;
driver_t *driver = driver_get_ptr();
settings_t *settings = config_get_ptr();
const struct retro_keybind *py_binds[MAX_USERS];
for (i = 0; i < MAX_USERS; i++)
py_binds[i] = settings->input.binds[i];
(void)self;
if (!driver->input_data)
return PyBool_FromLong(0);
if (!PyArg_ParseTuple(args, "III", &user, &index, &id))
return NULL;
if (user > MAX_USERS || user < 1 || index > 1 || id > 1)
return NULL;
res = input_driver_state(py_binds, user - 1, RETRO_DEVICE_ANALOG, index, id);
return PyFloat_FromDouble((double)res / 0x7fff);
}
static PyMethodDef RarchMethods[] = {
{ "read_wram", py_read_wram, METH_VARARGS, "Read WRAM from system." },
{ "read_vram", py_read_vram, METH_VARARGS, "Read VRAM from system." },
{ "input", py_read_input, METH_VARARGS, "Read input state from system." },
{ "input_analog", py_read_analog, METH_VARARGS, "Read analog input state from system." },
{ NULL, NULL, 0, NULL }
};
#define DECL_ATTR_RETRO(attr) PyObject_SetAttrString(mod, #attr, PyLong_FromLong(RETRO_DEVICE_ID_JOYPAD_##attr))
static void py_set_attrs(PyObject *mod)
{
DECL_ATTR_RETRO(B);
DECL_ATTR_RETRO(Y);
DECL_ATTR_RETRO(SELECT);
DECL_ATTR_RETRO(START);
DECL_ATTR_RETRO(UP);
DECL_ATTR_RETRO(DOWN);
DECL_ATTR_RETRO(LEFT);
DECL_ATTR_RETRO(RIGHT);
DECL_ATTR_RETRO(A);
DECL_ATTR_RETRO(X);
DECL_ATTR_RETRO(L);
DECL_ATTR_RETRO(R);
DECL_ATTR_RETRO(L2);
DECL_ATTR_RETRO(R2);
DECL_ATTR_RETRO(L3);
DECL_ATTR_RETRO(R3);
PyObject_SetAttrString(mod, "ANALOG_LEFT",
PyLong_FromLong(RETRO_DEVICE_INDEX_ANALOG_LEFT));
PyObject_SetAttrString(mod, "ANALOG_RIGHT",
PyLong_FromLong(RETRO_DEVICE_INDEX_ANALOG_RIGHT));
PyObject_SetAttrString(mod, "ANALOG_X",
PyLong_FromLong(RETRO_DEVICE_ID_ANALOG_X));
PyObject_SetAttrString(mod, "ANALOG_Y",
PyLong_FromLong(RETRO_DEVICE_ID_ANALOG_Y));
}
static PyModuleDef RarchModule = {
PyModuleDef_HEAD_INIT, "rarch", NULL, -1, RarchMethods,
NULL, NULL, NULL, NULL
};
static PyObject* PyInit_Retro(void)
{
PyObject *mod = PyModule_Create(&RarchModule);
if (!mod)
return NULL;
py_set_attrs(mod);
return mod;
}
struct py_state
{
PyObject *main;
PyObject *dict;
PyObject *inst;
bool warned_ret;
bool warned_type;
};
static char *dupe_newline(const char *str)
{
unsigned size;
char *ret = NULL;
if (!str)
return NULL;
size = strlen(str) + 2;
ret = (char*)malloc(size);
if (!ret)
return NULL;
strlcpy(ret, str, size);
ret[size - 2] = '\n';
ret[size - 1] = '\0';
return ret;
}
/* Need to make sure that first-line indentation is 0. */
static char *align_program(const char *program)
{
size_t prog_size;
char *new_prog = NULL;
char *save = NULL;
char *line = NULL;
unsigned skip_chars = 0;
char *prog = strdup(program);
if (!prog)
return NULL;
prog_size = strlen(program) + 1;
new_prog = (char*)calloc(1, prog_size);
if (!new_prog)
{
free(prog);
return NULL;
}
line = dupe_newline(strtok_r(prog, "\n", &save));
if (!line)
{
free(prog);
free(new_prog);
return NULL;
}
while (isblank(line[skip_chars]) && line[skip_chars])
skip_chars++;
while (line)
{
unsigned length = strlen(line);
unsigned skip_len = skip_chars > length ? length : skip_chars;
strlcat(new_prog, line + skip_len, prog_size);
free(line);
line = dupe_newline(strtok_r(NULL, "\n", &save));
}
free(prog);
return new_prog;
}
py_state_t *py_state_new(const char *script,
unsigned is_file, const char *pyclass)
{
py_state_t *handle;
PyObject *hook;
RARCH_LOG("Initializing Python runtime ...\n");
PyImport_AppendInittab("rarch", &PyInit_Retro);
Py_Initialize();
RARCH_LOG("Initialized Python runtime.\n");
handle = (py_state_t*)calloc(1, sizeof(*handle));
hook = NULL;
handle->main = PyImport_AddModule("__main__");
if (!handle->main)
goto error;
Py_INCREF(handle->main);
if (is_file)
{
/* Have to hack around the fact that the FILE struct
* isn't standardized across environments.
* PyRun_SimpleFile() breaks on Windows because it's
* compiled with MSVC. */
ssize_t len;
char *script_ = NULL;
bool ret = read_file(script, (void**)&script_, &len);
if (!ret || len < 0)
{
RARCH_ERR("Python: Failed to read script\n");
goto error;
}
PyRun_SimpleString(script_);
free(script_);
}
else
{
char *script_ = align_program(script);
if (script_)
{
PyRun_SimpleString(script_);
free(script_);
}
}
RARCH_LOG("Python: Script loaded.\n");
handle->dict = PyModule_GetDict(handle->main);
if (!handle->dict)
{
RARCH_ERR("Python: PyModule_GetDict() failed.\n");
goto error;
}
Py_INCREF(handle->dict);
hook = PyDict_GetItemString(handle->dict, pyclass);
if (!hook)
{
RARCH_ERR("Python: PyDict_GetItemString() failed.\n");
goto error;
}
handle->inst = PyObject_CallFunction(hook, NULL);
if (!handle->inst)
{
RARCH_ERR("Python: PyObject_CallFunction() failed.\n");
goto error;
}
Py_INCREF(handle->inst);
return handle;
error:
PyErr_Print();
PyErr_Clear();
py_state_free(handle);
return NULL;
}
void py_state_free(py_state_t *handle)
{
if (!handle)
return;
PyErr_Print();
PyErr_Clear();
Py_CLEAR(handle->inst);
Py_CLEAR(handle->dict);
Py_CLEAR(handle->main);
free(handle);
Py_Finalize();
}
float py_state_get(py_state_t *handle, const char *id,
unsigned frame_count)
{
unsigned i;
float retval;
PyObject *ret = NULL;
settings_t *settings = config_get_ptr();
for (i = 0; i < MAX_USERS; i++)
{
input_push_analog_dpad(settings->input.binds[i],
settings->input.analog_dpad_mode[i]);
input_push_analog_dpad(settings->input.autoconf_binds[i],
settings->input.analog_dpad_mode[i]);
}
ret = PyObject_CallMethod(handle->inst, (char*)id, (char*)"I", frame_count);
for (i = 0; i < MAX_USERS; i++)
{
input_pop_analog_dpad(settings->input.binds[i]);
input_pop_analog_dpad(settings->input.autoconf_binds[i]);
}
if (!ret)
{
if (!handle->warned_ret)
{
RARCH_WARN("Didn't get return value from script. Bug?\n");
PyErr_Print();
PyErr_Clear();
}
handle->warned_ret = true;
return 0.0f;
}
retval = (float)PyFloat_AsDouble(ret);
Py_DECREF(ret);
return retval;
}