main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <assert.h>
  8. #include <sys/ioctl.h>
  9. #include <sys/select.h>
  10. #include <LibC/gui.h>
  11. #include "Terminal.h"
  12. #include <Kernel/KeyCode.h>
  13. #include <LibGUI/GApplication.h>
  14. #include <LibGUI/GWidget.h>
  15. #include <LibGUI/GWindow.h>
  16. #include <LibGUI/GMenuBar.h>
  17. static void make_shell(int ptm_fd)
  18. {
  19. pid_t pid = fork();
  20. if (pid == 0) {
  21. const char* tty_name = ptsname(ptm_fd);
  22. if (!tty_name) {
  23. perror("ptsname");
  24. exit(1);
  25. }
  26. int rc = 0;
  27. close(ptm_fd);
  28. int pts_fd = open(tty_name, O_RDWR);
  29. rc = ioctl(0, TIOCNOTTY);
  30. if (rc < 0) {
  31. perror("ioctl(TIOCNOTTY)");
  32. exit(1);
  33. }
  34. close(0);
  35. close(1);
  36. close(2);
  37. dup2(pts_fd, 0);
  38. dup2(pts_fd, 1);
  39. dup2(pts_fd, 2);
  40. close(pts_fd);
  41. rc = ioctl(0, TIOCSCTTY);
  42. if (rc < 0) {
  43. perror("ioctl(TIOCSCTTY)");
  44. exit(1);
  45. }
  46. rc = execvp("/bin/sh", nullptr);
  47. if (rc < 0) {
  48. perror("execve");
  49. exit(1);
  50. }
  51. ASSERT_NOT_REACHED();
  52. }
  53. }
  54. int main(int argc, char** argv)
  55. {
  56. GApplication app(argc, argv);
  57. int ptm_fd = open("/dev/ptmx", O_RDWR);
  58. if (ptm_fd < 0) {
  59. perror("open(ptmx)");
  60. return 1;
  61. }
  62. make_shell(ptm_fd);
  63. auto* window = new GWindow;
  64. window->set_should_exit_app_on_close(true);
  65. Terminal terminal(ptm_fd);
  66. window->set_main_widget(&terminal);
  67. window->move_to(300, 300);
  68. window->show();
  69. auto menubar = make<GMenuBar>();
  70. auto app_menu = make<GMenu>("Terminal");
  71. app_menu->add_item(1, "Quit");
  72. menubar->add_menu(move(app_menu));
  73. auto font_menu = make<GMenu>("Font");
  74. font_menu->add_item(30, "Liza Thin");
  75. font_menu->add_item(31, "Liza Regular");
  76. font_menu->add_item(32, "Liza Bold");
  77. font_menu->on_item_activation = [&terminal] (unsigned identifier) {
  78. switch (identifier) {
  79. case 30:
  80. terminal.set_font(Font::load_from_file("/res/fonts/Liza8x10.font"));
  81. break;
  82. case 31:
  83. terminal.set_font(Font::load_from_file("/res/fonts/LizaRegular8x10.font"));
  84. break;
  85. case 32:
  86. terminal.set_font(Font::load_from_file("/res/fonts/LizaBold8x10.font"));
  87. break;
  88. }
  89. terminal.force_repaint();
  90. };
  91. menubar->add_menu(move(font_menu));
  92. auto help_menu = make<GMenu>("Help");
  93. help_menu->add_item(2, "About");
  94. menubar->add_menu(move(help_menu));
  95. app.set_menubar(move(menubar));
  96. return app.exec();
  97. }