summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRay Strode <rstrode@redhat.com>2008-06-02 11:03:28 -0400
committerRay Strode <rstrode@redhat.com>2008-06-02 11:03:28 -0400
commitccd95672582f2cfc98d0d6985603bda84f48f170 (patch)
tree782f7f320120dcbbc5c1c7f8e919a7ca42cf9793 /src
parent0af64a60e26ce0eb7dfaf65fc692d50f2b6d52b0 (diff)
downloadplymouth-ccd95672582f2cfc98d0d6985603bda84f48f170.tar.gz
plymouth-ccd95672582f2cfc98d0d6985603bda84f48f170.tar.xz
plymouth-ccd95672582f2cfc98d0d6985603bda84f48f170.zip
add new answer object for giving a deferred response
Right now we have this hack tower of function pointers cutting through layers of code to get the password from the splash plugin to the client. I'd like to cut most of that out by just having one answer object that the bottom layer and the the top layer know directly, without the middle layers having to get too involved.
Diffstat (limited to 'src')
-rw-r--r--src/ply-answer.c147
-rw-r--r--src/ply-answer.h50
2 files changed, 197 insertions, 0 deletions
diff --git a/src/ply-answer.c b/src/ply-answer.c
new file mode 100644
index 0000000..9213875
--- /dev/null
+++ b/src/ply-answer.c
@@ -0,0 +1,147 @@
+/* ply-answer.h - Object that takes a string and triggers a closure
+ * to use the string
+ *
+ * Copyright (C) 2007 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Written by: Ray Strode <rstrode@redhat.com>
+ */
+#include "config.h"
+#include "ply-answer.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "ply-logger.h"
+#include "ply-utils.h"
+
+struct _ply_answer
+{
+ ply_answer_handler_t handler;
+ void *user_data;
+};
+
+ply_answer_t *
+ply_answer_new (ply_answer_handler_t handler,
+ void *user_data)
+{
+ ply_answer_t *answer;
+
+ answer = calloc (1, sizeof (ply_answer_t));
+ answer->handler = handler;
+ answer->user_data = user_data;
+
+ return answer;
+}
+
+void
+ply_answer_free (ply_answer_t *answer)
+{
+ if (answer == NULL)
+ return;
+
+ free (answer);
+}
+
+void
+ply_answer_with_string (ply_answer_t *answer,
+ const char *string)
+{
+ assert (answer != NULL);
+
+ if (answer->handler != NULL)
+ answer->handler (answer->user_data, string, answer);
+
+}
+
+#ifdef PLY_ANSWER_ENABLE_TEST
+
+#include <stdio.h>
+
+#include "ply-event-loop.h"
+#include "ply-answer.h"
+
+static void
+on_timeout (ply_answer_t *answer,
+ ply_event_loop_t *loop)
+{
+ ply_event_loop_exit (loop, 0);
+}
+
+static void
+on_keypress (ply_answer_t *answer,
+ const char *keyboard_input)
+{
+ printf ("key '%c' (0x%x) was pressed\n",
+ keyboard_input[0], (unsigned int) keyboard_input[0]);
+}
+
+int
+main (int argc,
+ char **argv)
+{
+ ply_event_loop_t *loop;
+ ply_answer_t *answer;
+ int exit_code;
+ const char *tty_name;
+
+ exit_code = 0;
+
+ loop = ply_event_loop_new ();
+
+ if (argc > 1)
+ tty_name = argv[1];
+ else
+ tty_name = "/dev/tty1";
+
+ answer = ply_answer_new (tty_name);
+ ply_answer_attach_to_event_loop (answer, loop);
+ ply_answer_set_keyboard_input_handler (answer,
+ (ply_answer_keyboard_input_handler_t)
+ on_keypress, answer);
+
+ if (!ply_answer_open (answer))
+ {
+ ply_save_errno ();
+ perror ("could not open answer");
+ ply_restore_errno ();
+ return errno;
+ }
+
+ if (!ply_answer_set_mode (answer, PLY_ANSWER_MODE_TEXT))
+ {
+ ply_save_errno ();
+ perror ("could not set answer for graphics mode");
+ ply_restore_errno ();
+ }
+
+ ply_event_loop_watch_for_timeout (loop,
+ 15.0,
+ (ply_event_loop_timeout_handler_t)
+ on_timeout,
+ answer);
+ exit_code = ply_event_loop_run (loop);
+
+ ply_answer_close (answer);
+ ply_answer_free (answer);
+
+ return exit_code;
+}
+
+#endif /* PLY_ANSWER_ENABLE_TEST */
+/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */
diff --git a/src/ply-answer.h b/src/ply-answer.h
new file mode 100644
index 0000000..4e04a75
--- /dev/null
+++ b/src/ply-answer.h
@@ -0,0 +1,50 @@
+/* ply-answer.h - Object that takes a string and triggers a closure
+ * to use the string
+ *
+ * Copyright (C) 2008 Red Hat, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Written By: Ray Strode <rstrode@redhat.com>
+ */
+#ifndef PLY_ANSWER_H
+#define PLY_ANSWER_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "ply-buffer.h"
+#include "ply-event-loop.h"
+#include "ply-frame-buffer.h"
+
+typedef struct _ply_answer ply_answer_t;
+
+typedef void (* ply_answer_handler_t) (void *user_data,
+ const char *string,
+ ply_answer_t *answer);
+
+#ifndef PLY_HIDE_FUNCTION_DECLARATIONS
+ply_answer_t *ply_answer_new (ply_answer_handler_t handler,
+ void *user_data);
+void ply_answer_free (ply_answer_t *answer);
+
+void ply_answer_with_string (ply_answer_t *answer,
+ const char *string);
+#endif
+
+#endif /* PLY_ANSWER_H */
+/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */