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