static gboolean run_func(ObActionsData *data, gpointer options);
-static void prompt_cb(ObPrompt *p, gint result, gpointer options)
+static gboolean prompt_cb(ObPrompt *p, gint result, gpointer options)
{
if (result)
run_func(NULL, options);
+ return TRUE; /* call the cleanup func */
+}
+static void prompt_cleanup(ObPrompt *p, gpointer options)
+{
prompt_unref(p);
free_func(options);
}
};
ocp = dup_options(options);
- p = prompt_new(o->prompt, answers, 2, 0, 0, prompt_cb, ocp);
+ p = prompt_new(o->prompt, answers, 2, 0, 0,
+ prompt_cb, prompt_cleanup, ocp);
prompt_show(p, NULL, FALSE);
return FALSE;
return o;
}
-static void prompt_cb(ObPrompt *p, gint result, gpointer data)
+static gboolean prompt_cb(ObPrompt *p, gint result, gpointer data)
{
if (result)
ob_exit(0);
+ return TRUE; /* call the cleanup func */
+}
+
+static void prompt_cleanup(ObPrompt *p, gpointer data)
+{
prompt_unref(p);
}
};
p = prompt_new(_("Are you sure you want to exit Openbox?"),
- answers, 2, 0, 0, prompt_cb, NULL);
+ answers, 2, 0, 0, prompt_cb, prompt_cleanup, NULL);
prompt_show(p, NULL, FALSE);
}
else
return o;
}
-static void prompt_cb(ObPrompt *p, gint result, gpointer data)
+static gboolean prompt_cb(ObPrompt *p, gint result, gpointer data)
{
Options *o = data;
if (result) {
g_message(_("The SessionLogout actions is not available since Openbox was built without session management support"));
#endif
}
- g_free(o);
+ return TRUE; /* call cleanup func */
+}
+
+static void prompt_cleanup(ObPrompt *p, gpointer data)
+{
+ g_free(data);
prompt_unref(p);
}
o2 = g_memdup(o, sizeof(Options));
p = prompt_new(_("Are you sure you want to log out?"),
- answers, 2, 0, 0, prompt_cb, o2);
+ answers, 2, 0, 0, prompt_cb, prompt_cleanup, o2);
prompt_show(p, NULL, FALSE);
}
else
#define OB_KILL_RESULT_NO 0
#define OB_KILL_RESULT_YES 1
-static void client_kill_requested(ObPrompt *p, gint result, gpointer data)
+static gboolean client_kill_requested(ObPrompt *p, gint result, gpointer data)
{
ObClient *self = data;
if (result == OB_KILL_RESULT_YES)
client_kill(self);
+ return TRUE; /* call the cleanup func */
+}
+
+static void client_kill_cleanup(ObPrompt *p, gpointer data)
+{
+ ObClient *self = data;
+
+ g_assert(p == self->kill_prompt);
prompt_unref(self->kill_prompt);
self->kill_prompt = NULL;
sizeof(answers)/sizeof(answers[0]),
OB_KILL_RESULT_NO, /* default = no */
OB_KILL_RESULT_NO, /* cancel = no */
- client_kill_requested, self);
+ client_kill_requested,
+ client_kill_cleanup,
+ self);
g_free(m);
}
grab_startup(reconfigure);
group_startup(reconfigure);
ping_startup(reconfigure);
- prompt_startup(reconfigure);
client_startup(reconfigure);
dock_startup(reconfigure);
moveresize_startup(reconfigure);
mouse_startup(reconfigure);
menu_frame_startup(reconfigure);
menu_startup(reconfigure);
+ prompt_startup(reconfigure);
if (!reconfigure) {
guint32 xid;
client_unmanage_all();
}
+ prompt_shutdown(reconfigure);
menu_shutdown(reconfigure);
menu_frame_shutdown(reconfigure);
mouse_shutdown(reconfigure);
moveresize_shutdown(reconfigure);
dock_shutdown(reconfigure);
client_shutdown(reconfigure);
- prompt_shutdown(reconfigure);
ping_shutdown(reconfigure);
group_shutdown(reconfigure);
grab_shutdown(reconfigure);
#include "gettext.h"
static GList *prompt_list = NULL;
-static GList *prompt_msg_list = NULL;
/* we construct these */
static RrAppearance *prompt_a_bg;
static void render_all(ObPrompt *self);
static void render_button(ObPrompt *self, ObPromptElement *e);
static void prompt_resize(ObPrompt *self, gint w, gint h);
+static void prompt_run_callback(ObPrompt *self, gint result);
void prompt_startup(gboolean reconfig)
{
void prompt_shutdown(gboolean reconfig)
{
- while (prompt_msg_list)
- prompt_cancel(prompt_msg_list->data);
+ GList *it;
+
+ if (!reconfig) {
+ for (it = prompt_list; it; it = g_list_next(it)) {
+ ObPrompt *p = it->data;
+ if (p->cleanup) p->cleanup(p, p->data);
+ }
+
+ g_assert(prompt_list == NULL);
+ }
RrAppearanceFree(prompt_a_button);
RrAppearanceFree(prompt_a_focus);
ObPrompt* prompt_new(const gchar *msg,
const ObPromptAnswer *answers, gint n_answers,
gint default_result, gint cancel_result,
- ObPromptCallback func, gpointer data)
+ ObPromptCallback func, ObPromptCleanup cleanup,
+ gpointer data)
{
ObPrompt *self;
XSetWindowAttributes attrib;
self = g_new0(ObPrompt, 1);
self->ref = 1;
self->func = func;
+ self->cleanup = cleanup;
self->data = data;
self->default_result = default_result;
self->cancel_result = cancel_result;
else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) ||
e->xkey.keycode == ob_keycode(OB_KEY_SPACE))
{
- if (self->func) self->func(self, self->focus->result, self->data);
- prompt_hide(self);
+ prompt_run_callback(self, self->focus->result);
}
else if (e->xkey.keycode == ob_keycode(OB_KEY_TAB) ||
e->xkey.keycode == ob_keycode(OB_KEY_LEFT) ||
render_button(self, but);
}
else if (e->type == ButtonRelease) {
- if (but->pressed) {
- if (self->func) self->func(self, but->result, self->data);
- prompt_hide(self);
- }
+ if (but->pressed)
+ prompt_run_callback(self, but->result);
}
else if (e->type == MotionNotify) {
gboolean press;
void prompt_cancel(ObPrompt *self)
{
- if (self->func) self->func(self, self->cancel_result, self->data);
- prompt_hide(self);
+ prompt_run_callback(self, self->cancel_result);
}
-static void prompt_show_message_cb(ObPrompt *p, int res, gpointer data)
+static gboolean prompt_show_message_cb(ObPrompt *p, int res, gpointer data)
+{
+ return TRUE; /* call the cleanup func */
+}
+
+static void prompt_show_message_cleanup(ObPrompt *p, gpointer data)
{
- prompt_msg_list = g_list_remove(prompt_msg_list, p);
prompt_unref(p);
}
{ answer, 0 }
};
- p = prompt_new(msg, ans, 1, 0, 0, prompt_show_message_cb, NULL);
- prompt_msg_list = g_list_prepend(prompt_msg_list, p);
+ p = prompt_new(msg, ans, 1, 0, 0,
+ prompt_show_message_cb, prompt_show_message_cleanup, NULL);
prompt_show(p, NULL, FALSE);
}
+
+static void prompt_run_callback(ObPrompt *self, gint result)
+{
+ prompt_ref(self);
+ if (self->func) {
+ gboolean clean = self->func(self, self->focus->result, self->data);
+ if (clean && self->cleanup)
+ self->cleanup(self, self->data);
+ }
+ prompt_hide(self);
+ prompt_unref(self);
+}
typedef struct _ObPromptElement ObPromptElement;
typedef struct _ObPromptAnswer ObPromptAnswer;
-typedef void (*ObPromptCallback)(ObPrompt *p, gint result, gpointer data);
+typedef gboolean (*ObPromptCallback)(ObPrompt *p, gint result, gpointer data);
+typedef void (*ObPromptCleanup)(ObPrompt *p, gpointer data);
struct _ObPromptElement {
gchar *text;
gint cancel_result;
ObPromptCallback func;
+ ObPromptCleanup cleanup;
gpointer data;
};
of having a button presssed
@param func The callback function which is called when the dialog is closed
or a button is pressed
+ @param cleanup The cleanup function which is called if the prompt system
+ is shutting down, and someone is still holding a reference to the
+ prompt. This callback should cause the prompt's refcount to go to
+ zero so it can be freed, and free any other memory associated with
+ the prompt. The cleanup function is also called if the prompt's
+ callback function returns TRUE.
@param data User defined data which will be passed to the callback
*/
ObPrompt* prompt_new(const gchar *msg,
const ObPromptAnswer *answers, gint n_answers,
gint default_result, gint cancel_result,
- ObPromptCallback func, gpointer data);
+ ObPromptCallback func, ObPromptCleanup cleanup,
+ gpointer data);
void prompt_ref(ObPrompt *self);
void prompt_unref(ObPrompt *self);