toggle-abs-mouse.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * toggle-abs-mouse - Toggle mouse modes
  7. *
  8. * Set the mouse mode under VirtualBox, VMware, or QEMU to either
  9. * relative or absolute via ioctl to the relevant absolute mouse
  10. * device driver interface.
  11. */
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <sys/ioctl.h>
  17. int main(int argc, char * argv[]) {
  18. if (argc < 2) {
  19. fprintf(stderr, "%s: argument (relative, absolute, get) expected\n", argv[0]);
  20. return 1;
  21. }
  22. int fd = open("/dev/absmouse",O_WRONLY);
  23. if (fd < 0) {
  24. /* try vmmouse */
  25. fd = open("/dev/vmmouse",O_WRONLY);
  26. if (fd < 0) {
  27. fprintf(stderr, "%s: no valid mouse interface found.\n", argv[0]);
  28. return 1;
  29. }
  30. }
  31. int flag = 0;
  32. if (!strcmp(argv[1],"relative")) {
  33. flag = 1;
  34. }
  35. if (!strcmp(argv[1],"absolute")) {
  36. flag = 2;
  37. }
  38. if (!strcmp(argv[1],"get")) {
  39. flag = 3;
  40. }
  41. if (!flag) {
  42. fprintf(stderr, "%s: invalid argument\n", argv[0]);
  43. return 1;
  44. }
  45. int result = ioctl(fd, flag, NULL);
  46. if (flag == 3) {
  47. if (result == 0) {
  48. fprintf(stdout, "relative\n");
  49. } else {
  50. fprintf(stdout, "absolute\n");
  51. }
  52. return 0;
  53. }
  54. return result;
  55. }