From 17f949d137188d3359f6775d5e71246a37562244 Mon Sep 17 00:00:00 2001 From: David Schmidt Date: Fri, 1 Feb 2013 01:48:24 +0000 Subject: [PATCH] Add clipboard paste support --- src/macdriver_generic.c | 62 +++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/src/macdriver_generic.c b/src/macdriver_generic.c index 9a6cfcc..83bb1c1 100755 --- a/src/macdriver_generic.c +++ b/src/macdriver_generic.c @@ -1,6 +1,6 @@ /* GSport - an Apple //gs Emulator - Copyright (C) 2010 by GSport contributors + Copyright (C) 2010 - 2013 by GSport contributors Based on the KEGS emulator written by and Copyright (C) 2003 Kent Dickey @@ -30,7 +30,7 @@ #define ENABLEQD #endif - +#include "stdio.h" #include "defc.h" #include "protos_macdriver.h" @@ -50,7 +50,8 @@ WindowRef g_main_window; CGrafPtr mac_window_port; #endif - +char *g_clipboard = 0x00; +int g_clipboard_pos; @@ -505,15 +506,48 @@ int x_calc_ratio(float x,float y) return 1; } -void -clipboard_paste(void) -{ - // TODO: Add clipboard support -} - -int -clipboard_get_char(void) -{ - // TODO: Add clipboard support - return 0; +void +clipboard_paste(void) +{ +#define CHUNK_SIZE 1024 + char buffer[CHUNK_SIZE]; + int bufsize = 1; + void *expanding_buffer = 0x00; + if (g_clipboard) + { + g_clipboard_pos = 0; + free(g_clipboard); + g_clipboard = 0x00; + } + FILE *pipe = popen("pbpaste", "r"); + if (pipe) + { + expanding_buffer = calloc(CHUNK_SIZE+1,1); + bufsize = CHUNK_SIZE; + while (!feof(pipe)) + { + if (fgets(buffer, CHUNK_SIZE, pipe) != NULL) + { + while (strlen((char*)expanding_buffer) + strlen(buffer) > bufsize) + { + bufsize += CHUNK_SIZE + 1; + expanding_buffer = realloc(expanding_buffer, bufsize); + } + strcat((char*)expanding_buffer,"\r"); + strncat((char*)expanding_buffer,buffer,strlen(buffer)); + g_clipboard = (char*)expanding_buffer; + } + } + } +} + +int clipboard_get_char(void) +{ + if (!g_clipboard) + return 0; + if (g_clipboard[g_clipboard_pos] == '\n') + g_clipboard_pos++; + if (g_clipboard[g_clipboard_pos] == '\0') + return 0; + return g_clipboard[g_clipboard_pos++] | 0x80; }