|
|
1.1 root 1: /*
2: Hatari - control.c
3:
4: This file is distributed under the GNU Public License, version 2 or at
5: your option any later version. Read the file gpl.txt for details.
6:
7: This code processes commands from the Hatari control socket
8: */
1.1.1.2 root 9: const char Control_fileid[] = "Hatari control.c : " __DATE__ " " __TIME__;
1.1 root 10:
11: #include "config.h"
12:
1.1.1.3 root 13: #if HAVE_UNIX_DOMAIN_SOCKETS
1.1 root 14: # include <sys/socket.h>
1.1.1.3 root 15: # include <sys/un.h>
16: #endif
1.1 root 17:
18: #include <sys/types.h>
19: #include <sys/time.h>
20: #include <unistd.h>
21: #include <ctype.h>
22:
23: #include "main.h"
24: #include "change.h"
25: #include "configuration.h"
26: #include "control.h"
27: #include "debugui.h"
28: #include "file.h"
29: #include "ikbd.h"
30: #include "keymap.h"
31: #include "log.h"
32: #include "midi.h"
33: #include "printer.h"
34: #include "rs232.h"
35: #include "shortcut.h"
36: #include "str.h"
37:
38: typedef enum {
39: DO_DISABLE,
40: DO_ENABLE,
41: DO_TOGGLE
42: } action_t;
43:
44: /* Whether to send embedded window info */
45: static bool bSendEmbedInfo;
46: /* Pausing triggered remotely (battery save pause) */
47: static bool bRemotePaused;
48:
49:
50: /*-----------------------------------------------------------------------*/
51: /**
52: * Parse key command and synthetize key press/release
53: * corresponding to given keycode or character.
1.1.1.3 root 54: * Return false if parsing failed, true otherwise
1.1 root 55: *
56: * This can be used by external Hatari UI(s) for
57: * string macros, or on devices which lack keyboard
58: */
59: static bool Control_InsertKey(const char *event)
60: {
61: const char *key = NULL;
1.1.1.5 root 62: bool up, down;
1.1 root 63:
64: if (strncmp(event, "keypress ", 9) == 0) {
65: key = &event[9];
1.1.1.5 root 66: down = up = true;
67: } else if (strncmp(event, "keydown ", 8) == 0) {
68: key = &event[8];
69: down = true;
70: up = false;
71: } else if (strncmp(event, "keyup ", 6) == 0) {
72: key = &event[6];
73: down = false;
74: up = true;
1.1 root 75: }
76: if (!(key && key[0])) {
1.1.1.5 root 77: fprintf(stderr, "ERROR: '%s' contains no key press/down/up event\n", event);
1.1.1.3 root 78: return false;
1.1 root 79: }
80: if (key[1]) {
81: char *endptr;
82: /* multiple characters, assume it's a keycode */
83: int keycode = strtol(key, &endptr, 0);
84: /* not a valid number or keycode is out of range? */
85: if (*endptr || keycode < 0 || keycode > 255) {
1.1.1.6 ! root 86: fprintf(stderr, "ERROR: '%s' isn't a valid key scancode, got value %d\n",
1.1 root 87: key, keycode);
1.1.1.3 root 88: return false;
1.1 root 89: }
1.1.1.5 root 90: if (down) {
91: IKBD_PressSTKey(keycode, true);
92: }
93: if (up) {
94: IKBD_PressSTKey(keycode, false);
95: }
1.1 root 96: } else {
1.1.1.6 ! root 97: if (!isalnum(key[0])) {
! 98: fprintf(stderr, "ERROR: non-alphanumeric character '%c' needs to be given as keycode\n", key[0]);
! 99: return false;
! 100: }
1.1.1.5 root 101: if (down) {
102: Keymap_SimulateCharacter(key[0], true);
103: }
104: if (up) {
105: Keymap_SimulateCharacter(key[0], false);
106: }
1.1 root 107: }
1.1.1.5 root 108: #if 0
109: fprintf(stderr, "Simulated key %s of %d\n",
110: (down? (up? "press":"down") :"up"), key);
111: #endif
1.1.1.3 root 112: return true;
1.1 root 113: }
114:
115: /*-----------------------------------------------------------------------*/
116: /**
117: * Parse event name and synthetize corresponding event to emulation
1.1.1.3 root 118: * Return false if name parsing failed, true otherwise
1.1 root 119: *
120: * This can be used by external Hatari UI(s) on devices which input
121: * methods differ from normal keyboard and mouse, such as high DPI
122: * touchscreen (no right/middle button, inaccurate clicks)
123: */
124: static bool Control_InsertEvent(const char *event)
125: {
126: if (strcmp(event, "doubleclick") == 0) {
127: Keyboard.LButtonDblClk = 1;
1.1.1.3 root 128: return true;
1.1 root 129: }
1.1.1.5 root 130: if (strcmp(event, "rightdown") == 0) {
1.1 root 131: Keyboard.bRButtonDown |= BUTTON_MOUSE;
1.1.1.3 root 132: return true;
1.1 root 133: }
1.1.1.5 root 134: if (strcmp(event, "rightup") == 0) {
1.1 root 135: Keyboard.bRButtonDown &= ~BUTTON_MOUSE;
1.1.1.3 root 136: return true;
1.1 root 137: }
138: if (Control_InsertKey(event)) {
1.1.1.3 root 139: return true;
1.1 root 140: }
141: fprintf(stderr, "ERROR: unrecognized event: '%s'\n", event);
1.1.1.5 root 142: fprintf(stderr,
143: "Supported mouse button and key events are:\n"
144: "- doubleclick\n"
145: "- rightdown\n"
146: "- rightup\n"
147: "- keypress <key>\n"
148: "- keydown <key>\n"
149: "- keyup <key>\n"
150: "<key> can be either a single ASCII character or an ST scancode\n"
151: "(e.g. space has scancode of 57 and enter 28).\n"
152: );
1.1.1.3 root 153: return false;
1.1 root 154: }
155:
156: /*-----------------------------------------------------------------------*/
157: /**
158: * Parse device name and enable/disable/toggle & init/uninit it according
1.1.1.3 root 159: * to action. Return false if name parsing failed, true otherwise
1.1 root 160: */
161: static bool Control_DeviceAction(const char *name, action_t action)
162: {
163: /* Note: e.g. RTC would require restarting emulation
164: * and HD-boot setting emulation reboot. Devices
165: * listed here work just with init/uninit.
166: */
167: struct {
168: const char *name;
169: bool *pvalue;
170: void(*init)(void);
171: void(*uninit)(void);
172: } item[] = {
173: { "printer", &ConfigureParams.Printer.bEnablePrinting, Printer_Init, Printer_UnInit },
174: { "rs232", &ConfigureParams.RS232.bEnableRS232, RS232_Init, RS232_UnInit },
175: { "midi", &ConfigureParams.Midi.bEnableMidi, Midi_Init, Midi_UnInit },
176: { NULL, NULL, NULL, NULL }
177: };
178: int i;
179: bool value;
180: for (i = 0; item[i].name; i++)
181: {
182: if (strcmp(name, item[i].name) == 0)
183: {
184: switch (action) {
185: case DO_TOGGLE:
186: value = !*(item[i].pvalue);
187: break;
188: case DO_ENABLE:
1.1.1.3 root 189: value = true;
1.1 root 190: break;
191: case DO_DISABLE:
192: default:
1.1.1.3 root 193: value = false;
1.1 root 194: break;
195: }
196: *(item[i].pvalue) = value;
197: if (value) {
198: item[i].init();
199: } else {
200: item[i].uninit();
201: }
202: fprintf(stderr, "%s: %s\n", name, value?"ON":"OFF");
1.1.1.3 root 203: return true;
1.1 root 204: }
205: }
206: fprintf(stderr, "WARNING: unknown device '%s'\n\n", name);
207: fprintf(stderr, "Accepted devices are:\n");
208: for (i = 0; item[i].name; i++)
209: {
210: fprintf(stderr, "- %s\n", item[i].name);
211: }
1.1.1.3 root 212: return false;
1.1 root 213: }
214:
215: /*-----------------------------------------------------------------------*/
216: /**
217: * Parse path type name and set the path to given value.
1.1.1.3 root 218: * Return false if name parsing failed, true otherwise
1.1 root 219: */
220: static bool Control_SetPath(char *name)
221: {
222: struct {
223: const char *name;
224: char *path;
225: } item[] = {
226: { "memauto", ConfigureParams.Memory.szAutoSaveFileName },
227: { "memsave", ConfigureParams.Memory.szMemoryCaptureFileName },
1.1.1.2 root 228: { "midiin", ConfigureParams.Midi.sMidiInFileName },
229: { "midiout", ConfigureParams.Midi.sMidiOutFileName },
1.1 root 230: { "printout", ConfigureParams.Printer.szPrintToFileName },
231: { "soundout", ConfigureParams.Sound.szYMCaptureFileName },
232: { "rs232in", ConfigureParams.RS232.szInFileName },
233: { "rs232out", ConfigureParams.RS232.szOutFileName },
234: { NULL, NULL }
235: };
236: int i;
237: char *arg;
238: const char *value;
239:
240: /* argument? */
241: arg = strchr(name, ' ');
242: if (arg) {
243: *arg = '\0';
244: value = Str_Trim(arg+1);
245: } else {
1.1.1.3 root 246: return false;
1.1 root 247: }
248:
249: for (i = 0; item[i].name; i++)
250: {
251: if (strcmp(name, item[i].name) == 0)
252: {
253: fprintf(stderr, "%s: %s -> %s\n", name, item[i].path, value);
254: strncpy(item[i].path, value, FILENAME_MAX-1);
1.1.1.3 root 255: return true;
1.1 root 256: }
257: }
258: fprintf(stderr, "WARNING: unknown path type '%s'\n\n", name);
259: fprintf(stderr, "Accepted paths types are:\n");
260: for (i = 0; item[i].name; i++)
261: {
262: fprintf(stderr, "- %s\n", item[i].name);
263: }
1.1.1.3 root 264: return false;
1.1 root 265: }
266:
267: /*-----------------------------------------------------------------------*/
268: /**
1.1.1.3 root 269: * Show Hatari remote usage info and return false
1.1 root 270: */
271: static bool Control_Usage(const char *cmd)
272: {
273: fprintf(stderr, "ERROR: unrecognized hatari command: '%s'", cmd);
274: fprintf(stderr,
275: "Supported commands are:\n"
276: "- hatari-debug <Debug UI command>\n"
277: "- hatari-event <event to simulate>\n"
278: "- hatari-option <command line options>\n"
279: "- hatari-enable/disable/toggle <device name>\n"
280: "- hatari-path <config name> <new path>\n"
281: "- hatari-shortcut <shortcut name>\n"
282: "- hatari-embed-info\n"
283: "- hatari-stop\n"
284: "- hatari-cont\n"
285: "The last two can be used to stop and continue the Hatari emulation.\n"
1.1.1.6 ! root 286: "All commands need to be separated by newlines. Spaces in command\n"
! 287: "line option arguments need to be quoted with \\.\n"
1.1 root 288: );
1.1.1.3 root 289: return false;
1.1 root 290: }
291:
292: /*-----------------------------------------------------------------------*/
293: /**
294: * Parse Hatari debug/event/option/toggle/path/shortcut command buffer.
295: * Given buffer is modified in-place.
296: */
1.1.1.3 root 297: void Control_ProcessBuffer(char *buffer)
1.1 root 298: {
299: char *cmd, *cmdend, *arg;
1.1.1.3 root 300: int ok = true;
1.1 root 301:
302: cmd = buffer;
303: do {
304: /* command terminator? */
305: cmdend = strchr(cmd, '\n');
306: if (cmdend) {
307: *cmdend = '\0';
308: }
309: /* arguments? */
310: arg = strchr(cmd, ' ');
311: if (arg) {
312: *arg = '\0';
313: arg = Str_Trim(arg+1);
314: }
315: if (arg) {
316: if (strcmp(cmd, "hatari-option") == 0) {
317: ok = Change_ApplyCommandline(arg);
318: } else if (strcmp(cmd, "hatari-debug") == 0) {
1.1.1.4 root 319: ok = DebugUI_RemoteParse(arg);
1.1 root 320: } else if (strcmp(cmd, "hatari-shortcut") == 0) {
321: ok = Shortcut_Invoke(arg);
322: } else if (strcmp(cmd, "hatari-event") == 0) {
323: ok = Control_InsertEvent(arg);
324: } else if (strcmp(cmd, "hatari-path") == 0) {
325: ok = Control_SetPath(arg);
326: } else if (strcmp(cmd, "hatari-enable") == 0) {
327: ok = Control_DeviceAction(arg, DO_ENABLE);
328: } else if (strcmp(cmd, "hatari-disable") == 0) {
329: ok = Control_DeviceAction(arg, DO_DISABLE);
330: } else if (strcmp(cmd, "hatari-toggle") == 0) {
331: ok = Control_DeviceAction(arg, DO_TOGGLE);
332: } else {
333: ok = Control_Usage(cmd);
334: }
335: } else {
336: if (strcmp(cmd, "hatari-embed-info") == 0) {
337: fprintf(stderr, "Embedded window ID change messages = ON\n");
1.1.1.3 root 338: bSendEmbedInfo = true;
1.1 root 339: } else if (strcmp(cmd, "hatari-stop") == 0) {
1.1.1.3 root 340: Main_PauseEmulation(true);
341: bRemotePaused = true;
1.1 root 342: } else if (strcmp(cmd, "hatari-cont") == 0) {
343: Main_UnPauseEmulation();
1.1.1.3 root 344: bRemotePaused = false;
1.1 root 345: } else {
346: ok = Control_Usage(cmd);
347: }
348: }
349: if (cmdend) {
350: cmd = cmdend + 1;
351: }
352: } while (ok && cmdend && *cmd);
353: }
354:
355:
1.1.1.3 root 356: #if HAVE_UNIX_DOMAIN_SOCKETS
357:
358: /* socket from which control command line options are read */
359: static int ControlSocket;
360:
361: /* pre-declared local functions */
362: static int Control_GetUISocket(void);
363:
364:
1.1 root 365: /*-----------------------------------------------------------------------*/
366: /**
367: * Check ControlSocket for new commands and execute them.
368: * Commands should be separated by newlines.
369: *
1.1.1.3 root 370: * Return true if remote pause ON (and connected), false otherwise
1.1 root 371: */
372: bool Control_CheckUpdates(void)
373: {
374: /* just using all trace options with +/- are about 300 chars */
375: char buffer[400];
376: struct timeval tv;
377: fd_set readfds;
378: ssize_t bytes;
379: int status, sock;
380:
381: /* socket of file? */
382: if (ControlSocket) {
383: sock = ControlSocket;
384: } else {
1.1.1.3 root 385: return false;
1.1 root 386: }
387:
388: /* ready for reading? */
389: tv.tv_usec = tv.tv_sec = 0;
390: do {
391: FD_ZERO(&readfds);
392: FD_SET(sock, &readfds);
393: if (bRemotePaused) {
394: /* return only when there're UI events
395: * (redraws etc) to save battery:
396: * http://bugzilla.libsdl.org/show_bug.cgi?id=323
397: */
398: int uisock = Control_GetUISocket();
399: if (uisock) {
400: FD_SET(uisock, &readfds);
401: if (uisock < sock) {
402: uisock = sock;
403: }
404: }
405: status = select(uisock+1, &readfds, NULL, NULL, NULL);
406: } else {
407: status = select(sock+1, &readfds, NULL, NULL, &tv);
408: }
409: if (status < 0) {
410: perror("Control socket select() error");
1.1.1.3 root 411: return false;
1.1 root 412: }
413: /* nothing to process here */
414: if (status == 0) {
415: return bRemotePaused;
416: }
417: if (!FD_ISSET(sock, &readfds)) {
418: return bRemotePaused;
419: }
420:
421: /* assume whole command can be read in one go */
422: bytes = read(sock, buffer, sizeof(buffer)-1);
423: if (bytes < 0)
424: {
425: perror("Control socket read");
1.1.1.3 root 426: return false;
1.1 root 427: }
428: if (bytes == 0) {
429: /* closed */
430: close(ControlSocket);
431: ControlSocket = 0;
1.1.1.3 root 432: return false;
1.1 root 433: }
434: buffer[bytes] = '\0';
435: Control_ProcessBuffer(buffer);
436:
437: } while (bRemotePaused);
438:
1.1.1.3 root 439: return false;
1.1 root 440: }
441:
442:
443: /*-----------------------------------------------------------------------*/
444: /**
445: * Open given control socket.
446: * Return NULL for success, otherwise an error string
447: */
448: const char *Control_SetSocket(const char *socketpath)
449: {
450: struct sockaddr_un address;
451: int newsock;
452:
453: newsock = socket(AF_UNIX, SOCK_STREAM, 0);
454: if (newsock < 0)
455: {
456: perror("socket creation");
457: return "Can't create AF_UNIX socket";
458: }
459:
460: address.sun_family = AF_UNIX;
461: strncpy(address.sun_path, socketpath, sizeof(address.sun_path));
462: address.sun_path[sizeof(address.sun_path)-1] = '\0';
463: Log_Printf(LOG_INFO, "Connecting to control socket '%s'...\n", address.sun_path);
1.1.1.4 root 464: if (connect(newsock, (struct sockaddr *)&address, sizeof(address)) < 0)
1.1 root 465: {
466: perror("socket connect");
467: close(newsock);
468: return "connection to control socket failed";
469: }
470:
471: if (ControlSocket) {
472: close(ControlSocket);
473: }
474: ControlSocket = newsock;
475: Log_Printf(LOG_INFO, "new control socket is '%s'\n", socketpath);
476: return NULL;
477: }
478:
479:
480: /*-----------------------------------------------------------------------
481: * Currently works only on X11.
482: *
483: * SDL_syswm.h automatically includes everything else needed.
484: */
485:
1.1.1.3 root 486: #if HAVE_SDL_SDL_CONFIG_H
487: #include <SDL_config.h>
488: #endif
489:
490: /* X11 available and SDL_config.h states that SDL supports X11 */
491: #if HAVE_X11 && SDL_VIDEO_DRIVER_X11
1.1 root 492: #include <SDL_syswm.h>
493:
494: /**
495: * Reparent Hatari window if so requested. Needs to be done inside
496: * Hatari because if SDL itself is requested to reparent itself,
497: * SDL window stops accepting any input (specifically done like
498: * this in SDL backends for some reason).
499: *
500: * 'noembed' argument tells whether the SDL window should be embedded
501: * or not.
502: *
503: * If the window is embedded (which means that SDL WM window needs
504: * to be hidden) when SDL is asked to fullscreen, Hatari window just
505: * disappears when returning back from fullscreen. I.e. call this
1.1.1.3 root 506: * with noembed=true _before_ fullscreening and any other time with
507: * noembed=false after changing window size. You can do this by
1.1 root 508: * giving bInFullscreen as the noembed value.
509: */
510: void Control_ReparentWindow(int width, int height, bool noembed)
511: {
512: Display *display;
513: Window parent_win, sdl_win, wm_win;
514: const char *parent_win_id;
515: SDL_SysWMinfo info;
516:
517: parent_win_id = getenv("PARENT_WIN_ID");
518: if (!parent_win_id) {
519: return;
520: }
521: parent_win = strtol(parent_win_id, NULL, 0);
522: if (!parent_win) {
523: Log_Printf(LOG_WARN, "Invalid PARENT_WIN_ID value '%s'\n", parent_win_id);
524: return;
525: }
526:
527: SDL_VERSION(&info.version);
528: if (!SDL_GetWMInfo(&info)) {
529: Log_Printf(LOG_WARN, "Failed to get SDL_GetWMInfo()\n");
530: return;
531: }
532: display = info.info.x11.display;
533: sdl_win = info.info.x11.window;
534: wm_win = info.info.x11.wmwindow;
535: info.info.x11.lock_func();
536: if (noembed) {
537: /* show WM window again */
538: XMapWindow(display, wm_win);
539: } else {
540: char buffer[12]; /* 32-bits in hex (+ '\r') + '\n' + '\0' */
541:
542: /* hide WM window for Hatari */
543: XUnmapWindow(display, wm_win);
544: /* reparent main Hatari window to given parent */
545: XReparentWindow(display, sdl_win, parent_win, 0, 0);
546:
547: /* whether to send new window size */
548: if (bSendEmbedInfo && ControlSocket) {
549: fprintf(stderr, "New %dx%d SDL window with ID: %lx\n",
550: width, height, sdl_win);
551: sprintf(buffer, "%dx%d", width, height);
1.1.1.3 root 552: if (write(ControlSocket, buffer, strlen(buffer)) < 0)
553: perror("Control_ReparentWindow write");
1.1 root 554: }
555: }
556: info.info.x11.unlock_func();
557: }
558:
559: /**
560: * Return the X connection socket or zero
561: */
562: static int Control_GetUISocket(void)
563: {
564: SDL_SysWMinfo info;
565: SDL_VERSION(&info.version);
566: if (!SDL_GetWMInfo(&info)) {
567: Log_Printf(LOG_WARN, "Failed to get SDL_GetWMInfo()\n");
568: return 0;
569: }
570: return ConnectionNumber(info.info.x11.display);
571: }
572:
1.1.1.3 root 573: #else /* HAVE_X11 */
574:
1.1 root 575: static int Control_GetUISocket(void)
576: {
577: return 0;
578: }
579: void Control_ReparentWindow(int width, int height, bool noembed)
580: {
581: /* TODO: implement the Windows part. SDL sources offer example */
582: Log_Printf(LOG_TODO, "Support for Hatari window reparenting not built in\n");
583: }
584:
1.1.1.3 root 585: #endif /* HAVE_X11 */
1.1 root 586:
587: #endif /* HAVE_UNIX_DOMAIN_SOCKETS */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.