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"
23 #include "obt/xqueue.h"
31 void sn_startup(gboolean reconfig
) {}
32 void sn_shutdown(gboolean reconfig
) {}
33 gboolean
sn_app_starting() { return FALSE
; }
34 Time
sn_app_started(const gchar
*id
, const gchar
*wmclass
, const gchar
*name
)
38 gboolean
sn_get_desktop(gchar
*id
, guint
*desktop
) { return FALSE
; }
39 void sn_setup_spawn_environment(const gchar
*program
, const gchar
*name
,
40 const gchar
*icon_name
, const gchar
*wmclass
,
42 void sn_spawn_cancel() {}
49 #define SN_API_NOT_YET_FROZEN
52 static SnDisplay
*sn_display
;
53 static SnMonitorContext
*sn_context
;
54 static SnLauncherContext
*sn_launcher
;
55 static GSList
*sn_waits
; /* list of SnStartupSequences we're waiting on */
57 static SnStartupSequence
* sequence_find(const gchar
*id
);
59 static void sn_handler(const XEvent
*e
, gpointer data
);
60 static void sn_event_func(SnMonitorEvent
*event
, gpointer data
);
62 void sn_startup(gboolean reconfig
)
66 sn_display
= sn_display_new(obt_display
, NULL
, NULL
);
67 sn_context
= sn_monitor_context_new(sn_display
, ob_screen
,
68 sn_event_func
, NULL
, NULL
);
69 sn_launcher
= sn_launcher_context_new(sn_display
, ob_screen
);
71 xqueue_add_callback(sn_handler
, NULL
);
74 void sn_shutdown(gboolean reconfig
)
80 xqueue_remove_callback(sn_handler
, NULL
);
82 for (it
= sn_waits
; it
; it
= g_slist_next(it
))
83 sn_startup_sequence_unref((SnStartupSequence
*)it
->data
);
84 g_slist_free(sn_waits
);
87 screen_set_root_cursor();
89 sn_launcher_context_unref(sn_launcher
);
90 sn_monitor_context_unref(sn_context
);
91 sn_display_unref(sn_display
);
94 static SnStartupSequence
* sequence_find(const gchar
*id
)
96 SnStartupSequence
*ret
= NULL
;
99 for (it
= sn_waits
; it
; it
= g_slist_next(it
)) {
100 SnStartupSequence
*seq
= it
->data
;
101 if (!strcmp(id
, sn_startup_sequence_get_id(seq
))) {
109 gboolean
sn_app_starting(void)
111 return sn_waits
!= NULL
;
114 static gboolean
sn_wait_timeout(gpointer data
)
116 SnStartupSequence
*seq
= data
;
117 sn_waits
= g_slist_remove(sn_waits
, seq
);
118 screen_set_root_cursor();
120 return FALSE
; /* don't repeat */
123 static void sn_handler(const XEvent
*e
, gpointer data
)
127 sn_display_process_event(sn_display
, &ec
);
130 static void sn_event_func(SnMonitorEvent
*ev
, gpointer data
)
132 SnStartupSequence
*seq
;
133 gboolean change
= FALSE
;
135 if (!(seq
= sn_monitor_event_get_startup_sequence(ev
)))
138 switch (sn_monitor_event_get_type(ev
)) {
139 case SN_MONITOR_EVENT_INITIATED
:
140 sn_startup_sequence_ref(seq
);
141 sn_waits
= g_slist_prepend(sn_waits
, seq
);
142 /* 20 second timeout for apps to start if the launcher doesn't
144 g_timeout_add_full(G_PRIORITY_DEFAULT
,
145 20 * 1000, sn_wait_timeout
, seq
,
146 (GDestroyNotify
)sn_startup_sequence_unref
);
149 case SN_MONITOR_EVENT_CHANGED
:
150 /* XXX feedback changed? */
153 case SN_MONITOR_EVENT_COMPLETED
:
154 case SN_MONITOR_EVENT_CANCELED
:
155 if ((seq
= sequence_find(sn_startup_sequence_get_id(seq
)))) {
156 sn_waits
= g_slist_remove(sn_waits
, seq
);
157 g_source_remove_by_user_data(seq
);
164 screen_set_root_cursor();
167 Time
sn_app_started(const gchar
*id
, const gchar
*wmclass
, const gchar
*name
)
170 Time t
= CurrentTime
;
175 for (it
= sn_waits
; it
; it
= g_slist_next(it
)) {
176 SnStartupSequence
*seq
= it
->data
;
177 gboolean found
= FALSE
;
178 const gchar
*seqid
, *seqclass
, *seqbin
;
179 seqid
= sn_startup_sequence_get_id(seq
);
180 seqclass
= sn_startup_sequence_get_wmclass(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
))
190 /* seqclass = "a string to match against the "resource name" or
191 "resource class" hints. These are WM_CLASS[0] and WM_CLASS[1]"
192 - from the startup-notification spec
194 found
= (seqclass
&& !strcmp(seqclass
, wmclass
)) ||
195 (seqclass
&& !strcmp(seqclass
, name
));
198 /* Check the binary name against the class and name hints
199 as well, to help apps that don't have the class set
201 found
= (seqbin
&& !g_ascii_strcasecmp(seqbin
, wmclass
)) ||
202 (seqbin
&& !g_ascii_strcasecmp(seqbin
, name
));
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
);
233 return FALSE
; /* don't repeat */
236 void sn_setup_spawn_environment(const gchar
*program
, const gchar
*name
,
237 const gchar
*icon_name
, const gchar
*wmclass
,
243 desc
= g_strdup_printf(_("Running %s"), 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 (wmclass
) sn_launcher_context_set_wmclass(sn_launcher
, wmclass
);
256 if (desktop
>= 0 && (unsigned) desktop
< screen_num_desktops
)
257 sn_launcher_context_set_workspace(sn_launcher
, (signed) desktop
);
258 sn_launcher_context_initiate(sn_launcher
, "openbox", program
,
260 id
= sn_launcher_context_get_startup_id(sn_launcher
);
262 /* 20 second timeout for apps to start */
263 sn_launcher_context_ref(sn_launcher
);
264 g_timeout_add_full(G_PRIORITY_DEFAULT
,
265 20 * 1000, sn_launch_wait_timeout
, sn_launcher
,
266 (GDestroyNotify
)sn_launcher_context_unref
);
268 setenv("DESKTOP_STARTUP_ID", id
, TRUE
);
273 void sn_spawn_cancel(void)
275 sn_launcher_context_complete(sn_launcher
);