This repository has been archived by the owner on Dec 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 352
/
SRApplicationDelegate.mm
259 lines (203 loc) · 6.26 KB
/
SRApplicationDelegate.mm
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "SRApplicationDelegate.h"
#import "utils.h"
#import "ResMenuItem.h"
@implementation SRApplicationDelegate
- (void) showAbout
{
[NSApp activateIgnoringOtherApps:YES];
[NSApp orderFrontStandardAboutPanel:self];
}
- (void) quit
{
[NSApp terminate: self];
}
- (void) refreshStatusMenu
{
if(statusMenu)
[statusMenu release];
statusMenu = [[NSMenu alloc] initWithTitle: @""];
uint32_t nDisplays;
CGDirectDisplayID displays[0x10];
CGGetOnlineDisplayList(0x10, displays, &nDisplays);
for(int i=0; i<nDisplays; i++)
{
CGDirectDisplayID display = displays[i];
{
NSMenuItem* item;
NSString* title = i ? [NSString stringWithFormat: @"Display %d", i+1] : @"Main Display";
item = [[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""];
[item setEnabled: NO];
[statusMenu addItem: item];
}
int mainModeNum;
CGSGetCurrentDisplayMode(display, &mainModeNum);
//modes_D4 mainMode;
//CGSGetDisplayModeDescriptionOfLength(display, mainModeNum, &mainMode, 0xD4);
ResMenuItem* mainItem = nil;
int nModes;
modes_D4* modes;
CopyAllDisplayModes(display, &modes, &nModes);
{
NSMutableArray* displayMenuItems = [NSMutableArray new];
//ResMenuItem* mainItem = nil;
for(int j = 0; j <nModes; j++)
{
ResMenuItem* item = [[ResMenuItem alloc] initWithDisplay: display andMode: &modes[j]];
//[item autorelease];
if(mainModeNum == j)
{
mainItem = item;
[item setState: NSOnState];
}
[displayMenuItems addObject: item];
[item release];
}
int idealColorDepth = 32;
double idealRefreshRate = 0.0f;
if(mainItem)
{
idealColorDepth = [mainItem colorDepth];
idealRefreshRate = [mainItem refreshRate];
}
[displayMenuItems sortUsingSelector: @selector(compareResMenuItem:)];
NSMenu* submenu = [[NSMenu alloc] initWithTitle: @""];
ResMenuItem* lastAddedItem = nil;
for(int j=0; j < [displayMenuItems count]; j++)
{
ResMenuItem* item = [displayMenuItems objectAtIndex: j];
if([item colorDepth] == idealColorDepth)
{
if([item refreshRate] == idealRefreshRate)
{
[item setTextFormat: 1];
}
if(lastAddedItem && [lastAddedItem width]==[item width] && [lastAddedItem height]==[item height] && [lastAddedItem scale]==[item scale])
{
double lastRefreshRate = lastAddedItem ? [lastAddedItem refreshRate] : 0;
double refreshRate = [item refreshRate];
if(!lastAddedItem || (lastRefreshRate != idealRefreshRate && (refreshRate == idealRefreshRate || refreshRate > lastRefreshRate)))
{
if(lastAddedItem)
{
[submenu removeItem: lastAddedItem];
lastAddedItem = nil;
}
[submenu addItem: item];
lastAddedItem = item;
}
}
else
{
[submenu addItem: item];
lastAddedItem = item;
}
}
}
NSString* title;
{
if([mainItem scale] == 2.0f)
{
title = [NSString stringWithFormat: @"%d × %d ⚡️️", [mainItem width], [mainItem height]];
}
else
{
title = [NSString stringWithFormat: @"%d × %d", [mainItem width], [mainItem height]];
}
}
NSMenuItem* resolution = [[NSMenuItem alloc] initWithTitle: title action: nil keyEquivalent: @""];
[resolution setSubmenu: submenu];
[submenu release];
[statusMenu addItem: resolution];
[resolution release];
[displayMenuItems release];
}
{
NSMutableArray* displayMenuItems = [NSMutableArray new];
ResMenuItem* mainItem = nil;
for(int j = 0; j < nModes; j++)
{
ResMenuItem* item = [[ResMenuItem alloc] initWithDisplay: display andMode: &modes[j]];
[item setTextFormat: 2];
//[item autorelease];
if(mainModeNum == j)
{
mainItem = item;
[item setState: NSOnState];
}
[displayMenuItems addObject: item];
[item release];
}
int idealColorDepth = 32;
double idealRefreshRate = 0.0f;
if(mainItem)
{
idealColorDepth = [mainItem colorDepth];
idealRefreshRate = [mainItem refreshRate];
}
[displayMenuItems sortUsingSelector: @selector(compareResMenuItem:)];
NSMenu* submenu = [[NSMenu alloc] initWithTitle: @""];
for(int j=0; j< [displayMenuItems count]; j++)
{
ResMenuItem* item = [displayMenuItems objectAtIndex: j];
if([item colorDepth] == idealColorDepth)
{
if([mainItem width]==[item width] && [mainItem height]==[item height] && [mainItem scale]==[item scale])
{
[submenu addItem: item];
}
}
}
if(idealRefreshRate)
{
NSMenuItem* freq = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: @"%.0f Hz", [mainItem refreshRate]] action: nil keyEquivalent: @""];
if([submenu numberOfItems] > 1)
{
[freq setSubmenu: submenu];
}
else
{
[freq setEnabled: NO];
}
[statusMenu addItem: freq];
[freq release];
}
[submenu release];
[displayMenuItems release];
}
free(modes);
[statusMenu addItem: [NSMenuItem separatorItem]];
}
[statusMenu addItemWithTitle: @"About RDM" action: @selector(showAbout) keyEquivalent: @""];
[statusMenu addItemWithTitle: @"Quit" action: @selector(quit) keyEquivalent: @""];
[statusMenu setDelegate: self];
[statusItem setMenu: statusMenu];
}
- (void) setMode: (ResMenuItem*) item
{
CGDirectDisplayID display = [item display];
int modeNum = [item modeNum];
SetDisplayModeNum(display, modeNum);
/*
CGDisplayConfigRef config;
if (CGBeginDisplayConfiguration(&config) == kCGErrorSuccess) {
CGConfigureDisplayWithDisplayMode(config, display, mode, NULL);
CGCompleteDisplayConfiguration(config, kCGConfigureForSession);
}*/
[self refreshStatusMenu];
}
- (void) applicationDidFinishLaunching: (NSNotification*) notification
{
// NSLog(@"Finished launching");
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain];
NSImage* statusImage = [NSImage imageNamed: @"StatusIcon"];
[statusItem setImage: statusImage];
[statusItem setHighlightMode: YES];
BOOL supportsDarkMenu = !(floor(NSAppKitVersionNumber) < 1343); // NSAppKitVersionNumber10_10
if (supportsDarkMenu) {
[[statusItem image] setTemplate:YES];
}
[self refreshStatusMenu];
}
@end