From: Simon Glass <simon.glass@canonical.com>
Factor out common init code from scene_textline() and scene_texted()
into a new scene_txtin_init() helper. This handles buffer allocation,
clearing, and setting line_chars.
Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---
boot/scene_internal.h | 10 ++++++++++
boot/scene_textedit.c | 10 +++-------
boot/scene_textline.c | 9 +++------
boot/scene_txtin.c | 14 ++++++++++++++
4 files changed, 30 insertions(+), 13 deletions(-)
@@ -511,6 +511,16 @@ int scene_textline_calc_dims(struct scene_obj_textline *tline,
void scene_menu_calc_bbox(struct scene_obj_menu *menu,
struct vidconsole_bbox *bbox);
+/**
+ * scene_txtin_init() - Initialise common text-input fields
+ *
+ * @tin: Text-input info to init
+ * @size: Size to use for buffer
+ * @line_chars: Number of characters in the text line
+ * Return: 0 if OK, -ENOMEM if out of memory
+ */
+int scene_txtin_init(struct scene_txtin *tin, uint size, uint line_chars);
+
/**
* scene_txtin_calc_bbox() - Calculate bounding box for a text-input object
*
@@ -22,7 +22,6 @@ int scene_texted(struct scene *scn, const char *name, uint id,
uint line_chars, struct scene_obj_txtedit **teditp)
{
struct scene_obj_txtedit *ted;
- char *buf;
int ret;
ret = scene_obj_add(scn, name, id, SCENEOBJT_TEXTEDIT,
@@ -31,12 +30,9 @@ int scene_texted(struct scene *scn, const char *name, uint id,
if (ret < 0)
return log_msg_ret("obj", ret);
- abuf_init(&ted->tin.buf);
- if (!abuf_realloc(&ted->tin.buf, INITIAL_SIZE))
- return log_msg_ret("buf", -ENOMEM);
- buf = abuf_data(&ted->tin.buf);
- *buf = '\0';
- ted->tin.line_chars = line_chars;
+ ret = scene_txtin_init(&ted->tin, INITIAL_SIZE, line_chars);
+ if (ret)
+ return log_msg_ret("tin", ret);
if (teditp)
*teditp = ted;
@@ -20,7 +20,6 @@ int scene_textline(struct scene *scn, const char *name, uint id,
uint line_chars, struct scene_obj_textline **tlinep)
{
struct scene_obj_textline *tline;
- char *buf;
int ret;
if (line_chars >= EXPO_MAX_CHARS)
@@ -31,12 +30,10 @@ int scene_textline(struct scene *scn, const char *name, uint id,
(struct scene_obj **)&tline);
if (ret < 0)
return log_msg_ret("obj", -ENOMEM);
- if (!abuf_init_size(&tline->tin.buf, line_chars + 1))
- return log_msg_ret("buf", -ENOMEM);
- buf = abuf_data(&tline->tin.buf);
- *buf = '\0';
+ ret = scene_txtin_init(&tline->tin, line_chars + 1, line_chars);
+ if (ret)
+ return log_msg_ret("tin", ret);
tline->pos = line_chars;
- tline->tin.line_chars = line_chars;
if (tlinep)
*tlinep = tline;
@@ -11,8 +11,22 @@
#include <expo.h>
#include <log.h>
#include <video_console.h>
+#include <linux/errno.h>
#include "scene_internal.h"
+int scene_txtin_init(struct scene_txtin *tin, uint size, uint line_chars)
+{
+ char *buf;
+
+ if (!abuf_init_size(&tin->buf, size))
+ return log_msg_ret("buf", -ENOMEM);
+ buf = abuf_data(&tin->buf);
+ *buf = '\0';
+ tin->line_chars = line_chars;
+
+ return 0;
+}
+
void scene_txtin_calc_bbox(struct scene_obj *obj, struct scene_txtin *tin,
struct vidconsole_bbox *bbox,
struct vidconsole_bbox *edit_bbox)