[WIP] Refactor input buffer-related code

Some cleanup of input buffer code(moved from ui.c to os/input.c). Still needs some tests