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.

292 lines
8.3KB

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