Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
8.4KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008-2020 Jonathan Moore Liles (as "Non-Session-Manager") */
  3. /* */
  4. /* This file is part of New-Session-Manager */
  5. /* */
  6. /* New-Session-Manager is free software: you can redistribute it and/or modify */
  7. /* it under the terms of the GNU General Public License as published by */
  8. /* the Free Software Foundation, either version 3 of the License, or */
  9. /* (at your option) any later version. */
  10. /* */
  11. /* New-Session-Manager is distributed in the hope that it will be useful, */
  12. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  13. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  14. /* GNU General Public License for more details. */
  15. /* */
  16. /* You should have received a copy of the GNU General Public License */
  17. /* along with New-Session-Manager. If not, see <https://www.gnu.org/licenses/>.*/
  18. /*******************************************************************************/
  19. #pragma GCC diagnostic ignored "-Wunused-parameter"
  20. #define _MODULE_ "nsm-proxy-gui"
  21. #define APP_NAME "NSM Proxy"
  22. #define APP_TITLE "NSM Proxy"
  23. #include <FL/Fl_File_Chooser.H>
  24. #include <FL/Fl_Text_Display.H>
  25. #include "NSM_Proxy_UI.H"
  26. #include <lo/lo.h>
  27. #include <signal.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. lo_server losrv;
  32. lo_address nsmp_addr;
  33. static NSM_Proxy_UI *ui;
  34. static char *client_error;
  35. int
  36. osc_update ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  37. {
  38. printf( "Got update for %s\n", path );
  39. Fl::lock();
  40. if (!strcmp( path, "/nsm/proxy/label" ))
  41. ui->label_input->value( &argv[0]->s );
  42. else if (!strcmp( path, "/nsm/proxy/arguments" ))
  43. ui->arguments_input->value( &argv[0]->s );
  44. else if (!strcmp( path, "/nsm/proxy/executable" ))
  45. ui->executable_input->value( &argv[0]->s );
  46. else if (!strcmp( path, "/nsm/proxy/config_file" ))
  47. ui->config_file_input->value( &argv[0]->s );
  48. else if (!strcmp( path, "/nsm/proxy/save_signal" ))
  49. {
  50. if ( argv[0]->i == SIGUSR1 )
  51. ui->save_signal_choice->value( 1 );
  52. else if ( argv[0]->i == SIGUSR2 )
  53. ui->save_signal_choice->value( 2 );
  54. else if ( argv[0]->i == SIGINT )
  55. ui->save_signal_choice->value( 3 );
  56. else
  57. ui->save_signal_choice->value( 0 );
  58. }
  59. else if (!strcmp( path, "/nsm/proxy/stop_signal" ))
  60. {
  61. if ( argv[0]->i == SIGTERM )
  62. ui->stop_signal_choice->value( 0 );
  63. else if ( argv[0]->i == SIGINT )
  64. ui->stop_signal_choice->value( 1 );
  65. else if ( argv[0]->i == SIGHUP )
  66. ui->stop_signal_choice->value( 2 );
  67. }
  68. if (!strcmp( path, "/nsm/proxy/client_error" ))
  69. {
  70. if ( client_error != NULL )
  71. free(client_error);
  72. client_error = NULL;
  73. if ( strlen(&argv[0]->s) > 0 )
  74. client_error = strdup(&argv[0]->s);
  75. }
  76. Fl::unlock();
  77. return 0;
  78. }
  79. void
  80. init_osc ( const char *osc_port )
  81. {
  82. lo_server_thread loth = lo_server_thread_new( osc_port, NULL );
  83. losrv = lo_server_thread_get_server( loth );
  84. //error_handler );
  85. char *url = lo_server_get_url(losrv);
  86. printf("OSC: %s\n",url);
  87. free(url);
  88. /* GUI */
  89. lo_server_thread_add_method( loth, "/nsm/proxy/executable", "s", osc_update, NULL );
  90. lo_server_thread_add_method( loth, "/nsm/proxy/arguments", "s", osc_update, NULL );
  91. lo_server_thread_add_method( loth, "/nsm/proxy/config_file", "s", osc_update, NULL );
  92. lo_server_thread_add_method( loth, "/nsm/proxy/label", "s", osc_update, NULL );
  93. lo_server_thread_add_method( loth, "/nsm/proxy/save_signal", "i", osc_update, NULL );
  94. lo_server_thread_add_method( loth, "/nsm/proxy/stop_signal", "i", osc_update, NULL );
  95. lo_server_thread_add_method( loth, "/nsm/proxy/client_error", "s", osc_update, NULL );
  96. lo_server_thread_start( loth );
  97. }
  98. /*****************/
  99. /* GUI Callbacks */
  100. /*****************/
  101. void
  102. handle_kill ( Fl_Widget *o, void *v )
  103. {
  104. lo_send_from( nsmp_addr, losrv, LO_TT_IMMEDIATE, "/nsm/proxy/kill", "" );
  105. }
  106. void
  107. handle_start ( Fl_Widget *o, void *v )
  108. {
  109. lo_send_from( nsmp_addr, losrv, LO_TT_IMMEDIATE, "/nsm/proxy/start", "sss",
  110. ui->executable_input->value(),
  111. ui->arguments_input->value(),
  112. ui->config_file_input->value() );
  113. }
  114. void
  115. handle_label ( Fl_Widget *o, void *v )
  116. {
  117. lo_send_from( nsmp_addr, losrv, LO_TT_IMMEDIATE, "/nsm/proxy/label", "s",
  118. ui->label_input->value() );
  119. }
  120. void
  121. handle_executable ( Fl_Widget *o, void *v )
  122. {
  123. ui->label_input->value( ui->executable_input->value() );
  124. }
  125. void
  126. handle_config_file ( Fl_Widget *o, void *v )
  127. {
  128. }
  129. void
  130. handle_config_file_browse ( Fl_Widget *o, void *v )
  131. {
  132. const char * file = fl_file_chooser( "Pick file", "*", NULL, 1 );
  133. ui->config_file_input->value( file );
  134. }
  135. void
  136. handle_save_signal ( Fl_Widget *o, void *v )
  137. {
  138. int sig = 0;
  139. const char* picked = ui->save_signal_choice->mvalue()->label();
  140. if ( !strcmp( picked, "SIGUSR1" ) )
  141. sig = SIGUSR1;
  142. else if ( !strcmp( picked, "SIGUSR2" ) )
  143. sig = SIGUSR2;
  144. else if ( !strcmp( picked, "SIGINT" ) )
  145. sig = SIGINT;
  146. lo_send_from( nsmp_addr, losrv, LO_TT_IMMEDIATE,"/nsm/proxy/save_signal", "i",
  147. sig );
  148. }
  149. void
  150. handle_stop_signal ( Fl_Widget *o, void *v )
  151. {
  152. int sig = SIGTERM;
  153. const char* picked = ui->stop_signal_choice->mvalue()->label();
  154. if ( !strcmp( picked, "SIGTERM" ) )
  155. sig = SIGTERM;
  156. else if ( !strcmp( picked, "SIGINT" ) )
  157. sig = SIGINT;
  158. else if ( !strcmp( picked, "SIGHUP" ) )
  159. sig = SIGHUP;
  160. lo_send_from( nsmp_addr, losrv, LO_TT_IMMEDIATE,"/nsm/proxy/stop_signal", "i",
  161. sig );
  162. }
  163. void
  164. connect_ui ( void )
  165. {
  166. ui->executable_input->callback( handle_executable, NULL );
  167. ui->config_file_input->callback( handle_config_file, NULL );
  168. ui->kill_button->callback( handle_kill, NULL );
  169. ui->start_button->callback( handle_start, NULL );
  170. ui->save_signal_choice->callback( handle_save_signal, NULL );
  171. ui->stop_signal_choice->callback( handle_stop_signal, NULL );
  172. ui->label_input->callback( handle_label, NULL );
  173. ui->config_file_browse_button->callback( handle_config_file_browse, NULL );
  174. }
  175. void cb_dismiss_button ( Fl_Widget *w, void *v )
  176. {
  177. w->window()->hide();
  178. }
  179. void
  180. check_error ( void *v )
  181. {
  182. if ( client_error )
  183. {
  184. {
  185. Fl_Double_Window *o = new Fl_Double_Window(600,300+15,"Abnormal Termination");
  186. {
  187. Fl_Box *o = new Fl_Box(0+15,0+15,600-30,50);
  188. o->box(FL_BORDER_BOX);
  189. o->color(FL_RED);
  190. o->labelcolor(FL_WHITE);
  191. o->align(FL_ALIGN_CENTER|FL_ALIGN_WRAP);
  192. o->copy_label( client_error );
  193. }
  194. {
  195. Fl_Text_Display *o = new Fl_Text_Display(0+15,50+15,600-30,300-75-30);
  196. o->buffer(new Fl_Text_Buffer());
  197. o->buffer()->loadfile( "error.log" );
  198. }
  199. {
  200. Fl_Button *o = new Fl_Button(600-75-15,300-25,75,25,"Dismiss");
  201. o->callback(cb_dismiss_button,0);
  202. }
  203. o->show();
  204. }
  205. free(client_error);
  206. client_error = NULL;
  207. }
  208. Fl::repeat_timeout( 0.5f, check_error, v );
  209. }
  210. int
  211. main ( int argc, char **argv )
  212. {
  213. if ( argc != 3 )
  214. {
  215. fprintf( stderr, "Usage: %s --connect-to url\n", argv[0] );
  216. return 1;
  217. }
  218. init_osc( NULL );
  219. nsmp_addr = lo_address_new_from_url( argv[2] );
  220. printf( "Connecting to nsm-proxy at: %s\n", argv[2] );
  221. ui = new NSM_Proxy_UI;
  222. Fl_Double_Window *w = ui->make_window();
  223. connect_ui();
  224. lo_send_from( nsmp_addr, losrv, LO_TT_IMMEDIATE, "/nsm/proxy/update", "" );
  225. w->show();
  226. Fl::lock();
  227. Fl::add_timeout( 0.5f, check_error, NULL );
  228. Fl::run();
  229. return 0;
  230. }