Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 11 | * os_macosx.m -- Mac specific things for Mac OS X. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Bram Moolenaar | f536bf6 | 2018-03-06 17:55:01 +0100 | [diff] [blame] | 14 | /* Suppress compiler warnings to non-C89 code. */ |
| 15 | #if defined(__clang__) && defined(__STRICT_ANSI__) |
| 16 | # pragma clang diagnostic push |
| 17 | # pragma clang diagnostic ignored "-Wc99-extensions" |
| 18 | # pragma clang diagnostic push |
| 19 | # pragma clang diagnostic ignored "-Wdeclaration-after-statement" |
| 20 | #endif |
| 21 | |
Bram Moolenaar | 423f972 | 2010-10-10 17:08:43 +0200 | [diff] [blame] | 22 | /* Avoid a conflict for the definition of Boolean between Mac header files and |
| 23 | * X11 header files. */ |
| 24 | #define NO_X11_INCLUDES |
| 25 | |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 26 | #include <stdbool.h> |
| 27 | #include <mach/boolean.h> |
| 28 | #include <sys/errno.h> |
| 29 | #include <stdlib.h> |
| 30 | |
| 31 | #include <dispatch/dispatch.h> |
| 32 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 33 | #include "vim.h" |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 34 | #import <AppKit/AppKit.h> |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 35 | |
| 36 | |
Bram Moolenaar | e00289d | 2010-08-14 21:56:42 +0200 | [diff] [blame] | 37 | /* |
| 38 | * Clipboard support for the console. |
Bram Moolenaar | e00289d | 2010-08-14 21:56:42 +0200 | [diff] [blame] | 39 | */ |
Bram Moolenaar | be7529e | 2020-08-13 21:05:39 +0200 | [diff] [blame] | 40 | #if defined(FEAT_CLIPBOARD) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 41 | |
Bram Moolenaar | 66bd1c9 | 2010-07-14 20:31:44 +0200 | [diff] [blame] | 42 | /* Used to identify clipboard data copied from Vim. */ |
| 43 | |
| 44 | NSString *VimPboardType = @"VimPboardType"; |
| 45 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 46 | void |
Bram Moolenaar | 2fc39ae | 2019-06-14 23:27:29 +0200 | [diff] [blame] | 47 | clip_mch_lose_selection(Clipboard_T *cbd UNUSED) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | |
| 52 | int |
Bram Moolenaar | 2fc39ae | 2019-06-14 23:27:29 +0200 | [diff] [blame] | 53 | clip_mch_own_selection(Clipboard_T *cbd UNUSED) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 54 | { |
| 55 | /* This is called whenever there is a new selection and 'guioptions' |
| 56 | * contains the "a" flag (automatically copy selection). Return TRUE, else |
| 57 | * the "a" flag does nothing. Note that there is no concept of "ownership" |
| 58 | * of the clipboard in Mac OS X. |
| 59 | */ |
| 60 | return TRUE; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void |
Bram Moolenaar | 2fc39ae | 2019-06-14 23:27:29 +0200 | [diff] [blame] | 65 | clip_mch_request_selection(Clipboard_T *cbd) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 66 | { |
| 67 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 68 | |
| 69 | NSPasteboard *pb = [NSPasteboard generalPasteboard]; |
Bram Moolenaar | 1d3dbcf | 2018-10-08 20:07:39 +0200 | [diff] [blame] | 70 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 71 | NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, |
| 72 | NSPasteboardTypeString, nil]; |
| 73 | #else |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 74 | NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, |
| 75 | NSStringPboardType, nil]; |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 76 | #endif |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 77 | NSString *bestType = [pb availableTypeFromArray:supportedTypes]; |
| 78 | if (!bestType) goto releasepool; |
| 79 | |
Bram Moolenaar | 54b08a5 | 2011-06-20 00:25:44 +0200 | [diff] [blame] | 80 | int motion_type = MAUTO; |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 81 | NSString *string = nil; |
| 82 | |
| 83 | if ([bestType isEqual:VimPboardType]) |
| 84 | { |
| 85 | /* This type should consist of an array with two objects: |
| 86 | * 1. motion type (NSNumber) |
| 87 | * 2. text (NSString) |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 88 | * If this is not the case we fall back on using NSPasteboardTypeString. |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 89 | */ |
| 90 | id plist = [pb propertyListForType:VimPboardType]; |
| 91 | if ([plist isKindOfClass:[NSArray class]] && [plist count] == 2) |
| 92 | { |
| 93 | id obj = [plist objectAtIndex:1]; |
| 94 | if ([obj isKindOfClass:[NSString class]]) |
| 95 | { |
| 96 | motion_type = [[plist objectAtIndex:0] intValue]; |
| 97 | string = obj; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (!string) |
| 103 | { |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 104 | /* Use NSPasteboardTypeString. The motion type is detected automatically. |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 105 | */ |
Bram Moolenaar | 1d3dbcf | 2018-10-08 20:07:39 +0200 | [diff] [blame] | 106 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 107 | NSMutableString *mstring = |
| 108 | [[pb stringForType:NSPasteboardTypeString] mutableCopy]; |
| 109 | #else |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 110 | NSMutableString *mstring = |
| 111 | [[pb stringForType:NSStringPboardType] mutableCopy]; |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 112 | #endif |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 113 | if (!mstring) goto releasepool; |
| 114 | |
| 115 | /* Replace unrecognized end-of-line sequences with \x0a (line feed). */ |
| 116 | NSRange range = { 0, [mstring length] }; |
| 117 | unsigned n = [mstring replaceOccurrencesOfString:@"\x0d\x0a" |
| 118 | withString:@"\x0a" options:0 |
| 119 | range:range]; |
| 120 | if (0 == n) |
| 121 | { |
| 122 | n = [mstring replaceOccurrencesOfString:@"\x0d" withString:@"\x0a" |
| 123 | options:0 range:range]; |
| 124 | } |
Bram Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 125 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 126 | string = mstring; |
| 127 | } |
| 128 | |
Bram Moolenaar | 54b08a5 | 2011-06-20 00:25:44 +0200 | [diff] [blame] | 129 | /* Default to MAUTO, uses MCHAR or MLINE depending on trailing NL. */ |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 130 | if (!(MCHAR == motion_type || MLINE == motion_type || MBLOCK == motion_type |
| 131 | || MAUTO == motion_type)) |
Bram Moolenaar | 54b08a5 | 2011-06-20 00:25:44 +0200 | [diff] [blame] | 132 | motion_type = MAUTO; |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 133 | |
| 134 | char_u *str = (char_u*)[string UTF8String]; |
| 135 | int len = [string lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
| 136 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 137 | if (input_conv.vc_type != CONV_NONE) |
| 138 | str = string_convert(&input_conv, str, &len); |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 139 | |
| 140 | if (str) |
| 141 | clip_yank_selection(motion_type, str, len, cbd); |
| 142 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 143 | if (input_conv.vc_type != CONV_NONE) |
| 144 | vim_free(str); |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 145 | |
| 146 | releasepool: |
| 147 | [pool release]; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /* |
| 152 | * Send the current selection to the clipboard. |
| 153 | */ |
| 154 | void |
Bram Moolenaar | 2fc39ae | 2019-06-14 23:27:29 +0200 | [diff] [blame] | 155 | clip_mch_set_selection(Clipboard_T *cbd) |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 156 | { |
| 157 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 158 | |
| 159 | /* If the '*' register isn't already filled in, fill it in now. */ |
| 160 | cbd->owned = TRUE; |
| 161 | clip_get_selection(cbd); |
| 162 | cbd->owned = FALSE; |
Bram Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 163 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 164 | /* Get the text to put on the pasteboard. */ |
| 165 | long_u llen = 0; char_u *str = 0; |
| 166 | int motion_type = clip_convert_selection(&str, &llen, cbd); |
| 167 | if (motion_type < 0) |
| 168 | goto releasepool; |
| 169 | |
| 170 | /* TODO: Avoid overflow. */ |
| 171 | int len = (int)llen; |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 172 | if (output_conv.vc_type != CONV_NONE) |
| 173 | { |
| 174 | char_u *conv_str = string_convert(&output_conv, str, &len); |
| 175 | if (conv_str) |
| 176 | { |
| 177 | vim_free(str); |
| 178 | str = conv_str; |
| 179 | } |
| 180 | } |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 181 | |
| 182 | if (len > 0) |
| 183 | { |
| 184 | NSString *string = [[NSString alloc] |
| 185 | initWithBytes:str length:len encoding:NSUTF8StringEncoding]; |
| 186 | |
| 187 | /* See clip_mch_request_selection() for info on pasteboard types. */ |
| 188 | NSPasteboard *pb = [NSPasteboard generalPasteboard]; |
Bram Moolenaar | 1d3dbcf | 2018-10-08 20:07:39 +0200 | [diff] [blame] | 189 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 190 | NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, |
| 191 | NSPasteboardTypeString, nil]; |
| 192 | #else |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 193 | NSArray *supportedTypes = [NSArray arrayWithObjects:VimPboardType, |
| 194 | NSStringPboardType, nil]; |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 195 | #endif |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 196 | [pb declareTypes:supportedTypes owner:nil]; |
| 197 | |
| 198 | NSNumber *motion = [NSNumber numberWithInt:motion_type]; |
| 199 | NSArray *plist = [NSArray arrayWithObjects:motion, string, nil]; |
| 200 | [pb setPropertyList:plist forType:VimPboardType]; |
| 201 | |
Bram Moolenaar | 1d3dbcf | 2018-10-08 20:07:39 +0200 | [diff] [blame] | 202 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 203 | [pb setString:string forType:NSPasteboardTypeString]; |
| 204 | #else |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 205 | [pb setString:string forType:NSStringPboardType]; |
Bram Moolenaar | d595a19 | 2018-06-17 15:01:04 +0200 | [diff] [blame] | 206 | #endif |
Bram Moolenaar | d43848c | 2010-07-14 14:28:26 +0200 | [diff] [blame] | 207 | |
Bram Moolenaar | 164fca3 | 2010-07-14 13:58:07 +0200 | [diff] [blame] | 208 | [string release]; |
| 209 | } |
| 210 | |
| 211 | vim_free(str); |
| 212 | releasepool: |
| 213 | [pool release]; |
| 214 | } |
| 215 | |
| 216 | #endif /* FEAT_CLIPBOARD */ |
Bram Moolenaar | f536bf6 | 2018-03-06 17:55:01 +0100 | [diff] [blame] | 217 | |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 218 | #ifdef FEAT_RELTIME |
| 219 | /* |
| 220 | * The following timer code is based on a Gist by Jorgen Lundman: |
| 221 | * |
| 222 | * https://gist.github.com/lundman |
| 223 | */ |
| 224 | |
| 225 | typedef struct macos_timer macos_timer_T; |
| 226 | |
| 227 | static void |
| 228 | _timer_cancel(void *arg UNUSED) |
| 229 | { |
| 230 | // This is not currently used, but it might be useful in the future and |
| 231 | // it is non-trivial enough to provide as usable implementation. |
| 232 | # if 0 |
| 233 | macos_timer_T *timerid = (macos_timer_T *)arg; |
| 234 | |
| 235 | dispatch_release(timerid->tim_timer); |
| 236 | dispatch_release(timerid->tim_queue); |
| 237 | timerid->tim_timer = NULL; |
| 238 | timerid->tim_queue = NULL; |
| 239 | free(timerid); |
| 240 | # endif |
| 241 | } |
| 242 | |
| 243 | static void |
| 244 | _timer_handler(void *arg) |
| 245 | { |
| 246 | macos_timer_T *timerid = (macos_timer_T *)arg; |
| 247 | union sigval sv; |
| 248 | |
| 249 | sv.sival_ptr = timerid->tim_arg; |
| 250 | |
| 251 | if (timerid->tim_func != NULL) |
| 252 | timerid->tim_func(sv); |
| 253 | } |
| 254 | |
| 255 | static uint64_t |
| 256 | itime_to_ns(const struct timespec *it) |
| 257 | { |
| 258 | time_t sec = it->tv_sec; |
| 259 | long nsec = it->tv_nsec; |
| 260 | uint64_t ns = NSEC_PER_SEC * sec + nsec; |
| 261 | |
| 262 | return ns == 0 ? DISPATCH_TIME_FOREVER : ns; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * A partial emulation of the POSIX timer_create function. |
| 267 | * |
| 268 | * The limitations and differences include: |
| 269 | * |
| 270 | * - Only CLOCK_REALTIME and CLOCK_MONOTONIC are supported as clockid |
| 271 | * values. |
| 272 | * - Even if CLOCK_REALTIME is specified, internally the mach_absolute_time |
| 273 | * source is used internally. |
| 274 | * - The only notification method supported is SIGEV_THREAD. |
| 275 | */ |
| 276 | inline int |
| 277 | timer_create(clockid_t clockid, struct sigevent *sevp, timer_t *timerid) |
| 278 | { |
| 279 | macos_timer_T *timer = NULL; |
| 280 | |
| 281 | // We only support real time and monotonic clocks; and SIGEV_THREAD |
| 282 | // notification. In practice, there is no difference between the two |
| 283 | // types of clocks on MacOS - we always use the mach_machine_time |
| 284 | // source. |
| 285 | if ( (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC) |
| 286 | || sevp->sigev_notify != SIGEV_THREAD) |
| 287 | { |
| 288 | semsg("clockid: %d %d", clockid, CLOCK_REALTIME); |
| 289 | semsg("notify: %d %d", sevp->sigev_notify, SIGEV_THREAD); |
| 290 | errno = ENOTSUP; |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | timer = (macos_timer_T *)malloc(sizeof(macos_timer_T)); |
| 295 | if (timer == NULL) |
| 296 | { |
| 297 | errno = ENOMEM; |
| 298 | return -1; |
| 299 | } |
| 300 | *timerid = timer; |
| 301 | |
| 302 | timer->tim_queue = dispatch_queue_create( |
| 303 | "org.vim.timerqueue", NULL); |
| 304 | if (timer->tim_queue == NULL) |
| 305 | { |
| 306 | errno = ENOMEM; |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | timer->tim_timer = dispatch_source_create( |
| 311 | DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timer->tim_queue); |
| 312 | if (timer->tim_timer == NULL) |
| 313 | { |
| 314 | errno = ENOMEM; |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | timer->tim_func = sevp->sigev_notify_function; |
| 319 | timer->tim_arg = sevp->sigev_value.sival_ptr; |
| 320 | |
| 321 | dispatch_set_context(timer->tim_timer, timer); |
| 322 | dispatch_source_set_event_handler_f(timer->tim_timer, _timer_handler); |
| 323 | dispatch_source_set_cancel_handler_f(timer->tim_timer, _timer_cancel); |
| 324 | |
| 325 | dispatch_resume(timer->tim_timer); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * A partial emulation of the POSIX timer_settime function. |
| 332 | * |
| 333 | * The limitations and differences include: |
| 334 | * |
| 335 | * - The flags argument is ignored. The supplied new_value is therfore |
| 336 | * always treated as a relative time. |
| 337 | * - The old_value argument is ignored. |
| 338 | */ |
| 339 | int |
| 340 | timer_settime( |
| 341 | timer_t timerid, |
| 342 | int unused_flags UNUSED, |
| 343 | const struct itimerspec *new_value, |
| 344 | struct itimerspec *old_value UNUSED) |
| 345 | { |
| 346 | uint64_t first_shot = itime_to_ns(&new_value->it_value); |
| 347 | |
| 348 | if (timerid == NULL) |
| 349 | return 0; |
| 350 | |
| 351 | if (first_shot == DISPATCH_TIME_FOREVER) |
| 352 | { |
| 353 | dispatch_source_set_timer( |
| 354 | timerid->tim_timer, first_shot, first_shot, 0); |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | uint64_t interval = itime_to_ns(&new_value->it_interval); |
| 359 | |
| 360 | dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, first_shot); |
| 361 | dispatch_source_set_timer(timerid->tim_timer, start, interval, 0); |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * An emulation of the POSIX timer_delete function. |
| 369 | * |
| 370 | * Disabled because it is not currently used, but an implemented provided |
| 371 | * for completeness and possible future use. |
| 372 | */ |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 373 | int |
| 374 | timer_delete(timer_t timerid) |
| 375 | { |
| 376 | /* Calls _timer_cancel() */ |
| 377 | if (timerid != NULL) |
| 378 | dispatch_source_cancel(timerid->tim_timer); |
| 379 | |
| 380 | return 0; |
| 381 | } |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 382 | |
| 383 | #endif /* FEAT_RELTIME */ |
| 384 | |
Bram Moolenaar | f536bf6 | 2018-03-06 17:55:01 +0100 | [diff] [blame] | 385 | /* Lift the compiler warning suppression. */ |
| 386 | #if defined(__clang__) && defined(__STRICT_ANSI__) |
| 387 | # pragma clang diagnostic pop |
| 388 | # pragma clang diagnostic pop |
| 389 | #endif |