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
, const gchar
*name
)
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
)
63 sn_display
= sn_display_new(ob_display
, NULL
, NULL
);
64 sn_context
= sn_monitor_context_new(sn_display
, ob_screen
,
65 sn_event_func
, NULL
, NULL
);
66 sn_launcher
= sn_launcher_context_new(sn_display
, ob_screen
);
68 ob_main_loop_x_add(ob_main_loop
, sn_handler
, NULL
, NULL
);
71 void sn_shutdown(gboolean reconfig
)
77 ob_main_loop_x_remove(ob_main_loop
, sn_handler
);
79 for (it
= sn_waits
; it
; it
= g_slist_next(it
))
80 sn_startup_sequence_unref((SnStartupSequence
*)it
->data
);
81 g_slist_free(sn_waits
);
84 screen_set_root_cursor();
86 sn_launcher_context_unref(sn_launcher
);
87 sn_monitor_context_unref(sn_context
);
88 sn_display_unref(sn_display
);
91 static SnStartupSequence
* sequence_find(const gchar
*id
)
93 SnStartupSequence
*ret
= NULL
;
96 for (it
= sn_waits
; it
; it
= g_slist_next(it
)) {
97 SnStartupSequence
*seq
= it
->data
;
98 if (!strcmp(id
, sn_startup_sequence_get_id(seq
))) {
106 gboolean
sn_app_starting(void)
108 return sn_waits
!= NULL
;
111 static gboolean
sn_wait_timeout(gpointer data
)
113 SnStartupSequence
*seq
= data
;
114 sn_waits
= g_slist_remove(sn_waits
, seq
);
115 screen_set_root_cursor();
116 return FALSE
; /* don't repeat */
119 static void sn_handler(const XEvent
*e
, gpointer data
)
123 sn_display_process_event(sn_display
, &ec
);
126 static void sn_event_func(SnMonitorEvent
*ev
, gpointer data
)
128 SnStartupSequence
*seq
;
129 gboolean change
= FALSE
;
131 if (!(seq
= sn_monitor_event_get_startup_sequence(ev
)))
134 switch (sn_monitor_event_get_type(ev
)) {
135 case SN_MONITOR_EVENT_INITIATED
:
136 sn_startup_sequence_ref(seq
);
137 sn_waits
= g_slist_prepend(sn_waits
, seq
);
138 /* 20 second timeout for apps to start if the launcher doesn't
140 ob_main_loop_timeout_add(ob_main_loop
, 20 * G_USEC_PER_SEC
,
141 sn_wait_timeout
, seq
,
143 (GDestroyNotify
)sn_startup_sequence_unref
);
146 case SN_MONITOR_EVENT_CHANGED
:
147 /* XXX feedback changed? */
150 case SN_MONITOR_EVENT_COMPLETED
:
151 case SN_MONITOR_EVENT_CANCELED
:
152 if ((seq
= sequence_find(sn_startup_sequence_get_id(seq
)))) {
153 sn_waits
= g_slist_remove(sn_waits
, seq
);
154 ob_main_loop_timeout_remove_data(ob_main_loop
, sn_wait_timeout
,
162 screen_set_root_cursor();
165 Time
sn_app_started(const gchar
*id
, const gchar
*wmclass
, const gchar
*name
)
168 Time t
= CurrentTime
;
173 for (it
= sn_waits
; it
; it
= g_slist_next(it
)) {
174 SnStartupSequence
*seq
= it
->data
;
175 gboolean found
= FALSE
;
176 const gchar
*seqid
, *seqclass
, *seqname
, *seqbin
;
177 seqid
= sn_startup_sequence_get_id(seq
);
178 seqclass
= sn_startup_sequence_get_wmclass(seq
);
179 seqname
= sn_startup_sequence_get_name(seq
);
180 seqbin
= sn_startup_sequence_get_binary_name(seq
);
183 /* if the app has a startup id, then look for that for highest
185 if (!strcmp(seqid
, id
))
188 seqclass
= sn_startup_sequence_get_wmclass(seq
);
189 seqbin
= sn_startup_sequence_get_binary_name(seq
);
191 /* seqclass = "a string to match against the "resource name" or
192 "resource class" hints. These are WM_CLASS[0] and WM_CLASS[1]"
193 - from the startup-notification spec
195 if ((seqclass
&& !strcmp(seqclass
, wmclass
)) ||
196 (seqclass
&& !strcmp(seqclass
, name
)) ||
197 /* Check the binary name against the class and name hints
198 as well, to help apps that don't have the class set
200 (seqbin
&& !g_ascii_strcasecmp(seqbin
, wmclass
)) ||
201 (seqbin
&& !g_ascii_strcasecmp(seqbin
, name
)))
208 sn_startup_sequence_complete(seq
);
209 t
= sn_startup_sequence_get_timestamp(seq
);
216 gboolean
sn_get_desktop(gchar
*id
, guint
*desktop
)
218 SnStartupSequence
*seq
;
220 if (id
&& (seq
= sequence_find(id
))) {
221 gint desk
= sn_startup_sequence_get_workspace(seq
);
230 static gboolean
sn_launch_wait_timeout(gpointer data
)
232 SnLauncherContext
*sn
= data
;
233 sn_launcher_context_complete(sn
);
234 return FALSE
; /* don't repeat */
237 void sn_setup_spawn_environment(gchar
*program
, gchar
*name
,
238 gchar
*icon_name
, gint desktop
)
243 desc
= g_strdup_printf(_("Running %s\n"), program
);
245 if (sn_launcher_context_get_initiated(sn_launcher
)) {
246 sn_launcher_context_unref(sn_launcher
);
247 sn_launcher
= sn_launcher_context_new(sn_display
, ob_screen
);
250 sn_launcher_context_set_name(sn_launcher
, name
? name
: program
);
251 sn_launcher_context_set_description(sn_launcher
, desc
);
252 sn_launcher_context_set_icon_name(sn_launcher
, icon_name
?
253 icon_name
: program
);
254 sn_launcher_context_set_binary_name(sn_launcher
, program
);
255 if (desktop
>= 0 && (unsigned) desktop
< screen_num_desktops
)
256 sn_launcher_context_set_workspace(sn_launcher
, (signed) desktop
);
257 sn_launcher_context_initiate(sn_launcher
, "openbox", program
,
259 id
= sn_launcher_context_get_startup_id(sn_launcher
);
261 /* 20 second timeout for apps to start */
262 sn_launcher_context_ref(sn_launcher
);
263 ob_main_loop_timeout_add(ob_main_loop
, 20 * G_USEC_PER_SEC
,
264 sn_launch_wait_timeout
, sn_launcher
,
266 (GDestroyNotify
)sn_launcher_context_unref
);
268 putenv(g_strdup_printf("DESKTOP_STARTUP_ID=%s", id
));
273 void sn_spawn_cancel(void)
275 sn_launcher_context_complete(sn_launcher
);