forked from Eun/DisableMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
108 lines (104 loc) · 3.75 KB
/
main.m
1
/** * DisableMonitor, Disable Monitors on Mac * * Copyright (C) 2014 Tobias Salzmann * * DisableMonitor is free software: you can redistribute it and/or modify it under the terms of the * GNU General Public License as published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. * * DisableMonitor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU General Public License for more details. You should have received a copy of the GNU * General Public License along with DisableMonitor. If not, see <http://www.gnu.org/licenses/>. * * Authors: Tobias Salzmann */#import <Cocoa/Cocoa.h>#import "MonitorDataSource.h"#import "DisableMonitorAppDelegate.h"#import "DisplayIDAndName.h"void cmd_list(NSString* arg){ NSMutableArray *dict = [MonitorDataSource GetSortedDisplays]; if (dict == nil) { printf("No Displays found"); } else { printf(" ID Name\n"); printf("----------- -----------------\n"); for (DisplayIDAndName* idAndName in dict) { printf(" %-10u %s\n", [idAndName id], [[idAndName name] UTF8String]); } printf("----------- -----------------\n"); [dict release]; }}void cmd_disable(NSString *arg){ if ([arg length] < 2) return; CGDirectDisplayID displayID = [[NSUserDefaults standardUserDefaults] integerForKey:[arg substringFromIndex:1]]; if (CGDisplayIsOnline(displayID) && CGDisplayIsActive(displayID)) { [DisableMonitorAppDelegate toggleMonitor:displayID enabled:NO]; }}void cmd_enable(NSString *arg){ if ([arg length] < 2) return; CGDirectDisplayID displayID = [[NSUserDefaults standardUserDefaults] integerForKey:[arg substringFromIndex:1]]; if (CGDisplayIsOnline(displayID) && !CGDisplayIsActive(displayID)) [DisableMonitorAppDelegate toggleMonitor:displayID enabled:YES]; }void cmd_help(NSString *arg){ printf( "usage: DisableMonitor [options]\n" \ "Options:\n" \ "-l, --list list all attached monitors\n" \ "-d, --disable ID disable monitor with specified id\n" \ "-e, --enable ID enable monitor with specified id\n" \ "-h, --help show this help\n");}int main(int argc, char *argv[]){ NSArray *arguments = [[NSProcessInfo processInfo] arguments]; for (NSString* arg in arguments) { if ([arg caseInsensitiveCompare:@"--list"] == NSOrderedSame || [arg caseInsensitiveCompare:@"-list"] == NSOrderedSame || [arg caseInsensitiveCompare:@"-l"] == NSOrderedSame) { cmd_list(arg); return 0; } else if ([arg caseInsensitiveCompare:@"--disable"] == NSOrderedSame || [arg caseInsensitiveCompare:@"-disable"] == NSOrderedSame || [arg caseInsensitiveCompare:@"-d"] == NSOrderedSame) { cmd_disable(arg); return 0; } else if ([arg caseInsensitiveCompare:@"--enable"] == NSOrderedSame || [arg caseInsensitiveCompare:@"-enable"] == NSOrderedSame || [arg caseInsensitiveCompare:@"-e"] == NSOrderedSame) { cmd_enable(arg); return 0; } else if ([arg caseInsensitiveCompare:@"--help"] == NSOrderedSame || [arg caseInsensitiveCompare:@"-help"] == NSOrderedSame || [arg caseInsensitiveCompare:@"-h"] == NSOrderedSame) { cmd_help(arg); return 0; } } return NSApplicationMain(argc, (const char **) argv);}