mssh-terminal.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, 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_sync(VTE_TERMINAL(terminal),
  57. VTE_PTY_NO_LASTLOG|VTE_PTY_NO_UTMP|VTE_PTY_NO_WTMP,
  58. NULL, /* working dir */
  59. args,
  60. env,
  61. G_SPAWN_SEARCH_PATH,
  62. NULL, /* child_setup */
  63. NULL, /* child_setup_data */
  64. NULL, /* *child_pid */
  65. NULL, /* *cancellable */
  66. NULL); /* Error handling */
  67. free(args[0]);
  68. }
  69. void mssh_terminal_send_host(MSSHTerminal *terminal)
  70. {
  71. if(mssh_terminal_isactive(terminal))
  72. {
  73. vte_terminal_feed_child(VTE_TERMINAL(terminal),
  74. terminal->hostname, strlen(terminal->hostname));
  75. }
  76. }
  77. void mssh_terminal_send_string(MSSHTerminal *terminal, gchar *string)
  78. {
  79. if(mssh_terminal_isactive(terminal))
  80. {
  81. vte_terminal_feed_child(VTE_TERMINAL(terminal), string,
  82. strlen(string));
  83. }
  84. }
  85. void mssh_terminal_send_data(MSSHTerminal *terminal, GdkEventKey *event)
  86. {
  87. gboolean dummy;
  88. if(mssh_terminal_isactive(terminal))
  89. {
  90. g_signal_emit_by_name(terminal, "key-press-event", event, &dummy);
  91. }
  92. }
  93. static void mssh_terminal_init(MSSHTerminal* terminal)
  94. {
  95. terminal->started = 0;
  96. terminal->ended = 0;
  97. g_signal_connect(G_OBJECT(terminal), "child-exited",
  98. G_CALLBACK(mssh_terminal_child_exited), terminal);
  99. g_signal_connect(G_OBJECT(terminal), "focus-in-event",
  100. G_CALLBACK(mssh_terminal_focused), terminal);
  101. }
  102. static void mssh_terminal_class_init(MSSHTerminalClass *klass)
  103. {
  104. klass->session_closed_signal = g_signal_new("session-closed",
  105. G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
  106. G_STRUCT_OFFSET(MSSHTerminalClass, session_closed), NULL, NULL,
  107. g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
  108. klass->session_focused_signal = g_signal_new("session-focused",
  109. G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
  110. G_STRUCT_OFFSET(MSSHTerminalClass, session_focused), NULL, NULL,
  111. g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
  112. }
  113. static void mssh_terminal_child_exited(VteTerminal *vte, gpointer data)
  114. {
  115. char msg[] = "\r\n[Child Exited]\r\n";
  116. MSSHTerminal *terminal = MSSH_TERMINAL(data);
  117. terminal->ended = 1;
  118. vte_terminal_feed(vte, msg, strlen(msg));
  119. g_signal_emit_by_name(terminal, "session-closed");
  120. }
  121. static gboolean mssh_terminal_focused(GtkWidget *widget,
  122. GtkDirectionType dir, gpointer data)
  123. {
  124. MSSHTerminal *terminal = MSSH_TERMINAL(data);
  125. g_signal_emit_by_name(terminal, "session-focused");
  126. return FALSE;
  127. }