Repository navigation
Expand file tree
/
Copy pathcommand.c
More file actions
7748 lines (7145 loc) · 250 KB
/
Copy pathcommand.c
File metadata and controls
7748 lines (7145 loc) · 250 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2021 - David G.F.
*
* 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 "input/input_driver.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
#include <locale.h>
#ifdef HAVE_NETWORKING
#include <net/net_compat.h>
#include <net/net_socket.h>
#endif
#include <lists/dir_list.h>
#include <file/file_path.h>
#include <streams/stdin_stream.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#include <features/features_cpu.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_CHEEVOS
#include "cheevos/cheevos.h"
#endif
#ifdef HAVE_GFX_WIDGETS
#include "gfx/gfx_widgets.h"
#endif
#ifdef HAVE_MENU
#include "menu/menu_driver.h"
#endif
#ifdef HAVE_NETWORKING
#include "network/netplay/netplay.h"
#endif
#include "audio/audio_driver.h"
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
#include "gfx/video_shader_parse.h"
#endif
#include "autosave.h"
#include "command.h"
#include "core.h"
#include "core_info.h"
#include "playlist.h"
#include <queues/task_queue.h>
#include <retro_atomic.h>
#include "cheat_manager.h"
#include "content.h"
#include "dynamic.h"
#include "file_path_special.h"
#include "list_special.h"
#include "paths.h"
#include "record/record_driver.h"
#include "retroarch.h"
#include "runloop.h"
#include "verbosity.h"
#include "version.h"
#include "version_git.h"
#include "tasks/task_content.h"
#ifdef HAVE_TRANSLATE
#include "accessibility.h"
#endif
#ifdef HAVE_SCREENSHOTS
#include "tasks/tasks_internal.h"
#endif
#include <compat/strl.h>
#ifdef __MACH__
#include <TargetConditionals.h>
#endif
#define CMD_BUF_SIZE 4096
/* Replies with a failure and why, and marks the reply as one: a line
* client reads the text, a structured one is told outright. The text
* opens with @name and ERROR, as a deferred reply's does. */
void command_reply_error(command_t *cmd, const char *name,
const char *fmt, ...)
{
char msg[512];
int n;
size_t _len;
va_list ap;
if (!cmd || !cmd->replier)
return;
/* Both of these report the length they wanted rather than the one
* they wrote, so each is brought back inside the buffer before the
* next writes past its end. */
if ((n = snprintf(msg, sizeof(msg), "%s ERROR ", name)) < 0)
return;
_len = (size_t)n;
if (_len >= sizeof(msg))
_len = sizeof(msg) - 1;
va_start(ap, fmt);
n = vsnprintf(msg + _len, sizeof(msg) - _len, fmt, ap);
va_end(ap);
if (n > 0)
{
_len += (size_t)n;
if (_len >= sizeof(msg))
_len = sizeof(msg) - 1;
}
cmd->error = true;
cmd->replier(cmd, msg, _len);
}
#ifdef HAVE_COMMAND
/* a queued STEPS was dropped since the reply now sent began */
static bool command_queue_ended;
#endif
static void command_post_state_loaded(void)
{
#ifdef HAVE_COMMAND
/* steps queued from the state it left would play on from this one */
command_run_frames_forget();
#endif
#ifdef HAVE_CHEEVOS
if (rcheevos_hardcore_active())
{
const char *_msg = msg_hash_to_str(MSG_CHEEVOS_HARDCORE_MODE_DISABLED);
rcheevos_pause_hardcore();
runloop_msg_queue_push(_msg, strlen(_msg), 0, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
#endif
#ifdef HAVE_NETWORKING
netplay_driver_ctl(RARCH_NETPLAY_CTL_LOAD_SAVESTATE, NULL);
#endif
{
video_driver_state_t *video_st =
video_state_get_ptr();
bool frame_time_counter_auto_reset =
config_get_ptr()->bools.frame_time_counter_auto_reset;
if (frame_time_counter_auto_reset)
video_st->frame_time_count = 0;
}
#if defined(HAVE_GFX_WIDGETS) && defined(HAVE_SCREENSHOTS)
{
gfx_widget_state_slot_show(dispwidget_get_ptr(), NULL, NULL);
}
#endif
}
#if defined(HAVE_COMMAND)
/* Generic command parse utilities */
static bool command_get_arg(const char *tok,
const char **arg, unsigned *index)
{
unsigned i;
for (i = 0; i < ARRAY_SIZE(map); i++)
{
if (string_is_equal(tok, map[i].str))
{
if (arg)
*arg = NULL;
if (index)
*index = i;
return true;
}
}
for (i = 0; i < ARRAY_SIZE(action_map); i++)
{
const char *str = strstr(tok, action_map[i].str);
if (str == tok)
{
const char *argument = str + strlen(action_map[i].str);
if (!argument)
return false;
if (*argument != ' ' && *argument != '\0')
return false;
/* past the separating space; a bare command gets "", not the
* byte after its terminator */
if (arg)
*arg = *argument ? argument + 1 : argument;
if (index)
*index = i;
return true;
}
}
return false;
}
static void command_parse_sub_msg(command_t *handle, const char *tok)
{
const char *arg = NULL;
unsigned index = 0;
handle->error = false;
if (command_get_arg(tok, &arg, &index))
{
if (arg)
{
if (!action_map[index].action(handle, arg))
RARCH_ERR("[Command] Command \"%s\" failed.\n", arg);
}
else
{
/* For MENU_TOGGLE, bypass the press-and-release mechanism
* by directly invoking the command event. This avoids
* timing issues with runahead (single-instance) where the
* 1-frame pulse from network commands can be lost. */
if (map[index].id == RARCH_MENU_TOGGLE)
command_event(CMD_EVENT_MENU_TOGGLE, NULL);
else
handle->state[map[index].id] = true;
}
}
else
RARCH_WARN(msg_hash_to_str(MSG_UNRECOGNIZED_COMMAND), tok);
}
static void command_parse_msg(command_t *handle, char *buf)
{
char *save = NULL;
char *tok = strtok_r(buf, "\n", &save);
unsigned gen = input_driver_command_generation();
while (tok)
{
/* a client ending its lines in CRLF leaves no \r on the last word;
* a \r anywhere else is the command's own */
size_t len = strlen(tok);
if (len && tok[len - 1] == '\r')
tok[len - 1] = '\0';
command_parse_sub_msg(handle, tok);
/* The command may have reinitialised the input driver, which
* owns and has now freed 'handle'. Nothing further may use it. */
if (input_driver_command_generation() != gen)
return;
tok = strtok_r(NULL, "\n", &save);
}
}
#if defined(HAVE_NETWORK_CMD)
typedef struct
{
/* Network socket FD */
int net_fd;
/* Source address for the command received */
struct sockaddr_storage cmd_source;
/* Size of the previous structure in use */
socklen_t cmd_source_len;
} command_network_t;
static void network_command_reply(command_t *cmd,
const char *s, size_t len)
{
command_network_t *netcmd = (command_network_t*)cmd->userptr;
/* Respond (fire and forget since it's UDP) */
sendto(netcmd->net_fd, s, len, 0,
(struct sockaddr*)&netcmd->cmd_source, netcmd->cmd_source_len);
}
typedef struct
{
struct sockaddr_storage addr;
socklen_t addr_len;
} command_network_dest_t;
static void *network_command_reply_dest(command_t *cmd)
{
command_network_t *netcmd = (command_network_t*)cmd->userptr;
command_network_dest_t *dest = (command_network_dest_t*)
malloc(sizeof(*dest));
if (dest)
{
dest->addr = netcmd->cmd_source;
dest->addr_len = netcmd->cmd_source_len;
}
return dest;
}
static void network_command_reply_to(command_t *cmd, void *data,
const char *s, size_t len)
{
command_network_t *netcmd = (command_network_t*)cmd->userptr;
command_network_dest_t *dest = (command_network_dest_t*)data;
sendto(netcmd->net_fd, s, len, 0,
(struct sockaddr*)&dest->addr, dest->addr_len);
}
static void network_command_free(command_t *handle)
{
command_network_t *netcmd = (command_network_t*)handle->userptr;
if (netcmd->net_fd >= 0)
socket_close(netcmd->net_fd);
free(netcmd);
free(handle);
}
static void command_network_poll(command_t *handle)
{
ssize_t ret;
unsigned gen = input_driver_command_generation();
char buf[2048];
command_network_t *netcmd = (command_network_t*)handle->userptr;
if (netcmd->net_fd < 0)
return;
for (;;)
{
netcmd->cmd_source_len = sizeof(netcmd->cmd_source);
if ((ret = recvfrom(netcmd->net_fd, buf, sizeof(buf) - 1, 0,
(struct sockaddr*)&netcmd->cmd_source,
&netcmd->cmd_source_len)) <= 0)
return;
buf[ret] = '\0';
command_parse_msg(handle, buf);
/* See command_generation: the command may have freed this
* object. The remaining datagrams wait for the next poll. */
if ( input_driver_command_generation() != gen
|| command_interfaces_held())
return;
}
}
command_t* command_network_new(uint16_t port, const char *bind_address)
{
struct addrinfo *res = NULL;
command_t *cmd = NULL;
command_network_t *netcmd = NULL;
int fd = -1;
/* Allocate-then-check in sequence, matching command_uds_new.
* The previous code allocated cmd + netcmd back-to-back and then
* assigned netcmd->net_fd / cmd->userptr before NULL-checking
* either pointer - an OOM on either calloc would NULL-deref on
* line 252/253. It also leaked netcmd on the '!cmd' failure
* path via the 'error' label (which now cleanly frees both
* because they are initialised to NULL up-front; free(NULL) is
* a no-op). */
if (!(cmd = (command_t*)calloc(1, sizeof(*cmd))))
goto error;
if (!(netcmd = (command_network_t*)calloc(1, sizeof(command_network_t))))
goto error;
/* An empty bind address means every interface (NULL server ->
* AI_PASSIVE -> 0.0.0.0), which is what this interface has always
* done. Anyone on that network can then send LOAD_CORE or
* WRITE_CORE_RAM, so users who only drive RetroArch from the same
* machine can set network_cmd_bind_address = "127.0.0.1". */
if (bind_address && !*bind_address)
bind_address = NULL;
fd = socket_init((void**)&res, port, bind_address,
SOCKET_TYPE_DATAGRAM, AF_INET);
RARCH_LOG("[NetCMD] %s %hu (%s).\n",
msg_hash_to_str(MSG_BRINGING_UP_COMMAND_INTERFACE_ON_PORT),
(unsigned short)port,
bind_address ? bind_address : "all interfaces");
if (fd < 0)
goto error;
netcmd->net_fd = fd;
cmd->userptr = netcmd;
cmd->poll = command_network_poll;
cmd->replier = network_command_reply;
cmd->reply_dest = network_command_reply_dest;
cmd->reply_to = network_command_reply_to;
cmd->destroy = network_command_free;
if (!socket_nonblock(netcmd->net_fd))
goto error;
if (!socket_bind(netcmd->net_fd, (void*)res))
{
RARCH_ERR("[NetCMD] %s\n",
msg_hash_to_str(MSG_FAILED_TO_BIND_SOCKET));
goto error;
}
freeaddrinfo_retro(res);
return cmd;
error:
if (res)
freeaddrinfo_retro(res);
/* The only path that reaches 'error' with fd >= 0 is the
* socket_nonblock / socket_bind failure after fd was already
* stored in netcmd->net_fd. Close it so we don't leak the
* socket file descriptor along with the allocations. */
if (fd >= 0)
socket_close(fd);
free(netcmd);
free(cmd);
return NULL;
}
#endif
#if defined(HAVE_STDIN_CMD)
typedef struct
{
/* Buffer and pointer for stdin reads */
size_t stdin_buf_ptr;
char stdin_buf[CMD_BUF_SIZE];
} command_stdin_t;
static void stdin_command_reply(command_t *cmd,
const char *s, size_t len)
{
/* Just write to stdout! */
fwrite(s, 1, len, stdout);
fflush(stdout);
}
static void stdin_command_free(command_t *handle)
{
free(handle->userptr);
free(handle);
}
static void command_stdin_poll(command_t *handle)
{
command_stdin_t *stdincmd = (command_stdin_t*)handle->userptr;
ssize_t ret = read_stdin(
stdincmd->stdin_buf + stdincmd->stdin_buf_ptr,
CMD_BUF_SIZE - stdincmd->stdin_buf_ptr - 1);
if (ret != 0)
{
char *last_newline = NULL;
stdincmd->stdin_buf_ptr += ret;
/* Ensure we don't write past buffer bounds */
if (stdincmd->stdin_buf_ptr >= CMD_BUF_SIZE)
stdincmd->stdin_buf_ptr = CMD_BUF_SIZE - 1;
stdincmd->stdin_buf[stdincmd->stdin_buf_ptr] = '\0';
last_newline = strrchr(stdincmd->stdin_buf, '\n');
if (!last_newline)
{
/* We're receiving bogus data in pipe
* (no terminating newline), flush out the buffer. */
if (stdincmd->stdin_buf_ptr + 1 >= CMD_BUF_SIZE)
{
stdincmd->stdin_buf_ptr = 0;
stdincmd->stdin_buf[0] = '\0';
}
}
else
{
ptrdiff_t msg_len;
size_t rest;
char *msg;
*last_newline++ = '\0';
msg_len = last_newline - stdincmd->stdin_buf;
rest = stdincmd->stdin_buf_ptr - msg_len;
/* A command can free this object from under us (see
* command_generation): take the message off the buffer, tidy the
* buffer, and only then run the message from a private copy,
* touching nothing of this object afterwards. */
msg = strdup(stdincmd->stdin_buf);
memmove(stdincmd->stdin_buf, last_newline, rest);
stdincmd->stdin_buf_ptr = rest;
if (msg)
{
command_parse_msg(handle, msg);
free(msg);
}
return;
}
}
}
command_t* command_stdin_new(void)
{
command_t *cmd;
command_stdin_t *stdincmd;
#if !(defined(_WIN32) || defined(__EMSCRIPTEN__))
#ifdef HAVE_NETWORKING
if (!socket_nonblock(STDIN_FILENO))
return NULL;
#endif
#endif
/* Allocate in order with per-step failure handling. The earlier
* form allocated both cmd and stdincmd back-to-back then checked
* them, which leaked stdincmd on the '!cmd' failure path (the
* '!cmd' return simply dropped the stdincmd pointer). Also
* matches the command_uds_new pattern further down. */
if (!(cmd = (command_t*)calloc(1, sizeof(command_t))))
return NULL;
if (!(stdincmd = (command_stdin_t*)calloc(1, sizeof(command_stdin_t))))
{
free(cmd);
return NULL;
}
cmd->userptr = stdincmd;
cmd->poll = command_stdin_poll;
cmd->replier = stdin_command_reply;
cmd->destroy = stdin_command_free;
return cmd;
}
#endif
#if defined(__EMSCRIPTEN__)
#include "frontend/drivers/platform_emscripten.h"
typedef struct
{
char command_buf[CMD_BUF_SIZE];
} command_emscripten_t;
static void emscripten_command_reply(command_t *_cmd,
const char *s, size_t len)
{
platform_emscripten_command_reply(s, len);
}
static void emscripten_command_free(command_t *handle)
{
free(handle->userptr);
free(handle);
}
static void command_emscripten_poll(command_t *handle)
{
command_emscripten_t *emscriptencmd = (command_emscripten_t*)handle->userptr;
ptrdiff_t msg_len = platform_emscripten_command_read((char **)(&emscriptencmd->command_buf), CMD_BUF_SIZE);
if (msg_len != 0)
command_parse_msg(handle, emscriptencmd->command_buf);
}
command_t* command_emscripten_new(void)
{
command_t *cmd;
command_emscripten_t *emscriptencmd;
/* Same sequential-allocation-with-per-step-cleanup pattern as
* command_stdin_new / command_uds_new. Previously allocated both
* structs before any NULL check, leaking emscriptencmd on the
* '!cmd' failure path. */
if (!(cmd = (command_t*)calloc(1, sizeof(command_t))))
return NULL;
if (!(emscriptencmd = (command_emscripten_t*)calloc(1, sizeof(command_emscripten_t))))
{
free(cmd);
return NULL;
}
cmd->userptr = emscriptencmd;
cmd->poll = command_emscripten_poll;
cmd->replier = emscripten_command_reply;
cmd->destroy = emscripten_command_free;
return cmd;
}
#endif
#if defined(HAVE_LAKKA)
#include <sys/un.h>
#define MAX_USER_CONNECTIONS 4
typedef struct
{
/* File descriptor for the domain socket */
int sfd;
/* Client sockets */
int userfd[MAX_USER_CONNECTIONS];
/* Which connection holds each slot: a closed descriptor's number
* is handed out again, so the number alone does not say. */
unsigned serial[MAX_USER_CONNECTIONS];
unsigned next_serial;
/* Last received user socket */
int last_fd;
unsigned last_serial;
} command_uds_t;
typedef struct
{
int fd;
unsigned serial;
} command_uds_dest_t;
static void uds_command_reply(command_t *cmd,
const char *s, size_t len)
{
command_uds_t *subcmd = (command_uds_t*)cmd->userptr;
write(subcmd->last_fd, s, len);
}
static void *uds_command_reply_dest(command_t *cmd)
{
command_uds_t *subcmd = (command_uds_t*)cmd->userptr;
command_uds_dest_t *dest = (command_uds_dest_t*)malloc(sizeof(*dest));
if (dest)
{
dest->fd = subcmd->last_fd;
dest->serial = subcmd->last_serial;
}
return dest;
}
/* Only to the connection that asked: gone, the reply is dropped. */
static void uds_command_reply_to(command_t *cmd, void *data,
const char *s, size_t len)
{
int i;
command_uds_t *subcmd = (command_uds_t*)cmd->userptr;
command_uds_dest_t *dest = (command_uds_dest_t*)data;
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
if ( subcmd->userfd[i] == dest->fd
&& subcmd->serial[i] == dest->serial)
{
write(dest->fd, s, len);
return;
}
}
static void uds_command_free(command_t *handle)
{
int i;
command_uds_t *udscmd = (command_uds_t*)handle->userptr;
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
if (udscmd->userfd[i] >= 0)
socket_close(udscmd->userfd[i]);
socket_close(udscmd->sfd);
free(handle->userptr);
free(handle);
}
static void command_uds_poll(command_t *handle)
{
unsigned gen = input_driver_command_generation();
int i;
int fd;
ssize_t ret;
char buf[2048];
command_uds_t *udscmd = (command_uds_t*)handle->userptr;
if (udscmd->sfd < 0)
return;
/* Read data from clients and process commands */
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
{
bool err = false;
fd = udscmd->userfd[i];
if (fd < 0)
continue;
ret = socket_receive_all_nonblocking(fd, &err, buf, sizeof(buf) - 1);
if (!ret)
continue;
if (!err)
{
buf[ret] = '\0';
udscmd->last_fd = fd;
udscmd->last_serial = udscmd->serial[i];
command_parse_msg(handle, buf);
/* See command_generation: this object may be gone now. */
if ( input_driver_command_generation() != gen
|| command_interfaces_held())
return;
}
else
{
socket_close(fd);
udscmd->userfd[i] = -1;
}
}
/* Accepts new connections from clients */
fd = accept(udscmd->sfd, NULL, NULL);
if (fd >= 0)
{
if (socket_nonblock(fd))
{
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
{
if (udscmd->userfd[i] < 0)
{
udscmd->userfd[i] = fd;
udscmd->serial[i] = ++udscmd->next_serial;
return;
}
}
}
socket_close(fd);
}
}
command_t* command_uds_new(void)
{
int i;
command_t *cmd;
command_uds_t *subcmd;
struct sockaddr_un addr;
socklen_t addrsz = offsetof(struct sockaddr_un, sun_path) + STRLEN_CONST("retroarch/cmd") + 1;
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return NULL;
/* use an abstract socket for simplicity */
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strlcpy_lit(&addr.sun_path[1], "retroarch/cmd", sizeof(addr.sun_path) - 1);
if ( bind(fd, (struct sockaddr*)&addr, addrsz) < 0
|| listen(fd, MAX_USER_CONNECTIONS) < 0
|| !socket_nonblock(fd))
{
socket_close(fd);
return NULL;
}
cmd = (command_t*)calloc(1, sizeof(command_t));
if (!cmd)
{
socket_close(fd);
return NULL;
}
subcmd = (command_uds_t*)calloc(1, sizeof(command_uds_t));
if (!subcmd)
{
free(cmd);
socket_close(fd);
return NULL;
}
subcmd->sfd = fd;
subcmd->last_fd = -1;
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
subcmd->userfd[i] = -1;
cmd->userptr = subcmd;
cmd->poll = command_uds_poll;
cmd->replier = uds_command_reply;
cmd->reply_dest = uds_command_reply_dest;
cmd->reply_to = uds_command_reply_to;
cmd->destroy = uds_command_free;
return cmd;
}
#endif
/* Routines used to invoke retroarch command ... */
/* The family tool a command runs for, so its answers carry the tool's
* name: STATE save 1 rather than SAVE_STATE_SLOT 1. */
struct command_family
{
const char *family;
const char *word;
const char *name;
command_replier_t replier;
/* the command answered, or will */
bool answered;
};
static struct command_family command_family_now;
/* @msg under @f's name, allocated, its length in @out_len; NULL where
* it could not be, or where it is to go as it is. */
static char *command_family_rename(const struct command_family *f,
const char *msg, size_t len, size_t *out_len)
{
size_t n = strlen(f->name);
size_t size = len + strlen(f->family) + strlen(f->word) + 3;
char *s = (char*)malloc(size);
int _len;
if (!s)
return NULL;
if ( len > n && !strncmp(msg, f->name, n)
&& (msg[n] == ' ' || msg[n] == '\n' || msg[n] == ','))
{
msg += n;
len -= n;
if (!strncmp(msg, " ERROR", 6))
_len = snprintf(s, size, "%s%.*s", f->family, (int)len, msg);
else
_len = snprintf(s, size, "%s %s%.*s", f->family, f->word,
(int)len, msg);
}
else if (len == n && !strncmp(msg, f->name, n))
_len = snprintf(s, size, "%s %s", f->family, f->word);
/* rows, which name nothing, are the tool's answer as they are */
else
{
free(s);
return NULL;
}
if (_len <= 0 || (size_t)_len >= size)
{
free(s);
return NULL;
}
*out_len = (size_t)_len;
return s;
}
static void command_family_replier(command_t *cmd, const char *data,
size_t len)
{
size_t _len = 0;
char *msg = command_family_rename(&command_family_now, data, len,
&_len);
command_family_now.answered = true;
if (msg)
command_family_now.replier(cmd, msg, _len);
else
command_family_now.replier(cmd, data, len);
free(msg);
}
/* A reply sent after its command has returned: where it goes is taken
* while the command runs, as afterwards the interface may be answering
* someone else. */
typedef struct
{
command_t *cmd;
void *dest;
struct command_family family;
unsigned gen;
} command_deferred_t;
static void command_deferred_take(command_deferred_t *d, command_t *cmd)
{
d->cmd = cmd;
d->dest = cmd->reply_dest ? cmd->reply_dest(cmd) : NULL;
d->gen = input_driver_command_generation();
d->family = command_family_now;
if (command_family_now.family)
command_family_now.answered = true;
}
/* Sends @msg (none when NULL) and releases @d. Dropped, unsent, if the
* interface was torn down meanwhile (the command generation moved).
* @is_error says the reply reports a failure, so the interface sending
* it is told rather than left to read the text. */
static void command_deferred_send(command_deferred_t *d,
const char *msg, size_t len, bool is_error)
{
char *renamed = NULL;
if (msg && input_driver_command_generation() == d->gen)
{
size_t _len = 0;
if (d->family.family && (renamed = command_family_rename(&d->family,
msg, len, &_len)))
{
msg = renamed;
len = _len;
}
d->cmd->error = is_error;
if (d->cmd->reply_to && d->dest)
d->cmd->reply_to(d->cmd, d->dest, msg, len);
else if (!d->cmd->reply_dest)
d->cmd->replier(d->cmd, msg, len);
}
free(renamed);
free(d->dest);
d->dest = NULL;
}
#ifdef HAVE_SCREENSHOTS
/* SCREENSHOT's answer, and GET_FRAME's - the frame written nowhere, so
* with no path: the picture where the interface takes one, with the
* file's path or the frame's size as its text. */
static void command_screenshot_done(retro_task_t *task, void *task_data,
void *user_data, const char *error)
{
command_deferred_t *d = (command_deferred_t*)user_data;
const struct screenshot_result *r = (const struct screenshot_result*)task_data;
bool frame = r && !*r->path;
char msg[PATH_MAX_LENGTH];
if (error || !r || (frame && !r->png_base64))
{
snprintf(msg, sizeof(msg), "%s ERROR %s",
frame ? "GET_FRAME" : "SCREENSHOT",
error ? error : "no result");
command_deferred_send(d, msg, strlen(msg), true);
}
else
{
if (frame)
snprintf(msg, sizeof(msg), "GET_FRAME %ux%u", r->width, r->height);
else
strlcpy(msg, r->path, sizeof(msg));
if ( r->png_base64 && d->cmd->reply_image_to && d->dest
&& input_driver_command_generation() == d->gen)
{
d->cmd->reply_image_to(d->cmd, d->dest, msg, strlen(msg),
"image/png", r->png_base64, r->png_base64_len);
command_deferred_send(d, NULL, 0, false);
}
else
command_deferred_send(d, msg, strlen(msg), false);
}
free(d);
}
/* SCREENSHOT run by command_run(): answered with the file's path once
* the screenshot task has written it. */
static bool command_screenshot(command_t *cmd)
{
settings_t *settings = config_get_ptr();
runloop_state_t *runloop_st = runloop_state_get_ptr();
command_deferred_t *d = (command_deferred_t*)calloc(1, sizeof(*d));
if (!d)
return false;
/* Threaded (use_thread true), so the callback, which reads @d, runs
* from the task queue on a later frame, after it is taken below.
* Unthreaded, the callback would run inside this call and free @d
* before it was taken. It is taken only once the screenshot is
* queued, as taking it marks the asking MCP request as waiting. */
if (!take_screenshot_notify(settings->paths.directory_screenshot,
runloop_st->runtime_content_path_basename, false,
video_driver_cached_frame_is_hw_render(), false, true,
command_screenshot_done, d))
{
free(d);
return false;
}
/* the task's callback, which uses this, runs on a later frame */
command_deferred_take(d, cmd);
return true;
}
#ifdef HAVE_RPNG
/* GET_FRAME [size]: the frame on screen as image content, scaled to fit
* a square of @size and never up. Nothing is written: a software core's
* frame is copied, a hardware core's read back from the GPU. */
bool command_get_frame(command_t *cmd, const char *arg)
{
command_deferred_t *d;
unsigned long size = COMMAND_FRAME_SIZE;
char *end = NULL;
while (arg && *arg == ' ')
arg++;
if (arg && *arg)
{
size = strtoul(arg, &end, 10);
if ( *arg < '0' || *arg > '9' || *end
|| size < COMMAND_FRAME_SIZE_MIN || size > COMMAND_FRAME_SIZE_MAX)
{
command_reply_error(cmd, "GET_FRAME", "a size from %u to %u",
COMMAND_FRAME_SIZE_MIN, COMMAND_FRAME_SIZE_MAX);
return false;
}
}
/* the line interfaces carry text alone */
if (!cmd->reply_image_to)
{
command_reply_error(cmd, "GET_FRAME", "this interface carries no "
"image; SCREENSHOT writes one to a file");
return false;
}
if (!(runloop_get_flags() & RUNLOOP_FLAG_CORE_RUNNING))
{
command_reply_error(cmd, "GET_FRAME", "no content is running");