Remove QEMU-ism from UI code.

This gets rid of qemu_set_fd_handler() users in the UI
program. We can remove its implementation from vl-android-ui.c

+ Really remove qemu-timer-ui.c :-)

Change-Id: I490df8ad5d5eea36b332bf54e2b156f6b5872bf4
diff --git a/android/protocol/fb-updates-impl.c b/android/protocol/fb-updates-impl.c
index 170fb47..2f3ff71 100644
--- a/android/protocol/fb-updates-impl.c
+++ b/android/protocol/fb-updates-impl.c
@@ -15,7 +15,6 @@
  * from the core.
  */
 
-#include "sysemu.h"
 #include "android/utils/system.h"
 #include "android/utils/debug.h"
 #include "android/utils/panic.h"
@@ -60,6 +59,9 @@
     /* Socket descriptor for the framebuffer client. */
     int             sock;
 
+    /* Custom i/o handler */
+    LoopIo          io[1];
+
     /* Number of bits used to encode single pixel. */
     int             bits_per_pixel;
 } ImplFramebuffer;
@@ -103,7 +105,7 @@
  *  opaque - ImplFramebuffer instance.
  */
 static void
-_implFb_read_cb(void* opaque)
+_implFb_io_callback(void* opaque, int fd, unsigned events)
 {
     ImplFramebuffer* fb_client = opaque;
     int  ret;
@@ -165,7 +167,10 @@
 }
 
 int
