aboutsummaryrefslogtreecommitdiff
path: root/QSCapturaPlugin/Actions/StartRecordingAction.m
blob: a945f29ffc7ded24f56b2a4130ba563400babae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#import "StartRecordingAction.h"
#import <ApplicationServices/ApplicationServices.h>

NS_ASSUME_NONNULL_BEGIN

@implementation StartRecordingAction

- (QSObject *)startRecordingFor:(QSObject *)directObject
{
  id window = [directObject objectForType:kWindowsType];
  
  if (!window) {
    NSLog(@"Could not get a window ID.");
    return directObject;
  }
  
  AXUIElementRef windowElement = (AXUIElementRef)window;
  CFTypeRef positionValue = NULL;
  CFTypeRef sizeValue = NULL;

  AXError positionError = AXUIElementCopyAttributeValue(windowElement, kAXPositionAttribute, &positionValue);
  AXError sizeError = AXUIElementCopyAttributeValue(windowElement, kAXSizeAttribute, &sizeValue);

  if (positionError == kAXErrorSuccess && sizeError == kAXErrorSuccess) {
      CGPoint position;
      CGSize size;
      
      if (AXValueGetValue(positionValue, kAXValueCGPointType, &position) &&
          AXValueGetValue(sizeValue, kAXValueCGSizeType, &size)) {
        
       CGRect mainDisplayBounds = CGDisplayBounds(CGMainDisplayID());
       CGFloat displayHeight = mainDisplayBounds.size.height;
       CGFloat flippedY = displayHeight - position.y - size.height;

        NSString *capturaURL = [NSString stringWithFormat:@"captura:?action=record&auto_start=true&x=%.0f&y=%.0f&width=%.0f&height=%.0f",
                                      position.x, flippedY, size.width, size.height];
               
        NSURL *url = [NSURL URLWithString:capturaURL];
        if (url) {
          [[NSWorkspace sharedWorkspace] openURL:url];
        } else {
          NSLog(@"Failed to create valid URL");
        }
      }
  } else {
    NSLog(@"Could not get size and position of window.");
  }

  if (positionValue) CFRelease(positionValue);
  if (sizeValue) CFRelease(sizeValue);
  
  return directObject;
}

@end

NS_ASSUME_NONNULL_END