From f7b0b05426b539cebd041607f6ab4c72dbcd0013 Mon Sep 17 00:00:00 2001 From: Yuval Dolev Date: Mon, 13 Jan 2020 03:20:58 +0200 Subject: [PATCH] Posting to clipboard now works. --- platform_mac/mac_4ed.mm | 36 +++++++++++++++++++++++++++++-- platform_mac/mac_4ed_functions.mm | 16 +++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/platform_mac/mac_4ed.mm b/platform_mac/mac_4ed.mm index 564d3cd5..ba62017d 100644 --- a/platform_mac/mac_4ed.mm +++ b/platform_mac/mac_4ed.mm @@ -193,6 +193,9 @@ struct Mac_Vars { u32 clipboard_change_count; b32 next_clipboard_is_self; + Arena clip_post_arena; + String_Const_u8 clip_post; + NSWindow *window; FCoder_View *view; f32 screen_scale_factor; @@ -480,8 +483,8 @@ mac_read_clipboard_contents(Arena *scratch){ { NSPasteboard *board = [NSPasteboard generalPasteboard]; NSString *utf8_type = @"public.utf8-plain-text"; - NSArray *array = [NSArray arrayWithObjects:utf8_type, nil]; - NSString *has_string = [board availableTypeFromArray:array]; + NSArray *types_array = [NSArray arrayWithObjects:utf8_type, nil]; + NSString *has_string = [board availableTypeFromArray:types_array]; if (has_string != nil){ NSData *data = [board dataForType:utf8_type]; if (data != nil){ @@ -504,6 +507,25 @@ mac_read_clipboard_contents(Arena *scratch){ return(result); } +function void +mac_post_clipboard(Arena *scratch, char *text, i32 len){ + NSPasteboard *board = [NSPasteboard generalPasteboard]; + + NSString *utf8_type = @"public.utf8-plain-text"; + NSArray *types_array = [NSArray arrayWithObjects:utf8_type, nil]; + [board declareTypes:types_array + owner:nil]; + + NSString *paste_string = [[NSString alloc] initWithBytes:text + length:len + encoding:NSUTF8StringEncoding]; + [board setString:paste_string + forType:utf8_type]; + [paste_string release]; + + mac_vars.next_clipboard_is_self = true; +} + #if defined(FRED_INTERNAL) function inline void mac_profile(char *name, u64 begin, u64 end){ @@ -659,6 +681,8 @@ i = 1, mac_profile(name, begin, system_now_time())) input.clipboard = mac_vars.clipboard_contents; } + mac_vars.clip_post.size = 0; + Application_Step_Result result = {}; MacProfileScope("Step"){ // NOTE(yuval): Application Core Update @@ -675,6 +699,14 @@ i = 1, mac_profile(name, begin, system_now_time())) } } + // NOTE(yuval): Post new clipboard content + MacProfileScope("Post Clipboard"){ + if (mac_vars.clip_post.size > 0){ + Scratch_Block scratch(mac_vars.tctx, Scratch_Share); + mac_post_clipboard(scratch, (char*)mac_vars.clip_post.str, (i32)mac_vars.clip_post.size); + } + } + MacProfileScope("Render"){ // NOTE(yuval): Render renderer->render(renderer, &target); diff --git a/platform_mac/mac_4ed_functions.mm b/platform_mac/mac_4ed_functions.mm index aeeaab27..9076a100 100644 --- a/platform_mac/mac_4ed_functions.mm +++ b/platform_mac/mac_4ed_functions.mm @@ -431,7 +431,21 @@ system_sleep_sig(){ function system_post_clipboard_sig(){ - NotImplemented; + Arena *arena = &mac_vars.clip_post_arena; + if (arena->base_allocator == 0){ + *arena = make_arena_system(); + } else{ + linalloc_clear(arena); + } + + mac_vars.clip_post.str = push_array(arena, u8, str.size + 1); + if (mac_vars.clip_post.str != 0){ + block_copy(mac_vars.clip_post.str, str.str, str.size); + mac_vars.clip_post.str[str.size] = 0; + mac_vars.clip_post.size = str.size; + } else{ + // NOTE(yuval): Failed to allocate buffer for clipboard post + } } ////////////////////////////////