main-gtk-application.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * main-gtk-application.c: a top-level front end to GUI PuTTY and
  3. * pterm, using GtkApplication. Suitable for OS X. Currently
  4. * unfinished.
  5. *
  6. * (You could run it on ordinary Linux GTK too, in principle, but I
  7. * don't think it would be particularly useful to do so, even once
  8. * it's fully working.)
  9. */
  10. /*
  11. Building this for OS X is currently broken, because the new
  12. CMake-based build system doesn't support it yet. Probably what needs
  13. doing is to add it back in to unix/CMakeLists.txt under a condition
  14. like if(CMAKE_SYSTEM_NAME MATCHES "Darwin").
  15. */
  16. /*
  17. TODO list for a sensible GTK3 PuTTY/pterm on OS X:
  18. Still to do on the application menu bar: items that have to vary with
  19. context or user action (saved sessions and mid-session special
  20. commands), and disabling/enabling the main actions in parallel with
  21. their counterparts in the Ctrl-rightclick context menu.
  22. Mouse wheel events and trackpad scrolling gestures don't work quite
  23. right in the terminal drawing area. This seems to be a combination of
  24. two things, neither of which I completely understand yet. Firstly, on
  25. OS X GTK my trackpad seems to generate GDK scroll events for which
  26. gdk_event_get_scroll_deltas returns integers rather than integer
  27. multiples of 1/30, so we end up scrolling by very large amounts;
  28. secondly, the window doesn't seem to receive a GTK "draw" event until
  29. after the entire scroll gesture is complete, which means we don't get
  30. constant visual feedback on how much we're scrolling by.
  31. There doesn't seem to be a resize handle on terminal windows. Then
  32. again, they do seem to _be_ resizable; the handle just isn't shown.
  33. Perhaps that's a feature (certainly in a scrollbarless configuration
  34. the handle gets in the way of the bottom right character cell in the
  35. terminal itself), but it would be nice to at least understand _why_ it
  36. happens and perhaps include an option to put it back again.
  37. A slight oddity with menus that pop up directly under the mouse
  38. pointer: mousing over the menu items doesn't highlight them initially,
  39. but if I mouse off the menu and back on (without un-popping-it-up)
  40. then suddenly that does work. I don't know if this is something I can
  41. fix, though; it might very well be a quirk of the underlying GTK.
  42. Does OS X have a standard system of online help that I could tie into?
  43. Need to work out what if anything we can do with Pageant on OS X.
  44. Perhaps it's too much bother and we should just talk to the
  45. system-provided SSH agent? Or perhaps not.
  46. Nice-to-have: a custom right-click menu from the application's dock
  47. tile, listing the saved sessions for quick launch. As far as I know
  48. there's nothing built in to GtkApplication that can produce this, but
  49. it's possible we might be able to drop a piece of native Cocoa code in
  50. under ifdef, substituting an application delegate of our own which
  51. forwards all methods we're not interested in to the GTK-provided one?
  52. At the point where this becomes polished enough to publish pre-built,
  53. I suppose I'll have to look into OS X code signing.
  54. https://wiki.gnome.org/Projects/GTK%2B/OSX/Bundling has some links.
  55. */
  56. #include <assert.h>
  57. #include <stdlib.h>
  58. #include <unistd.h>
  59. #include <gtk/gtk.h>
  60. #define MAY_REFER_TO_GTK_IN_HEADERS
  61. #include "putty.h"
  62. #include "gtkmisc.h"
  63. #include "gtkcompat.h"
  64. char *x_get_default(const char *key) { return NULL; }
  65. const bool buildinfo_gtk_relevant = true;
  66. #if !GTK_CHECK_VERSION(3,0,0)
  67. #error This front end only works in GTK 3
  68. #endif
  69. static void startup(GApplication *app, gpointer user_data)
  70. {
  71. GMenu *menubar, *menu, *section;
  72. menubar = g_menu_new();
  73. menu = g_menu_new();
  74. g_menu_append_submenu(menubar, "File", G_MENU_MODEL(menu));
  75. section = g_menu_new();
  76. g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
  77. g_menu_append(section, "New Window", "app.newwin");
  78. menu = g_menu_new();
  79. g_menu_append_submenu(menubar, "Edit", G_MENU_MODEL(menu));
  80. section = g_menu_new();
  81. g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
  82. g_menu_append(section, "Copy", "win.copy");
  83. g_menu_append(section, "Paste", "win.paste");
  84. g_menu_append(section, "Copy All", "win.copyall");
  85. menu = g_menu_new();
  86. g_menu_append_submenu(menubar, "Window", G_MENU_MODEL(menu));
  87. section = g_menu_new();
  88. g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
  89. g_menu_append(section, "Restart Session", "win.restart");
  90. g_menu_append(section, "Duplicate Session", "win.duplicate");
  91. section = g_menu_new();
  92. g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
  93. g_menu_append(section, "Change Settings", "win.changesettings");
  94. if (use_event_log) {
  95. section = g_menu_new();
  96. g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
  97. g_menu_append(section, "Event Log", "win.eventlog");
  98. }
  99. section = g_menu_new();
  100. g_menu_append_section(menu, NULL, G_MENU_MODEL(section));
  101. g_menu_append(section, "Clear Scrollback", "win.clearscrollback");
  102. g_menu_append(section, "Reset Terminal", "win.resetterm");
  103. #if GTK_CHECK_VERSION(3,12,0)
  104. #define SET_ACCEL(app, command, accel) do \
  105. { \
  106. static const char *const accels[] = { accel, NULL }; \
  107. gtk_application_set_accels_for_action( \
  108. GTK_APPLICATION(app), command, accels); \
  109. } while (0)
  110. #else
  111. /* The Gtk function used above was new in 3.12; the one below
  112. * was deprecated from 3.14. */
  113. #define SET_ACCEL(app, command, accel) \
  114. gtk_application_add_accelerator(GTK_APPLICATION(app), accel, \
  115. command, NULL)
  116. #endif
  117. SET_ACCEL(app, "app.newwin", "<Primary>n");
  118. SET_ACCEL(app, "win.copy", "<Primary>c");
  119. SET_ACCEL(app, "win.paste", "<Primary>v");
  120. #undef SET_ACCEL
  121. gtk_application_set_menubar(GTK_APPLICATION(app),
  122. G_MENU_MODEL(menubar));
  123. }
  124. #define WIN_ACTION_LIST(X) \
  125. X("copy", MA_COPY) \
  126. X("paste", MA_PASTE) \
  127. X("copyall", MA_COPY_ALL) \
  128. X("duplicate", MA_DUPLICATE_SESSION) \
  129. X("restart", MA_RESTART_SESSION) \
  130. X("changesettings", MA_CHANGE_SETTINGS) \
  131. X("clearscrollback", MA_CLEAR_SCROLLBACK) \
  132. X("resetterm", MA_RESET_TERMINAL) \
  133. X("eventlog", MA_EVENT_LOG) \
  134. /* end of list */
  135. #define WIN_ACTION_CALLBACK(name, id) \
  136. static void win_action_cb_ ## id(GSimpleAction *a, GVariant *p, gpointer d) \
  137. { app_menu_action(d, id); }
  138. WIN_ACTION_LIST(WIN_ACTION_CALLBACK)
  139. #undef WIN_ACTION_CALLBACK
  140. static const GActionEntry win_actions[] = {
  141. #define WIN_ACTION_ENTRY(name, id) { name, win_action_cb_ ## id },
  142. WIN_ACTION_LIST(WIN_ACTION_ENTRY)
  143. #undef WIN_ACTION_ENTRY
  144. };
  145. static GtkApplication *app;
  146. GtkWidget *make_gtk_toplevel_window(GtkFrontend *frontend)
  147. {
  148. GtkWidget *win = gtk_application_window_new(app);
  149. g_action_map_add_action_entries(G_ACTION_MAP(win),
  150. win_actions,
  151. G_N_ELEMENTS(win_actions),
  152. frontend);
  153. return win;
  154. }
  155. void launch_duplicate_session(Conf *conf)
  156. {
  157. assert(!dup_check_launchable || conf_launchable(conf));
  158. g_application_hold(G_APPLICATION(app));
  159. new_session_window(conf_copy(conf), NULL);
  160. }
  161. void session_window_closed(void)
  162. {
  163. g_application_release(G_APPLICATION(app));
  164. }
  165. static void post_initial_config_box(void *vctx, int result)
  166. {
  167. Conf *conf = (Conf *)vctx;
  168. if (result > 0) {
  169. new_session_window(conf, NULL);
  170. } else if (result == 0) {
  171. conf_free(conf);
  172. g_application_release(G_APPLICATION(app));
  173. }
  174. }
  175. void launch_saved_session(const char *str)
  176. {
  177. Conf *conf = conf_new();
  178. do_defaults(str, conf);
  179. g_application_hold(G_APPLICATION(app));
  180. if (!conf_launchable(conf)) {
  181. initial_config_box(conf, post_initial_config_box, conf);
  182. } else {
  183. new_session_window(conf, NULL);
  184. }
  185. }
  186. void launch_new_session(void)
  187. {
  188. /* Same as launch_saved_session except that we pass NULL to
  189. * do_defaults. */
  190. launch_saved_session(NULL);
  191. }
  192. void new_app_win(GtkApplication *app)
  193. {
  194. launch_new_session();
  195. }
  196. static void window_setup_error_callback(void *vctx, int result)
  197. {
  198. g_application_release(G_APPLICATION(app));
  199. }
  200. void window_setup_error(const char *errmsg)
  201. {
  202. create_message_box(NULL, "Error creating session window", errmsg,
  203. string_width("Some sort of fiddly error message that "
  204. "might be technical"),
  205. true, &buttons_ok, window_setup_error_callback, NULL);
  206. }
  207. static void activate(GApplication *app,
  208. gpointer user_data)
  209. {
  210. new_app_win(GTK_APPLICATION(app));
  211. }
  212. static void newwin_cb(GSimpleAction *action,
  213. GVariant *parameter,
  214. gpointer user_data)
  215. {
  216. new_app_win(GTK_APPLICATION(user_data));
  217. }
  218. static void quit_cb(GSimpleAction *action,
  219. GVariant *parameter,
  220. gpointer user_data)
  221. {
  222. g_application_quit(G_APPLICATION(user_data));
  223. }
  224. static void about_cb(GSimpleAction *action,
  225. GVariant *parameter,
  226. gpointer user_data)
  227. {
  228. about_box(NULL);
  229. }
  230. static const GActionEntry app_actions[] = {
  231. { "newwin", newwin_cb },
  232. { "about", about_cb },
  233. { "quit", quit_cb },
  234. };
  235. int main(int argc, char **argv)
  236. {
  237. int status;
  238. /* Call the function in ux{putty,pterm}.c to do app-type
  239. * specific setup */
  240. setup(false); /* false means we are not a one-session process */
  241. if (argc > 1) {
  242. pty_osx_envrestore_prefix = argv[--argc];
  243. }
  244. {
  245. const char *home = getenv("HOME");
  246. if (home) {
  247. if (chdir(home)) {}
  248. }
  249. }
  250. gtkcomm_setup();
  251. app = gtk_application_new("org.tartarus.projects.putty.macputty",
  252. G_APPLICATION_DEFAULT_FLAGS);
  253. g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
  254. g_signal_connect(app, "startup", G_CALLBACK(startup), NULL);
  255. g_action_map_add_action_entries(G_ACTION_MAP(app),
  256. app_actions,
  257. G_N_ELEMENTS(app_actions),
  258. app);
  259. status = g_application_run(G_APPLICATION(app), argc, argv);
  260. g_object_unref(app);
  261. return status;
  262. }