From 0399293e5b9e5443b82103fa8e2c97deadef9825 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 3 Mar 2016 09:16:48 -0700 Subject: util: Shorten references into SocketAddress An upcoming patch will alter how simple unions, like SocketAddress, are laid out, which will impact all lines of the form 'addr->u.XXX' (expanding it to the longer 'addr->u.XXX.data'). For better legibility in that patch, and less need for line wrapping, it's better to use a temporary variable to reduce the effect of a layout change to just the variable initializations, rather than every reference within a SocketAddress. Also, take advantage of some C99 initialization where it makes sense (simplifying g_new0() to g_new()). Signed-off-by: Eric Blake Message-Id: <1457021813-10704-7-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- ui/vnc.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) (limited to 'ui') diff --git a/ui/vnc.c b/ui/vnc.c index ce4c669..6cd6314 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -3530,12 +3530,13 @@ void vnc_display_open(const char *id, Error **errp) } } else { unsigned long long baseport; + InetSocketAddress *inet; saddr->type = SOCKET_ADDRESS_KIND_INET; - saddr->u.inet = g_new0(InetSocketAddress, 1); + inet = saddr->u.inet = g_new0(InetSocketAddress, 1); if (vnc[0] == '[' && vnc[hlen - 1] == ']') { - saddr->u.inet->host = g_strndup(vnc + 1, hlen - 2); + inet->host = g_strndup(vnc + 1, hlen - 2); } else { - saddr->u.inet->host = g_strndup(vnc, hlen); + inet->host = g_strndup(vnc, hlen); } if (parse_uint_full(h + 1, &baseport, 10) < 0) { error_setg(errp, "can't convert to a number: %s", h + 1); @@ -3546,32 +3547,32 @@ void vnc_display_open(const char *id, Error **errp) error_setg(errp, "port %s out of range", h + 1); goto fail; } - saddr->u.inet->port = g_strdup_printf( + inet->port = g_strdup_printf( "%d", (int)baseport + 5900); if (to) { - saddr->u.inet->has_to = true; - saddr->u.inet->to = to + 5900; + inet->has_to = true; + inet->to = to + 5900; } - saddr->u.inet->ipv4 = ipv4; - saddr->u.inet->has_ipv4 = has_ipv4; - saddr->u.inet->ipv6 = ipv6; - saddr->u.inet->has_ipv6 = has_ipv6; + inet->ipv4 = ipv4; + inet->has_ipv4 = has_ipv4; + inet->ipv6 = ipv6; + inet->has_ipv6 = has_ipv6; if (vs->ws_enabled) { wsaddr->type = SOCKET_ADDRESS_KIND_INET; - wsaddr->u.inet = g_new0(InetSocketAddress, 1); - wsaddr->u.inet->host = g_strdup(saddr->u.inet->host); - wsaddr->u.inet->port = g_strdup(websocket); + inet = wsaddr->u.inet = g_new0(InetSocketAddress, 1); + inet->host = g_strdup(saddr->u.inet->host); + inet->port = g_strdup(websocket); if (to) { - wsaddr->u.inet->has_to = true; - wsaddr->u.inet->to = to; + inet->has_to = true; + inet->to = to; } - wsaddr->u.inet->ipv4 = ipv4; - wsaddr->u.inet->has_ipv4 = has_ipv4; - wsaddr->u.inet->ipv6 = ipv6; - wsaddr->u.inet->has_ipv6 = has_ipv6; + inet->ipv4 = ipv4; + inet->has_ipv4 = has_ipv4; + inet->ipv6 = ipv6; + inet->has_ipv6 = has_ipv6; } } } else { -- cgit v1.1 From b5a1b443183f56e0b9ad0f72614bdff7ace780d5 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 3 Mar 2016 09:16:49 -0700 Subject: ui: Shorten references into InputEvent An upcoming patch will alter how simple unions, like InputEvent, are laid out, which will impact all lines of the form 'evt->u.XXX' (expanding it to the longer 'evt->u.XXX.data'). For better legibility in that patch, and less need for line wrapping, it's better to use a temporary variable to reduce the effect of a layout change to just the variable initializations, rather than every reference within an InputEvent. There was one instance in hid.c:hid_pointer_event() where the code was referring to evt->u.rel inside the case label where evt->u.abs is the correct name; thankfully, both members of the union have the same type, so it happened to work, but it is now cleaner. Signed-off-by: Eric Blake Message-Id: <1457021813-10704-8-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- ui/input-legacy.c | 25 +++++++++++++++---------- ui/input.c | 54 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 46 insertions(+), 33 deletions(-) (limited to 'ui') diff --git a/ui/input-legacy.c b/ui/input-legacy.c index 703f0a6..f1c5cb4a 100644 --- a/ui/input-legacy.c +++ b/ui/input-legacy.c @@ -110,12 +110,13 @@ static void legacy_kbd_event(DeviceState *dev, QemuConsole *src, { QEMUPutKbdEntry *entry = (QEMUPutKbdEntry *)dev; int scancodes[3], i, count; + InputKeyEvent *key = evt->u.key; if (!entry || !entry->put_kbd) { return; } - count = qemu_input_key_value_to_scancode(evt->u.key->key, - evt->u.key->down, + count = qemu_input_key_value_to_scancode(key->key, + key->down, scancodes); for (i = 0; i < count; i++) { entry->put_kbd(entry->opaque, scancodes[i]); @@ -150,23 +151,25 @@ static void legacy_mouse_event(DeviceState *dev, QemuConsole *src, [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON, }; QEMUPutMouseEntry *s = (QEMUPutMouseEntry *)dev; + InputBtnEvent *btn; + InputMoveEvent *move; switch (evt->type) { case INPUT_EVENT_KIND_BTN: - if (evt->u.btn->down) { - s->buttons |= bmap[evt->u.btn->button]; + btn = evt->u.btn; + if (btn->down) { + s->buttons |= bmap[btn->button]; } else { - s->buttons &= ~bmap[evt->u.btn->button]; + s->buttons &= ~bmap[btn->button]; } - if (evt->u.btn->down && evt->u.btn->button == INPUT_BUTTON_WHEEL_UP) { + if (btn->down && btn->button == INPUT_BUTTON_WHEEL_UP) { s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque, s->axis[INPUT_AXIS_X], s->axis[INPUT_AXIS_Y], -1, s->buttons); } - if (evt->u.btn->down && - evt->u.btn->button == INPUT_BUTTON_WHEEL_DOWN) { + if (btn->down && btn->button == INPUT_BUTTON_WHEEL_DOWN) { s->qemu_put_mouse_event(s->qemu_put_mouse_event_opaque, s->axis[INPUT_AXIS_X], s->axis[INPUT_AXIS_Y], @@ -175,10 +178,12 @@ static void legacy_mouse_event(DeviceState *dev, QemuConsole *src, } break; case INPUT_EVENT_KIND_ABS: - s->axis[evt->u.abs->axis] = evt->u.abs->value; + move = evt->u.abs; + s->axis[move->axis] = move->value; break; case INPUT_EVENT_KIND_REL: - s->axis[evt->u.rel->axis] += evt->u.rel->value; + move = evt->u.rel; + s->axis[move->axis] += move->value; break; default: break; diff --git a/ui/input.c b/ui/input.c index 6fd48ef..13ee117 100644 --- a/ui/input.c +++ b/ui/input.c @@ -166,24 +166,25 @@ void qmp_input_send_event(bool has_device, const char *device, static void qemu_input_transform_abs_rotate(InputEvent *evt) { + InputMoveEvent *move = evt->u.abs; switch (graphic_rotate) { case 90: - if (evt->u.abs->axis == INPUT_AXIS_X) { - evt->u.abs->axis = INPUT_AXIS_Y; - } else if (evt->u.abs->axis == INPUT_AXIS_Y) { - evt->u.abs->axis = INPUT_AXIS_X; - evt->u.abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->u.abs->value; + if (move->axis == INPUT_AXIS_X) { + move->axis = INPUT_AXIS_Y; + } else if (move->axis == INPUT_AXIS_Y) { + move->axis = INPUT_AXIS_X; + move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value; } break; case 180: - evt->u.abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->u.abs->value; + move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value; break; case 270: - if (evt->u.abs->axis == INPUT_AXIS_X) { - evt->u.abs->axis = INPUT_AXIS_Y; - evt->u.abs->value = INPUT_EVENT_ABS_SIZE - 1 - evt->u.abs->value; - } else if (evt->u.abs->axis == INPUT_AXIS_Y) { - evt->u.abs->axis = INPUT_AXIS_X; + if (move->axis == INPUT_AXIS_X) { + move->axis = INPUT_AXIS_Y; + move->value = INPUT_EVENT_ABS_SIZE - 1 - move->value; + } else if (move->axis == INPUT_AXIS_Y) { + move->axis = INPUT_AXIS_X; } break; } @@ -193,22 +194,26 @@ static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt) { const char *name; int qcode, idx = -1; + InputKeyEvent *key; + InputBtnEvent *btn; + InputMoveEvent *move; if (src) { idx = qemu_console_get_index(src); } switch (evt->type) { case INPUT_EVENT_KIND_KEY: - switch (evt->u.key->key->type) { + key = evt->u.key; + switch (key->key->type) { case KEY_VALUE_KIND_NUMBER: - qcode = qemu_input_key_number_to_qcode(evt->u.key->key->u.number); + qcode = qemu_input_key_number_to_qcode(key->key->u.number); name = QKeyCode_lookup[qcode]; - trace_input_event_key_number(idx, evt->u.key->key->u.number, - name, evt->u.key->down); + trace_input_event_key_number(idx, key->key->u.number, + name, key->down); break; case KEY_VALUE_KIND_QCODE: - name = QKeyCode_lookup[evt->u.key->key->u.qcode]; - trace_input_event_key_qcode(idx, name, evt->u.key->down); + name = QKeyCode_lookup[key->key->u.qcode]; + trace_input_event_key_qcode(idx, name, key->down); break; case KEY_VALUE_KIND__MAX: /* keep gcc happy */ @@ -216,16 +221,19 @@ static void qemu_input_event_trace(QemuConsole *src, InputEvent *evt) } break; case INPUT_EVENT_KIND_BTN: - name = InputButton_lookup[evt->u.btn->button]; - trace_input_event_btn(idx, name, evt->u.btn->down); + btn = evt->u.btn; + name = InputButton_lookup[btn->button]; + trace_input_event_btn(idx, name, btn->down); break; case INPUT_EVENT_KIND_REL: - name = InputAxis_lookup[evt->u.rel->axis]; - trace_input_event_rel(idx, name, evt->u.rel->value); + move = evt->u.rel; + name = InputAxis_lookup[move->axis]; + trace_input_event_rel(idx, name, move->value); break; case INPUT_EVENT_KIND_ABS: - name = InputAxis_lookup[evt->u.abs->axis]; - trace_input_event_abs(idx, name, evt->u.abs->value); + move = evt->u.abs; + name = InputAxis_lookup[move->axis]; + trace_input_event_abs(idx, name, move->value); break; case INPUT_EVENT_KIND__MAX: /* keep gcc happy */ -- cgit v1.1 From 10f759079e616a1cc4701fc26ab5e2a048ce912c Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Thu, 3 Mar 2016 09:16:50 -0700 Subject: qapi: Avoid use of 'data' member of QAPI unions QAPI code generators currently create a 'void *data' member as part of the anonymous union embedded in the C struct corresponding to a QAPI union. However, directly assigning to this member of the union feels a bit fishy, when we can assign to another member of the struct instead. Signed-off-by: Eric Blake Reviewed-by: Daniel P. Berrange Message-Id: <1457021813-10704-9-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- ui/input.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ui') diff --git a/ui/input.c b/ui/input.c index 13ee117..b035f86 100644 --- a/ui/input.c +++ b/ui/input.c @@ -470,7 +470,7 @@ InputEvent *qemu_input_event_new_move(InputEventKind kind, InputMoveEvent *move = g_new0(InputMoveEvent, 1); evt->type = kind; - evt->u.data = move; + evt->u.rel = move; /* evt->u.rel is the same as evt->u.abs */ move->axis = axis; move->value = value; return evt; -- cgit v1.1