mssh-terminal.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include "mssh-terminal.h"
  4. G_DEFINE_TYPE(MSSHTerminal, mssh_terminal, VTE_TYPE_TERMINAL)
  5. static void mssh_terminal_init(MSSHTerminal* terminal);
  6. static void mssh_terminal_class_init(MSSHTerminalClass *klass);
  7. static void mssh_terminal_child_exited(VteTerminal *vte, int pid, gpointer data);
  8. static gboolean mssh_terminal_focused(GtkWidget *widget,
  9. GtkDirectionType dir, gpointer data);
  10. GtkWidget* mssh_terminal_new()
  11. {
  12. return g_object_new(MSSH_TYPE_TERMINAL, NULL);
  13. }
  14. void mssh_terminal_destroy(MSSHTerminal *terminal)
  15. {
  16. free(terminal->hostname);
  17. }
  18. gboolean mssh_terminal_isactive(MSSHTerminal *terminal)
  19. {
  20. return gtk_check_menu_item_get_active(
  21. GTK_CHECK_MENU_ITEM(terminal->menu_item));
  22. }
  23. void mssh_terminal_set_backscroll_size(MSSHTerminal *terminal, gint *backscroll_buffer_size)
  24. {
  25. /* use scrollback buffer from gconf*/
  26. vte_terminal_set_scrollback_lines(VTE_TERMINAL(terminal), *backscroll_buffer_size);
  27. }
  28. void mssh_terminal_init_session(MSSHTerminal *terminal, char *hostname)
  29. {
  30. terminal->hostname = hostname;
  31. /* use scrollback buffer from gconf*/
  32. mssh_terminal_set_backscroll_size(terminal, &terminal->backscroll_buffer_size);
  33. terminal->menu_item = gtk_check_menu_item_new_with_label(
  34. terminal->hostname);
  35. gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
  36. terminal->menu_item), TRUE);
  37. }
  38. void mssh_terminal_start_session(MSSHTerminal *terminal, char **env)
  39. {
  40. char *args[5];
  41. char *fullhost;
  42. char *host = NULL;
  43. char *port = NULL;
  44. fullhost = strdup(terminal->hostname);
  45. host = strtok(fullhost, ":");
  46. port = strtok(NULL, "");
  47. args[0] = strdup("ssh");
  48. args[1] = host;
  49. if (!port)
  50. args[2] = NULL;
  51. else {
  52. args[2] = strdup("-p");
  53. args[3] = port;
  54. args[4] = NULL;
  55. }
  56. vte_terminal_spawn_async(VTE_TERMINAL(terminal), /* terminal */
  57. VTE_PTY_NO_LASTLOG|VTE_PTY_NO_UTMP|VTE_PTY_NO_WTMP, /* pty_flags */
  58. NULL, /* working dir */
  59. args,
  60. env,
  61. G_SPAWN_SEARCH_PATH, /* spawn flags */
  62. NULL, /* child_setup */
  63. NULL, /* child_setup_data */
  64. NULL, /* child setup data destroy */
  65. 1000, /* timeout */
  66. NULL, /* *cancellable */
  67. NULL, /* callback */
  68. NULL); /* user data for callback */
  69. free(args[0]);
  70. }
  71. void mssh_terminal_send_host(MSSHTerminal *terminal)
  72. {
  73. if(mssh_terminal_isactive(terminal))
  74. {
  75. vte_terminal_feed_child(VTE_TERMINAL(terminal),
  76. terminal->hostname, strlen(terminal->hostname));
  77. }
  78. }
  79. void mssh_terminal_send_string(MSSHTerminal *terminal, gchar *string)
  80. {
  81. if(mssh_terminal_isactive(terminal))
  82. {
  83. vte_terminal_feed_child(VTE_TERMINAL(terminal), string,
  84. strlen(string));
  85. }
  86. }
  87. void mssh_terminal_send_data(MSSHTerminal *terminal, GdkEventKey *event)
  88. {
  89. gboolean dummy;
  90. if(mssh_terminal_isactive(terminal))
  91. {
  92. g_signal_emit_by_name(terminal, "key-press-event", event, &dummy);
  93. }
  94. }
  95. static void mssh_terminal_init(MSSHTerminal* terminal)
  96. {
  97. terminal->started = 0;
  98. terminal->ended = 0;
  99. g_signal_connect(G_OBJECT(terminal), "child-exited",
  100. G_CALLBACK(mssh_terminal_child_exited), terminal);
  101. g_signal_connect(G_OBJECT(terminal), "focus-in-event",
  102. G_CALLBACK(mssh_terminal_focused), terminal);
  103. }
  104. static void mssh_terminal_class_init(MSSHTerminalClass *klass)
  105. {
  106. klass->session_closed_signal = g_signal_new("session-closed",
  107. G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
  108. G_STRUCT_OFFSET(MSSHTerminalClass, session_closed), NULL, NULL,
  109. g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
  110. klass->session_focused_signal = g_signal_new("session-focused",
  111. G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
  112. G_STRUCT_OFFSET(MSSHTerminalClass, session_focused), NULL, NULL,
  113. g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
  114. }
  115. static void mssh_terminal_child_exited(VteTerminal *vte, int pid, gpointer data)
  116. {
  117. char msg[] = "\r\n[Child Exited]\r\n";
  118. MSSHTerminal *terminal = MSSH_TERMINAL(data);
  119. terminal->ended = 1;
  120. vte_terminal_feed(vte, msg, strlen(msg));
  121. g_signal_emit_by_name(terminal, "session-closed");
  122. }
  123. static gboolean mssh_terminal_focused(GtkWidget *widget,
  124. GtkDirectionType dir, gpointer data)
  125. {
  126. MSSHTerminal *terminal = MSSH_TERMINAL(data);
  127. g_signal_emit_by_name(terminal, "session-focused");
  128. return FALSE;
  129. }