mssh-window.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 gboolean mssh_window_mouse_paste_cb(MSSHTerminal *terminal,
  21. gpointer data);
  22. static void mssh_window_insert(GtkWidget *widget, gchar *new_text,
  23. gint new_text_length, gint *position, gpointer data);
  24. static void mssh_window_add_session(MSSHWindow *window, char *hostname);
  25. static void mssh_window_init(MSSHWindow* window);
  26. static void mssh_window_class_init(MSSHWindowClass *klass);
  27. G_DEFINE_TYPE(MSSHWindow, mssh_window, GTK_TYPE_WINDOW)
  28. struct WinTermPair
  29. {
  30. MSSHWindow *window;
  31. MSSHTerminal *terminal;
  32. };
  33. GtkWidget* mssh_window_new(void)
  34. {
  35. return g_object_new(MSSH_TYPE_WINDOW, NULL);
  36. }
  37. static void mssh_window_sendhost(GtkWidget *widget, gpointer data)
  38. {
  39. int i;
  40. MSSHWindow *window = MSSH_WINDOW(data);
  41. for(i = 0; i < window->terminals->len; i++)
  42. {
  43. mssh_terminal_send_host(g_array_index(window->terminals,
  44. MSSHTerminal*, i));
  45. }
  46. }
  47. static void mssh_window_destroy(GtkWidget *widget, gpointer data)
  48. {
  49. gtk_main_quit();
  50. }
  51. static void mssh_window_pref(GtkWidget *widget, gpointer data)
  52. {
  53. MSSHWindow *window = MSSH_WINDOW(data);
  54. GtkWidget *pref = mssh_pref_new();
  55. gtk_window_set_transient_for(GTK_WINDOW(pref), GTK_WINDOW(window));
  56. gtk_window_set_modal(GTK_WINDOW(pref), TRUE);
  57. gtk_window_set_position(GTK_WINDOW(pref),
  58. GTK_WIN_POS_CENTER_ON_PARENT);
  59. gtk_widget_show_all(pref);
  60. }
  61. static void mssh_window_insert(GtkWidget *widget, gchar *new_text,
  62. gint new_text_length, gint *position, gpointer data)
  63. {
  64. int i;
  65. MSSHWindow *window = MSSH_WINDOW(data);
  66. for(i = 0; i < window->terminals->len; i++)
  67. {
  68. mssh_terminal_send_string(g_array_index(window->terminals,
  69. MSSHTerminal*, i), new_text);
  70. }
  71. g_signal_stop_emission_by_name(G_OBJECT(widget), "insert-text");
  72. }
  73. static gboolean mssh_window_key_press(GtkWidget *widget,
  74. GdkEventKey *event, gpointer data)
  75. {
  76. int i;
  77. MSSHWindow *window = MSSH_WINDOW(data);
  78. for(i = 0; i < window->terminals->len; i++)
  79. {
  80. mssh_terminal_send_data(g_array_index(window->terminals,
  81. MSSHTerminal*, i), event);
  82. }
  83. return TRUE;
  84. }
  85. static gboolean mssh_window_entry_focused(GtkWidget *widget,
  86. GtkDirectionType dir, gpointer data)
  87. {
  88. MSSHWindow *window = MSSH_WINDOW(data);
  89. gtk_window_set_title(GTK_WINDOW(window), PACKAGE_NAME" - All");
  90. return FALSE;
  91. }
  92. gboolean mssh_window_focus(GtkWidget *widget, GObject *acceleratable,
  93. guint keyval, GdkModifierType modifier, gpointer data)
  94. {
  95. MSSHWindow *window = MSSH_WINDOW(data);
  96. GtkWidget *focus;
  97. int i, idx = -1, len = window->terminals->len;
  98. int wcols = window->columns_override ? window->columns_override :
  99. window->columns;
  100. int cols = (len < wcols) ? len : wcols;
  101. focus = gtk_window_get_focus(GTK_WINDOW(window));
  102. for(i = 0; i < len; i++)
  103. {
  104. if(focus == GTK_WIDGET(g_array_index(window->terminals,
  105. MSSHTerminal*, i)))
  106. {
  107. idx = i;
  108. break;
  109. }
  110. }
  111. if(focus == window->global_entry && keyval == GDK_KEY_Down &&
  112. window->dir_focus)
  113. idx = 0;
  114. else if(idx == -1 && window->dir_focus)
  115. return TRUE;
  116. else
  117. {
  118. switch(keyval)
  119. {
  120. case GDK_KEY_Up:
  121. if(window->dir_focus)
  122. idx = idx - cols;
  123. break;
  124. case GDK_KEY_Down:
  125. if(window->dir_focus)
  126. {
  127. if((idx + cols >= len) && (idx < len -
  128. (len % cols) ? (len % cols) : cols))
  129. idx = len - 1;
  130. else
  131. idx = idx + cols;
  132. }
  133. break;
  134. case GDK_KEY_Left:
  135. if(idx % cols != 0 || !window->dir_focus)
  136. idx = idx - 1;
  137. break;
  138. case GDK_KEY_Right:
  139. if(idx % cols != cols - 1 || !window->dir_focus)
  140. idx = idx + 1;
  141. break;
  142. }
  143. }
  144. if(idx >= len && !window->dir_focus)
  145. focus = window->global_entry;
  146. if(idx < -1 && !window->dir_focus)
  147. idx = len - 1;
  148. if(idx < 0)
  149. focus = window->global_entry;
  150. else if(idx < len)
  151. {
  152. focus = GTK_WIDGET(g_array_index(window->terminals,
  153. MSSHTerminal*, idx));
  154. }
  155. gtk_window_set_focus(GTK_WINDOW(window), focus);
  156. return TRUE;
  157. }
  158. static gboolean mssh_window_session_close(gpointer data)
  159. {
  160. int i, idx = -1;
  161. struct WinTermPair *data_pair = (struct WinTermPair*)data;
  162. for(i = 0; i < data_pair->window->terminals->len; i++)
  163. {
  164. if(data_pair->terminal == g_array_index(
  165. data_pair->window->terminals, MSSHTerminal*, i))
  166. {
  167. idx = i;
  168. break;
  169. }
  170. }
  171. data_pair->window->last_closed = idx;
  172. if(idx == -1)
  173. {
  174. fprintf(stderr,
  175. "mssh: Fatal Error: Can't find terminal to remove!\n");
  176. }
  177. else
  178. {
  179. gtk_widget_destroy(data_pair->terminal->menu_item);
  180. gtk_container_remove(GTK_CONTAINER(data_pair->window->table),
  181. GTK_WIDGET(data_pair->terminal));
  182. g_array_remove_index(data_pair->window->terminals, idx);
  183. mssh_window_relayout(data_pair->window);
  184. }
  185. if(data_pair->window->terminals->len == 0 &&
  186. data_pair->window->exit_on_all_closed)
  187. {
  188. mssh_window_destroy(NULL, (void*)data_pair->window);
  189. }
  190. free(data_pair);
  191. return FALSE;
  192. }
  193. void mssh_window_session_closed(MSSHTerminal *terminal, gpointer data)
  194. {
  195. struct WinTermPair *data_pair = malloc(sizeof(struct WinTermPair));
  196. data_pair->terminal = terminal;
  197. data_pair->window = MSSH_WINDOW(data);
  198. if(data_pair->window->close_ended_sessions)
  199. {
  200. g_timeout_add_seconds(data_pair->window->timeout,
  201. mssh_window_session_close, data_pair);
  202. }
  203. }
  204. static gboolean mssh_window_mouse_paste_cb(MSSHTerminal *terminal,
  205. gpointer data)
  206. {
  207. gtk_widget_grab_focus(GTK_WIDGET(terminal));
  208. return FALSE;
  209. }
  210. static void mssh_window_session_focused(MSSHTerminal *terminal,
  211. gpointer data)
  212. {
  213. char *title;
  214. size_t len;
  215. MSSHWindow *window = MSSH_WINDOW(data);
  216. len = strlen(PACKAGE_NAME" - ") + strlen(terminal->hostname) + 1;
  217. title = malloc(len);
  218. snprintf(title, len, PACKAGE_NAME" - %s", terminal->hostname);
  219. gtk_window_set_title(GTK_WINDOW(window), title);
  220. free(title);
  221. }
  222. void mssh_window_relayout(MSSHWindow *window)
  223. {
  224. GConfClient *client;
  225. GConfEntry *entry;
  226. GtkWidget *focus;
  227. int i, len = window->terminals->len;
  228. int wcols = window->columns_override ? window->columns_override :
  229. window->columns;
  230. int cols = (len < wcols) ? len : wcols;
  231. int rows = (len + 0.5) / cols;
  232. focus = gtk_window_get_focus(GTK_WINDOW(window));
  233. if(!focus)
  234. {
  235. if(window->last_closed < 0)
  236. window->last_closed = 0;
  237. if(len == 0)
  238. focus = window->global_entry;
  239. else if(window->last_closed < len)
  240. {
  241. focus = GTK_WIDGET(g_array_index(window->terminals,
  242. MSSHTerminal*, window->last_closed));
  243. }
  244. else
  245. {
  246. focus = GTK_WIDGET(g_array_index(window->terminals,
  247. MSSHTerminal*, 0));
  248. }
  249. }
  250. for(i = 0; i < len; i++)
  251. {
  252. MSSHTerminal *terminal = g_array_index(window->terminals,
  253. MSSHTerminal*, i);
  254. g_object_ref(terminal);
  255. if(gtk_widget_get_parent(GTK_WIDGET(terminal)) == GTK_WIDGET(window->table))
  256. {
  257. gtk_container_remove(GTK_CONTAINER(window->table),
  258. GTK_WIDGET(terminal));
  259. }
  260. gtk_table_attach(GTK_TABLE(window->table), GTK_WIDGET(terminal),
  261. (i % cols), (i == len - 1) ? cols : (i % cols) + 1, i / cols,
  262. (i / cols) + 1,
  263. GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 1, 1);
  264. g_object_unref(terminal);
  265. if(!terminal->started)
  266. {
  267. mssh_terminal_start_session(terminal, window->env);
  268. terminal->started = 1;
  269. }
  270. }
  271. if(len > 0)
  272. {
  273. gtk_table_resize(GTK_TABLE(window->table), rows, cols);
  274. }
  275. client = gconf_client_get_default();
  276. gtk_widget_show_all(GTK_WIDGET(window));
  277. entry = gconf_client_get_entry(client, MSSH_GCONF_KEY_FONT, NULL,
  278. TRUE, NULL);
  279. mssh_gconf_notify_font(client, 0, entry, window);
  280. entry = gconf_client_get_entry(client, MSSH_GCONF_KEY_FG_COLOUR, NULL,
  281. TRUE, NULL);
  282. mssh_gconf_notify_fg_colour(client, 0, entry, window);
  283. entry = gconf_client_get_entry(client, MSSH_GCONF_KEY_BG_COLOUR, NULL,
  284. TRUE, NULL);
  285. mssh_gconf_notify_bg_colour(client, 0, entry, window);
  286. gtk_window_set_focus(GTK_WINDOW(window), GTK_WIDGET(focus));
  287. }
  288. static void mssh_window_add_session(MSSHWindow *window, char *hostname)
  289. {
  290. MSSHTerminal *terminal = MSSH_TERMINAL(mssh_terminal_new());
  291. g_array_append_val(window->terminals, terminal);
  292. g_signal_connect(G_OBJECT(terminal), "session-closed",
  293. G_CALLBACK(mssh_window_session_closed), window);
  294. g_signal_connect(G_OBJECT(terminal), "session-focused",
  295. G_CALLBACK(mssh_window_session_focused), window);
  296. g_signal_connect(GTK_WIDGET(terminal), "button-release-event",
  297. G_CALLBACK(mssh_window_mouse_paste_cb), window);
  298. mssh_terminal_init_session(terminal, hostname);
  299. gtk_menu_shell_append(GTK_MENU_SHELL(window->server_menu),
  300. terminal->menu_item);
  301. }
  302. static void mssh_window_init(MSSHWindow* window)
  303. {
  304. GConfClient *client;
  305. GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
  306. GtkWidget *entry = gtk_entry_new();
  307. GtkWidget *menu_bar = gtk_menu_bar_new();
  308. GtkWidget *file_menu = gtk_menu_new();
  309. GtkWidget *edit_menu = gtk_menu_new();
  310. GtkWidget *file_item = gtk_menu_item_new_with_label("File");
  311. GtkWidget *edit_item = gtk_menu_item_new_with_label("Edit");
  312. GtkWidget *server_item = gtk_menu_item_new_with_label("Servers");
  313. GtkWidget *file_quit = gtk_image_menu_item_new_from_stock(
  314. GTK_STOCK_QUIT, NULL);
  315. GtkWidget *file_sendhost = gtk_image_menu_item_new_with_label(
  316. "Send hostname");
  317. /* GtkWidget *file_add = gtk_image_menu_item_new_with_label(
  318. "Add session");*/
  319. GtkWidget *edit_pref = gtk_image_menu_item_new_from_stock(
  320. GTK_STOCK_PREFERENCES, NULL);
  321. GtkAccelGroup *accel = gtk_accel_group_new();
  322. window->accel = NULL;
  323. window->server_menu = gtk_menu_new();
  324. window->global_entry = entry;
  325. window->last_closed = -1;
  326. window->terminals = g_array_new(FALSE, TRUE, sizeof(MSSHTerminal*));
  327. gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
  328. gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_item), edit_menu);
  329. gtk_menu_item_set_submenu(GTK_MENU_ITEM(server_item),
  330. window->server_menu);
  331. /* gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_add);*/
  332. gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_sendhost);
  333. gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_quit);
  334. gtk_menu_shell_append(GTK_MENU_SHELL(edit_menu), edit_pref);
  335. g_signal_connect(G_OBJECT(file_sendhost), "activate",
  336. G_CALLBACK(mssh_window_sendhost), window);
  337. g_signal_connect(G_OBJECT(file_quit), "activate",
  338. G_CALLBACK(mssh_window_destroy), window);
  339. g_signal_connect(G_OBJECT(edit_pref), "activate",
  340. G_CALLBACK(mssh_window_pref), window);
  341. gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), file_item);
  342. gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), edit_item);
  343. gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), server_item);
  344. g_signal_connect(G_OBJECT(entry), "key-press-event",
  345. G_CALLBACK(mssh_window_key_press), window);
  346. g_signal_connect(G_OBJECT(entry), "insert-text",
  347. G_CALLBACK(mssh_window_insert), window);
  348. g_signal_connect(G_OBJECT(entry), "focus-in-event",
  349. G_CALLBACK(mssh_window_entry_focused), window);
  350. gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, TRUE, 0);
  351. gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, TRUE, 2);
  352. window->table = gtk_table_new(1, 1, TRUE);
  353. gtk_box_pack_start(GTK_BOX(vbox), window->table, TRUE, TRUE, 0);
  354. gtk_container_add(GTK_CONTAINER(window), vbox);
  355. gtk_widget_set_size_request(GTK_WIDGET(window), 0, 0);
  356. gtk_window_set_default_size(GTK_WINDOW(window), 1024, 768);
  357. gtk_window_set_title(GTK_WINDOW(window), PACKAGE_NAME);
  358. client = gconf_client_get_default();
  359. gconf_client_add_dir(client, MSSH_GCONF_PATH,
  360. GCONF_CLIENT_PRELOAD_RECURSIVE, NULL);
  361. gconf_client_notify_add(client, MSSH_GCONF_KEY_FONT,
  362. mssh_gconf_notify_font, window, NULL, NULL);
  363. gconf_client_notify_add(client, MSSH_GCONF_KEY_FG_COLOUR,
  364. mssh_gconf_notify_fg_colour, window, NULL, NULL);
  365. gconf_client_notify_add(client, MSSH_GCONF_KEY_BG_COLOUR,
  366. mssh_gconf_notify_bg_colour, window, NULL, NULL);
  367. gconf_client_notify_add(client, MSSH_GCONF_KEY_COLUMNS,
  368. mssh_gconf_notify_columns, window, NULL, NULL);
  369. gconf_client_notify_add(client, MSSH_GCONF_KEY_TIMEOUT,
  370. mssh_gconf_notify_timeout, window, NULL, NULL);
  371. gconf_client_notify_add(client, MSSH_GCONF_KEY_CLOSE_ENDED,
  372. mssh_gconf_notify_close_ended, window, NULL, NULL);
  373. gconf_client_notify_add(client, MSSH_GCONF_KEY_QUIT_ALL_ENDED,
  374. mssh_gconf_notify_quit_all_ended, window, NULL, NULL);
  375. gconf_client_notify_add(client, MSSH_GCONF_KEY_DIR_FOCUS,
  376. mssh_gconf_notify_dir_focus, window, NULL, NULL);
  377. gconf_client_notify_add(client, MSSH_GCONF_KEY_MODIFIER,
  378. mssh_gconf_notify_modifier, window, NULL, NULL);
  379. gconf_client_notify(client, MSSH_GCONF_KEY_COLUMNS);
  380. gconf_client_notify(client, MSSH_GCONF_KEY_TIMEOUT);
  381. gconf_client_notify(client, MSSH_GCONF_KEY_CLOSE_ENDED);
  382. gconf_client_notify(client, MSSH_GCONF_KEY_QUIT_ALL_ENDED);
  383. gconf_client_notify(client, MSSH_GCONF_KEY_DIR_FOCUS);
  384. gconf_client_notify(client, MSSH_GCONF_KEY_MODIFIER);
  385. gtk_accel_group_connect(accel, GDK_KEY_Up, window->modifier,
  386. GTK_ACCEL_VISIBLE, g_cclosure_new(
  387. G_CALLBACK(mssh_window_focus), window, NULL));
  388. gtk_accel_group_connect(accel, GDK_KEY_Down, window->modifier,
  389. GTK_ACCEL_VISIBLE, g_cclosure_new(
  390. G_CALLBACK(mssh_window_focus), window, NULL));
  391. gtk_accel_group_connect(accel, GDK_KEY_Left, window->modifier,
  392. GTK_ACCEL_VISIBLE, g_cclosure_new(
  393. G_CALLBACK(mssh_window_focus), window, NULL));
  394. gtk_accel_group_connect(accel, GDK_KEY_Right, window->modifier,
  395. GTK_ACCEL_VISIBLE, g_cclosure_new(
  396. G_CALLBACK(mssh_window_focus), window, NULL));
  397. window->accel = accel;
  398. gtk_window_add_accel_group(GTK_WINDOW(window), accel);
  399. }
  400. void mssh_window_start_session(MSSHWindow* window, char **env,
  401. GArray *hosts, long cols)
  402. {
  403. int i, j, k;
  404. int nhosts = hosts->len;
  405. int rows = (nhosts / 2) + (nhosts % 2);
  406. window->env = env;
  407. window->columns_override = cols;
  408. for(i = 0; i < rows; i++)
  409. {
  410. for(j = 0; j < 2; j++)
  411. {
  412. k = j + i*2;
  413. if(k < nhosts)
  414. {
  415. mssh_window_add_session(window, g_array_index(hosts,
  416. char*, k));
  417. }
  418. }
  419. }
  420. mssh_window_relayout(window);
  421. }
  422. static void mssh_window_class_init(MSSHWindowClass *klass)
  423. {
  424. }