Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 582864374412f5318586a9f60d5c86574fb3dfc5 (plain) (blame)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Kevin Cornell (Rational Software Corporation)
 *******************************************************************************/
 
/* Photon specific logic for displaying the splash screen. */

#include "eclipseOS.h"
#include "eclipseUtil.h"
#include <Pt.h>

#define PX_IMAGE_MODULES
#define PX_BMP_SUPPORT

#include <photon/PxImage.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <libgen.h>

/* Global Variables */
char   dirSeparator  = '/';
char   pathSeparator = ':';
#ifndef J9VM
char*  defaultVM     = "java";
char*  shippedVMDir  = "jre/bin/";
#else
char*  defaultVM     = "j9";
char*  shippedVMDir  = "ive/bin/";
#endif

/* Define the window system arguments for the various Java VMs. */
static char*  argVM_JAVA[] = { NULL };
static char*  argVM_J9[]   = { "-jit", "-ms:32", "-mso:256", NULL };


/* Define local variables for the main window. */
static PtWidget_t*  topWindow = NULL;

/* Define local variables for running the JVM and detecting its exit. */
static pid_t   jvmProcess = 0;
static int     jvmExitCode;

/* Local functions */
static void       bringDownSplashWindow( int );
static void       centreWindow( PtWidget_t *widget, PtWidget_t *label);
static int        splashTimeout( PtWidget_t* widget, void* data, PtCallbackInfo_t* id );

/* Display a Message */
void displayMessage( char* title, char* message )
{
    if (topWindow == 0) 
    {
		initWindowSystem( NULL, NULL, 0 );
    }
    
 	PtNotice( NULL,NULL, title, NULL, message, NULL, NULL, NULL, Pt_CENTER | Pt_MODAL );
}


/* Initialize Window System
 *
 * Initialize Photon.
 */
void initWindowSystem( int* pArgc, char* argv[], int showSplash )
{
    PtArg_t arg[5];
    int     nArgs;
   
    /* Create a top level window with no decorations. */
    setlocale(LC_ALL, "");
    PtInit( NULL );
    nArgs = 0;
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_WINDOW_RENDER_FLAGS, 0, ~0 ); 
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_WINDOW_MANAGED_FLAGS, Ph_WM_TASKBAR | Ph_WM_CLOSE, ~0 ); 
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_WINDOW_STATE, Ph_WM_STATE_ISFRONT, ~0 ); 
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_WINDOW_TITLE, officialName, ~0 ); 
    topWindow = PtCreateWidget( PtWindow, Pt_NO_PARENT, nArgs, arg );
}


/* Show the Splash Window
 *
 * Create the splash window, load the bitmap and display the splash window.
 *
 */
int showSplash( char* timeoutString, char* featureImage )
{
	int          timeout = 0;
    PtWidget_t*  label;
    PtArg_t      arg[10];
    PhImage_t*   image = NULL;
    int          nArgs;
    int          depth;
	PgDisplaySettings_t settings;
	PgVideoModeInfo_t   mode_info;
    
	/* Determine the splash timeout value (in seconds). */
	if (timeoutString != NULL && strlen( timeoutString ) > 0)
	{
	    sscanf( timeoutString, "%d", &timeout );
	}
	
    /* Install a signal handler to catch SIGUSR2 (which will shut down the window). */
    signal( SIGUSR2, bringDownSplashWindow );
  
	/* Load the splash image from the feature directory. */
    PgGetVideoMode( &settings );
    PgGetVideoModeInfo( settings.mode, &mode_info );
    depth = mode_info.bits_per_pixel;
    if (featureImage != NULL)
    	image = PxLoadImage( featureImage, NULL );

    /* If the splash image could not be found, return an error. */
    if (image == NULL)
    	return ENOENT;

    /* Create a label widget (only child of top window) with the image. */
    nArgs = 0;
	image->flags |= Ph_RELEASE_IMAGE_ALL;
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_LABEL_TYPE, Pt_IMAGE, 0 );
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_LABEL_IMAGE, image, 0 );
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_TEXT_STRING, officialName, 0 );
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_MARGIN_WIDTH, 0, 0 );
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_MARGIN_HEIGHT, 0, 0 );
    label = PtCreateWidget( PtLabel, topWindow, nArgs, arg );
    
    /* Free the image */
    free( image );

    /* Centre the splash window and display it. */
    centreWindow( topWindow, label );
	
	/* If a timeout for the splash window was given */
	if (timeout != 0)
	{
        PtAddEventHandler( topWindow, Ph_EV_TIMER, splashTimeout, NULL );
        PtTimerArm( topWindow, (timeout * 1000) );
	}

    /* Process messages until the splash window is closed or process is terminated. */
 	while (PtWidgetIsRealized( topWindow ))
 	{
  	    PtProcessEvent();
    }

    /* Destroy the splash window. */
    PtDestroyWidget( topWindow );
    topWindow = 0;

    return 0;
}


