qemu-display-hack.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * qemu-display-hack - Manage display size under QEMU
  7. *
  8. * Communicates with a harness on the host running QEMU to
  9. * automatically update the display resolution when the
  10. * QEMU window size changes, similar to how VirtualBox's
  11. * display size changing works.
  12. *
  13. * This is automatically run at startup if the harness is
  14. * detected by the 90_qemu_hack.sh startup script.
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include <sys/ioctl.h>
  23. #include <kernel/video.h>
  24. int main(int argc, char * argv[]) {
  25. if (system("qemu-fwcfg -q opt/org.toaruos.displayharness") != 0) {
  26. fprintf(stderr, "%s: display harness not enabled\n", argv[0]);
  27. return 1;
  28. }
  29. int fd = open("/dev/fb0", O_RDONLY);
  30. if (fd < 0) {
  31. fprintf(stderr, "%s: failed to open framebuffer: %s\n", argv[0], strerror(errno));
  32. return 1;
  33. }
  34. struct vid_size s;
  35. FILE * f = fopen("/dev/ttyS1","r+");
  36. if (!f) {
  37. fprintf(stderr, "%s: failed to open serial: %s\n", argv[0], strerror(errno));
  38. return 1;
  39. }
  40. if (!fork()) {
  41. while (!feof(f)) {
  42. char data[128];
  43. fgets(data, 128, f);
  44. char * linefeed = strstr(data,"\n");
  45. if (linefeed) { *linefeed = '\0'; }
  46. char * width;
  47. char * height;
  48. width = strstr(data, " ");
  49. if (width) {
  50. *width = '\0';
  51. width++;
  52. } else {
  53. continue; /* bad line */
  54. }
  55. height = strstr(width, " ");
  56. if (height) {
  57. *height = '\0';
  58. height++;
  59. } else {
  60. continue; /* bad line */
  61. }
  62. s.width = atoi(width);
  63. s.height = atoi(height);
  64. ioctl(fd, IO_VID_SET, &s);
  65. fprintf(f, "X");
  66. fflush(f);
  67. }
  68. return 0;
  69. }
  70. return 0;
  71. }