efl/src/lib/ecore_cocoa/ecore_cocoa_cnp.m

166 lines
4.0 KiB
Mathematica
Raw Normal View History

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <Eina.h>
#import <Cocoa/Cocoa.h>
#import "ecore_cocoa_window.h"
#include "ecore_cocoa_private.h"
#import "ecore_cocoa_app.h"
ecore_cocoa: Rename EAPI macro to ECORE_COCOA_API in Ecore Cocoa library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-10-29 05:33:06 -07:00
ECORE_COCOA_API Eina_Bool
ecore_cocoa_clipboard_set(const void *data,
int size,
const char *raw_mime_type)
{
NSMutableArray *objects;
NSString *str = nil;
BOOL ok = YES;
NSString *mime_type = [NSString stringWithUTF8String:raw_mime_type];
objects = [[NSMutableArray alloc] init];
if ([mime_type hasPrefix:@"text/"])
{
str = [[NSString alloc] initWithBytes: data
length: size
encoding: NSUTF8StringEncoding];
if (str)
[objects addObject: str];
}
else
{
ERR("Mimetype %s is not handled yet", raw_mime_type);
}
/* Write to pasteboard */
if ([objects count] > 0)
{
NSPasteboard *pb;
pb = [NSPasteboard generalPasteboard];
[pb clearContents];
ok = [pb writeObjects: objects];
[objects removeAllObjects];
}
return (ok) ? EINA_TRUE : EINA_FALSE;
}
ecore_cocoa: Rename EAPI macro to ECORE_COCOA_API in Ecore Cocoa library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-10-29 05:33:06 -07:00
ECORE_COCOA_API Eina_Bool
ecore_cocoa_clipboard_exists(void)
{
NSDictionary *options;
NSPasteboard *pb;
NSArray *items;
NSMutableArray *classes;
classes = [[NSMutableArray alloc] init];
[classes addObject: [NSString class]]; // we only support strings for now
pb = [NSPasteboard generalPasteboard];
options = [NSDictionary dictionary];
return [pb canReadItemWithDataConformingToTypes: classes];
}
ecore_cocoa: Rename EAPI macro to ECORE_COCOA_API in Ecore Cocoa library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-10-29 05:33:06 -07:00
ECORE_COCOA_API void *
ecore_cocoa_clipboard_get(int *size,
const char *raw_mime_type)
{
NSMutableArray *classes;
void *data = NULL;
NSDictionary *options;
NSPasteboard *pb;
NSArray *items;
unsigned int len;
BOOL string_class = NO;
NSString *mime_type = [NSString stringWithUTF8String:raw_mime_type];
classes = [[NSMutableArray alloc] init];
if ([mime_type hasPrefix:@"text/"])
{
string_class = YES;
[classes addObject: [NSString class]];
}
else
{
ERR("Mimetype %s is not handled yet", raw_mime_type);
goto fail;
}
if ([classes count] <= 0)
{
ERR("No registered classes... got nothing from pasteboard");
goto fail;
}
pb = [NSPasteboard generalPasteboard];
options = [NSDictionary dictionary];
items = [pb readObjectsForClasses: classes
options: options];
if (!items)
{
ERR("No items in the clipboard");
goto remove_fail;
}
if ([items count] != 1)
{
ERR("%lu items in pasteboard. Only one at the time can be handled",
[items count]);
goto fail;
}
if (string_class)
{
NSString *str = [items objectAtIndex: 0];
data = (void *)[str UTF8String];
len = [str lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
data = eina_strndup((const char *)data, len);
if (EINA_UNLIKELY(!data))
{
CRI("Failed to strndup() string \"%s\" (len: %u)",
(const char *)data, len);
goto remove_fail;
}
#if 0
if (type & ECORE_COCOA_CNP_TYPE_MARKUP)
{
char *markup;
markup = evas_textblock_text_utf8_to_markup(NULL, data);
free(data);
data = markup;
if (EINA_UNLIKELY(!data))
{
CRI("Failed to retrieve markup from UTF8");
goto remove_fail;
}
len = strlen(markup);
}
#endif
}
if (!data)
{
ERR("No types retrieved!");
goto remove_fail;
}
[classes removeAllObjects];
if (size) *size = len;
return data;
remove_fail:
[classes removeAllObjects];
fail:
if (size) *size = 0;
return NULL;
}
ecore_cocoa: Rename EAPI macro to ECORE_COCOA_API in Ecore Cocoa library Patch from a series of patches to rename EAPI symbols to specific library DSOs. EAPI was designed to be able to pass ```__attribute__ ((visibility ("default")))``` for symbols with GCC, which would mean that even if -fvisibility=hidden was used when compiling the library, the needed symbols would get exported. MSVC __almost__ works like GCC (or mingw) in which you can declare everything as export and it will just work (slower, but it will work). But there's a caveat: global variables will not work the same way for MSVC, but works for mingw and GCC. For global variables (as opposed to functions), MSVC requires correct DSO visibility for MSVC: instead of declaring a symbol as export for everything, you need to declare it as import when importing from another DSO and export when defining it locally. With current EAPI definitions, we get the following example working in mingw and MSVC (observe it doesn't define any global variables as exported symbols). Example 1: dll1: ``` EAPI void foo(void); EAPI void bar() { foo(); } ``` dll2: ``` EAPI void foo() { printf ("foo\n"); } ``` This works fine with API defined as __declspec(dllexport) in both cases and for gcc defining as ```__atttribute__((visibility("default")))```. However, the following: Example 2: dll1: ``` EAPI extern int foo; EAPI void foobar(void); EAPI void bar() { foo = 5; foobar(); } ``` dll2: ``` EAPI int foo = 0; EAPI void foobar() { printf ("foo %d\n", foo); } ``` This will work on mingw but will not work for MSVC. And that's why EAPI is the only solution that worked for MSVC. Co-authored-by: João Paulo Taylor Ienczak Zanette <jpaulotiz@gmail.com> Co-authored-by: Ricardo Campos <ricardo.campos@expertise.dev> Co-authored-by: Lucas Cavalcante de Sousa <lucks.sousa@gmail.com>
2020-10-29 05:33:06 -07:00
ECORE_COCOA_API void
ecore_cocoa_clipboard_clear(void)
{
[[NSPasteboard generalPasteboard] clearContents];
}