-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstartupnotification.cpp
More file actions
271 lines (227 loc) · 7.07 KB
/
startupnotification.cpp
File metadata and controls
271 lines (227 loc) · 7.07 KB
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
#include "config.h"
#include "i18n.h"
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <fx.h>
#include "xfedefs.h"
#include "xfeutils.h"
#include "startupnotification.h"
#ifdef STARTUP_NOTIFICATION // Use startup notification
// Indicate that launchee has completed startup
void startup_completed(void)
{
SnLauncheeContext* launchee;
Display* xdisplay;
SnDisplay* display;
// Open display
xdisplay = XOpenDisplay(NULL);
if (xdisplay != NULL)
{
// Create startup notification context
display = sn_display_new(xdisplay, NULL, NULL);
launchee = sn_launchee_context_new_from_environment(display, DefaultScreen(xdisplay));
// Indicate startup has completed and free resources
if (launchee)
{
sn_launchee_context_complete(launchee);
sn_launchee_context_unref(launchee);
}
sn_display_unref(display);
XCloseDisplay(xdisplay);
}
}
// Hack to obtain a timestamp for startup notification
// Create a fake window and set up a property change event
// Code snippet borrowed from the Internet
Time gettimestamp(Display *display)
{
Window window;
XEvent event;
Atom atom_name, atom_type;
window = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, 0, InputOnly, 0, 0, NULL);
XSelectInput(display, window, PropertyChangeMask);
atom_name = XInternAtom(display, "_NET_WM_USER_TIME_WINDOW", False);
atom_type = XInternAtom(display, "WINDOW", true);
XChangeProperty(display, window, atom_name, atom_type, 8, PropModeReplace, (const FXuchar*)&window, 1);
XNextEvent(display, &event);
assert(event.type == PropertyNotify);
return(((XPropertyEvent*)&event)->time);
}
// Launch a command and initiate a startup notification
int runcmd(FXString cmd, FXString cmdname, FXString dir, FXString startdir, FXbool usesn = true, FXString snexcepts = "")
{
int ret;
// Change to current directory
ret = chdir(dir.text());
if (ret < 0)
{
int errcode = errno;
if (errcode)
{
fprintf(stderr, _("Error: Can't enter folder %s: %s"), dir.text(), strerror(errcode));
}
else
{
fprintf(stderr, _("Error: Can't enter folder %s"), dir.text());
}
return(-1);
}
// Get rid of possible command options
cmdname = cmdname.before(' ');
// Check if command is in the startup notification exception list
FXbool startup_notify = true;
if (snexcepts != "")
{
FXString entry;
for (int i = 0; ; i++)
{
entry = snexcepts.section(':', i);
if (streq(entry.text(), ""))
{
break;
}
if (streq(entry.text(), cmdname.text()))
{
startup_notify = false;
break;
}
}
}
// Run command with startup notification
if (usesn && startup_notify)
{
Display* xdisplay;
SnDisplay* display;
SnLauncherContext* context;
Time timestamp;
// Open display
xdisplay = XOpenDisplay(NULL);
if (xdisplay == NULL)
{
fprintf(stderr, _("Error: Can't open display\n"));
ret = chdir(startdir.text());
if (ret < 0)
{
int errcode = errno;
if (errcode)
{
fprintf(stderr, _("Error: Can't enter folder %s: %s"), startdir.text(), strerror(errcode));
}
else
{
fprintf(stderr, _("Error: Can't enter folder %s"), startdir.text());
}
}
return(-1);
}
// Message displayed in the task bar (if any)
FXString message;
message.format(_("Start of %s"), cmdname.text());
// Initiate launcher context
display = sn_display_new(xdisplay, NULL, NULL);
context = sn_launcher_context_new(display, DefaultScreen(xdisplay));
sn_launcher_context_set_name(context, message.text());
sn_launcher_context_set_binary_name(context, cmdname.text());
sn_launcher_context_set_description(context, message.text());
sn_launcher_context_set_icon_name(context, cmdname.text());
timestamp = gettimestamp(xdisplay);
sn_launcher_context_initiate(context, "Xfe", cmd.text(), timestamp);
// Run command in background
cmd += " &";
static pid_t child_pid = 0;
switch ((child_pid = fork()))
{
case -1:
fprintf(stderr, _("Error: Fork failed: %s\n"), strerror(errno));
break;
case 0: // Child
sn_launcher_context_setup_child_process(context);
execl("/bin/sh", "sh", "-c", cmd.text(), (char*)NULL);
_exit(EXIT_SUCCESS);
break;
}
sn_launcher_context_unref(context);
XCloseDisplay(xdisplay);
}
// Run command without startup notification
else
{
// Run command in background
cmd += " &";
ret = system(cmd.text());
if (ret < 0)
{
fprintf(stderr, _("Error: Can't execute command %s"), cmd.text());
return(-1);
}
// Just display the wait cursor during a second
sleep(1);
}
// Go back to startup directory
ret = chdir(startdir.text());
if (ret < 0)
{
int errcode = errno;
if (errcode)
{
fprintf(stderr, _("Error: Can't enter folder %s: %s"), startdir.text(), strerror(errcode));
}
else
{
fprintf(stderr, _("Error: Can't enter folder %s"), startdir.text());
}
return(-1);
}
return(0);
}
#else // Don't use startup notification
// Run a command and simulate a startup time
int runcmd(FXString cmd, FXString dir, FXString startdir)
{
int ret;
// Change to current directory
ret = chdir(dir.text());
if (ret < 0)
{
int errcode = errno;
if (errcode)
{
fprintf(stderr, _("Error: Can't enter folder %s: %s"), dir.text(), strerror(errcode));
}
else
{
fprintf(stderr, _("Error: Can't enter folder %s"), dir.text());
}
return(-1);
}
// Run the command in background
cmd += " &";
ret = system(cmd.text());
if (ret < 0)
{
fprintf(stderr, _("Error: Can't execute command %s"), cmd.text());
return(-1);
}
// Very ugly simulation of a startup time!!!
sleep(SIMULATED_STARTUP_TIME);
// Go back to startup directory
ret = chdir(startdir.text());
if (ret < 0)
{
int errcode = errno;
if (errcode)
{
fprintf(stderr, _("Error: Can't enter folder %s: %s"), startdir.text(), strerror(errcode));
}
else
{
fprintf(stderr, _("Error: Can't enter folder %s"), startdir.text());
}
return(-1);
}
return(0);
}
#endif