forked from rebolsource/r3
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: macOS basic clipboard port implementation
- Loading branch information
Showing
4 changed files
with
246 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/*********************************************************************** | ||
** | ||
** REBOL [R3] Language Interpreter and Run-time Environment | ||
** | ||
** Copyright 2012 REBOL Technologies | ||
** REBOL is a trademark of REBOL Technologies | ||
** | ||
** Licensed under the Apache License, Version 2.0 (the "License"); | ||
** you may not use this file except in compliance with the License. | ||
** You may obtain a copy of the License at | ||
** | ||
** http://www.apache.org/licenses/LICENSE-2.0 | ||
** | ||
** Unless required by applicable law or agreed to in writing, software | ||
** distributed under the License is distributed on an "AS IS" BASIS, | ||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
** See the License for the specific language governing permissions and | ||
** limitations under the License. | ||
** | ||
************************************************************************ | ||
** | ||
** Title: Device: Clipboard access for Win32 | ||
** Author: Carl Sassenrath, Oldes | ||
** Purpose: | ||
** Provides a very simple interface to the clipboard for text. | ||
** May be expanded in the future for images, etc. | ||
** | ||
************************************************************************ | ||
** | ||
** NOTE to PROGRAMMERS: | ||
** | ||
** 1. Keep code clear and simple. | ||
** 2. Document unusual code, reasoning, or gotchas. | ||
** 3. Use same style for code, vars, indent(4), comments, etc. | ||
** 4. Keep in mind Linux, OS X, BSD, big/little endian CPUs. | ||
** 5. Test everything, then test it again. | ||
** | ||
***********************************************************************/ | ||
|
||
#include <stdio.h> | ||
|
||
#include "sys-net.h" | ||
#include "reb-host.h" | ||
#include "host-lib.h" | ||
|
||
|
||
REBOOL GetClipboardStringData(REBREQ *req); | ||
REBOOL SetClipboardStringData(REBREQ *req); | ||
|
||
/*********************************************************************** | ||
** | ||
*/ DEVICE_CMD Open_Clipboard(REBREQ *req) | ||
/* | ||
***********************************************************************/ | ||
{ | ||
SET_OPEN(req); | ||
return DR_DONE; | ||
} | ||
|
||
/*********************************************************************** | ||
** | ||
*/ DEVICE_CMD Close_Clipboard(REBREQ *req) | ||
/* | ||
***********************************************************************/ | ||
{ | ||
SET_CLOSED(req); | ||
return DR_DONE; | ||
} | ||
|
||
|
||
/*********************************************************************** | ||
** | ||
*/ DEVICE_CMD Read_Clipboard(REBREQ *req) | ||
/* | ||
***********************************************************************/ | ||
{ | ||
req->actual = 0; | ||
|
||
if(!GetClipboardStringData(req)){ | ||
req->error = 20; | ||
// just a random number... would be better to have some system for it | ||
return DR_ERROR; | ||
} | ||
|
||
return DR_DONE; | ||
} | ||
|
||
|
||
/*********************************************************************** | ||
** | ||
*/ DEVICE_CMD Write_Clipboard(REBREQ *req) | ||
/* | ||
** Expects data in UTF8 encoding in req->data! | ||
** | ||
***********************************************************************/ | ||
{ | ||
if(!SetClipboardStringData(req)) { | ||
req->error = 20; | ||
// just a random number... would be better to have some system for it | ||
return DR_ERROR; | ||
} | ||
return DR_DONE; | ||
} | ||
|
||
|
||
/*********************************************************************** | ||
** | ||
*/ DEVICE_CMD Poll_Clipboard(REBREQ *req) | ||
/* | ||
***********************************************************************/ | ||
{ | ||
return DR_DONE; | ||
} | ||
|
||
|
||
/*********************************************************************** | ||
** | ||
** Command Dispatch Table (RDC_ enum order) | ||
** | ||
***********************************************************************/ | ||
|
||
static DEVICE_CMD_FUNC Dev_Cmds[RDC_MAX] = | ||
{ | ||
0, | ||
0, | ||
Open_Clipboard, | ||
Close_Clipboard, | ||
Read_Clipboard, | ||
Write_Clipboard, | ||
Poll_Clipboard, | ||
}; | ||
|
||
DEFINE_DEV(Dev_Clipboard, "Clipboard", 1, Dev_Cmds, RDC_MAX, 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*********************************************************************** | ||
** | ||
** REBOL [R3] Language Interpreter and Run-time Environment | ||
** | ||
** Copyright 2021 Oldes | ||
** | ||
** Licensed under the Apache License, Version 2.0 (the "License"); | ||
** you may not use this file except in compliance with the License. | ||
** You may obtain a copy of the License at | ||
** | ||
** http://www.apache.org/licenses/LICENSE-2.0 | ||
** | ||
** Unless required by applicable law or agreed to in writing, software | ||
** distributed under the License is distributed on an "AS IS" BASIS, | ||
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
** See the License for the specific language governing permissions and | ||
** limitations under the License. | ||
** | ||
************************************************************************ | ||
** | ||
** Title: Device: Clipboard access for macOS | ||
** Author: Oldes | ||
** Purpose: | ||
** Provides a very simple interface to the clipboard for text. | ||
** May be expanded in the future for images, etc. | ||
** | ||
************************************************************************/ | ||
|
||
#include <AppKit/AppKit.h> | ||
#include "reb-host.h" | ||
#include "host-lib.h" // for OS_Make | ||
|
||
void EmptyClipboard(void){ | ||
[[NSPasteboard generalPasteboard] clearContents]; | ||
} | ||
|
||
REBOOL GetClipboardStringData(REBREQ *req){ | ||
unsigned long len; | ||
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; | ||
NSArray *classes=[NSArray arrayWithObject:[NSString class]]; | ||
NSDictionary *options=[NSDictionary dictionary]; | ||
NSArray *clipboardContent=[pasteboard readObjectsForClasses:classes options:options]; | ||
|
||
if(clipboardContent) | ||
{ | ||
if(0<[clipboardContent count]) | ||
{ | ||
NSString *str=[clipboardContent objectAtIndex:0]; | ||
if(NULL!=str) | ||
{ | ||
len = strlen([str UTF8String]); | ||
// req->length is used when reading just part of the result | ||
if (req->length > 0 && req->length < len) | ||
len = req->length; | ||
|
||
req->data = (REBYTE*)OS_Make(len+1); | ||
req->actual = len; | ||
COPY_STR(req->data, [str UTF8String], len); | ||
return TRUE; | ||
} | ||
} | ||
} | ||
return FALSE; | ||
} | ||
|
||
REBOOL SetClipboardStringData(REBREQ *req){ | ||
const char *str = cs_cast(req->data); | ||
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; | ||
[pasteboard clearContents]; | ||
|
||
NSString *nsstr = [NSString stringWithUTF8String:str]; | ||
|
||
if (!nsstr) return FALSE; | ||
NSArray *arr = [NSArray arrayWithObject:[NSString stringWithUTF8String:str]]; | ||
BOOL res = [pasteboard writeObjects:arr]; | ||
req->actual = req->length; | ||
|
||
return res; | ||
} |