mssh-terminal.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_init_session(MSSHTerminal *terminal, char *hostname)
  24. {
  25. terminal->hostname = hostname;
  26. terminal->menu_item = gtk_check_menu_item_new_with_label(
  27. terminal->hostname);
  28. gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(
  29. terminal->menu_item), TRUE);
  30. }
  31. void mssh_terminal_start_session(MSSHTerminal *terminal, char **env)
  32. {
  33. char *args[3];
  34. args[0] = strdup("ssh");
  35. args[1] = terminal->hostname;
  36. args[2] = NULL;
  37. vte_terminal_fork_command(VTE_TERMINAL(terminal), "ssh", args,
  38. env, NULL, FALSE, FALSE, FALSE);
  39. free(args[0]);
  40. }
  41. void mssh_terminal_send_host(MSSHTerminal *terminal)
  42. {
  43. if(mssh_terminal_isactive(terminal))
  44. {
  45. vte_terminal_feed_child(VTE_TERMINAL(terminal),
  46. terminal->hostname, strlen(terminal->hostname));
  47. }
  48. }
  49. void mssh_terminal_send_string(MSSHTerminal *terminal, gchar *string)
  50. {
  51. if(mssh_terminal_isactive(terminal))
  52. {
  53. vte_terminal_feed_child(VTE_TERMINAL(terminal), string,
  54. strlen(string));
  55. }
  56. }
  57. void mssh_terminal_send_data(MSSHTerminal *terminal, GdkEventKey *event)
  58. {
  59. gboolean dummy;
  60. if(mssh_terminal_isactive(terminal))
  61. {
  62. g_signal_emit_by_name(terminal, "key-press-event", event, &dummy);
  63. }
  64. }
  65. static void mssh_terminal_init(MSSHTerminal* terminal)
  66. {
  67. terminal->started = 0;
  68. terminal->ended = 0;
  69. vte_terminal_set_word_chars(VTE_TERMINAL(terminal),
  70. "-A-Za-z0-9,./?%&#:_=+@~");
  71. g_signal_connect(G_OBJECT(terminal), "child-exited",
  72. G_CALLBACK(mssh_terminal_child_exited), terminal);
  73. g_signal_connect(G_OBJECT(terminal), "focus-in-event",
  74. G_CALLBACK(mssh_terminal_focused), terminal);
  75. }
  76. static void mssh_terminal_class_init(MSSHTerminalClass *klass)
  77. {
  78. klass->session_closed_signal = g_signal_new("session-closed",
  79. G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
  80. G_STRUCT_OFFSET(MSSHTerminalClass, session_closed), NULL, NULL,
  81. g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
  82. klass->session_focused_signal = g_signal_new("session-focused",
  83. G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
  84. G_STRUCT_OFFSET(MSSHTerminalClass, session_focused), NULL, NULL,
  85. g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, NULL);
  86. }
  87. static void mssh_terminal_child_exited(VteTerminal *vte, gpointer data)
  88. {
  89. char msg[] = "\r\n[Child Exited]\r\n";
  90. MSSHTerminal *terminal = MSSH_TERMINAL(data);
  91. terminal->ended = 1;
  92. vte_terminal_feed(vte, msg, strlen(msg));
  93. g_signal_emit_by_name(terminal, "session-closed");
  94. }
  95. static gboolean mssh_terminal_focused(GtkWidget *widget,
  96. GtkDirectionType dir, gpointer data)
  97. {
  98. MSSHTerminal *terminal = MSSH_TERMINAL(data);
  99. g_signal_emit_by_name(terminal, "session-focused");
  100. return FALSE;
  101. }