Skip to content

Commit

Permalink
ui/cocoa: Statically allocate dcl
Browse files Browse the repository at this point in the history
There is no need of dynamic allocation as dcl is a small singleton.
Static allocation reduces code size and makes hacking with ui/cocoa a
bit easier.

Signed-off-by: Akihiko Odaki <akihiko.odaki@gmail.com>
Message-Id: <20210219084419.90181-1-akihiko.odaki@gmail.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
  • Loading branch information
akihikodaki authored and kraxel committed Feb 19, 2021
1 parent 4295f83 commit cc7859c
Showing 1 changed file with 31 additions and 34 deletions.
65 changes: 31 additions & 34 deletions ui/cocoa.m
Expand Up @@ -72,8 +72,24 @@
int height;
} QEMUScreen;

static void cocoa_update(DisplayChangeListener *dcl,
int x, int y, int w, int h);

static void cocoa_switch(DisplayChangeListener *dcl,
DisplaySurface *surface);

static void cocoa_refresh(DisplayChangeListener *dcl);

NSWindow *normalWindow, *about_window;
static DisplayChangeListener *dcl;
static const DisplayChangeListenerOps dcl_ops = {
.dpy_name = "cocoa",
.dpy_gfx_update = cocoa_update,
.dpy_gfx_switch = cocoa_switch,
.dpy_refresh = cocoa_refresh,
};
static DisplayChangeListener dcl = {
.ops = &dcl_ops,
};
static int last_buttons;
static int cursor_hide = 1;

Expand Down Expand Up @@ -607,15 +623,15 @@ - (void) toggleModifier: (int)keycode {
// Toggle the stored state.
modifiers_state[keycode] = !modifiers_state[keycode];
// Send a keyup or keydown depending on the state.
qemu_input_event_send_key_qcode(dcl->con, keycode, modifiers_state[keycode]);
qemu_input_event_send_key_qcode(dcl.con, keycode, modifiers_state[keycode]);
}

- (void) toggleStatefulModifier: (int)keycode {
// Toggle the stored state.
modifiers_state[keycode] = !modifiers_state[keycode];
// Generate keydown and keyup.
qemu_input_event_send_key_qcode(dcl->con, keycode, true);
qemu_input_event_send_key_qcode(dcl->con, keycode, false);
qemu_input_event_send_key_qcode(dcl.con, keycode, true);
qemu_input_event_send_key_qcode(dcl.con, keycode, false);
}

// Does the work of sending input to the monitor
Expand Down Expand Up @@ -799,7 +815,7 @@ - (bool) handleEventLocked:(NSEvent *)event
}

if (qemu_console_is_graphic(NULL)) {
qemu_input_event_send_key_qcode(dcl->con, keycode, true);
qemu_input_event_send_key_qcode(dcl.con, keycode, true);
} else {
[self handleMonitorInput: event];
}
Expand All @@ -814,7 +830,7 @@ - (bool) handleEventLocked:(NSEvent *)event
}

if (qemu_console_is_graphic(NULL)) {
qemu_input_event_send_key_qcode(dcl->con, keycode, false);
qemu_input_event_send_key_qcode(dcl.con, keycode, false);
}
break;
case NSEventTypeMouseMoved:
Expand Down Expand Up @@ -892,9 +908,9 @@ - (bool) handleEventLocked:(NSEvent *)event
/* Determine if this is a scroll up or scroll down event */
buttons = ([event deltaY] > 0) ?
INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
qemu_input_queue_btn(dcl->con, buttons, true);
qemu_input_queue_btn(dcl.con, buttons, true);
qemu_input_event_sync();
qemu_input_queue_btn(dcl->con, buttons, false);
qemu_input_queue_btn(dcl.con, buttons, false);
qemu_input_event_sync();
}
/*
Expand Down Expand Up @@ -922,7 +938,7 @@ - (bool) handleEventLocked:(NSEvent *)event
[INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
[INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON
};
qemu_input_update_buttons(dcl->con, bmap, last_buttons, buttons);
qemu_input_update_buttons(dcl.con, bmap, last_buttons, buttons);
last_buttons = buttons;
}
if (isMouseGrabbed) {
Expand All @@ -932,12 +948,12 @@ - (bool) handleEventLocked:(NSEvent *)event
* clicks in the titlebar.
*/
if ([self screenContainsPoint:p]) {
qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, p.x, 0, screen.width);
qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
qemu_input_queue_abs(dcl.con, INPUT_AXIS_X, p.x, 0, screen.width);
qemu_input_queue_abs(dcl.con, INPUT_AXIS_Y, screen.height - p.y, 0, screen.height);
}
} else {
qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, (int)[event deltaX]);
qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, (int)[event deltaY]);
qemu_input_queue_rel(dcl.con, INPUT_AXIS_X, (int)[event deltaX]);
qemu_input_queue_rel(dcl.con, INPUT_AXIS_Y, (int)[event deltaY]);
}
} else {
return false;
Expand Down Expand Up @@ -1006,7 +1022,7 @@ - (void) raiseAllKeys
for (index = 0; index < max_index; index++) {
if (modifiers_state[index]) {
modifiers_state[index] = 0;
qemu_input_event_send_key_qcode(dcl->con, index, false);
qemu_input_event_send_key_qcode(dcl.con, index, false);
}
}
});
Expand Down Expand Up @@ -1833,19 +1849,6 @@ static void cocoa_refresh(DisplayChangeListener *dcl)
[pool release];
}

static void cocoa_cleanup(void)
{
COCOA_DEBUG("qemu_cocoa: cocoa_cleanup\n");
g_free(dcl);
}

static const DisplayChangeListenerOps dcl_ops = {
.dpy_name = "cocoa",
.dpy_gfx_update = cocoa_update,
.dpy_gfx_switch = cocoa_switch,
.dpy_refresh = cocoa_refresh,
};

static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
{
COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
Expand All @@ -1866,14 +1869,8 @@ static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
cursor_hide = 0;
}

dcl = g_malloc0(sizeof(DisplayChangeListener));

// register vga output callbacks
dcl->ops = &dcl_ops;
register_displaychangelistener(dcl);

// register cleanup function
atexit(cocoa_cleanup);
register_displaychangelistener(&dcl);
}

static QemuDisplay qemu_display_cocoa = {
Expand Down

0 comments on commit cc7859c

Please sign in to comment.