Lines 32-37
Link Here
|
32 |
#include <stdlib.h> |
32 |
#include <stdlib.h> |
33 |
#include <string.h> |
33 |
#include <string.h> |
34 |
#include <locale.h> |
34 |
#include <locale.h> |
|
|
35 |
#include <semaphore.h> |
36 |
#include <fcntl.h> |
35 |
|
37 |
|
36 |
/* Global Variables */ |
38 |
/* Global Variables */ |
37 |
char* defaultVM = "java"; |
39 |
char* defaultVM = "java"; |
Lines 47-56
Link Here
|
47 |
static GdkPixbuf* pixbuf = 0; |
49 |
static GdkPixbuf* pixbuf = 0; |
48 |
static GtkWidget* image = 0; |
50 |
static GtkWidget* image = 0; |
49 |
|
51 |
|
|
|
52 |
static sem_t* mutex; |
53 |
static Atom appAtom, launcherAtom; |
54 |
static char* file = NULL; |
55 |
static int timerAttempts; |
56 |
|
57 |
static struct sigaction quitAction; |
58 |
static struct sigaction intAction; |
59 |
|
50 |
/* Local functions */ |
60 |
/* Local functions */ |
|
|
61 |
static void catch_signal(int sig) { |
62 |
//catch signals, free the lock, reinstall the original |
63 |
//signal handlers and reraise the signal. |
64 |
sem_post(mutex); |
65 |
sem_close(mutex); |
66 |
sigaction(SIGINT, &intAction, NULL); |
67 |
sigaction(SIGQUIT, &intAction, NULL); |
68 |
raise(sig); |
69 |
} |
70 |
|
71 |
typedef int (*LockFunc) (); |
72 |
int executeWithLock (char *name, LockFunc func) { |
73 |
int result; |
74 |
struct sigaction action; |
75 |
|
76 |
mutex = sem_open(name, O_CREAT|O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO, 1); |
77 |
if (mutex == SEM_FAILED) { |
78 |
//create failed. Probably lock is already created so try opening the |
79 |
//existing lock. |
80 |
mutex = sem_open(name, 0); |
81 |
} |
82 |
if (mutex == SEM_FAILED) return -1; //this is an error. |
83 |
|
84 |
//install signal handler to free the lock if something bad |
85 |
//happens. sem_t is not freed automatically when a process ends. |
86 |
action.sa_handler = catch_signal; |
87 |
sigaction(SIGINT, &action, &intAction); |
88 |
sigaction(SIGQUIT, &action, &quitAction); |
89 |
//sem_t has no timeouts on wait. :-( |
90 |
sem_wait(mutex); |
91 |
|
92 |
result = func(); |
93 |
|
94 |
sem_post(mutex); |
95 |
sem_close(mutex); |
96 |
//reinstall the original signal handlers |
97 |
sigaction(SIGINT, &intAction, NULL); |
98 |
sigaction(SIGQUIT, &quitAction, NULL); |
99 |
return result; |
100 |
} |
101 |
|
51 |
static void log_handler(const gchar* domain, GLogLevelFlags flags, const gchar* msg, gpointer data) { |
102 |
static void log_handler(const gchar* domain, GLogLevelFlags flags, const gchar* msg, gpointer data) { |
52 |
/* nothing */ |
103 |
/* nothing */ |
53 |
} |
104 |
} |
|
|
105 |
|
106 |
static int setAppWindowProperty() { |
107 |
Window appWindow; |
108 |
GdkWindow *propWindow; |
109 |
GdkAtom propAtom; |
110 |
char *propVal; |
111 |
|
112 |
//Look for the SWT window. If it's there, set a property on it. |
113 |
appWindow = gtk.XGetSelectionOwner(GDK_DISPLAY(), appAtom); |
114 |
if (appWindow) { |
115 |
propWindow = gtk.gdk_window_foreign_new (appWindow); |
116 |
propAtom = gtk.gdk_atom_intern("org.eclipse.swt.filePath.message", FALSE); |
117 |
//append a colon delimiter in case more than one file gets |
118 |
//appended to the app windows property. |
119 |
if (file != NULL) { |
120 |
propVal = malloc((_tcslen(file) + 2) * sizeof(char)); |
121 |
_stprintf(propVal, _T_ECLIPSE("%s%s"), file, ":"); |
122 |
} else { |
123 |
propVal = malloc(2); |
124 |
_stprintf(propVal, _T_ECLIPSE("%s"), ":"); |
125 |
} |
126 |
gtk.gdk_property_change(propWindow, propAtom, propAtom, 8, GDK_PROP_MODE_APPEND, (guchar *)propVal, _tcslen(propVal)); |
127 |
free(propVal); |
128 |
return 1; |
129 |
} |
130 |
return 0; |
131 |
} |
132 |
|
133 |
int setWindowProperty() { |
134 |
char *mutexPrefix = "SWT_Window_"; |
135 |
char *appName; |
136 |
int result; |
137 |
|
138 |
appName = malloc((_tcslen(mutexPrefix) + _tcslen(getOfficialName()) + 1) * sizeof(char)); |
139 |
_stprintf(appName, _T_ECLIPSE("%s%s"), mutexPrefix, getOfficialName()); |
140 |
result = executeWithLock(appName, setAppWindowProperty); |
141 |
gtk.XSync (GDK_DISPLAY (), False); |
142 |
free(appName); |
143 |
return result; |
144 |
} |
145 |
|
146 |
static gboolean timerProc (gpointer data) { |
147 |
//try to se the app window property. If unsuccessfull return |
148 |
//true to reschedule the timer. Try 60 times (1 minute) max. |
149 |
timerAttempts ++; |
150 |
return !setWindowProperty() && timerAttempts < 60; |
151 |
} |
152 |
|
153 |
void findWindow() { |
154 |
//try setWindowProperty every second until it succeeds |
155 |
//or a minute has passed. |
156 |
int count = 0; |
157 |
while (count < 60 && !setWindowProperty()) { |
158 |
count++; |
159 |
sleep(1); |
160 |
} |
161 |
} |
162 |
|
163 |
int createLauncherWindow() { |
164 |
Window window, launcherWindow; |
165 |
//check if a launcher window exists. If none exists, we know we are the |
166 |
//first and we should be launching the app. |
167 |
window = gtk.XGetSelectionOwner(GDK_DISPLAY(), launcherAtom); |
168 |
if (window == 0) { |
169 |
//create a launcher window that other processes can find. |
170 |
launcherWindow = gtk.XCreateWindow(GDK_DISPLAY(), gtk.XRootWindow(GDK_DISPLAY(), gtk.XDefaultScreen(GDK_DISPLAY())), -10, -10, 1, 1, 0, 0, |
171 |
InputOnly, CopyFromParent, (unsigned long) 0, (XSetWindowAttributes *)NULL); |
172 |
//for some reason Set and Get are both necessary. Set alone does nothing. |
173 |
gtk.XSetSelectionOwner(GDK_DISPLAY(), launcherAtom, launcherWindow, CurrentTime); |
174 |
gtk.XGetSelectionOwner(GDK_DISPLAY(), launcherAtom); |
175 |
//add a timeout to set the property on the apps window once the app is launched. |
176 |
if (file != NULL) { |
177 |
timerAttempts = 0; |
178 |
gtk.g_timeout_add(1000, timerProc, 0); |
179 |
} |
180 |
return 0; |
181 |
} |
182 |
return 1; |
183 |
} |
184 |
|
185 |
int reuseWorkbench(char* filePath, int timeout) { |
186 |
char *mutexPrefix = "SWT_Window_"; |
187 |
char *mutexSuffix = "_Launcher"; |
188 |
char *appName, *launcherName; |
189 |
int result = 0; |
190 |
|
191 |
if(initWindowSystem(&initialArgc, initialArgv, 1) != 0) |
192 |
return -1; |
193 |
|
194 |
file = filePath; |
195 |
|
196 |
//App name is defined in SWT as well. Values must be consistent. |
197 |
appName = malloc((_tcslen(mutexPrefix) + _tcslen(getOfficialName()) + 1) * sizeof(char)); |
198 |
_stprintf(appName, _T_ECLIPSE("%s%s"), mutexPrefix, getOfficialName()); |
199 |
appAtom = XInternAtom(GDK_DISPLAY(), appName, FALSE); |
200 |
free(appName); |
201 |
launcherName = malloc((_tcslen(mutexPrefix) + _tcslen(getOfficialName()) + _tcslen(mutexSuffix) + 1) * sizeof(char)); |
202 |
_stprintf(launcherName, _T_ECLIPSE("%s%s%s"), mutexPrefix, getOfficialName(), mutexSuffix); |
203 |
launcherAtom = XInternAtom(GDK_DISPLAY(), launcherName, FALSE); |
204 |
|
205 |
//check if app is already running. Just set property if it is. |
206 |
if (setWindowProperty()) return 1; |
207 |
|
208 |
result = executeWithLock(launcherName, createLauncherWindow); |
209 |
|
210 |
free(launcherName); |
211 |
|
212 |
if (result == 1) { |
213 |
//The app is already being launched in another process. |
214 |
//Wait for it to complete, set the property on its window, |
215 |
//and exit this process. |
216 |
if (file != NULL) findWindow(); |
217 |
result = 1; |
218 |
} |
219 |
|
220 |
return result; |
221 |
} |
222 |
|
54 |
/* Create and Display the Splash Window */ |
223 |
/* Create and Display the Splash Window */ |
55 |
int showSplash( const char* featureImage ) |
224 |
int showSplash( const char* featureImage ) |
56 |
{ |
225 |
{ |