Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #import "CallView.h" |
Anthony Léonard | 14e7bf3 | 2017-06-08 08:13:16 -0400 | [diff] [blame] | 21 | #import "CallLayer.h" |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 22 | |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 23 | #import <QUrl> |
| 24 | |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 25 | @interface CallView () |
| 26 | |
| 27 | @property NSMenu *contextualMenu; |
| 28 | |
| 29 | @end |
| 30 | |
| 31 | @implementation CallView |
Kateryna Kostiuk | 32cf6be | 2019-10-28 12:22:45 -0400 | [diff] [blame] | 32 | |
| 33 | NSString *currentDevice; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 34 | @synthesize contextualMenu; |
| 35 | @synthesize shouldAcceptInteractions; |
| 36 | |
| 37 | |
| 38 | - (id)initWithFrame:(NSRect)frame |
| 39 | { |
| 40 | self = [super initWithFrame:frame]; |
| 41 | if (self) |
| 42 | { |
| 43 | [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; |
Anthony Léonard | 14e7bf3 | 2017-06-08 08:13:16 -0400 | [diff] [blame] | 44 | [self setWantsLayer:YES]; |
Kateryna Kostiuk | c17db8d | 2018-10-23 09:48:03 -0400 | [diff] [blame] | 45 | [self setLayer:[CALayer layer]]; |
| 46 | [self.layer setBackgroundColor:[[NSColor blackColor] CGColor]]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 47 | } |
Alexandre Lision | a1eee3c | 2015-08-10 13:44:51 -0400 | [diff] [blame] | 48 | |
| 49 | [self.window setAcceptsMouseMovedEvents:YES]; |
| 50 | |
| 51 | NSTrackingAreaOptions options = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved); |
| 52 | |
| 53 | NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:frame |
| 54 | options:options |
| 55 | owner:self |
| 56 | userInfo:nil]; |
| 57 | |
| 58 | [self addTrackingArea:area]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 59 | return self; |
| 60 | } |
| 61 | |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 62 | #pragma mark - Destination Operations |
| 63 | |
| 64 | - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender |
| 65 | { |
| 66 | /*------------------------------------------------------ |
| 67 | method called whenever a drag enters our drop zone |
| 68 | --------------------------------------------------------*/ |
| 69 | NSLog(@"Dragging entered"); |
| 70 | |
| 71 | NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 72 | CFStringRef fileExtension = (__bridge CFStringRef) [fileURL.path pathExtension]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 73 | CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL); |
| 74 | |
| 75 | // Check if the pasteboard contains image data and source/user wants it copied |
| 76 | if ( [sender draggingSourceOperationMask] & NSDragOperationCopy && |
Alexandre Lision | ba9b708 | 2015-08-03 16:53:37 -0400 | [diff] [blame] | 77 | (UTTypeConformsTo(fileUTI, kUTTypeVideo)) || |
| 78 | (UTTypeConformsTo(fileUTI, kUTTypeMovie)) || |
| 79 | (UTTypeConformsTo(fileUTI, kUTTypeImage))) { |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 80 | |
| 81 | //highlight our drop zone |
| 82 | highlight=YES; |
| 83 | |
| 84 | [self setNeedsDisplay: YES]; |
| 85 | |
| 86 | /* When an image from one window is dragged over another, we want to resize the dragging item to |
| 87 | * preview the size of the image as it would appear if the user dropped it in. */ |
| 88 | [sender enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent |
| 89 | forView:self |
| 90 | classes:[NSArray arrayWithObject:[NSPasteboardItem class]] |
| 91 | searchOptions:nil |
| 92 | usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop) { |
| 93 | *stop = YES; |
| 94 | }]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 95 | CFRelease(fileUTI); |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 96 | //accept data as a copy operation |
| 97 | return NSDragOperationCopy; |
| 98 | } |
| 99 | |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 100 | CFRelease(fileUTI); |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 101 | return NSDragOperationNone; |
| 102 | } |
| 103 | |
| 104 | - (void)draggingExited:(id <NSDraggingInfo>)sender |
| 105 | { |
| 106 | /*------------------------------------------------------ |
| 107 | method called whenever a drag exits our drop zone |
| 108 | --------------------------------------------------------*/ |
| 109 | //remove highlight of the drop zone |
| 110 | highlight=NO; |
| 111 | |
| 112 | [self setNeedsDisplay: YES]; |
| 113 | } |
| 114 | |
| 115 | -(void)drawRect:(NSRect)rect |
| 116 | { |
| 117 | /*------------------------------------------------------ |
| 118 | draw method is overridden to do drop highlighing |
| 119 | --------------------------------------------------------*/ |
| 120 | //do the usual draw operation to display the image |
| 121 | [super drawRect:rect]; |
| 122 | |
| 123 | if ( highlight ) { |
| 124 | //highlight by overlaying a gray border |
| 125 | [[NSColor blueColor] set]; |
| 126 | [NSBezierPath setDefaultLineWidth: 5]; |
| 127 | [NSBezierPath strokeRect: rect]; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender |
| 132 | { |
| 133 | /*------------------------------------------------------ |
| 134 | method to determine if we can accept the drop |
| 135 | --------------------------------------------------------*/ |
| 136 | //finished with the drag so remove any highlighting |
| 137 | highlight=NO; |
| 138 | |
| 139 | [self setNeedsDisplay: YES]; |
| 140 | |
| 141 | NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 142 | CFStringRef fileExtension = (__bridge CFStringRef) [fileURL.path pathExtension]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 143 | CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL); |
| 144 | |
Alexandre Lision | ba9b708 | 2015-08-03 16:53:37 -0400 | [diff] [blame] | 145 | BOOL conforms = (UTTypeConformsTo(fileUTI, kUTTypeVideo)) || |
| 146 | (UTTypeConformsTo(fileUTI, kUTTypeMovie)) || |
| 147 | UTTypeConformsTo(fileUTI, kUTTypeImage); |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 148 | CFRelease(fileUTI); |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 149 | //check to see if we can accept the data |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 150 | return conforms; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 151 | } |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 152 | #if 0 |
| 153 | // TODO: add file as a source |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 154 | - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender |
| 155 | { |
| 156 | /*------------------------------------------------------ |
| 157 | method that should handle the drop data |
| 158 | --------------------------------------------------------*/ |
| 159 | if ( [sender draggingSource] != self ) { |
| 160 | NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]]; |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 161 | Call* call = [self getCurrentCall]; |
| 162 | if (call == nullptr) return; |
| 163 | if (auto outVideo = call->firstMedia<media::Video>(media::Media::Direction::OUT)) { |
| 164 | outVideo->sourceModel()->setFile(QUrl::fromLocalFile(QString::fromUtf8([fileURL.path UTF8String]))); |
| 165 | return YES; |
Alexandre Lision | e7a604c | 2015-12-10 09:29:02 -0500 | [diff] [blame] | 166 | } |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Alexandre Lision | e7a604c | 2015-12-10 09:29:02 -0500 | [diff] [blame] | 169 | return NO; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 170 | } |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 171 | #endif |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 172 | |
| 173 | - (void)showContextualMenu:(NSEvent *)theEvent { |
| 174 | |
| 175 | contextualMenu = [[NSMenu alloc] initWithTitle:@"Switch camera"]; |
| 176 | |
Kateryna Kostiuk | 5d90c3b | 2019-07-18 12:03:52 -0400 | [diff] [blame] | 177 | auto devices = [self.callDelegate getDeviceList]; |
Kateryna Kostiuk | 32cf6be | 2019-10-28 12:22:45 -0400 | [diff] [blame] | 178 | |
| 179 | currentDevice = [self.callDelegate getDefaultDeviceName]; |
Kateryna Kostiuk | 5d90c3b | 2019-07-18 12:03:52 -0400 | [diff] [blame] | 180 | |
| 181 | for(int i = 0 ; i < devices.size() ; ++i) { |
| 182 | std::string device = devices[i]; |
| 183 | [contextualMenu insertItemWithTitle:@(device.c_str()) action:@selector(switchInput:) keyEquivalent:@"" atIndex:i]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 184 | } |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 185 | [contextualMenu insertItemWithTitle:NSLocalizedString(@"Share screen", @"Contextual menu entry") |
| 186 | action:@selector(captureScreen:) |
| 187 | keyEquivalent:@"" |
| 188 | atIndex:contextualMenu.itemArray.count]; |
| 189 | #if 0 |
| 190 | // TODO: add file as a source |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 191 | [contextualMenu addItem:[NSMenuItem separatorItem]]; |
Alexandre Lision | 7f01b07 | 2015-09-15 14:57:05 -0400 | [diff] [blame] | 192 | [contextualMenu insertItemWithTitle:NSLocalizedString(@"Choose file", @"Contextual menu entry") |
| 193 | action:@selector(chooseFile:) |
| 194 | keyEquivalent:@"" |
| 195 | atIndex:contextualMenu.itemArray.count]; |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 196 | #endif |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 197 | |
Kateryna Kostiuk | 32cf6be | 2019-10-28 12:22:45 -0400 | [diff] [blame] | 198 | auto menuItem = [contextualMenu itemWithTitle:currentDevice]; |
| 199 | if(menuItem) { |
| 200 | [menuItem setState: NSControlStateValueOn]; |
| 201 | } |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 202 | [NSMenu popUpContextMenu:contextualMenu withEvent:theEvent forView:self]; |
| 203 | } |
| 204 | |
Alexandre Lision | a1eee3c | 2015-08-10 13:44:51 -0400 | [diff] [blame] | 205 | - (void)mouseMoved:(NSEvent *)theEvent |
| 206 | { |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 207 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; // cancel showContextualMenu |
| 208 | [self performSelector:@selector(mouseIdle:) withObject:theEvent afterDelay:3]; |
| 209 | if (self.callDelegate && shouldAcceptInteractions) |
| 210 | [self.callDelegate mouseIsMoving:YES]; |
Alexandre Lision | a1eee3c | 2015-08-10 13:44:51 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | - (void) mouseIdle:(NSEvent *)theEvent |
| 214 | { |
| 215 | if (self.callDelegate && shouldAcceptInteractions) |
| 216 | [self.callDelegate mouseIsMoving:NO]; |
| 217 | } |
| 218 | |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 219 | - (void) rightMouseUp: (NSEvent*) theEvent { |
| 220 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; |
| 221 | if([theEvent clickCount] == 1 && shouldAcceptInteractions) { |
| 222 | [self performSelector:@selector(showContextualMenu:) withObject:theEvent]; |
| 223 | } |
| 224 | } |
Alexandre Lision | a1eee3c | 2015-08-10 13:44:51 -0400 | [diff] [blame] | 225 | |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 226 | - (void)mouseUp:(NSEvent *)theEvent |
| 227 | { |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 228 | if([theEvent clickCount] == 2 && self.callDelegate) { |
| 229 | [self.callDelegate callShouldToggleFullScreen]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | - (void) switchInput:(NSMenuItem*) sender |
| 234 | { |
Kateryna Kostiuk | 32cf6be | 2019-10-28 12:22:45 -0400 | [diff] [blame] | 235 | if([sender.title isEqualToString:currentDevice]) { |
| 236 | return; |
| 237 | } |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 238 | int index = [contextualMenu indexOfItem:sender]; |
Kateryna Kostiuk | 5d90c3b | 2019-07-18 12:03:52 -0400 | [diff] [blame] | 239 | [self.callDelegate switchToDevice: index]; |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | - (void) captureScreen:(NSMenuItem*) sender |
| 243 | { |
Kateryna Kostiuk | 32cf6be | 2019-10-28 12:22:45 -0400 | [diff] [blame] | 244 | if([sender.title isEqualToString:currentDevice]) { |
| 245 | return; |
| 246 | } |
Kateryna Kostiuk | 5d90c3b | 2019-07-18 12:03:52 -0400 | [diff] [blame] | 247 | [self.callDelegate screenShare]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | - (void) chooseFile:(NSMenuItem*) sender |
| 251 | { |
Kateryna Kostiuk | 32cf6be | 2019-10-28 12:22:45 -0400 | [diff] [blame] | 252 | if([sender.title isEqualToString:currentDevice]) { |
| 253 | return; |
| 254 | } |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 255 | NSOpenPanel *browsePanel = [[NSOpenPanel alloc] init]; |
| 256 | [browsePanel setDirectoryURL:[NSURL URLWithString:NSHomeDirectory()]]; |
| 257 | [browsePanel setCanChooseFiles:YES]; |
| 258 | [browsePanel setCanChooseDirectories:NO]; |
| 259 | [browsePanel setCanCreateDirectories:NO]; |
| 260 | |
Kateryna Kostiuk | 5d90c3b | 2019-07-18 12:03:52 -0400 | [diff] [blame] | 261 | NSMutableArray* fileTypes = [[NSMutableArray alloc] initWithArray:[NSImage imageTypes]]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 262 | [fileTypes addObject:(__bridge NSString *)kUTTypeVideo]; |
| 263 | [fileTypes addObject:(__bridge NSString *)kUTTypeMovie]; |
Alexandre Lision | ba9b708 | 2015-08-03 16:53:37 -0400 | [diff] [blame] | 264 | [fileTypes addObject:(__bridge NSString *)kUTTypeImage]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 265 | [browsePanel setAllowedFileTypes:fileTypes]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 266 | [browsePanel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) { |
| 267 | if (result == NSFileHandlingPanelOKButton) { |
| 268 | NSURL* theDoc = [[browsePanel URLs] objectAtIndex:0]; |
Kateryna Kostiuk | 5d90c3b | 2019-07-18 12:03:52 -0400 | [diff] [blame] | 269 | [self.callDelegate switchToFile: [theDoc.path UTF8String]]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 270 | } |
| 271 | }]; |
Kateryna Kostiuk | d76f620 | 2018-09-11 17:15:28 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 274 | @end |