mouse: wrap device button mapping into gdk_error_trap_push/pop

The device may have disappeared by the time we get here, so make sure we catch
BadDevice errors as well by moving the error trap push up before the first X
call.
mouse: wrap pointer acceleration changes into a gdk_error_trap
common: provide a helper function to close an XDevice safely

XCloseDevice may cause a BadDevice error if the device disappeared before we
close it.