mssh-window.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <gconf/gconf-client.h>
  4. #include <gdk/gdkkeysyms.h>
  5. #include "mssh-terminal.h"
  6. #include "mssh-pref.h"
  7. #include "mssh-gconf.h"
  8. #include "mssh-window.h"
  9. #include "config.h"
  10. static void mssh_window_sendhost(GtkWidget *widget, gpointer data);
  11. static void mssh_window_destroy(GtkWidget *widget, gpointer data);
  12. static void mssh_window_pref(GtkWidget *widget, gpointer data);
  13. static gboolean mssh_window_key_press(GtkWidget *widget,
  14. GdkEventKey *event, gpointer data);
  15. static gboolean mssh_window_entry_focused(GtkWidget *widget,
  16. GtkDirectionType dir, gpointer data);
  17. static gboolean mssh_window_session_close(gpointer data);
  18. static void mssh_window_session_focused(MSSHTerminal *terminal,
  19. gpointer data);
  20. static void mssh_window_insert(GtkWidget *widget, gchar *new_text,
  21. gint new_text_length, gint *position, gpointer data);
  22. static void mssh_window_add_session(MSSHWindow *window, char *hostname);
  23. static void mssh_window_init(MSSHWindow* window);
  24. static void mssh_window_class_init(MSSHWindowClass *klass);
  25. G_DEFINE_TYPE(MSSHWindow, mssh_window, GTK_TYPE_WINDOW)
  26. struct WinTermPair
  27. {
  28. MSSHWindow *window;
  29. MSSHTerminal *terminal;
  30. };
  31. GtkWidget* mssh_window_new(void)
  32. {
  33. return g_object_new(MSSH_TYPE_WINDOW, NULL);
  34. }
  35. static void mssh_window_sendhost(GtkWidget *widget, gpointer data)
  36. {
  37. int i;
  38. MSSHWindow *window = MSSH_WINDOW(data);
  39. for(i = 0; i < window->terminals->len; i++)
  40. {
  41. mssh_terminal_send_host(g_array_index(window->terminals,
  42. MSSHTerminal*, i));
  43. }
  44. }
  45. static void mssh_window_destroy(GtkWidget *widget, gpointer data)
  46. {
  47. gtk_main_quit();
  48. }
  49. static void mssh_window_pref(GtkWidget *widget, gpointer data)
  50. {
  51. MSSHWindow *window = MSSH_WINDOW(data);
  52. GtkWidget *pref = mssh_pref_new();
  53. gtk_window_set_transient_for(GTK_WINDOW(pref), GTK_WINDOW(window));
  54. gtk_window_set_modal(GTK_WINDOW(pref), TRUE);
  55. gtk_window_set_position(GTK_WINDOW(pref),
  56. GTK_WIN_POS_CENTER_ON_PARENT);
  57. gtk_widget_show_all(pref);
  58. }
  59. static void mssh_window_insert(GtkWidget *widget, gchar *new_text,
  60. gint new_text_length, gint *position, gpointer data)
  61. {
  62. int i;
  63. MSSHWindow *window = MSSH_WINDOW(data);
  64. for(i = 0; i < window->terminals->len; i++)
  65. {
  66. mssh_terminal_send_string(g_array_index(window->terminals,
  67. MSSHTerminal*, i), new_text);
  68. }
  69. g_signal_stop_emission_by_name(G_OBJECT(widget), "insert-text");
  70. }
  71. static gboolean mssh_window_key_press(GtkWidget *widget,
  72. GdkEventKey *event, gpointer data)
  73. {
  74. int i;
  75. MSSHWindow *window = MSSH_WINDOW(data);
  76. for(i = 0; i < window->terminals->len; i++)
  77. {
  78. mssh_terminal_send_data(g_array_index(window->terminals,
  79. MSSHTerminal*, i), event);
  80. }
  81. return TRUE;
  82. }
  83. static gboolean mssh_window_entry_focused(GtkWidget *widget,
  84. GtkDirectionType dir, gpointer data)
  85. {
  86. MSSHWindow *window = MSSH_WINDOW(data);
  87. gtk_window_set_title(GTK_WINDOW(window), PACKAGE_NAME" - All");
  88. return FALSE;
  89. }
  90. static gboolean mssh_window_session_close(gpointer data)
  91. {
  92. int i, idx = -1;
  93. struct WinTermPair *data_pair = (struct WinTermPair*)data;
  94. for(i = 0; i < data_pair->window->terminals->len; i++)
  95. {
  96. if(data_pair->terminal == g_array_index(
  97. data_pair->window->terminals, MSSHTerminal*, i))
  98. {
  99. idx = i;
  100. break;
  101. }
  102. }
  103. if(idx == -1)
  104. {
  105. fprintf(stderr,
  106. "mssh: Fatal Error: Can't find terminal to remove!\n");
  107. }
  108. else
  109. {
  110. gtk_widget_destroy(data_pair->terminal->menu_item);
  111. gtk_container_remove(GTK_CONTAINER(data_pair->window->table),
  112. GTK_WIDGET(data_pair->terminal));
  113. g_array_remove_index(data_pair->window->terminals, idx);
  114. mssh_window_relayout(data_pair->window);
  115. }
  116. if(data_pair->window->terminals->len == 0 &&
  117. data_pair->window->exit_on_all_closed)
  118. {
  119. mssh_window_destroy(NULL, (void*)data_pair->window);
  120. }
  121. free(data_pair);
  122. return FALSE;
  123. }
  124. void mssh_window_session_closed(MSSHTerminal *terminal, gpointer data)
  125. {
  126. struct WinTermPair *data_pair = malloc(sizeof(struct WinTermPair));
  127. data_pair->terminal = terminal;
  128. data_pair->window = MSSH_WINDOW(data);
  129. if(data_pair->window->close_ended_sessions)
  130. {
  131. g_timeout_add_seconds(data_pair->window->timeout,
  132. mssh_window_session_close, data_pair);
  133. }
  134. }
  135. static void mssh_window_session_focused(MSSHTerminal *terminal,
  136. gpointer data)
  137. {
  138. char *title;
  139. size_t len;
  140. MSSHWindow *window = MSSH_WINDOW(data);
  141. len = strlen(PACKAGE_NAME" - ") + strlen(terminal->hostname) + 1;
  142. title = malloc(len);
  143. snprintf(title, len, PACKAGE_NAME" - %s", terminal->hostname);
  144. gtk_window_set_title(GTK_WINDOW(window), title);
  145. free(title);
  146. }
  147. void mssh_window_relayout(MSSHWindow *window)
  148. {
  149. GConfClient *client;
  150. GConfEntry *entry;
  151. int i, len = window->terminals->len;
  152. int cols = (len < window->columns) ? len : window->columns;
  153. int rows = (len + 0.5) / cols;
  154. for(i = 0; i < len; i++)
  155. {
  156. MSSHTerminal *terminal = g_array_index(window->terminals,
  157. MSSHTerminal*, i);
  158. g_object_ref(terminal);
  159. if(GTK_WIDGET(terminal)->parent == GTK_WIDGET(window->table))
  160. {
  161. gtk_container_remove(GTK_CONTAINER(window->table),
  162. GTK_WIDGET(terminal));
  163. }
  164. gtk_table_attach(GTK_TABLE(window->table), GTK_WIDGET(terminal),
  165. (i % cols), (i == len - 1) ? cols : (i % cols) + 1, i / cols,
  166. (i / cols) + 1,
  167. GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 1, 1);
  168. g_object_unref(terminal);
  169. if(!terminal->started)
  170. {
  171. mssh_terminal_start_session(terminal, window->env);
  172. terminal->started = 1;
  173. }
  174. }
  175. if(len > 0)
  176. {
  177. gtk_table_resize(GTK_TABLE(window->table), rows, cols);
  178. }
  179. client = gconf_client_get_default();
  180. gtk_widget_show_all(GTK_WIDGET(window));
  181. entry = gconf_client_get_entry(client, MSSH_GCONF_KEY_FONT, NULL,
  182. TRUE, NULL);
  183. mssh_gconf_notify_font(client, 0, entry, window);
  184. entry = gconf_client_get_entry(client, MSSH_GCONF_KEY_FG_COLOUR, NULL,
  185. TRUE, NULL);
  186. mssh_gconf_notify_fg_colour(client, 0, entry, window);
  187. entry = gconf_client_get_entry(client, MSSH_GCONF_KEY_BG_COLOUR, NULL,
  188. TRUE, NULL);
  189. mssh_gconf_notify_bg_colour(client, 0, entry, window);
  190. }
  191. static void mssh_window_add_session(MSSHWindow *window, char *hostname)
  192. {
  193. MSSHTerminal *terminal = MSSH_TERMINAL(mssh_terminal_new());
  194. g_array_append_val(window->terminals, terminal);
  195. g_signal_connect(G_OBJECT(terminal), "session-closed",
  196. G_CALLBACK(mssh_window_session_closed), window);
  197. g_signal_connect(G_OBJECT(terminal), "session-focused",
  198. G_CALLBACK(mssh_window_session_focused), window);
  199. mssh_terminal_init_session(terminal, hostname);
  200. gtk_menu_shell_append(GTK_MENU_SHELL(window->server_menu),
  201. terminal->menu_item);
  202. }
  203. static void mssh_window_init(MSSHWindow* window)
  204. {
  205. GConfClient *client;
  206. GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
  207. GtkWidget *entry = gtk_entry_new();
  208. GtkWidget *menu_bar = gtk_menu_bar_new();
  209. GtkWidget *file_menu = gtk_menu_new();
  210. GtkWidget *edit_menu = gtk_menu_new();
  211. GtkWidget *file_item = gtk_menu_item_new_with_label("File");
  212. GtkWidget *edit_item = gtk_menu_item_new_with_label("Edit");
  213. GtkWidget *server_item = gtk_menu_item_new_with_label("Servers");
  214. GtkWidget *file_quit = gtk_image_menu_item_new_from_stock(
  215. GTK_STOCK_QUIT, NULL);
  216. GtkWidget *file_sendhost = gtk_image_menu_item_new_with_label(
  217. "Send hostname");
  218. /* GtkWidget *file_add = gtk_image_menu_item_new_with_label(
  219. "Add session");*/
  220. GtkWidget *edit_pref = gtk_image_menu_item_new_from_stock(
  221. GTK_STOCK_PREFERENCES, NULL);
  222. window->server_menu = gtk_menu_new();
  223. window->terminals = g_array_new(FALSE, TRUE, sizeof(MSSHTerminal*));
  224. gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
  225. gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_item), edit_menu);
  226. gtk_menu_item_set_submenu(GTK_MENU_ITEM(server_item),
  227. window->server_menu);
  228. /* gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_add);*/
  229. gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_sendhost);
  230. gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_quit);
  231. gtk_menu_shell_append(GTK_MENU_SHELL(edit_menu), edit_pref);
  232. g_signal_connect(G_OBJECT(file_sendhost), "activate",
  233. G_CALLBACK(mssh_window_sendhost), window);
  234. g_signal_connect(G_OBJECT(file_quit), "activate",
  235. G_CALLBACK(mssh_window_destroy), window);
  236. g_signal_connect(G_OBJECT(edit_pref), "activate",
  237. G_CALLBACK(mssh_window_pref), window);
  238. gtk_menu_bar_append(GTK_MENU_BAR(menu_bar), file_item);
  239. gtk_menu_bar_append(GTK_MENU_BAR(menu_bar), edit_item);
  240. gtk_menu_bar_append(GTK_MENU_BAR(menu_bar), server_item);
  241. g_signal_connect(G_OBJECT(entry), "key-press-event",
  242. G_CALLBACK(mssh_window_key_press), window);
  243. g_signal_connect(G_OBJECT(entry), "insert-text",
  244. G_CALLBACK(mssh_window_insert), window);
  245. g_signal_connect(G_OBJECT(entry), "focus-in-event",
  246. G_CALLBACK(mssh_window_entry_focused), window);
  247. gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, TRUE, 0);
  248. gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, TRUE, 2);
  249. window->table = gtk_table_new(1, 1, TRUE);
  250. gtk_box_pack_start(GTK_BOX(vbox), window->table, TRUE, TRUE, 0);
  251. gtk_container_add(GTK_CONTAINER(window), vbox);
  252. gtk_widget_set_size_request(GTK_WIDGET(window), 1024, 768);
  253. gtk_window_set_title(GTK_WINDOW(window), PACKAGE_NAME);
  254. client = gconf_client_get_default();
  255. gconf_client_add_dir(client, MSSH_GCONF_PATH,
  256. GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
  257. gconf_client_notify_add(client, MSSH_GCONF_KEY_FONT,
  258. mssh_gconf_notify_font, window, NULL, NULL);
  259. gconf_client_notify_add(client, MSSH_GCONF_KEY_FG_COLOUR,
  260. mssh_gconf_notify_fg_colour, window, NULL, NULL);
  261. gconf_client_notify_add(client, MSSH_GCONF_KEY_BG_COLOUR,
  262. mssh_gconf_notify_bg_colour, window, NULL, NULL);
  263. gconf_client_notify_add(client, MSSH_GCONF_KEY_COLUMNS,
  264. mssh_gconf_notify_columns, window, NULL, NULL);
  265. gconf_client_notify_add(client, MSSH_GCONF_KEY_TIMEOUT,
  266. mssh_gconf_notify_timeout, window, NULL, NULL);
  267. gconf_client_notify_add(client, MSSH_GCONF_KEY_CLOSE_ENDED,
  268. mssh_gconf_notify_close_ended, window, NULL, NULL);
  269. gconf_client_notify_add(client, MSSH_GCONF_KEY_QUIT_ALL_ENDED,
  270. mssh_gconf_notify_quit_all_ended, window, NULL, NULL);
  271. gconf_client_notify(client, MSSH_GCONF_KEY_COLUMNS);
  272. gconf_client_notify(client, MSSH_GCONF_KEY_TIMEOUT);
  273. gconf_client_notify(client, MSSH_GCONF_KEY_CLOSE_ENDED);
  274. gconf_client_notify(client, MSSH_GCONF_KEY_QUIT_ALL_ENDED);
  275. }
  276. void mssh_window_start_session(MSSHWindow* window, char **env, int nhosts,
  277. char **servers)
  278. {
  279. int i, j, k;
  280. int rows = (nhosts / 2) + (nhosts % 2);
  281. window->env = env;
  282. for(i = 0; i < rows; i++)
  283. {
  284. for(j = 0; j < 2; j++)
  285. {
  286. k = j + i*2;
  287. if(k < nhosts)
  288. {
  289. mssh_window_add_session(window, servers[k]);
  290. }
  291. }
  292. }
  293. mssh_window_relayout(window);
  294. }
  295. static void mssh_window_class_init(MSSHWindowClass *klass)
  296. {
  297. }