Welcome to mirror list, hosted at ThFree Co, Russian Federation.

query-vsmac.mm « MacPlatform « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 88a8a7dac6e9bddf4a437d890ac395d5f95d8343 (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
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
//
// Copyright (c) Microsoft Corp. (https://www.microsoft.com)
//
// Authors:
//   Aaron Bockover <abock@microsoft.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// % clang++ -o query-vsmac -ObjC -fcxx-modules -fmodules -Werror -Wall -Wpedantic query-vsmac.mm

@import AppKit;
@import CoreServices;

#ifdef __cplusplus
#define VS_EXTERN extern "C"
#else
#define VS_EXTERN extern
#endif

VS_EXTERN NSRunningApplication **
VSQueryRunningInstances()
{
    NSArray<NSRunningApplication *> *allApps = NSWorkspace.sharedWorkspace.runningApplications;
    if (!allApps)
        return nil;

    NSRunningApplication **instances = (NSRunningApplication **)calloc(
        [allApps count] + 1,
        sizeof(NSRunningApplication *));
    if (!instances)
        goto ret;

    int i = 0;

    for (NSRunningApplication *runningApp in NSWorkspace.sharedWorkspace.runningApplications) {
        if ([runningApp.bundleIdentifier isEqual: @"com.microsoft.visual-studio"] ||
            [runningApp.bundleIdentifier isEqual: @"com.xamarin.monodevelop"])
            instances[i++] = [runningApp retain];
    }

ret:
    [allApps release];
    return instances;
}

VS_EXTERN void
VSFreeRunningInstances(NSRunningApplication **instances)
{
    if (!instances)
        return;

    for (int i = 0; instances && instances[i]; i++)
        [instances[i] release];

    free(instances);
}

static char *
NSStringToUTF8Dup(NSString *str)
{
    if (str) {
        const char *utf8str = [str UTF8String];
        if (utf8str)
            return strdup(utf8str);
    }

    return NULL;
}

VS_EXTERN char *
VSQueryInstanceCurrentSelectedSolutionPath(NSRunningApplication *runningApplication)
{
    NSAppleEventDescriptor *targetDescriptor = [NSAppleEventDescriptor
        descriptorWithProcessIdentifier: runningApplication.processIdentifier];

    NSAppleEventDescriptor* appleEvent = [NSAppleEventDescriptor
        appleEventWithEventClass: 1448302419
        eventID: 1129534288
        targetDescriptor: targetDescriptor
        returnID: kAutoGenerateReturnID
        transactionID: kAnyTransactionID];

    AEDesc aeReply = { 0, };

    OSErr sendResult = AESendMessage(
        [appleEvent aeDesc],
        &aeReply,
        kAEWaitReply | kAENeverInteract,
        kAEDefaultTimeout);

    [targetDescriptor release];
    [appleEvent release];

    if (sendResult != noErr) {
        return NULL;
    }

    NSAppleEventDescriptor *reply = [[NSAppleEventDescriptor alloc] initWithAEDescNoCopy: &aeReply];
    NSString *path = [[reply descriptorForKeyword: keyDirectObject] stringValue];
    [reply release];

    return NSStringToUTF8Dup(path);
}

VS_EXTERN pid_t
VSGetInstanceProcessIdentifier(NSRunningApplication *instance)
{
    if (!instance)
        return -1;

    return instance.processIdentifier;
}

VS_EXTERN char *
VSGetInstanceBundlePath(NSRunningApplication *instance)
{
    if (!instance || !instance.bundleURL)
        return NULL;

    return NSStringToUTF8Dup(instance.bundleURL.path);
}

VS_EXTERN char *
VSGetInstanceExecutablePath(NSRunningApplication *instance)
{
    if (!instance || !instance.executableURL)
        return NULL;

    return NSStringToUTF8Dup(instance.executableURL.path);
}

VS_EXTERN char *
VSGetInstanceLocalizedName(NSRunningApplication *instance)
{
    if (!instance)
        return NULL;

    return NSStringToUTF8Dup(instance.localizedName);
}

VS_EXTERN char *
VSGetInstanceVersion(NSRunningApplication *instance)
{
    if (!instance || !instance.bundleURL)
        return NULL;

    NSBundle *bundle = [NSBundle bundleWithURL: instance.bundleURL];
    if (!bundle)
        return NULL;

    id versionValue = [bundle objectForInfoDictionaryKey: @"CFBundleVersion"];
    if (versionValue && [versionValue isKindOfClass: [NSString class]])
        return NSStringToUTF8Dup((NSString *)versionValue);

    return NULL;
}

int main(int argc, char **argv)
{
    NSRunningApplication **instances = VSQueryRunningInstances();

    for (int i = 0; instances && instances[i]; i++) {
        NSRunningApplication *instance = instances[i];

        pid_t pid = VSGetInstanceProcessIdentifier(instance);
        char *name = VSGetInstanceLocalizedName(instance);
        char *version = VSGetInstanceVersion(instance);
        char *bunPath = VSGetInstanceBundlePath(instance);
        char *exePath = VSGetInstanceExecutablePath(instance);
        char *slnPath = VSQueryInstanceCurrentSelectedSolutionPath(instance);

        printf("%s %s\n", name, version);
        printf("  pid: %u\n", pid);
        printf("  bun: %s\n", bunPath);
        printf("  exe: %s\n", exePath);
        printf("  sln: %s\n", slnPath);

        free(name);
        free(version);
        free(bunPath);
        free(exePath);
        free(slnPath);
    }

    VSFreeRunningInstances(instances);

    return 0;
}