-implFb_create(SockAddress* console_socket, const char* protocol, QFrameBuffer* fb)
+implFb_create(SockAddress* console_socket,
+              const char* protocol,
+              QFrameBuffer* fb,
+              Looper* looper)
 {
     char* handshake = NULL;
     char switch_cmd[256];
@@ -212,11 +217,9 @@
     _implFb.sock = core_connection_get_socket(_implFb.core_connection);
 
     // At last setup read callback, and start receiving the updates.
-    if (qemu_set_fd_handler(_implFb.sock, _implFb_read_cb, NULL, &_implFb)) {
-        derror("Unable to set up framebuffer read callback.\n");
-        implFb_destroy();
-        return -1;
-    }
+    loopIo_init(_implFb.io, looper, _implFb.sock,
+                _implFb_io_callback, &_implFb);
+    loopIo_wantRead(_implFb.io);
     {
         // Force the core to send us entire framebuffer now, when we're prepared
         // to receive it.
@@ -248,7 +251,7 @@
 {
     if (_implFb.core_connection != NULL) {
         // Disable the reader callback.
-        qemu_set_fd_handler(_implFb.sock, NULL, NULL, NULL);
+        loopIo_done(_implFb.io);
 
         // Close framebuffer connection.
         core_connection_close(_implFb.core_connection);
diff --git a/android/protocol/fb-updates-impl.h b/android/protocol/fb-updates-impl.h
index c4dd2e0..db06244 100644
--- a/android/protocol/fb-updates-impl.h
+++ b/android/protocol/fb-updates-impl.h
@@ -18,7 +18,7 @@
 #ifndef _ANDROID_FRAMEBUFFER_UI_H
 #define _ANDROID_FRAMEBUFFER_UI_H
 
-#include "console.h"
+#include "android/looper.h"
 #include "android/framebuffer.h"
 #include "android/looper.h"
 #include "android/async-utils.h"
@@ -35,7 +35,8 @@
  */
 int implFb_create(SockAddress* console_socket,
                   const char* protocol,
-                  QFrameBuffer* fb);
+                  QFrameBuffer* fb,
+                  Looper* looper);
 
 /* Disconnects and destroys framebuffer client. */
 void implFb_destroy(void);
diff --git a/android/protocol/ui-commands-impl.c b/android/protocol/ui-commands-impl.c
index 2ca4194..f07a1ad 100644
--- a/android/protocol/ui-commands-impl.c
+++ b/android/protocol/ui-commands-impl.c
@@ -16,7 +16,7 @@
  * from the Core.
  */
 
-#include "console.h"
+#include <unistd.h>
 #include "android/looper.h"
 #include "android/async-utils.h"
 #include "android/sync-utils.h"
@@ -44,6 +44,9 @@
     /* Socket descriptor for the UI service. */
     int             sock;
 
+    /* Custom i/o handler */
+    LoopIo          io[1];
+
     /* Command reader state. */
     UICmdImplState  reader_state;
 
@@ -113,7 +116,7 @@
  *  opaque - UICmdImpl instance.
  */
 static void
-_uiCmdImpl_io_read(void* opaque)
+_uiCmdImpl_io_callback(void* opaque, int fd, unsigned events)
 {
     UICmdImpl* uicmd = opaque;
     int status;
@@ -181,7 +184,7 @@
 }
 
 int
-uiCmdImpl_create(SockAddress* console_socket)
+uiCmdImpl_create(SockAddress* console_socket, Looper* looper)
 {
     char* handshake = NULL;
 
@@ -203,16 +206,10 @@
 
     // Initialize UI command reader.
     _uiCmdImpl.sock = core_connection_get_socket(_uiCmdImpl.core_connection);
-    if (qemu_set_fd_handler(_uiCmdImpl.sock, _uiCmdImpl_io_read, NULL,
-                            &_uiCmdImpl)) {
-        derror("Unable to set up UI _uiCmdImpl_io_read callback: %s\n",
-               errno_str);
-        uiCmdImpl_destroy();
-        if (handshake != NULL) {
-            free(handshake);
-        }
-        return -1;
-    }
+    loopIo_init(_uiCmdImpl.io, looper, _uiCmdImpl.sock,
+                _uiCmdImpl_io_callback,
+                &_uiCmdImpl);
+    loopIo_wantRead(_uiCmdImpl.io);
 
     fprintf(stdout, "core-ui-control is now connected to the core at %s.",
             sock_address_to_string(console_socket));
@@ -232,7 +229,7 @@
 {
     if (_uiCmdImpl.core_connection != NULL) {
         // Disable I/O callbacks.
-        qemu_set_fd_handler(_uiCmdImpl.sock, NULL, NULL, NULL);
+        loopIo_done(_uiCmdImpl.io);
         core_connection_close(_uiCmdImpl.core_connection);
         core_connection_free(_uiCmdImpl.core_connection);
         _uiCmdImpl.core_connection = NULL;
diff --git a/android/protocol/ui-commands-impl.h b/android/protocol/ui-commands-impl.h
index fa05e8e..f575bd0 100644
--- a/android/protocol/ui-commands-impl.h
+++ b/android/protocol/ui-commands-impl.h
@@ -14,6 +14,7 @@
 #define _ANDROID_PROTOCOL_UI_COMMANDS_IMPL_H
 
 #include "sockets.h"
+#include "android/looper.h"
 #include "android/protocol/ui-commands.h"
 
 /*
@@ -29,7 +30,7 @@
  * Return:
  *  0 on success, or < 0 on failure.
  */
-extern int uiCmdImpl_create(SockAddress* console_socket);
+extern int uiCmdImpl_create(SockAddress* console_socket, Looper* looper);
 
 /* Destroys UI-side of the "core-ui-control" service. */
 extern void uiCmdImpl_destroy();
diff --git a/qemu-timer-ui.c b/qemu-timer-ui.c
deleted file mode 100644
index 095036d..0000000
--- a/qemu-timer-ui.c
+++ /dev/null
@@ -1,783 +0,0 @@
-/*
- * QEMU System Emulator
- *
- * Copyright (c) 2003-2008 Fabrice Bellard
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "qemu-timer.h"
-#include "console.h"
-#include "android/utils/system.h"
-
-extern QEMUClock*  rtc_clock;
-
-#include <unistd.h>
-#include <fcntl.h>
-#include <time.h>
-#include <errno.h>
-#include <sys/time.h>
-#include <signal.h>
-#ifdef __FreeBSD__
-#include <sys/param.h>
-#endif
-
-#ifdef __linux__
-#include <sys/ioctl.h>
-#endif
-
-#ifdef _WIN32
-#include <windows.h>
-#include <mmsystem.h>
-#endif
-
-#include "qemu-timer.h"
-
-QEMUClock *rtc_clock;
-
-/***********************************************************/
-/* real time host monotonic timer */
-
-
-static int64_t get_clock_realtime(void)
-{
-    struct timeval tv;
-
-    gettimeofday(&tv, NULL);
-    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
-}
-
-#ifdef WIN32
-
-static int64_t clock_freq;
-
-static void init_get_clock(void)
-{
-    LARGE_INTEGER freq;
-    int ret;
-    ret = QueryPerformanceFrequency(&freq);
-    if (ret == 0) {
-        fprintf(stderr, "Could not calibrate ticks\n");
-        exit(1);
-    }
-    clock_freq = freq.QuadPart;
-}
-
-static int64_t get_clock(void)
-{
-    LARGE_INTEGER ti;
-    QueryPerformanceCounter(&ti);
-    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
-}
-
-#else
-
-static int use_rt_clock;
-
-static void init_get_clock(void)
-{
-    use_rt_clock = 0;
-#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
-    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
-    {
-        struct timespec ts;
-        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
-            use_rt_clock = 1;
-        }
-    }
-#endif
-}
-
-static int64_t get_clock(void)
-{
-#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
-	|| defined(__DragonFly__) || defined(__FreeBSD_kernel__)
-    if (use_rt_clock) {
-        struct timespec ts;
-        clock_gettime(CLOCK_MONOTONIC, &ts);
-        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
-    } else
-#endif
-    {
-        /* XXX: using gettimeofday leads to problems if the date
-           changes, so it should be avoided. */
-        return get_clock_realtime();
-    }
-}
-#endif
-
-/***********************************************************/
-/* guest cycle counter */
-
-typedef struct TimersState {
-    int64_t cpu_ticks_prev;
-    int64_t cpu_ticks_offset;
-    int64_t cpu_clock_offset;
-    int32_t cpu_ticks_enabled;
-    int64_t dummy;
-} TimersState;
-
-TimersState timers_state;
-
-/* return the host CPU cycle counter and handle stop/restart */
-int64_t cpu_get_ticks(void)
-{
-    if (!timers_state.cpu_ticks_enabled) {
-        return timers_state.cpu_ticks_offset;
-    } else {
-        int64_t ticks;
-        ticks = cpu_get_real_ticks();
-        if (timers_state.cpu_ticks_prev > ticks) {
-            /* Note: non increasing ticks may happen if the host uses
-               software suspend */
-            timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
-        }
-        timers_state.cpu_ticks_prev = ticks;
-        return ticks + timers_state.cpu_ticks_offset;
-    }
-}
-
-/* return the host CPU monotonic timer and handle stop/restart */
-static int64_t cpu_get_clock(void)
-{
-    int64_t ti;
-    if (!timers_state.cpu_ticks_enabled) {
-        return timers_state.cpu_clock_offset;
-    } else {
-        ti = get_clock();
-        return ti + timers_state.cpu_clock_offset;
-    }
-}
-
-/* enable cpu_get_ticks() */
-void cpu_enable_ticks(void)
-{
-    if (!timers_state.cpu_ticks_enabled) {
-        timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
-        timers_state.cpu_clock_offset -= get_clock();
-        timers_state.cpu_ticks_enabled = 1;
-    }
-}
-
-/* disable cpu_get_ticks() : the clock is stopped. You must not call
-   cpu_get_ticks() after that.  */
-void cpu_disable_ticks(void)
-{
-    if (timers_state.cpu_ticks_enabled) {
-        timers_state.cpu_ticks_offset = cpu_get_ticks();
-        timers_state.cpu_clock_offset = cpu_get_clock();
-        timers_state.cpu_ticks_enabled = 0;
-    }
-}
-
-/***********************************************************/
-/* timers */
-
-#define QEMU_CLOCK_REALTIME 0
-#define QEMU_CLOCK_VIRTUAL  1
-#define QEMU_CLOCK_HOST     2
-
-struct QEMUClock {
-    int type;
-    int enabled;
-    /* XXX: add frequency */
-};
-
-struct QEMUTimer {
-    QEMUClock *clock;
-    int64_t expire_time;
-    QEMUTimerCB *cb;
-    void *opaque;
-    struct QEMUTimer *next;
-};
-
-struct qemu_alarm_timer {
-    char const *name;
-    int (*start)(struct qemu_alarm_timer *t);
-    void (*stop)(struct qemu_alarm_timer *t);
-    void (*rearm)(struct qemu_alarm_timer *t);
-    void *priv;
-
-    char expired;
-    char pending;
-};
-
-static struct qemu_alarm_timer *alarm_timer;
-
-int qemu_alarm_pending(void)
-{
-    return alarm_timer->pending;
-}
-
-static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
-{
-    return !!t->rearm;
-}
-
-static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
-{
-    if (!alarm_has_dynticks(t))
-        return;
-
-    t->rearm(t);
-}
-
-/* TODO: MIN_TIMER_REARM_US should be optimized */
-#define MIN_TIMER_REARM_US 250
-
-#ifdef _WIN32
-
-struct qemu_alarm_win32 {
-    MMRESULT timerId;
-    unsigned int period;
-} alarm_win32_data = {0, 0};
-
-static int win32_start_timer(struct qemu_alarm_timer *t);
-static void win32_stop_timer(struct qemu_alarm_timer *t);
-static void win32_rearm_timer(struct qemu_alarm_timer *t);
-
-#else
-
-static int unix_start_timer(struct qemu_alarm_timer *t);
-static void unix_stop_timer(struct qemu_alarm_timer *t);
-
-#ifdef __linux__
-
-static int dynticks_start_timer(struct qemu_alarm_timer *t);
-static void dynticks_stop_timer(struct qemu_alarm_timer *t);
-static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
-
-#endif /* __linux__ */
-
-#endif /* _WIN32 */
-
-static struct qemu_alarm_timer alarm_timers[] = {
-#ifndef _WIN32
-#ifdef __linux__
-    {"dynticks", dynticks_start_timer,
-     dynticks_stop_timer, dynticks_rearm_timer, NULL},
-#endif
-    {"unix", unix_start_timer, unix_stop_timer, NULL, NULL},
-#else
-    {"dynticks", win32_start_timer,
-     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
-    {"win32", win32_start_timer,
-     win32_stop_timer, NULL, &alarm_win32_data},
-#endif
-    {NULL, }
-};
-
-#define QEMU_NUM_CLOCKS 3
-
-QEMUClock *rt_clock;
-QEMUClock *vm_clock;
-QEMUClock *host_clock;
-
-static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
-
-static QEMUClock *qemu_new_clock(int type)
-{
-    QEMUClock *clock;
-    ANEW0(clock);
-    clock->type = type;
-    clock->enabled = 1;
-    return clock;
-}
-
-QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
-{
-    QEMUTimer *ts;
-
-    ANEW0(ts);
-    ts->clock = clock;
-    ts->cb = cb;
-    ts->opaque = opaque;
-    return ts;
-}
-
-void qemu_free_timer(QEMUTimer *ts)
-{
-    AFREE(ts);
-}
-
-/* stop a timer, but do not dealloc it */
-void qemu_del_timer(QEMUTimer *ts)
-{
-    QEMUTimer **pt, *t;
-
-    /* NOTE: this code must be signal safe because
-       qemu_timer_expired() can be called from a signal. */
-    pt = &active_timers[ts->clock->type];
-    for(;;) {
-        t = *pt;
-        if (!t)
-            break;
-        if (t == ts) {
-            *pt = t->next;
-            break;
-        }
-        pt = &t->next;
-    }
-}
-
-/* modify the current timer so that it will be fired when current_time
-   >= expire_time. The corresponding callback will be called. */
-void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
-{
-    QEMUTimer **pt, *t;
-
-    qemu_del_timer(ts);
-
-    /* add the timer in the sorted list */
-    /* NOTE: this code must be signal safe because
-       qemu_timer_expired() can be called from a signal. */
-    pt = &active_timers[ts->clock->type];
-    for(;;) {
-        t = *pt;
-        if (!t)
-            break;
-        if (t->expire_time > expire_time)
-            break;
-        pt = &t->next;
-    }
-    ts->expire_time = expire_time;
-    ts->next = *pt;
-    *pt = ts;
-
-    /* Rearm if necessary  */
-    if (pt == &active_timers[ts->clock->type]) {
-        if (!alarm_timer->pending) {
-            qemu_rearm_alarm_timer(alarm_timer);
-        }
-    }
-}
-
-int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
-{
-    if (!timer_head)
-        return 0;
-    return (timer_head->expire_time <= current_time);
-}
-
-static void qemu_run_timers(QEMUClock *clock)
-{
-    QEMUTimer **ptimer_head, *ts;
-    int64_t current_time;
-
-    if (!clock->enabled)
-        return;
-
-    current_time = qemu_get_clock (clock);
-    ptimer_head = &active_timers[clock->type];
-    for(;;) {
-        ts = *ptimer_head;
-        if (!ts || ts->expire_time > current_time)
-            break;
-        /* remove timer from the list before calling the callback */
-        *ptimer_head = ts->next;
-        ts->next = NULL;
-
-        /* run the callback (the timer list can be modified) */
-        ts->cb(ts->opaque);
-    }
-}
-
-int64_t qemu_get_clock(QEMUClock *clock)
-{
-    switch(clock->type) {
-    case QEMU_CLOCK_REALTIME:
-        return get_clock() / 1000000;
-    default:
-    case QEMU_CLOCK_VIRTUAL:
-        return cpu_get_clock();
-    case QEMU_CLOCK_HOST:
-        return get_clock_realtime();
-    }
-}
-
-int64_t qemu_get_clock_ns(QEMUClock *clock)
-{
-    switch(clock->type) {
-    case QEMU_CLOCK_REALTIME:
-        return get_clock();
-    default:
-    case QEMU_CLOCK_VIRTUAL:
-        return cpu_get_clock();
-    case QEMU_CLOCK_HOST:
-        return get_clock_realtime();
-    }
-}
-
-void init_clocks(void)
-{
-    init_get_clock();
-    rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
-    vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
-    host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
-
-    rtc_clock = host_clock;
-}
-
-void qemu_run_all_timers(void)
-{
-    alarm_timer->pending = 0;
-
-    /* rearm timer, if not periodic */
-    if (alarm_timer->expired) {
-        alarm_timer->expired = 0;
-        qemu_rearm_alarm_timer(alarm_timer);
-    }
-
-    /* vm time timers */
-    qemu_run_timers(vm_clock);
-
-    qemu_run_timers(rt_clock);
-    qemu_run_timers(host_clock);
-}
-
-#ifdef _WIN32
-static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
-                                        DWORD_PTR dwUser, DWORD_PTR dw1,
-                                        DWORD_PTR dw2)
-#else
-static void host_alarm_handler(int host_signum)
-#endif
-{
-    struct qemu_alarm_timer *t = alarm_timer;
-    if (!t)
-	return;
-
-#if 0
-#define DISP_FREQ 1000
-    {
-        static int64_t delta_min = INT64_MAX;
-        static int64_t delta_max, delta_cum, last_clock, delta, ti;
-        static int count;
-        ti = qemu_get_clock(vm_clock);
-        if (last_clock != 0) {
-            delta = ti - last_clock;
-            if (delta < delta_min)
-                delta_min = delta;
-            if (delta > delta_max)
-                delta_max = delta;
-            delta_cum += delta;
-            if (++count == DISP_FREQ) {
-                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
-                       muldiv64(delta_min, 1000000, get_ticks_per_sec()),
-                       muldiv64(delta_max, 1000000, get_ticks_per_sec()),
-                       muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
-                       (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
-                count = 0;
-                delta_min = INT64_MAX;
-                delta_max = 0;
-                delta_cum = 0;
-            }
-        }
-        last_clock = ti;
-    }
-#endif
-    if (alarm_has_dynticks(t) ||
-        (qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
-                               qemu_get_clock(vm_clock))) ||
-        qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
-                           qemu_get_clock(rt_clock)) ||
-        qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
-                           qemu_get_clock(host_clock))) {
-
-        t->expired = alarm_has_dynticks(t);
-        t->pending = 1;
-    }
-}
-
-int64_t qemu_next_deadline(void)
-{
-    /* To avoid problems with overflow limit this to 2^32.  */
-    int64_t delta = INT32_MAX;
-
-    if (active_timers[QEMU_CLOCK_VIRTUAL]) {
-        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
-                     qemu_get_clock(vm_clock);
-    }
-    if (active_timers[QEMU_CLOCK_HOST]) {
-        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
-                 qemu_get_clock(host_clock);
-        if (hdelta < delta)
-            delta = hdelta;
-    }
-
-    if (delta < 0)
-        delta = 0;
-
-    return delta;
-}
-
-#ifndef _WIN32
-
-#if defined(__linux__)
-
-static uint64_t qemu_next_deadline_dyntick(void)
-{
-    int64_t delta;
-    int64_t rtdelta;
-
-    delta = (qemu_next_deadline() + 999) / 1000;
-
-    if (active_timers[QEMU_CLOCK_REALTIME]) {
-        rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
-                 qemu_get_clock(rt_clock))*1000;
-        if (rtdelta < delta)
-            delta = rtdelta;
-    }
-
-    if (delta < MIN_TIMER_REARM_US)
-        delta = MIN_TIMER_REARM_US;
-
-    return delta;
-}
-
-static int dynticks_start_timer(struct qemu_alarm_timer *t)
-{
-    struct sigevent ev;
-    timer_t host_timer;
-    struct sigaction act;
-
-    sigfillset(&act.sa_mask);
-    act.sa_flags = 0;
-    act.sa_handler = host_alarm_handler;
-
-    sigaction(SIGALRM, &act, NULL);
-
-    /*
-     * Initialize ev struct to 0 to avoid valgrind complaining
-     * about uninitialized data in timer_create call
-     */
-    memset(&ev, 0, sizeof(ev));
-    ev.sigev_value.sival_int = 0;
-    ev.sigev_notify = SIGEV_SIGNAL;
-    ev.sigev_signo = SIGALRM;
-
-    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
-        perror("timer_create");
-
-        /* disable dynticks */
-        fprintf(stderr, "Dynamic Ticks disabled\n");
-
-        return -1;
-    }
-
-    t->priv = (void *)(long)host_timer;
-
-    return 0;
-}
-
-static void dynticks_stop_timer(struct qemu_alarm_timer *t)
-{
-    timer_t host_timer = (timer_t)(long)t->priv;
-
-    timer_delete(host_timer);
-}
-
-static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
-{
-    timer_t host_timer = (timer_t)(long)t->priv;
-    struct itimerspec timeout;
-    int64_t nearest_delta_us = INT64_MAX;
-    int64_t current_us;
-
-    assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME] &&
-        !active_timers[QEMU_CLOCK_VIRTUAL] &&
-        !active_timers[QEMU_CLOCK_HOST])
-        return;
-
-    nearest_delta_us = qemu_next_deadline_dyntick();
-
-    /* check whether a timer is already running */
-    if (timer_gettime(host_timer, &timeout)) {
-        perror("gettime");
-        fprintf(stderr, "Internal timer error: aborting\n");
-        exit(1);
-    }
-    current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
-    if (current_us && current_us <= nearest_delta_us)
-        return;
-
-    timeout.it_interval.tv_sec = 0;
-    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
-    timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
-    timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
-    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
-        perror("settime");
-        fprintf(stderr, "Internal timer error: aborting\n");
-        exit(1);
-    }
-}
-
-#endif /* defined(__linux__) */
-
-static int unix_start_timer(struct qemu_alarm_timer *t)
-{
-    struct sigaction act;
-    struct itimerval itv;
-    int err;
-
-    /* timer signal */
-    sigfillset(&act.sa_mask);
-    act.sa_flags = 0;
-    act.sa_handler = host_alarm_handler;
-
-    sigaction(SIGALRM, &act, NULL);
-
-    itv.it_interval.tv_sec = 0;
-    /* for i386 kernel 2.6 to get 1 ms */
-    itv.it_interval.tv_usec = 999;
-    itv.it_value.tv_sec = 0;
-    itv.it_value.tv_usec = 10 * 1000;
-
-    err = setitimer(ITIMER_REAL, &itv, NULL);
-    if (err)
-        return -1;
-
-    return 0;
-}
-
-static void unix_stop_timer(struct qemu_alarm_timer *t)
-{
-    struct itimerval itv;
-
-    memset(&itv, 0, sizeof(itv));
-    setitimer(ITIMER_REAL, &itv, NULL);
-}
-
-#endif /* !defined(_WIN32) */
-
-
-#ifdef _WIN32
-
-static int win32_start_timer(struct qemu_alarm_timer *t)
-{
-    TIMECAPS tc;
-    struct qemu_alarm_win32 *data = t->priv;
-    UINT flags;
-
-    memset(&tc, 0, sizeof(tc));
-    timeGetDevCaps(&tc, sizeof(tc));
-
-    data->period = tc.wPeriodMin;
-    timeBeginPeriod(data->period);
-
-    flags = TIME_CALLBACK_FUNCTION;
-    if (alarm_has_dynticks(t))
-        flags |= TIME_ONESHOT;
-    else
-        flags |= TIME_PERIODIC;
-
-    data->timerId = timeSetEvent(1,         // interval (ms)
-                        data->period,       // resolution
-                        host_alarm_handler, // function
-                        (DWORD)t,           // parameter
-                        flags);
-
-    if (!data->timerId) {
-        fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
-                GetLastError());
-        timeEndPeriod(data->period);
-        return -1;
-    }
-
-    return 0;
-}
-
-static void win32_stop_timer(struct qemu_alarm_timer *t)
-{
-    struct qemu_alarm_win32 *data = t->priv;
-
-    timeKillEvent(data->timerId);
-    timeEndPeriod(data->period);
-}
-
-static void win32_rearm_timer(struct qemu_alarm_timer *t)
-{
-    struct qemu_alarm_win32 *data = t->priv;
-
-    assert(alarm_has_dynticks(t));
-    if (!active_timers[QEMU_CLOCK_REALTIME] &&
-        !active_timers[QEMU_CLOCK_VIRTUAL] &&
-        !active_timers[QEMU_CLOCK_HOST])
-        return;
-
-    timeKillEvent(data->timerId);
-
-    data->timerId = timeSetEvent(1,
-                        data->period,
-                        host_alarm_handler,
-                        (DWORD)t,
-                        TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
-
-    if (!data->timerId) {
-        fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
-                GetLastError());
-
-        timeEndPeriod(data->period);
-        exit(1);
-    }
-}
-
-#endif /* _WIN32 */
-
-int init_timer_alarm(void)
-{
-    struct qemu_alarm_timer *t = NULL;
-    int i, err = -1;
-
-    for (i = 0; alarm_timers[i].name; i++) {
-        t = &alarm_timers[i];
-
-        err = t->start(t);
-        if (!err)
-            break;
-    }
-
-    if (err) {
-        err = -ENOENT;
-        goto fail;
-    }
-
-    /* first event is at time 0 */
-    t->pending = 1;
-    alarm_timer = t;
-
-    return 0;
-
-fail:
-    return err;
-}
-
-void quit_timers(void)
-{
-    struct qemu_alarm_timer *t = alarm_timer;
-    alarm_timer = NULL;
-    t->stop(t);
-}
-
-int qemu_calculate_timeout(void)
-{
-	/* Deliver user events at 30 Hz */
-	return 1000/30;
-}
diff --git a/vl-android-ui.c b/vl-android-ui.c
index 91a7764..b0a4e7f 100644
--- a/vl-android-ui.c
+++ b/vl-android-ui.c
@@ -54,90 +54,6 @@
 static Looper*  mainLooper;
 
 /***********************************************************/
