1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 2008-2018. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  */
20 
21 #include <stdio.h>
22 #include "wxe_driver.h"
23 
24 /* Platform specific initialisation stuff */
25 #ifdef _MACOSX
26 
27 #include <Cocoa/Cocoa.h>
28 #include <objc/objc-runtime.h>
29 
30 extern OSErr  CPSSetProcessName (ProcessSerialNumber *psn, char *processname);
31 
wxe_ps_init()32 void * wxe_ps_init()
33 {
34    ProcessSerialNumber psn;
35    // Enable GUI
36    if(!GetCurrentProcess(&psn)) {
37       TransformProcessType(&psn, kProcessTransformToForegroundApplication);
38 #ifdef  MAC_OS_X_VERSION_10_6
39       [[NSRunningApplication currentApplication] activateWithOptions:
40        (NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];
41 #else
42       SetFrontProcess(&psn);
43 #endif
44    }
45    return (void *) 0;
46 }
47 
is_packaged_app()48 int is_packaged_app() {
49    // Can get lost in when execing around, we use the name instead
50    /* if(mainBundle) { */
51    /*    return (CFBundleGetValueForInfoDictionaryKey(mainBundle, CFSTR("CFBundlePackageType")) != nil); */
52    /* } */
53 #ifdef MAC_OS_X_VERSION_10_6
54    NSString *  appName = [[NSRunningApplication currentApplication] localizedName];
55    return (strncmp("beam", [appName UTF8String], 4) != 0);
56 #else
57    return 0;
58 #endif
59 }
60 
wxe_ps_init2()61 void * wxe_ps_init2() {
62    NSAutoreleasePool *pool;
63    ProcessSerialNumber psn;
64    size_t app_len = 127;
65    char app_title_buf[128];
66    char * app_title;
67    size_t app_icon_len = 1023;
68    char app_icon_buf[1024];
69    char * app_icon;
70 
71    // Setup and enable gui
72    pool = [[NSAutoreleasePool alloc] init];
73 
74    if( !is_packaged_app() ) {
75       // Undocumented function (but no documented way of doing this exists)
76       int res = erl_drv_getenv("WX_APP_TITLE", app_title_buf, &app_len);
77       if (res >= 0) {
78           app_title = app_title_buf;
79       } else {
80           app_title = NULL;
81       }
82       if(!GetCurrentProcess(&psn)) {
83       	 CPSSetProcessName(&psn, app_title?app_title:"Erlang");
84       }
85       // Enable setting custom application icon for Mac OS X
86       res = erl_drv_getenv("WX_APP_ICON", app_icon_buf, &app_icon_len);
87       NSMutableString *file = [[NSMutableString alloc] init];
88       if (res >= 0) {
89           [file appendFormat:@"%s", app_icon_buf];
90       } else {
91           [file appendFormat:@"%s/%s", erl_wx_privdir, "erlang-logo128.png"];
92       }
93       // Load and set icon
94       NSImage *icon = [[NSImage alloc] initWithContentsOfFile: file];
95       [NSApp setApplicationIconImage: icon];
96    };
97 
98    return pool;
99 }
100 
101 /* _MACOSX */
102 #else
wxe_ps_init()103 void * wxe_ps_init()
104 {
105    return (void *) 0;
106 }
wxe_ps_init2()107 void * wxe_ps_init2()
108 {
109    return (void *) 0;
110 }
111 #endif
112