1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 startupnotify.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
20 #include "startupnotify.h"
28 void sn_startup(gboolean reconfig
) {}
29 void sn_shutdown(gboolean reconfig
) {}
30 gboolean
sn_app_starting() { return FALSE
; }
31 Time
sn_app_started(const gchar
*id
, const gchar
*wmclass
)
35 gboolean
sn_get_desktop(gchar
*id
, guint
*desktop
) { return FALSE
; }
36 void sn_setup_spawn_environment(gchar
*program
, gchar
*name
,
37 gchar
*icon_name
, gint desktop
) {}
38 void sn_spawn_cancel() {}
46 #define SN_API_NOT_YET_FROZEN
49 static SnDisplay
*sn_display
;
50 static SnMonitorContext
*sn_context
;
51 static SnLauncherContext
*sn_launcher
;
52 static GSList
*sn_waits
; /* list of SnStartupSequences we're waiting on */
54 static SnStartupSequence
* sequence_find(const gchar
*id
);
56 static void sn_handler(const XEvent
*e
, gpointer data
);
57 static void sn_event_func(SnMonitorEvent
*event
, gpointer data
);
59 void sn_startup(gboolean reconfig
)
65 /* unset this so we don't pass it on unknowingly */
66 s
= g_strdup("DESKTOP_STARTUP_ID");
70 sn_display
= sn_display_new(ob_display
, NULL
, NULL
);
71 sn_context
= sn_monitor_context_new(sn_display
, ob_screen
,
72 sn_event_func
, NULL
, NULL
);
73 sn_launcher
= sn_launcher_context_new(sn_display
, ob_screen
);
75 ob_main_loop_x_add(ob_main_loop
, sn_handler
, NULL
, NULL
);
78 void sn_shutdown(gboolean reconfig
)
84 ob_main_loop_x_remove(ob_main_loop
, sn_handler
);
86 for (it
= sn_waits
; it
; it
= g_slist_next(it
))
87 sn_startup_sequence_unref((SnStartupSequence
*)it
->data
);
88 g_slist_free(sn_waits
);
91 screen_set_root_cursor();
93 sn_launcher_context_unref(sn_launcher
);
94 sn_monitor_context_unref(sn_context
);
95 sn_display_unref(sn_display
);
98 static SnStartupSequence
* sequence_find(const gchar
*id
)
100 SnStartupSequence
*ret
= NULL
;
103 for (it
= sn_waits
; it
; it
= g_slist_next(it
)) {
104 SnStartupSequence
*seq
= it
->data
;
105 if (!strcmp(id
, sn_startup_sequence_get_id(seq
))) {
113 gboolean
sn_app_starting(void)
115 return sn_waits
!= NULL
;
118 static gboolean
sn_wait_timeout(gpointer data
)
120 SnStartupSequence
*seq
= data
;
121 sn_waits
= g_slist_remove(sn_waits
, seq
);
122 screen_set_root_cursor();
123 return FALSE
; /* don't repeat */
126 static void sn_handler(const XEvent
*e
, gpointer data
)
130 sn_display_process_event(sn_display
, &ec
);
133 static void sn_event_func(SnMonitorEvent
*ev
, gpointer data
)
135 SnStartupSequence
*seq
;
136 gboolean change
= FALSE
;
138 if (!(seq
= sn_monitor_event_get_startup_sequence(ev
)))
141 switch (sn_monitor_event_get_type(ev
)) {
142 case SN_MONITOR_EVENT_INITIATED
:
143 sn_startup_sequence_ref(seq
);
144 sn_waits
= g_slist_prepend(sn_waits
, seq
);
145 /* 20 second timeout for apps to start if the launcher doesn't
147 ob_main_loop_timeout_add(ob_main_loop
, 20 * G_USEC_PER_SEC
,
148 sn_wait_timeout
, seq
,
150 (GDestroyNotify
)sn_startup_sequence_unref
);
153 case SN_MONITOR_EVENT_CHANGED
:
154 /* XXX feedback changed? */
157 case SN_MONITOR_EVENT_COMPLETED
:
158 case SN_MONITOR_EVENT_CANCELED
:
159 if ((seq
= sequence_find(sn_startup_sequence_get_id(seq
)))) {
160 sn_waits
= g_slist_remove(sn_waits
, seq
);
161 ob_main_loop_timeout_remove_data(ob_main_loop
, sn_wait_timeout
,
169 screen_set_root_cursor();
172 Time
sn_app_started(const gchar
*id
, const gchar
*wmclass
)
175 Time t
= CurrentTime
;
180 for (it
= sn_waits
; it
; it
= g_slist_next(it
)) {
181 SnStartupSequence
*seq
= it
->data
;
182 gboolean found
= FALSE
;
183 const gchar
*seqid
, *seqclass
, *seqname
, *seqbin
;
184 seqid
= sn_startup_sequence_get_id(seq
);
185 seqclass
= sn_startup_sequence_get_wmclass(seq
);
186 seqname
= sn_startup_sequence_get_name(seq
);
187 seqbin
= sn_startup_sequence_get_binary_name(seq
);
190 /* if the app has a startup id, then look for that for highest
192 if (!strcmp(seqid
, id
))
195 seqclass
= sn_startup_sequence_get_wmclass(seq
);
196 seqname
= sn_startup_sequence_get_name(seq
);
197 seqbin
= sn_startup_sequence_get_binary_name(seq
);
199 if ((seqname
&& !g_ascii_strcasecmp(seqname
, wmclass
)) ||
200 (seqbin
&& !g_ascii_strcasecmp(seqbin
, wmclass
)) ||
201 (seqclass
&& !strcmp(seqclass
, wmclass
)))
206 sn_startup_sequence_complete(seq
);
207 t
= sn_startup_sequence_get_timestamp(seq
);
214 gboolean
sn_get_desktop(gchar
*id
, guint
*desktop
)
216 SnStartupSequence
*seq
;
218 if (id
&& (seq
= sequence_find(id
))) {
219 gint desk
= sn_startup_sequence_get_workspace(seq
);
228 static gboolean
sn_launch_wait_timeout(gpointer data
)
230 SnLauncherContext
*sn
= data
;
231 sn_launcher_context_complete(sn
);
232 return FALSE
; /* don't repeat */
235 void sn_setup_spawn_environment(gchar
*program
, gchar
*name
,
236 gchar
*icon_name
, gint desktop
)
241 desc
= g_strdup_printf(_("Running %s\n"), program
);
243 if (sn_launcher_context_get_initiated(sn_launcher
)) {
244 sn_launcher_context_unref(sn_launcher
);
245 sn_launcher
= sn_launcher_context_new(sn_display
, ob_screen
);
248 sn_launcher_context_set_name(sn_launcher
, name
? name
: program
);
249 sn_launcher_context_set_description(sn_launcher
, desc
);
250 sn_launcher_context_set_icon_name(sn_launcher
, icon_name
?
251 icon_name
: program
);
252 sn_launcher_context_set_binary_name(sn_launcher
, program
);
253 if (desktop
>= 0 && (unsigned) desktop
< screen_num_desktops
)
254 sn_launcher_context_set_workspace(sn_launcher
, (signed) desktop
);
255 sn_launcher_context_initiate(sn_launcher
, "openbox", program
,
257 id
= sn_launcher_context_get_startup_id(sn_launcher
);
259 /* 20 second timeout for apps to start */
260 sn_launcher_context_ref(sn_launcher
);
261 ob_main_loop_timeout_add(ob_main_loop
, 20 * G_USEC_PER_SEC
,
262 sn_launch_wait_timeout
, sn_launcher
,
264 (GDestroyNotify
)sn_launcher_context_unref
);
266 putenv(g_strdup_printf("DESKTOP_STARTUP_ID=%s", id
));
271 void sn_spawn_cancel(void)
273 sn_launcher_context_complete(sn_launcher
);