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.

294 lines
8.4KB

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