char: move callbacks in CharDriver
This makes the code more declarative, and avoids duplicating the information on all instances. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
a1698bf183
commit
b68e956abe
@ -622,7 +622,8 @@ static void baum_free(struct CharDriverState *chr)
|
||||
g_free(baum);
|
||||
}
|
||||
|
||||
static CharDriverState *chr_baum_init(const char *id,
|
||||
static CharDriverState *chr_baum_init(const CharDriver *driver,
|
||||
const char *id,
|
||||
ChardevBackend *backend,
|
||||
ChardevReturn *ret,
|
||||
bool *be_opened,
|
||||
@ -633,7 +634,7 @@ static CharDriverState *chr_baum_init(const char *id,
|
||||
CharDriverState *chr;
|
||||
brlapi_handle_t *handle;
|
||||
|
||||
chr = qemu_chr_alloc(common, errp);
|
||||
chr = qemu_chr_alloc(driver, common, errp);
|
||||
if (!chr) {
|
||||
return NULL;
|
||||
}
|
||||
@ -641,9 +642,6 @@ static CharDriverState *chr_baum_init(const char *id,
|
||||
baum->chr = chr;
|
||||
|
||||
chr->opaque = baum;
|
||||
chr->chr_write = baum_write;
|
||||
chr->chr_accept_input = baum_accept_input;
|
||||
chr->chr_free = baum_free;
|
||||
|
||||
handle = g_malloc0(brlapi_getHandleSize());
|
||||
baum->brlapi = handle;
|
||||
@ -674,6 +672,9 @@ static void register_types(void)
|
||||
static const CharDriver driver = {
|
||||
.kind = CHARDEV_BACKEND_KIND_BRAILLE,
|
||||
.create = chr_baum_init,
|
||||
.chr_write = baum_write,
|
||||
.chr_accept_input = baum_accept_input,
|
||||
.chr_free = baum_free,
|
||||
};
|
||||
|
||||
register_char_driver(&driver);
|
||||
|
@ -148,7 +148,8 @@ static QemuInputHandler msmouse_handler = {
|
||||
.sync = msmouse_input_sync,
|
||||
};
|
||||
|
||||
static CharDriverState *qemu_chr_open_msmouse(const char *id,
|
||||
static CharDriverState *qemu_chr_open_msmouse(const CharDriver *driver,
|
||||
const char *id,
|
||||
ChardevBackend *backend,
|
||||
ChardevReturn *ret,
|
||||
bool *be_opened,
|
||||
@ -158,13 +159,10 @@ static CharDriverState *qemu_chr_open_msmouse(const char *id,
|
||||
MouseState *mouse;
|
||||
CharDriverState *chr;
|
||||
|
||||
chr = qemu_chr_alloc(common, errp);
|
||||
chr = qemu_chr_alloc(driver, common, errp);
|
||||
if (!chr) {
|
||||
return NULL;
|
||||
}
|
||||
chr->chr_write = msmouse_chr_write;
|
||||
chr->chr_free = msmouse_chr_free;
|
||||
chr->chr_accept_input = msmouse_chr_accept_input;
|
||||
*be_opened = false;
|
||||
|
||||
mouse = g_new0(MouseState, 1);
|
||||
@ -182,6 +180,9 @@ static void register_types(void)
|
||||
static const CharDriver driver = {
|
||||
.kind = CHARDEV_BACKEND_KIND_MSMOUSE,
|
||||
.create = qemu_chr_open_msmouse,
|
||||
.chr_write = msmouse_chr_write,
|
||||
.chr_accept_input = msmouse_chr_accept_input,
|
||||
.chr_free = msmouse_chr_free,
|
||||
};
|
||||
register_char_driver(&driver);
|
||||
}
|
||||
|
@ -109,7 +109,8 @@ static void testdev_free(struct CharDriverState *chr)
|
||||
g_free(testdev);
|
||||
}
|
||||
|
||||
static CharDriverState *chr_testdev_init(const char *id,
|
||||
static CharDriverState *chr_testdev_init(const CharDriver *driver,
|
||||
const char *id,
|
||||
ChardevBackend *backend,
|
||||
ChardevReturn *ret,
|
||||
bool *be_opened,
|
||||
@ -121,9 +122,8 @@ static CharDriverState *chr_testdev_init(const char *id,
|
||||
testdev = g_new0(TestdevCharState, 1);
|
||||
testdev->chr = chr = g_new0(CharDriverState, 1);
|
||||
|
||||
chr->driver = driver;
|
||||
chr->opaque = testdev;
|
||||
chr->chr_write = testdev_write;
|
||||
chr->chr_free = testdev_free;
|
||||
|
||||
return chr;
|
||||
}
|
||||
@ -133,6 +133,8 @@ static void register_types(void)
|
||||
static const CharDriver driver = {
|
||||
.kind = CHARDEV_BACKEND_KIND_TESTDEV,
|
||||
.create = chr_testdev_init,
|
||||
.chr_write = testdev_write,
|
||||
.chr_free = testdev_free,
|
||||
};
|
||||
register_char_driver(&driver);
|
||||
}
|
||||
|
@ -1732,6 +1732,10 @@ int gdbserver_start(const char *device)
|
||||
CharDriverState *chr = NULL;
|
||||
CharDriverState *mon_chr;
|
||||
ChardevCommon common = { 0 };
|
||||
static const CharDriver driver = {
|
||||
.kind = -1,
|
||||
.chr_write = gdb_monitor_write,
|
||||
};
|
||||
|
||||
if (!first_cpu) {
|
||||
error_report("gdbstub: meaningless to attach gdb to a "
|
||||
@ -1770,8 +1774,7 @@ int gdbserver_start(const char *device)
|
||||
qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
|
||||
|
||||
/* Initialize a monitor terminal for gdb */
|
||||
mon_chr = qemu_chr_alloc(&common, &error_abort);
|
||||
mon_chr->chr_write = gdb_monitor_write;
|
||||
mon_chr = qemu_chr_alloc(&driver, &common, &error_abort);
|
||||
monitor_init(mon_chr, 0);
|
||||
} else {
|
||||
if (qemu_chr_fe_get_driver(&s->chr)) {
|
||||
|
@ -462,12 +462,16 @@ qemu_irq *csrhci_pins_get(CharDriverState *chr)
|
||||
|
||||
CharDriverState *uart_hci_init(void)
|
||||
{
|
||||
static const CharDriver hci_driver = {
|
||||
.kind = -1,
|
||||
.chr_write = csrhci_write,
|
||||
.chr_ioctl = csrhci_ioctl,
|
||||
};
|
||||
struct csrhci_s *s = (struct csrhci_s *)
|
||||
g_malloc0(sizeof(struct csrhci_s));
|
||||
|
||||
s->chr.opaque = s;
|
||||
s->chr.chr_write = csrhci_write;
|
||||
s->chr.chr_ioctl = csrhci_ioctl;
|
||||
s->chr.driver = &hci_driver;
|
||||
|
||||
s->hci = qemu_next_hci();
|
||||
s->hci->opaque = s;
|
||||
|
@ -85,24 +85,11 @@ typedef struct CharBackend {
|
||||
int fe_open;
|
||||
} CharBackend;
|
||||
|
||||
typedef struct CharDriver CharDriver;
|
||||
|
||||
struct CharDriverState {
|
||||
const CharDriver *driver;
|
||||
QemuMutex chr_write_lock;
|
||||
int (*chr_write)(struct CharDriverState *s, const uint8_t *buf, int len);
|
||||
int (*chr_sync_read)(struct CharDriverState *s,
|
||||
const uint8_t *buf, int len);
|
||||
GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond);
|
||||
void (*chr_update_read_handler)(struct CharDriverState *s,
|
||||
GMainContext *context);
|
||||
int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
|
||||
int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
|
||||
int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
|
||||
int (*chr_add_client)(struct CharDriverState *chr, int fd);
|
||||
int (*chr_wait_connected)(struct CharDriverState *chr, Error **errp);
|
||||
void (*chr_free)(struct CharDriverState *chr);
|
||||
void (*chr_disconnect)(struct CharDriverState *chr);
|
||||
void (*chr_accept_input)(struct CharDriverState *chr);
|
||||
void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
|
||||
void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
|
||||
CharBackend *be;
|
||||
void *opaque;
|
||||
char *label;
|
||||
@ -125,7 +112,8 @@ struct CharDriverState {
|
||||
*
|
||||
* Returns: a newly allocated CharDriverState, or NULL on error.
|
||||
*/
|
||||
CharDriverState *qemu_chr_alloc(ChardevCommon *backend, Error **errp);
|
||||
CharDriverState *qemu_chr_alloc(const CharDriver *driver,
|
||||
ChardevCommon *backend, Error **errp);
|
||||
|
||||
/**
|
||||
* @qemu_chr_new_from_opts:
|
||||
@ -475,15 +463,33 @@ void qemu_chr_set_feature(CharDriverState *chr,
|
||||
CharDriverFeature feature);
|
||||
QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
|
||||
|
||||
typedef struct CharDriver {
|
||||
struct CharDriver {
|
||||
ChardevBackendKind kind;
|
||||
const char *alias;
|
||||
void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
|
||||
CharDriverState *(*create)(const char *id,
|
||||
CharDriverState *(*create)(const CharDriver *driver,
|
||||
const char *id,
|
||||
ChardevBackend *backend,
|
||||
ChardevReturn *ret, bool *be_opened,
|
||||
Error **errp);
|
||||
} CharDriver;
|
||||
|
||||
int (*chr_write)(struct CharDriverState *s, const uint8_t *buf, int len);
|
||||
int (*chr_sync_read)(struct CharDriverState *s,
|
||||
const uint8_t *buf, int len);
|
||||
GSource *(*chr_add_watch)(struct CharDriverState *s, GIOCondition cond);
|
||||
void (*chr_update_read_handler)(struct CharDriverState *s,
|
||||
GMainContext *context);
|
||||
int (*chr_ioctl)(struct CharDriverState *s, int cmd, void *arg);
|
||||
int (*get_msgfds)(struct CharDriverState *s, int* fds, int num);
|
||||
int (*set_msgfds)(struct CharDriverState *s, int *fds, int num);
|
||||
int (*chr_add_client)(struct CharDriverState *chr, int fd);
|
||||
int (*chr_wait_connected)(struct CharDriverState *chr, Error **errp);
|
||||
void (*chr_free)(struct CharDriverState *chr);
|
||||
void (*chr_disconnect)(struct CharDriverState *chr);
|
||||
void (*chr_accept_input)(struct CharDriverState *chr);
|
||||
void (*chr_set_echo)(struct CharDriverState *chr, bool echo);
|
||||
void (*chr_set_fe_open)(struct CharDriverState *chr, int fe_open);
|
||||
};
|
||||
|
||||
void register_char_driver(const CharDriver *driver);
|
||||
|
||||
|
479
qemu-char.c
479
qemu-char.c
File diff suppressed because it is too large
Load Diff
@ -260,16 +260,15 @@ static void spice_chr_accept_input(struct CharDriverState *chr)
|
||||
spice_server_char_device_wakeup(&s->sin);
|
||||
}
|
||||
|
||||
static CharDriverState *chr_open(const char *subtype,
|
||||
void (*set_fe_open)(struct CharDriverState *,
|
||||
int),
|
||||
static CharDriverState *chr_open(const CharDriver *driver,
|
||||
const char *subtype,
|
||||
ChardevCommon *backend,
|
||||
Error **errp)
|
||||
{
|
||||
CharDriverState *chr;
|
||||
SpiceCharDriver *s;
|
||||
|
||||
chr = qemu_chr_alloc(backend, errp);
|
||||
chr = qemu_chr_alloc(driver, backend, errp);
|
||||
if (!chr) {
|
||||
return NULL;
|
||||
}
|
||||
@ -278,18 +277,14 @@ static CharDriverState *chr_open(const char *subtype,
|
||||
s->active = false;
|
||||
s->sin.subtype = g_strdup(subtype);
|
||||
chr->opaque = s;
|
||||
chr->chr_write = spice_chr_write;
|
||||
chr->chr_add_watch = spice_chr_add_watch;
|
||||
chr->chr_free = spice_chr_free;
|
||||
chr->chr_set_fe_open = set_fe_open;
|
||||
chr->chr_accept_input = spice_chr_accept_input;
|
||||
|
||||
QLIST_INSERT_HEAD(&spice_chars, s, next);
|
||||
|
||||
return chr;
|
||||
}
|
||||
|
||||
static CharDriverState *qemu_chr_open_spice_vmc(const char *id,
|
||||
static CharDriverState *qemu_chr_open_spice_vmc(const CharDriver *driver,
|
||||
const char *id,
|
||||
ChardevBackend *backend,
|
||||
ChardevReturn *ret,
|
||||
bool *be_opened,
|
||||
@ -312,11 +307,12 @@ static CharDriverState *qemu_chr_open_spice_vmc(const char *id,
|
||||
}
|
||||
|
||||
*be_opened = false;
|
||||
return chr_open(type, spice_vmc_set_fe_open, common, errp);
|
||||
return chr_open(driver, type, common, errp);
|
||||
}
|
||||
|
||||
#if SPICE_SERVER_VERSION >= 0x000c02
|
||||
static CharDriverState *qemu_chr_open_spice_port(const char *id,
|
||||
static CharDriverState *qemu_chr_open_spice_port(const CharDriver *driver,
|
||||
const char *id,
|
||||
ChardevBackend *backend,
|
||||
ChardevReturn *ret,
|
||||
bool *be_opened,
|
||||
@ -333,7 +329,7 @@ static CharDriverState *qemu_chr_open_spice_port(const char *id,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
chr = chr_open("port", spice_port_set_fe_open, common, errp);
|
||||
chr = chr_open(driver, "port", common, errp);
|
||||
if (!chr) {
|
||||
return NULL;
|
||||
}
|
||||
@ -393,11 +389,21 @@ static void register_types(void)
|
||||
.kind = CHARDEV_BACKEND_KIND_SPICEVMC,
|
||||
.parse = qemu_chr_parse_spice_vmc,
|
||||
.create = qemu_chr_open_spice_vmc,
|
||||
.chr_write = spice_chr_write,
|
||||
.chr_add_watch = spice_chr_add_watch,
|
||||
.chr_set_fe_open = spice_vmc_set_fe_open,
|
||||
.chr_accept_input = spice_chr_accept_input,
|
||||
.chr_free = spice_chr_free,
|
||||
};
|
||||
static const CharDriver port_driver = {
|
||||
.kind = CHARDEV_BACKEND_KIND_SPICEPORT,
|
||||
.parse = qemu_chr_parse_spice_port,
|
||||
.create = qemu_chr_open_spice_port,
|
||||
.chr_write = spice_chr_write,
|
||||
.chr_add_watch = spice_chr_add_watch,
|
||||
.chr_set_fe_open = spice_port_set_fe_open,
|
||||
.chr_accept_input = spice_chr_accept_input,
|
||||
.chr_free = spice_chr_free,
|
||||
};
|
||||
register_char_driver(&vmc_driver);
|
||||
register_char_driver(&port_driver);
|
||||
|
28
ui/console.c
28
ui/console.c
@ -1051,6 +1051,10 @@ static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
|
||||
QemuConsole *s = chr->opaque;
|
||||
int i;
|
||||
|
||||
if (!s->ds) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
s->update_x0 = s->width * FONT_WIDTH;
|
||||
s->update_y0 = s->height * FONT_HEIGHT;
|
||||
s->update_x1 = 0;
|
||||
@ -2000,8 +2004,6 @@ static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
|
||||
|
||||
s = chr->opaque;
|
||||
|
||||
chr->chr_write = console_puts;
|
||||
|
||||
s->out_fifo.buf = s->out_fifo_buf;
|
||||
s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
|
||||
s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
|
||||
@ -2048,6 +2050,8 @@ static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
|
||||
qemu_chr_be_generic_open(chr);
|
||||
}
|
||||
|
||||
static const CharDriver vc_driver;
|
||||
|
||||
static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
|
||||
{
|
||||
ChardevCommon *common = qapi_ChardevVC_base(vc);
|
||||
@ -2056,7 +2060,7 @@ static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
|
||||
unsigned width = 0;
|
||||
unsigned height = 0;
|
||||
|
||||
chr = qemu_chr_alloc(common, errp);
|
||||
chr = qemu_chr_alloc(&vc_driver, common, errp);
|
||||
if (!chr) {
|
||||
return NULL;
|
||||
}
|
||||
@ -2089,7 +2093,6 @@ static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
|
||||
|
||||
s->chr = chr;
|
||||
chr->opaque = s;
|
||||
chr->chr_set_echo = text_console_set_echo;
|
||||
|
||||
if (display_state) {
|
||||
text_console_do_init(chr, display_state);
|
||||
@ -2099,7 +2102,8 @@ static CharDriverState *text_console_init(ChardevVC *vc, Error **errp)
|
||||
|
||||
static VcHandler *vc_handler = text_console_init;
|
||||
|
||||
static CharDriverState *vc_init(const char *id, ChardevBackend *backend,
|
||||
static CharDriverState *vc_init(const CharDriver *driver,
|
||||
const char *id, ChardevBackend *backend,
|
||||
ChardevReturn *ret, bool *be_opened,
|
||||
Error **errp)
|
||||
{
|
||||
@ -2191,14 +2195,16 @@ static const TypeInfo qemu_console_info = {
|
||||
.class_size = sizeof(QemuConsoleClass),
|
||||
};
|
||||
|
||||
static const CharDriver vc_driver = {
|
||||
.kind = CHARDEV_BACKEND_KIND_VC,
|
||||
.parse = qemu_chr_parse_vc,
|
||||
.create = vc_init,
|
||||
.chr_write = console_puts,
|
||||
.chr_set_echo = text_console_set_echo,
|
||||
};
|
||||
|
||||
static void register_types(void)
|
||||
{
|
||||
static const CharDriver vc_driver = {
|
||||
.kind = CHARDEV_BACKEND_KIND_VC,
|
||||
.parse = qemu_chr_parse_vc,
|
||||
.create = vc_init,
|
||||
};
|
||||
|
||||
type_register_static(&qemu_console_info);
|
||||
register_char_driver(&vc_driver);
|
||||
}
|
||||
|
11
ui/gtk.c
11
ui/gtk.c
@ -1703,6 +1703,12 @@ static CharDriverState *vcs[MAX_VCS];
|
||||
|
||||
static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)
|
||||
{
|
||||
static const CharDriver gd_vc_driver = {
|
||||
.kind = CHARDEV_BACKEND_KIND_VC,
|
||||
.chr_write = gd_vc_chr_write,
|
||||
.chr_set_echo = gd_vc_chr_set_echo,
|
||||
};
|
||||
|
||||
ChardevCommon *common = qapi_ChardevVC_base(vc);
|
||||
CharDriverState *chr;
|
||||
|
||||
@ -1711,14 +1717,11 @@ static CharDriverState *gd_vc_handler(ChardevVC *vc, Error **errp)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
chr = qemu_chr_alloc(common, errp);
|
||||
chr = qemu_chr_alloc(&gd_vc_driver, common, errp);
|
||||
if (!chr) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
chr->chr_write = gd_vc_chr_write;
|
||||
chr->chr_set_echo = gd_vc_chr_set_echo;
|
||||
|
||||
/* Temporary, until gd_vc_vte_init runs. */
|
||||
chr->opaque = g_new0(VirtualConsole, 1);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user