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 Ben 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"
23 extern gchar
**environ
;
27 void sn_startup(gboolean reconfig
) {}
28 void sn_shutdown(gboolean reconfig
) {}
29 gboolean
sn_app_starting() { return FALSE
; }
30 Time
sn_app_started(const gchar
*id
, const gchar
*wmclass
)
34 gboolean
sn_get_desktop(gchar
*id
, guint
*desktop
) { return FALSE
; }
35 gchar
**sn_get_spawn_environment(char *program
, char *name
,
36 char *icon_name
, Time time
)
38 return g_strdupv(environ
);
40 void sn_spawn_cancel() {}
48 #define SN_API_NOT_YET_FROZEN
51 static SnDisplay
*sn_display
;
52 static SnMonitorContext
*sn_context
;
53 static SnLauncherContext
*sn_launcher
;
54 static GSList
*sn_waits
; /* list of SnStartupSequences we're waiting on */
56 static SnStartupSequence
* sequence_find(const gchar
*id
);
58 static void sn_handler(const XEvent
*e
, gpointer data
);
59 static void sn_event_func(SnMonitorEvent
*event
, gpointer data
);
61 void sn_startup(gboolean reconfig
)
65 sn_display
= sn_display_new(ob_display
, NULL
, NULL
);
66 sn_context
= sn_monitor_context_new(sn_display
, ob_screen
,
67 sn_event_func
, NULL
, NULL
);
68 sn_launcher
= sn_launcher_context_new(sn_display
, ob_screen
);
70 ob_main_loop_x_add(ob_main_loop
, sn_handler
, NULL
, NULL
);
73 void sn_shutdown(gboolean reconfig
)
79 ob_main_loop_x_remove(ob_main_loop
, sn_handler
);
81 for (it
= sn_waits
; it
; it
= g_slist_next(it
))
82 sn_startup_sequence_unref((SnStartupSequence
*)it
->data
);
83 g_slist_free(sn_waits
);
86 screen_set_root_cursor();
88 sn_launcher_context_unref(sn_launcher
);
89 sn_monitor_context_unref(sn_context
);
90 sn_display_unref(sn_display
);
93 static SnStartupSequence
* sequence_find(const gchar
*id
)
95 SnStartupSequence
*ret
= NULL
;
98 for (it
= sn_waits
; it
; it
= g_slist_next(it
)) {
99 SnStartupSequence
*seq
= it
->data
;
100 if (!strcmp(id
, sn_startup_sequence_get_id(seq
))) {
108 gboolean
sn_app_starting()
110 return sn_waits
!= NULL
;
113 static gboolean
sn_wait_timeout(gpointer data
)
115 SnStartupSequence
*seq
= data
;
116 sn_waits
= g_slist_remove(sn_waits
, seq
);
117 screen_set_root_cursor();
118 return FALSE
; /* don't repeat */
121 static void sn_handler(const XEvent
*e
, gpointer data
)
125 sn_display_process_event(sn_display
, &ec
);
128 static void sn_event_func(SnMonitorEvent
*ev
, gpointer data
)
130 SnStartupSequence
*seq
;
131 gboolean change
= FALSE
;
133 if (!(seq
= sn_monitor_event_get_startup_sequence(ev
)))
136 switch (sn_monitor_event_get_type(ev
)) {
137 case SN_MONITOR_EVENT_INITIATED
:
138 sn_startup_sequence_ref(seq
);
139 sn_waits
= g_slist_prepend(sn_waits
, seq
);
140 /* 30 second timeout for apps to start if the launcher doesn't
142 ob_main_loop_timeout_add(ob_main_loop
, 30 * G_USEC_PER_SEC
,
143 sn_wait_timeout
, seq
,
144 (GDestroyNotify
)sn_startup_sequence_unref
);
147 case SN_MONITOR_EVENT_CHANGED
:
148 /* XXX feedback changed? */
151 case SN_MONITOR_EVENT_COMPLETED
:
152 case SN_MONITOR_EVENT_CANCELED
:
153 if ((seq
= sequence_find(sn_startup_sequence_get_id(seq
)))) {
154 sn_waits
= g_slist_remove(sn_waits
, seq
);
155 ob_main_loop_timeout_remove_data(ob_main_loop
, sn_wait_timeout
,
163 screen_set_root_cursor();
166 Time
sn_app_started(const gchar
*id
, const gchar
*wmclass
)
169 Time t
= CurrentTime
;
174 for (it
= sn_waits
; it
; it
= g_slist_next(it
)) {
175 SnStartupSequence
*seq
= it
->data
;
176 gboolean found
= FALSE
;
177 const gchar
*seqid
, *seqclass
, *seqname
, *seqbin
;
178 seqid
= sn_startup_sequence_get_id(seq
);
179 seqclass
= sn_startup_sequence_get_wmclass(seq
);
180 seqname
= sn_startup_sequence_get_name(seq
);
181 seqbin
= sn_startup_sequence_get_binary_name(seq
);
184 /* if the app has a startup id, then look for that for highest
186 if (!strcmp(seqid
, id
))
189 seqclass
= sn_startup_sequence_get_wmclass(seq
);
190 seqname
= sn_startup_sequence_get_name(seq
);
191 seqbin
= sn_startup_sequence_get_binary_name(seq
);
193 if ((seqname
&& !g_ascii_strcasecmp(seqname
, wmclass
)) ||
194 (seqbin
&& !g_ascii_strcasecmp(seqbin
, wmclass
)) ||
195 (seqclass
&& !strcmp(seqclass
, wmclass
)))
200 sn_startup_sequence_complete(seq
);
201 t
= sn_startup_sequence_get_timestamp(seq
);
208 gboolean
sn_get_desktop(gchar
*id
, guint
*desktop
)
210 SnStartupSequence
*seq
;
212 if (id
&& (seq
= sequence_find(id
))) {
213 gint desk
= sn_startup_sequence_get_workspace(seq
);
222 static gboolean
sn_launch_wait_timeout(gpointer data
)
224 SnLauncherContext
*sn
= data
;
225 sn_launcher_context_complete(sn
);
226 return FALSE
; /* don't repeat */
229 gchar
**sn_get_spawn_environment(char *program
, char *name
,
230 char *icon_name
, Time time
)
236 desc
= g_strdup_printf(_("Running %s\n"), program
);
238 if (sn_launcher_context_get_initiated(sn_launcher
)) {
239 sn_launcher_context_unref(sn_launcher
);
240 sn_launcher
= sn_launcher_context_new(sn_display
, ob_screen
);
243 sn_launcher_context_set_name(sn_launcher
, name
? name
: program
);
244 sn_launcher_context_set_description(sn_launcher
, desc
);
245 sn_launcher_context_set_icon_name(sn_launcher
, icon_name
? icon_name
: program
);
246 sn_launcher_context_set_binary_name(sn_launcher
, program
);
247 sn_launcher_context_initiate(sn_launcher
, "openbox", program
, time
);
248 id
= sn_launcher_context_get_startup_id(sn_launcher
);
250 /* 30 second timeout for apps to start */
251 sn_launcher_context_ref(sn_launcher
);
252 ob_main_loop_timeout_add(ob_main_loop
, 30 * G_USEC_PER_SEC
,
253 sn_launch_wait_timeout
, sn_launcher
,
254 (GDestroyNotify
)sn_launcher_context_unref
);
256 env
= g_strdupv(environ
);
257 len
= g_strv_length(env
); /* includes last null */
258 env
= g_renew(gchar
*, env
, ++len
); /* add one spot */
259 env
[len
-2] = g_strdup_printf("DESKTOP_STARTUP_ID=%s", id
);
267 void sn_spawn_cancel()
269 sn_launcher_context_complete(sn_launcher
);