/* Get the window system specific VM arguments */
char** getArgVM( char* vm ) 
{
    return (isJ9VM( vm ) ? argVM_J9 : argVM_JAVA);
}

void fixEnvForJ9( char* vm ) {
    if (isJ9VM( vm )) {
        char   *ldpath;
        char   newpath[PATH_MAX+1];

        ldpath = getenv( "LD_LIBRARY_PATH" );

        /* Always dup the string so we can free later */
        if( ldpath != NULL )
            ldpath = strdup( ldpath );
        else
            ldpath = strdup( "" );

        /* Get the j9 binary location */ 
        strncpy( newpath, vm, PATH_MAX );
        dirname( newpath );

		/* Add j9 binary location to LD_LIBRARY_PATH */
        ldpath = realloc( ldpath, strlen( ldpath ) + strlen( newpath ) + 2 );
        if( ldpath != NULL )
        {
            strcat( ldpath, ":" );
            strcat( ldpath, newpath );
        }

        setenv( "LD_LIBRARY_PATH", ldpath, 1 );

        free( ldpath );
    }
}

/* Start the Java VM 
 *
 * This method is called to start the Java virtual machine and to wait until it
 * terminates. The function returns the exit code from the JVM.
 */
int startJavaVM( char* args[] ) 
{
    int    exitCode;
    
    fixEnvForJ9 (args [0]);
	
	/* Create a child process for the JVM. */	
	jvmProcess = fork();
	if (jvmProcess == 0) 
	{
		/* Child process ... start the JVM */
		execv( args[0], args );
		
		/* The JVM would not start ... return error code to parent process. */
		jvmExitCode = errno;
        exit( jvmExitCode );
	}
	
	/* If the JVM is still running, wait for it to terminate. */
	if (jvmProcess != 0)
	{
		wait( &exitCode );
		jvmExitCode = ((exitCode & 0x00ff) == 0 ? (exitCode >> 8) : exitCode); /* see wait(2) */
	}
	
	/* Return the exit code from the JVM. */
	return jvmExitCode;
}

/*------ Local functions -----*/


/* Catch a signal that indicates the splash window is to be brought down. */
static void bringDownSplashWindow( int sig )
{
    if (topWindow != 0) 
    {
        PtUnrealizeWidget( topWindow );
    }
}


/* Centre the top window on the screen. */

static void centreWindow( PtWidget_t* window, PtWidget_t* expose )
{
    PtArg_t   arg[2];
    int   nArgs;
    PhPoint_t pos;
	PhArea_t area;
    PhRect_t rect;
    int width, height;

    /* Realize the top window to calculate its width/height. */
    PtExtentWidgetFamily( window );

    /* Get the desired dimensions of the window. */
    PtWidgetArea( window, &area );

    /* Calculate the X and Y position for the window. */
    PhWindowQueryVisible( Ph_QUERY_WORKSPACE, 0, PhInputGroup(0), &rect );
    width = rect.lr.x - rect.ul.x + 1;
    height = rect.lr.y - rect.ul.y + 1;
    pos.x = rect.ul.x + (width  - area.size.w) / 2;
    pos.y = rect.ul.y + (height - area.size.h) / 2;

    /* Set the new shell position and display it. */
    nArgs = 0;
    PtSetArg( &arg[ nArgs++ ], Pt_ARG_POS, &pos, 0 );
    PtSetResources( window, nArgs, arg );
    PtRealizeWidget( window );
}


/* Splash Timeout */
static int splashTimeout( PtWidget_t* widget, void* data, PtCallbackInfo_t* info )
{
    bringDownSplashWindow( 0 );
   	return 1;
}

Back to the top