2 * MACDRV Cocoa clipboard code
4 * Copyright 2012, 2013 Ken Thomases for CodeWeavers Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "macdrv_cocoa.h"
23 #import "cocoa_event.h"
24 #import "cocoa_window.h"
27 static int owned_change_count = -1;
28 static int change_count = -1;
30 static NSArray* BitmapOutputTypes;
31 static NSDictionary* BitmapOutputTypeMap;
32 static dispatch_once_t BitmapOutputTypesInitOnce;
34 static NSString* const OwnershipSentinel = @"org.winehq.wine.winemac.pasteboard-ownership-sentinel";
37 /***********************************************************************
38 * macdrv_is_pasteboard_owner
40 int macdrv_is_pasteboard_owner(macdrv_window w)
43 WineWindow* window = (WineWindow*)w;
46 NSPasteboard* pb = [NSPasteboard generalPasteboard];
47 ret = ([pb changeCount] == owned_change_count);
49 [window.queue discardEventsMatchingMask:event_mask_for_type(LOST_PASTEBOARD_OWNERSHIP)
56 /***********************************************************************
57 * macdrv_has_pasteboard_changed
59 int macdrv_has_pasteboard_changed(void)
61 __block int new_change_count;
65 NSPasteboard* pb = [NSPasteboard generalPasteboard];
66 new_change_count = [pb changeCount];
69 ret = (change_count != new_change_count);
70 change_count = new_change_count;
74 /***********************************************************************
75 * macdrv_copy_pasteboard_types
77 * Returns an array of UTI strings for the types of data available on
78 * the pasteboard, or NULL on error. The caller is responsible for
79 * releasing the returned array with CFRelease().
81 CFArrayRef macdrv_copy_pasteboard_types(CFTypeRef pasteboard)
83 NSPasteboard* pb = (NSPasteboard*)pasteboard;
84 __block CFArrayRef ret = NULL;
85 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
87 dispatch_once(&BitmapOutputTypesInitOnce, ^{
88 NSArray* bitmapFileTypes = [NSArray arrayWithObjects:
89 [NSNumber numberWithUnsignedInteger:NSTIFFFileType],
90 [NSNumber numberWithUnsignedInteger:NSPNGFileType],
91 [NSNumber numberWithUnsignedInteger:NSBMPFileType],
92 [NSNumber numberWithUnsignedInteger:NSGIFFileType],
93 [NSNumber numberWithUnsignedInteger:NSJPEGFileType],
96 BitmapOutputTypes = [[NSArray alloc] initWithObjects:@"public.tiff", @"public.png",
97 @"com.microsoft.bmp", @"com.compuserve.gif", @"public.jpeg", nil];
99 BitmapOutputTypeMap = [[NSDictionary alloc] initWithObjects:bitmapFileTypes
100 forKeys:BitmapOutputTypes];
106 NSPasteboard* local_pb = pb;
109 if (!local_pb) local_pb = [NSPasteboard generalPasteboard];
110 types = [local_pb types];
112 // If there are any types understood by NSBitmapImageRep, then we
113 // can offer all of the types that it can output, too. For example,
114 // if TIFF is on the pasteboard, we can offer PNG, BMP, etc. to the
115 // Windows program. We'll convert on demand.
116 if ([types firstObjectCommonWithArray:[NSBitmapImageRep imageTypes]] ||
117 [types firstObjectCommonWithArray:[NSBitmapImageRep imagePasteboardTypes]])
119 NSMutableArray* newTypes = [BitmapOutputTypes mutableCopy];
120 [newTypes removeObjectsInArray:types];
121 types = [types arrayByAddingObjectsFromArray:newTypes];
125 ret = (CFArrayRef)[types copy];
129 ERR(@"Exception discarded while copying pasteboard types: %@\n", e);
138 /***********************************************************************
139 * macdrv_copy_pasteboard_data
141 * Returns the pasteboard data for a specified type, or NULL on error or
142 * if there's no such type on the pasteboard. The caller is responsible
143 * for releasing the returned data object with CFRelease().
145 CFDataRef macdrv_copy_pasteboard_data(CFTypeRef pasteboard, CFStringRef type)
147 NSPasteboard* pb = (NSPasteboard*)pasteboard;
148 __block NSData* ret = nil;
153 NSPasteboard* local_pb = pb;
154 if (!local_pb) local_pb = [NSPasteboard generalPasteboard];
155 if ([local_pb availableTypeFromArray:[NSArray arrayWithObject:(NSString*)type]])
156 ret = [[local_pb dataForType:(NSString*)type] copy];
159 NSNumber* bitmapType = [BitmapOutputTypeMap objectForKey:(NSString*)type];
162 NSArray* reps = [NSBitmapImageRep imageRepsWithPasteboard:local_pb];
163 ret = [NSBitmapImageRep representationOfImageRepsInArray:reps
164 usingType:[bitmapType unsignedIntegerValue]
172 ERR(@"Exception discarded while copying pasteboard types: %@\n", e);
176 return (CFDataRef)ret;
180 /***********************************************************************
181 * macdrv_clear_pasteboard
183 * Takes ownership of the Mac pasteboard and clears it of all data types.
185 void macdrv_clear_pasteboard(macdrv_window w)
187 WineWindow* window = (WineWindow*)w;
192 NSPasteboard* pb = [NSPasteboard generalPasteboard];
193 owned_change_count = [pb declareTypes:[NSArray arrayWithObject:OwnershipSentinel]
195 [window.queue discardEventsMatchingMask:event_mask_for_type(LOST_PASTEBOARD_OWNERSHIP)
200 ERR(@"Exception discarded while clearing pasteboard: %@\n", e);
206 /***********************************************************************
207 * macdrv_set_pasteboard_data
209 * Sets the pasteboard data for a specified type. Replaces any data of
210 * that type already on the pasteboard. If data is NULL, promises the
213 * Returns 0 on error, non-zero on success.
215 int macdrv_set_pasteboard_data(CFStringRef type, CFDataRef data, macdrv_window w)
218 WineWindow* window = (WineWindow*)w;
223 NSPasteboard* pb = [NSPasteboard generalPasteboard];
224 NSInteger change_count = [pb addTypes:[NSArray arrayWithObject:(NSString*)type]
228 owned_change_count = change_count;
230 ret = [pb setData:(NSData*)data forType:(NSString*)type];
237 ERR(@"Exception discarded while copying pasteboard types: %@\n", e);