-/* I/O handling */
-
-typedef struct IOHandlerRecord {
-    LoopIo  io[1];
-    IOHandler* fd_read;
-    IOHandler* fd_write;
-    int        running;
-    int        deleted;
-    void*      opaque;
-    struct IOHandlerRecord *next;
-} IOHandlerRecord;
-
-static IOHandlerRecord *first_io_handler;
-
-static void ioh_callback(void* opaque, int fd, unsigned events)
-{
-    IOHandlerRecord* ioh = opaque;
-    ioh->running = 1;
-    if ((events & LOOP_IO_READ) != 0) {
-        ioh->fd_read(ioh->opaque);
-    }
-    if (!ioh->deleted && (events & LOOP_IO_WRITE) != 0) {
-        ioh->fd_write(ioh->opaque);
-    }
-    ioh->running = 0;
-    if (ioh->deleted) {
-        loopIo_done(ioh->io);
-        free(ioh);
-    }
-}
-
-int qemu_set_fd_handler(int fd,
-                        IOHandler *fd_read,
-                        IOHandler *fd_write,
-                        void *opaque)
-{
-    IOHandlerRecord **pioh, *ioh;
-
-    if (!fd_read && !fd_write) {
-        pioh = &first_io_handler;
-        for(;;) {
-            ioh = *pioh;
-            if (ioh == NULL)
-                return 0;
-            if (ioh->io->fd == fd) {
-                break;
-            }
-            pioh = &ioh->next;
-        }
-        if (ioh->running) {
-            ioh->deleted = 1;
-        } else {
-            *pioh = ioh->next;
-            loopIo_done(ioh->io);
-            free(ioh);
-        }
-    } else {
-        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
-            if (ioh->io->fd == fd)
-                goto found;
-        }
-        ANEW0(ioh);
-        ioh->next = first_io_handler;
-        first_io_handler = ioh;
-        loopIo_init(ioh->io, mainLooper, fd, ioh_callback, ioh);
-    found:
-        ioh->fd_read  = fd_read;
-        ioh->fd_write = fd_write;
-        ioh->opaque   = opaque;
-
-        if (fd_read != NULL)
-            loopIo_wantRead(ioh->io);
-        else
-            loopIo_dontWantRead(ioh->io);
-
-        if (fd_write != NULL)
-            loopIo_wantWrite(ioh->io);
-        else
-            loopIo_dontWantWrite(ioh->io);
-    }
-    return 0;
-}
-
-/***********************************************************/
 /* main execution loop */
 
 static LoopTimer  gui_timer[1];
@@ -247,12 +163,13 @@
 
     // Connect to the core's framebuffer service
     if (implFb_create(attachUiImpl_get_console_socket(), "-raw",
-                      qemulator_get_first_framebuffer(qemulator_get()))) {
+                      qemulator_get_first_framebuffer(qemulator_get()),
+                      mainLooper)) {
         return -1;
     }
 
     // Attach the recepient of UI commands.
-    if (uiCmdImpl_create(attachUiImpl_get_console_socket())) {
+    if (uiCmdImpl_create(attachUiImpl_get_console_socket(), mainLooper)) {
         return -1;
     }