mssh-terminal.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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[3];
  41. args[0] = strdup("ssh");
  42. args[1] = terminal->hostname;
  43. args[2] = NULL;
  44. vte_terminal_fork_command_full( VTE_TERMINAL(terminal),
  45. VTE_PTY_DEFAULT,
  46. NULL, /* working dir */
  47. args,
  48. env,
  49. G_SPAWN_SEARCH_PATH,
  50. NULL, /* child_setup */
  51. NULL, /* child_setup_data */
  52. NULL, /* *child_pid */
  53. NULL); /* Error handling */
  54. free(args[0]);
  55. }
  56. void mssh_terminal_send_host(MSSHTerminal *terminal)
  57. {
  58. if(mssh_terminal_isactive(terminal))
  59. {
  60. vte_terminal_feed_child(VTE_TERMINAL(terminal),
  61. terminal->hostname, strlen(terminal->hostname));
  62. }
  63. }
  64. void mssh_terminal_send_string(MSSHTerminal *terminal, gchar *string)
  65. {
  66. if(mssh_terminal_isactive(terminal))
  67. {
  68. vte_terminal_feed_child(VTE_TERMINAL(terminal), string,
  69. strlen(string));
  70. }
  71. }
  72. void mssh_terminal_send_data(MSSHTerminal *terminal, GdkEventKey *event)
  73. {
  74. gboolean dummy;
  75. if(mssh_terminal_isactive(terminal))
  76. {
  77. g_signal_emit_by_name(terminal, "key-press-event", event, &dummy);
  78. }
  79. }
  80. static void mssh_terminal_init(MSSHTerminal* terminal)
  81. {
  82. terminal->started = 0;
  83. terminal->ended = 0;
  84. vte_terminal_set_word_chars(VTE_TERMINAL(terminal),
  85. "-A-Za-z0-9,./?%&#:_=+@~");
  86. g_signal_connect(G_OBJECT(terminal), "child-exited",
  87. G_CALLBACK(mssh_terminal_child_exited), terminal);
  88. g_signal_connect(G_OBJECT(terminal), "focus-in-event",
  89. G_CALLBACK(mssh_terminal_focused), terminal);
  90. }
  91. static void mssh_terminal_class_init(MSSHTerminalClass *klass)
  92. {
  93. klass->session_closed_signal = g_signal_new("session-closed",
  94. G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
  95. G_STRUCT_OFFSET(MSSHTerminalClass, session_closed), NULL, NULL,
  96. g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
  97. klass->session_focused_signal = g_signal_new("session-focused",
  98. G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
  99. G_STRUCT_OFFSET(MSSHTerminalClass, session_focused), NULL, NULL,
  100. g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
  101. }
  102. static void mssh_terminal_child_exited(VteTerminal *vte, gpointer data)
  103. {
  104. char msg[] = "\r\n[Child Exited]\r\n";
  105. MSSHTerminal *terminal = MSSH_TERMINAL(data);
  106. terminal->ended = 1;
  107. vte_terminal_feed(vte, msg, strlen(msg));
  108. g_signal_emit_by_name(terminal, "session-closed");
  109. }
  110. static gboolean mssh_terminal_focused(GtkWidget *widget,
  111. GtkDirectionType dir, gpointer data)
  112. {
  113. MSSHTerminal *terminal = MSSH_TERMINAL(data);
  114. g_signal_emit_by_name(terminal, "session-focused");
  115. return FALSE;
  116. }