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.

1448 lines
43KB

  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. #include "Endpoint.hpp"
  21. #include <FL/Fl.H>
  22. #include <FL/Fl_Window.H>
  23. #include <FL/Fl_Double_Window.H>
  24. #include <FL/Fl_Widget.H>
  25. #include <FL/Fl.H>
  26. #include <FL/Fl_File_Chooser.H>
  27. #include <FL/Fl_Box.H>
  28. #include <FL/Fl_Pack.H>
  29. #include <FL/Fl_File_Chooser.H>
  30. #include <FL/Fl_Progress.H>
  31. #include "debug.h"
  32. #include <FL/Fl_Browser.H>
  33. #include <FL/Fl_Select_Browser.H>
  34. #include <FL/Fl_Tree.H>
  35. #include <FL/Fl_Hold_Browser.H>
  36. #include <FL/Fl_Tile.H>
  37. #include <FL/Fl_Shared_Image.H>
  38. #include <FL/Fl_Box.H>
  39. #include <FL/Fl_Text_Display.H>
  40. #include "FL/Fl_Packscroller.H"
  41. #include "FL/Fl_Scalepack.H"
  42. #include <unistd.h>
  43. #include <errno.h>
  44. #include <time.h>
  45. #include <getopt.h>
  46. #define APP_NAME "NSM Legacy GUI"
  47. #define APP_TITLE "NSM Legacy GUI"
  48. #pragma GCC diagnostic ignored "-Wunused-result"
  49. // static lo_address nsm_addr = NULL;
  50. static time_t last_ping_response;
  51. static OSC::Endpoint *osc;
  52. struct Daemon
  53. {
  54. const char *url;
  55. lo_address addr;
  56. bool is_child;
  57. Daemon ( )
  58. {
  59. url = NULL;
  60. addr = NULL;
  61. is_child = false;
  62. }
  63. };
  64. static std::list<Daemon*> daemon_list; /* list of all connected daemons */
  65. #define foreach_daemon( _it ) for ( std::list<Daemon*>::iterator _it = daemon_list.begin(); _it != daemon_list.end(); ++ _it )
  66. static
  67. Fl_Image *
  68. get_program_icon ( const char *name )
  69. {
  70. const char *tries[] =
  71. {
  72. "/usr/local/share/icons/hicolor/32x32/apps/%s.png",
  73. "/usr/local/share/pixmaps/%s.png",
  74. "/usr/local/share/pixmaps/%s.xpm",
  75. "/usr/share/icons/hicolor/32x32/apps/%s.png",
  76. "/usr/share/icons/hicolor/128x128/apps/%s.png",
  77. "/usr/share/pixmaps/%s.png",
  78. "/usr/share/pixmaps/%s.xpm",
  79. };
  80. for ( unsigned int i = 0; i < 6; i++ )
  81. {
  82. char *icon_p;
  83. asprintf( &icon_p, tries[i], name );
  84. Fl_Image *img = Fl_Shared_Image::get( icon_p, 32, 32 );
  85. free( icon_p );
  86. if ( img )
  87. return img;
  88. }
  89. return NULL;
  90. }
  91. class NSM_Client : public Fl_Group
  92. {
  93. char *_client_id;
  94. char *_client_label;
  95. char *_client_name;
  96. Fl_Box *client_name;
  97. Fl_Box *icon_box;
  98. Fl_Progress *_progress;
  99. Fl_Light_Button *_dirty;
  100. Fl_Light_Button *_gui;
  101. Fl_Button *_remove_button;
  102. Fl_Button *_restart_button;
  103. Fl_Button *_kill_button;
  104. void
  105. set_label ( void )
  106. {
  107. char *l;
  108. if ( _client_label )
  109. asprintf( &l, "%s (%s)", _client_name, _client_label );
  110. else
  111. l = strdup( _client_name );
  112. if ( ! icon_box->image() )
  113. {
  114. Fl_Image *img = get_program_icon( _client_name );
  115. if ( img )
  116. {
  117. icon_box->image( img );
  118. }
  119. }
  120. client_name->copy_label( l );
  121. free(l);
  122. redraw();
  123. }
  124. public:
  125. void
  126. name ( const char *v )
  127. {
  128. if ( _client_name )
  129. free( _client_name );
  130. _client_name = strdup( v );
  131. set_label();
  132. }
  133. void
  134. client_label ( const char *s )
  135. {
  136. if ( _client_label )
  137. free( _client_label );
  138. _client_label = strdup( s );
  139. set_label();
  140. }
  141. void
  142. client_id ( const char *v )
  143. {
  144. if ( _client_id )
  145. free( _client_id );
  146. _client_id = strdup( v );
  147. }
  148. void
  149. progress ( float f )
  150. {
  151. _progress->value( f );
  152. _progress->redraw();
  153. }
  154. void
  155. dirty ( bool b )
  156. {
  157. _dirty->value( b );
  158. _dirty->redraw();
  159. }
  160. void
  161. gui_visible ( bool b )
  162. {
  163. _gui->value( b );
  164. _gui->redraw();
  165. }
  166. void
  167. has_optional_gui ( void )
  168. {
  169. _gui->show();
  170. _gui->redraw();
  171. }
  172. void
  173. stopped ( bool b )
  174. {
  175. if ( b )
  176. {
  177. _remove_button->show();
  178. _restart_button->show();
  179. _kill_button->hide();
  180. _gui->deactivate();
  181. _dirty->deactivate();
  182. color( fl_color_average( FL_BLACK, FL_RED, 0.50 ) );
  183. redraw();
  184. }
  185. else
  186. {
  187. _gui->activate();
  188. _dirty->activate();
  189. _kill_button->show();
  190. _restart_button->hide();
  191. _remove_button->hide();
  192. }
  193. /* _restart_button->redraw(); */
  194. /* _remove_button->redraw(); */
  195. }
  196. void
  197. pending_command ( const char *command )
  198. {
  199. _progress->copy_label( command );
  200. stopped( 0 );
  201. if ( ! strcmp( command, "ready" ) )
  202. {
  203. color( fl_color_average( FL_BLACK, FL_GREEN, 0.50 ) );
  204. _progress->value( 0.0f );
  205. }
  206. else if ( ! strcmp( command, "quit" ) ||
  207. ! strcmp( command, "kill" ) ||
  208. ! strcmp( command, "error" ) )
  209. {
  210. color( fl_color_average( FL_BLACK, FL_RED, 0.50 ) );
  211. }
  212. else if ( ! strcmp( command, "stopped" ) )
  213. {
  214. stopped( 1 );
  215. }
  216. else
  217. {
  218. color( fl_color_average( FL_BLACK, FL_YELLOW, 0.50 ) );
  219. }
  220. redraw();
  221. }
  222. static void
  223. cb_button ( Fl_Widget *o, void * v )
  224. {
  225. ((NSM_Client*)v)->cb_button( o );
  226. }
  227. void
  228. cb_button ( Fl_Widget *o )
  229. {
  230. if ( o == _dirty )
  231. {
  232. MESSAGE( "Sending save.");
  233. foreach_daemon ( d )
  234. {
  235. osc->send( (*d)->addr, "/nsm/gui/client/save", _client_id );
  236. }
  237. }
  238. else if ( o == _gui )
  239. {
  240. MESSAGE( "Sending hide/show GUI.");
  241. foreach_daemon ( d )
  242. {
  243. if ( !_gui->value() )
  244. osc->send( (*d)->addr, "/nsm/gui/client/show_optional_gui", _client_id );
  245. else
  246. osc->send( (*d)->addr, "/nsm/gui/client/hide_optional_gui", _client_id );
  247. }
  248. }
  249. else if ( o == _remove_button )
  250. {
  251. MESSAGE( "Sending remove.");
  252. foreach_daemon ( d )
  253. {
  254. osc->send( (*d)->addr, "/nsm/gui/client/remove", _client_id );
  255. }
  256. }
  257. else if ( o == _restart_button )
  258. {
  259. MESSAGE( "Sending resume" );
  260. foreach_daemon ( d )
  261. {
  262. osc->send( (*d)->addr, "/nsm/gui/client/resume", _client_id );
  263. }
  264. }
  265. else if ( o == _kill_button )
  266. {
  267. MESSAGE( "Sending stop" );
  268. foreach_daemon ( d )
  269. {
  270. osc->send( (*d)->addr, "/nsm/gui/client/stop", _client_id );
  271. }
  272. }
  273. }
  274. const char *
  275. client_id ( void )
  276. { return _client_id; }
  277. NSM_Client ( int X, int Y, int W, int H, const char *L ) :
  278. Fl_Group( X, Y, W, H, L )
  279. {
  280. _client_id = NULL;
  281. _client_name = NULL;
  282. _client_label = NULL;
  283. align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  284. color( fl_darker( FL_RED ) );
  285. box( FL_UP_FRAME );
  286. int yy = Y + H * 0.25;
  287. int hh = H * 0.50;
  288. int xx = X + W - ( 75 + Fl::box_dw( box() ) );
  289. int ss = 2;
  290. /* /\* dummy group *\/ */
  291. /* { Fl_Group *o = new Fl_Group( X, Y, W, H ); */
  292. /* o->end(); */
  293. /* resizable( o ); */
  294. /* } */
  295. { Fl_Pack *o = new Fl_Pack( X + 15, Y, 300 - 5, H );
  296. o->type( FL_HORIZONTAL );
  297. o->spacing( 10 );
  298. { icon_box = new Fl_Box( 0, 0, 32, 32 );
  299. }
  300. { Fl_Box *o = client_name = new Fl_Box( 0, 0, 300, 48 );
  301. /* o->color( FL_BLUE ); */
  302. o->align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT );
  303. o->labeltype( FL_NORMAL_LABEL );
  304. }
  305. o->end();
  306. }
  307. { Fl_Box *o = new Fl_Box( X + 300, Y, 100, h() );
  308. Fl_Group::current()->resizable(o);
  309. }
  310. { Fl_Progress *o = _progress = new Fl_Progress( xx, Y + H * 0.25, 75, H * 0.50, NULL );
  311. o->box( FL_FLAT_BOX );
  312. o->color( FL_DARK1, FL_LIGHT1 );
  313. o->copy_label( "launch" );
  314. o->labelsize( 12 );
  315. o->minimum( 0.0f );
  316. o->maximum( 1.0f );
  317. }
  318. { Fl_Group *o = new Fl_Group( X + W - 400, Y, 400, H );
  319. xx -= 50 + ss;
  320. { Fl_Light_Button *o = _dirty = new Fl_Light_Button( xx, yy, 50, hh, "SAVE" );
  321. o->align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  322. o->labelsize( 9 );
  323. o->box( FL_UP_BOX );
  324. o->type(0);
  325. o->color();
  326. o->selection_color( FL_YELLOW );
  327. o->value( 0 );
  328. o->callback( cb_button, this );
  329. }
  330. xx -= 40 + ss;
  331. { Fl_Light_Button *o = _gui = new Fl_Light_Button( xx, yy, 40, hh, "GUI" );
  332. o->align( FL_ALIGN_LEFT | FL_ALIGN_INSIDE );
  333. o->labelsize( 9 );
  334. o->box( FL_UP_BOX );
  335. o->type(0);
  336. o->color();
  337. o->selection_color( FL_YELLOW );
  338. o->value( 0 );
  339. o->hide();
  340. o->callback( cb_button, this );
  341. }
  342. xx -= 25 + ss;
  343. { Fl_Button *o = _kill_button = new Fl_Button( xx, yy, 25, hh, "@square" );
  344. o->labelsize( 9 );
  345. o->box( FL_UP_BOX );
  346. o->type(0);
  347. // o->color( FL_RED );
  348. o->value( 0 );
  349. o->tooltip( "Stop" );
  350. o->callback( cb_button, this );
  351. }
  352. xx -= 25 + ss;
  353. { Fl_Button *o = _restart_button = new Fl_Button( xx, yy, 25, hh );
  354. o->box( FL_UP_BOX );
  355. o->type(0);
  356. // o->color( FL_GREEN );
  357. o->value( 0 );
  358. o->label( "@>" );
  359. o->tooltip( "Resume" );
  360. o->hide();
  361. o->callback( cb_button, this );
  362. }
  363. xx -= 25 + ss;
  364. { Fl_Button *o = _remove_button = new Fl_Button( xx, yy, 25, hh );
  365. o->box( FL_UP_BOX );
  366. o->type(0);
  367. // o->color( FL_RED );
  368. o->value( 0 );
  369. o->label( "X" );
  370. o->tooltip( "Remove" );
  371. o->hide();
  372. o->callback( cb_button, this );
  373. }
  374. o->end();
  375. }
  376. end();
  377. }
  378. ~NSM_Client ( )
  379. {
  380. if ( _client_id )
  381. {
  382. free( _client_id );
  383. _client_id = NULL;
  384. }
  385. if ( _client_name )
  386. {
  387. free( _client_name );
  388. _client_name = NULL;
  389. }
  390. if ( _client_label )
  391. {
  392. free( _client_label );
  393. _client_label = NULL;
  394. }
  395. if ( label() )
  396. {
  397. free( (char*)label() );
  398. label( NULL );
  399. }
  400. }
  401. };
  402. static
  403. void
  404. fl_awake_alert( void *v )
  405. {
  406. if ( v )
  407. {
  408. fl_alert( "%s", (char*)v );
  409. free( v );
  410. }
  411. }
  412. void
  413. browser_callback ( Fl_Widget *w, void * )
  414. {
  415. w->window()->hide();
  416. }
  417. class NSM_Controller : public Fl_Group
  418. {
  419. Fl_Text_Display *status_display;
  420. public:
  421. Fl_Pack *clients_pack;
  422. Fl_Pack *buttons_pack;
  423. Fl_Button *close_button;
  424. Fl_Button *abort_button;
  425. Fl_Button *save_button;
  426. Fl_Button *open_button;
  427. Fl_Button *new_button;
  428. Fl_Button *add_button;
  429. Fl_Button *duplicate_button;
  430. Fl_Button *quit_button;
  431. Fl_Button *refresh_button;
  432. Fl_Box *session_name_box;
  433. Fl_Tree *session_browser;
  434. int status_lines;
  435. static void cb_handle ( Fl_Widget *w, void *v )
  436. {
  437. ((NSM_Controller*)v)->cb_handle( w );
  438. }
  439. void log_status ( const char *s )
  440. {
  441. time_t now;
  442. now = time( NULL );
  443. struct tm * tm = localtime( &now );
  444. char *ts;
  445. asprintf( &ts, "%02i:%02i:%02i ", tm->tm_hour, tm->tm_min, tm->tm_sec );
  446. status_display->buffer()->append( ts );
  447. free( ts );
  448. status_display->buffer()->append( s );
  449. status_display->scroll( ++status_lines, 0 );
  450. status_display->buffer()->append( "\n" );
  451. }
  452. void
  453. cb_handle ( Fl_Widget *w )
  454. {
  455. if ( w == abort_button )
  456. {
  457. if ( 0 == fl_choice( "Are you sure you want to abort this session? Unsaved changes will be lost.", "Abort", "Cancel", NULL ) )
  458. {
  459. MESSAGE( "Sending abort." );
  460. foreach_daemon ( d )
  461. {
  462. osc->send( (*d)->addr, "/nsm/server/abort" );
  463. }
  464. }
  465. }
  466. if ( w == close_button )
  467. {
  468. MESSAGE( "Sending close." );
  469. foreach_daemon ( d )
  470. {
  471. osc->send( (*d)->addr, "/nsm/server/close" );
  472. }
  473. }
  474. else if ( w == save_button )
  475. {
  476. MESSAGE( "Sending save." );
  477. foreach_daemon ( d )
  478. {
  479. osc->send( (*d)->addr, "/nsm/server/save" );
  480. }
  481. }
  482. else if ( w == open_button )
  483. {
  484. const char *name = fl_input( "Open Session", NULL );
  485. if ( ! name )
  486. return;
  487. Fl_Tree_Item *item = session_browser->find_item( name );
  488. if ( item )
  489. session_browser->select_only( item, 1 );
  490. }
  491. else if ( w == duplicate_button )
  492. {
  493. const char *name = fl_input( "New Session", NULL );
  494. if ( ! name )
  495. return;
  496. MESSAGE( "Sending duplicate for: %s", name );
  497. foreach_daemon ( d )
  498. {
  499. osc->send( (*d)->addr, "/nsm/server/duplicate", name );
  500. }
  501. }
  502. else if ( w == quit_button )
  503. {
  504. window()->do_callback( window(), this );
  505. }
  506. else if ( w == refresh_button )
  507. {
  508. session_browser->clear();
  509. session_browser->redraw();
  510. MESSAGE( "Refreshing session list." );
  511. foreach_daemon ( d )
  512. {
  513. osc->send( (*d)->addr, "/nsm/server/list" );
  514. }
  515. }
  516. else if ( w == session_browser )
  517. {
  518. if ( session_browser->callback_reason() != FL_TREE_REASON_SELECTED )
  519. return;
  520. Fl_Tree_Item *item = session_browser->callback_item();
  521. // session_browser->deselect( item, 0 );
  522. if ( item->children() )
  523. return;
  524. char name[1024];
  525. session_browser->item_pathname( name, sizeof(name), item );
  526. foreach_daemon ( d )
  527. {
  528. osc->send( (*d)->addr, "/nsm/server/open", name );
  529. }
  530. }
  531. else if ( w == new_button )
  532. {
  533. const char *name = fl_input( "New Session", NULL );
  534. if ( !name )
  535. return;
  536. MESSAGE( "Sending new for: %s", name );
  537. foreach_daemon ( d )
  538. {
  539. osc->send( (*d)->addr, "/nsm/server/new", name );
  540. }
  541. }
  542. else if ( w == add_button )
  543. {
  544. Fl_Select_Browser *browser;
  545. if ( daemon_list.size() > 1 )
  546. {
  547. Fl_Window* win = new Fl_Window( window()->x(), window()->y(), 300, 400, "Choose Server" );
  548. {
  549. {
  550. Fl_Box *o = new Fl_Box( 0,0, 300, 100 );
  551. o->label( "Connected to multiple NSM servers, please select which one to add a client to." );
  552. o->align( FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_WRAP );
  553. }
  554. {
  555. Fl_Select_Browser *o = browser = new Fl_Select_Browser( 0, 100, 300, 300 );
  556. o->box( FL_ROUNDED_BOX );
  557. o->color( FL_BLACK );
  558. o->callback( browser_callback, win );
  559. foreach_daemon( d )
  560. {
  561. o->add( (*d)->url );
  562. }
  563. }
  564. }
  565. win->end();
  566. win->show();
  567. while ( win->visible() )
  568. {
  569. Fl::wait();
  570. }
  571. if ( ! browser->value() )
  572. return;
  573. const char *n = fl_input( "Enter executable name" );
  574. if ( !n )
  575. return;
  576. char *name = strdup( n );
  577. if ( index( name, ' ' ) )
  578. {
  579. free( name );
  580. name = strdup( "nsm-proxy" );
  581. }
  582. lo_address nsm_addr = lo_address_new_from_url( browser->text( browser->value() ) );
  583. osc->send( nsm_addr, "/nsm/server/add", name );
  584. free( name );
  585. delete win;
  586. }
  587. else
  588. {
  589. const char *n = fl_input( "Enter executable name" );
  590. if ( !n )
  591. return;
  592. char *name = strdup( n );
  593. if ( index( name, ' ' ) )
  594. {
  595. free( name );
  596. name = strdup( "nsm-proxy" );
  597. }
  598. MESSAGE( "Sending add for: %s", name );
  599. /* FIXME: user should get to choose which system to do the add on */
  600. foreach_daemon ( d )
  601. {
  602. osc->send( (*d)->addr, "/nsm/server/add", name );
  603. }
  604. free( name );
  605. }
  606. }
  607. }
  608. NSM_Client *
  609. client_by_id ( const char *id )
  610. {
  611. for ( int i = clients_pack->children(); i--; )
  612. {
  613. NSM_Client *c = (NSM_Client*)clients_pack->child( i );
  614. if ( ! strcmp( c->client_id(), id ) )
  615. {
  616. return c;
  617. }
  618. }
  619. return NULL;
  620. }
  621. const char *session_name ( void ) const
  622. {
  623. return session_name_box->label();
  624. }
  625. void
  626. session_name ( const char *name )
  627. {
  628. session_name_box->copy_label( name );
  629. if ( strlen( name ) )
  630. {
  631. save_button->activate();
  632. add_button->activate();
  633. duplicate_button->activate();
  634. abort_button->activate();
  635. close_button->activate();
  636. }
  637. else
  638. {
  639. save_button->deactivate();
  640. add_button->deactivate();
  641. duplicate_button->deactivate();
  642. abort_button->deactivate();
  643. close_button->deactivate();
  644. }
  645. redraw();
  646. }
  647. void
  648. client_stopped ( const char *client_id )
  649. {
  650. NSM_Client *c = client_by_id( client_id );
  651. if ( c )
  652. {
  653. c->stopped( 1 );
  654. }
  655. }
  656. void
  657. client_quit ( const char *client_id )
  658. {
  659. NSM_Client *c = client_by_id( client_id );
  660. if ( c )
  661. {
  662. clients_pack->remove( c );
  663. delete c;
  664. }
  665. if ( clients_pack->children() == 0 )
  666. {
  667. ((Fl_Packscroller*)clients_pack->parent())->yposition( 0 );
  668. }
  669. parent()->redraw();
  670. }
  671. void
  672. client_new ( const char *client_id, const char *client_name )
  673. {
  674. NSM_Client *c;
  675. c = client_by_id( client_id );
  676. if ( c )
  677. {
  678. c->name( client_name );
  679. return;
  680. }
  681. c = new NSM_Client( 0, 0, w(), 40, NULL );
  682. c->name( client_name );
  683. c->client_id( client_id );
  684. c->stopped( 0 );
  685. clients_pack->add( c );
  686. redraw();
  687. }
  688. void client_pending_command ( NSM_Client *c, const char *command )
  689. {
  690. if ( c )
  691. {
  692. if ( ! strcmp( command, "removed" ) )
  693. {
  694. clients_pack->remove( c );
  695. delete c;
  696. parent()->redraw();
  697. }
  698. else
  699. c->pending_command( command );
  700. }
  701. }
  702. void add_session_to_list ( const char *name )
  703. {
  704. session_browser->add( name );
  705. session_browser->redraw();
  706. }
  707. NSM_Controller ( int X, int Y, int W, int H, const char *L ) :
  708. Fl_Group( X, Y, W, H, L )
  709. {
  710. status_lines = 0;
  711. align( FL_ALIGN_RIGHT | FL_ALIGN_CENTER | FL_ALIGN_INSIDE );
  712. { Fl_Pack *o = buttons_pack = new Fl_Pack( X, Y, W, 30 );
  713. o->type( Fl_Pack::HORIZONTAL );
  714. o->box( FL_NO_BOX );
  715. { Fl_Button *o = quit_button = new Fl_Button( 0, 0, 50, 50, "&Quit" );
  716. o->shortcut( FL_CTRL | 'q' );
  717. o->box( FL_UP_BOX );
  718. o->callback( cb_handle, (void*)this );
  719. }
  720. { Fl_Button *o = refresh_button = new Fl_Button( 0, 0, 70, 50, "&Refresh" );
  721. o->shortcut( FL_CTRL | 'r' );
  722. o->box( FL_UP_BOX );
  723. o->callback( cb_handle, (void*)this );
  724. }
  725. { Fl_Button *o = new_button = new Fl_Button( 0, 0, 100, 50, "&New Session" );
  726. o->shortcut( FL_CTRL | 'n' );
  727. o->box( FL_UP_BOX );
  728. o->callback( cb_handle, (void*)this );
  729. }
  730. { Fl_Button *o = save_button = new Fl_Button( 0, 0, 105, 50, "&Save" );
  731. o->shortcut( FL_CTRL | 's' );
  732. o->box( FL_UP_BOX );
  733. o->callback( cb_handle, (void*)this );
  734. }
  735. { Fl_Button *o = close_button = new Fl_Button( 0, 0, 105, 50, "Save && Close" );
  736. o->shortcut( FL_CTRL | 'e' ); // is this a good key?
  737. o->box( FL_UP_BOX );
  738. o->callback( cb_handle, (void*)this );
  739. }
  740. { Fl_Button *o = duplicate_button = new Fl_Button( 0, 0, 105, 50, "Save && &Dupl." );
  741. o->shortcut( FL_CTRL | 'd' );
  742. o->box( FL_UP_BOX );
  743. o->callback( cb_handle, (void*)this );
  744. }
  745. { Fl_Button *o = open_button = new Fl_Button( 0, 0, 105, 50, "Save && &Open" );
  746. o->shortcut( FL_CTRL | 'o' );
  747. o->box( FL_UP_BOX );
  748. o->callback( cb_handle, (void*)this );
  749. }
  750. { Fl_Button *o = abort_button = new Fl_Button( 0, 0, 160, 50, "Close &without Saving" );
  751. o->shortcut( FL_CTRL | 'w' );
  752. o->box( FL_UP_BOX );
  753. o->color( fl_color_average( FL_RED, fl_rgb_color(10,10,10), 0.5f ) );
  754. o->callback( cb_handle, (void*)this );
  755. }
  756. o->end();
  757. }
  758. int SH = 14;
  759. { Fl_Tile *o = new Fl_Tile( X, Y + 30, W, H - 30 );
  760. { Fl_Scalepack *o = new Fl_Scalepack( X, Y + 30, 300, H - ( 30 + SH ) );
  761. o->type( FL_VERTICAL );
  762. o->spacing( 2 );
  763. { new Fl_Box( 0,0,100, 24, "Sessions" );
  764. }
  765. {
  766. Fl_Tree *o = session_browser = new Fl_Tree( X, Y + 50, W / 3, H - ( 50 + SH ) );
  767. o->callback( cb_handle, (void *)this );
  768. o->color( FL_DARK1 );
  769. o->item_labelbgcolor( o->color() );
  770. o->item_labelfgcolor( FL_FOREGROUND_COLOR );
  771. o->sortorder( FL_TREE_SORT_ASCENDING );
  772. o->showroot( 0 );
  773. o->selection_color( fl_darker( FL_GREEN ) );
  774. o->selectbox( FL_UP_FRAME );
  775. o->box( FL_FLAT_BOX );
  776. /* o->label( "Sessions" ); */
  777. o->end();
  778. Fl_Group::current()->resizable( o );
  779. } // Fl_Tree
  780. o->end();
  781. }
  782. Fl_Scalepack *scalepack;
  783. { Fl_Scalepack *o = scalepack = new Fl_Scalepack( X + 300, Y + 30, W - 300, H - ( 30 + SH ) );
  784. o->type( FL_VERTICAL );
  785. o->spacing( 2 );
  786. { session_name_box = new Fl_Box( 0, 0, 100, 25, "" );
  787. }
  788. { Fl_Button *o = add_button = new Fl_Button( 0, 0, 100, 25, "&Add Client to Session" );
  789. o->shortcut( FL_CTRL | 'a' );
  790. o->box( FL_UP_BOX );
  791. o->align( FL_ALIGN_CLIP );
  792. o->callback( cb_handle, (void*)this );
  793. }
  794. {
  795. Fl_Packscroller *o = new Fl_Packscroller( 0, 0, 100, H - ( 30 + SH ) );
  796. o->align( FL_ALIGN_TOP );
  797. o->labeltype( FL_SHADOW_LABEL );
  798. {
  799. Fl_Pack *o = clients_pack = new Fl_Pack( 0, 0, 100, 100 );
  800. o->align( FL_ALIGN_TOP );
  801. o->spacing( 4 );
  802. o->type( Fl_Pack::VERTICAL );
  803. o->end();
  804. }
  805. o->end();
  806. Fl_Group::current()->resizable( o );
  807. } // Fl_Packscroller
  808. o->end();
  809. /* Fl_Group::current()->resizable( o ); */
  810. } // Fl_Scalepack
  811. { Fl_Box *o = new Fl_Box( X + 300, Y + 30, 100, H - ( 30 + SH ));
  812. Fl_Group::current()->resizable(o);
  813. }
  814. { Fl_Text_Display *o = status_display = new Fl_Text_Display( X, Y + H - SH, W, SH );
  815. o->color( FL_DARK1 );
  816. o->textcolor( FL_FOREGROUND_COLOR );
  817. o->box( FL_UP_BOX );
  818. o->textfont( FL_COURIER );
  819. o->textsize( 10 );
  820. Fl_Text_Buffer *b = new Fl_Text_Buffer();
  821. o->buffer(b);
  822. }
  823. o->end();
  824. resizable( o );
  825. } // Fl_tile
  826. end();
  827. deactivate();
  828. }
  829. int min_h ( void )
  830. {
  831. return 500;
  832. }
  833. void
  834. ping ( void )
  835. {
  836. if ( daemon_list.size() )
  837. {
  838. foreach_daemon( d )
  839. {
  840. osc->send( (*d)->addr, "/osc/ping" );
  841. }
  842. }
  843. if ( last_ping_response )
  844. {
  845. if ( time(NULL) - last_ping_response > 10 )
  846. {
  847. if ( active() )
  848. {
  849. deactivate();
  850. log_status( "Server is not responding..." );
  851. }
  852. }
  853. else
  854. {
  855. if ( !active() )
  856. {
  857. log_status( "Server is back." );
  858. activate();
  859. }
  860. }
  861. }
  862. }
  863. int init_osc ( void )
  864. {
  865. osc = new OSC::Endpoint();
  866. if ( int r = osc->init( LO_UDP ) )
  867. return r;
  868. osc->owner = this;
  869. osc->add_method( "/error", "sis", osc_handler, osc, "msg" );
  870. osc->add_method( "/reply", "ss", osc_handler, osc, "msg" );
  871. osc->add_method( "/reply", "s", osc_handler, osc, "" );
  872. osc->add_method( "/nsm/server/broadcast", NULL, osc_broadcast_handler, osc, "msg" );
  873. osc->add_method( "/nsm/gui/server_announce", "s", osc_handler, osc, "msg" );
  874. osc->add_method( "/nsm/gui/server/message", "s", osc_handler, osc, "msg" );
  875. osc->add_method( "/nsm/gui/gui_announce", "s", osc_handler, osc, "msg" );
  876. osc->add_method( "/nsm/gui/session/session", "s", osc_handler, osc, "path,display_name" );
  877. osc->add_method( "/nsm/gui/session/name", "ss", osc_handler, osc, "path,display_name" );
  878. osc->add_method( "/nsm/gui/client/new", "ss", osc_handler, osc, "path,display_name" );
  879. osc->add_method( "/nsm/gui/client/status", "ss", osc_handler, osc, "path,display_name" );
  880. osc->add_method( "/nsm/gui/client/switch", "ss", osc_handler, osc, "path,display_name" );
  881. osc->add_method( "/nsm/gui/client/progress", "sf", osc_handler, osc, "path,display_name" );
  882. osc->add_method( "/nsm/gui/client/dirty", "si", osc_handler, osc, "path,display_name" );
  883. osc->add_method( "/nsm/gui/client/has_optional_gui", "s", osc_handler, osc, "path,display_name" );
  884. osc->add_method( "/nsm/gui/client/gui_visible", "si", osc_handler, osc, "path,display_name" );
  885. osc->add_method( "/nsm/gui/client/label", "ss", osc_handler, osc, "path,display_name" );
  886. osc->start();
  887. return 0;
  888. }
  889. void announce ( const char *nsm_url )
  890. {
  891. /* Daemon *d = new Daemon; */
  892. /* d->url = nsm_url; */
  893. lo_address nsm_addr = lo_address_new_from_url( nsm_url );
  894. // d->is_child = true;
  895. /* daemon_list.push_back( d ); */
  896. osc->send( nsm_addr, "/nsm/gui/gui_announce" );
  897. }
  898. private:
  899. static int osc_broadcast_handler ( const char *path, const char *, lo_arg **, int argc, lo_message msg, void * )
  900. {
  901. if ( ! argc )
  902. /* need at least one argument... */
  903. return 0;
  904. DMESSAGE( "Relaying broadcast" );
  905. foreach_daemon( d )
  906. {
  907. char *u1 = lo_address_get_url( (*d)->addr );
  908. char *u2 = lo_address_get_url( lo_message_get_source( msg ) );
  909. if ( strcmp( u1, u2 ) )
  910. {
  911. osc->send( (*d)->addr, path, msg );
  912. }
  913. free( u1 );
  914. free( u2 );
  915. }
  916. return 0;
  917. }
  918. static int osc_handler ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  919. {
  920. // OSC_DMSG();
  921. NSM_Controller *controller = (NSM_Controller*)((OSC::Endpoint*)user_data)->owner;
  922. Fl::lock();
  923. if ( !strcmp( path, "/nsm/gui/server/message" ) && !strcmp( types, "s" ) )
  924. {
  925. controller->log_status( &argv[0]->s );
  926. }
  927. else if ( !strcmp( path, "/nsm/gui/session/session" ) &&
  928. ! strcmp( types, "s" ) )
  929. {
  930. controller->add_session_to_list( &argv[0]->s );
  931. }
  932. else if ( !strcmp( path, "/nsm/gui/gui_announce" ) )
  933. {
  934. /* pre-existing server is replying to our announce message */
  935. controller->activate();
  936. lo_address nsm_addr = lo_message_get_source( msg );
  937. osc->send( nsm_addr, "/nsm/server/list" );
  938. }
  939. else if ( !strcmp( path, "/nsm/gui/server_announce" ) )
  940. {
  941. /* must be a server we launched */
  942. controller->activate();
  943. Daemon *d = new Daemon;
  944. d->url = lo_address_get_url( lo_message_get_source( msg ) );
  945. d->addr = lo_address_new_from_url( d->url );
  946. d->is_child = true;
  947. daemon_list.push_back( d );
  948. osc->send( d->addr, "/nsm/server/list" );
  949. }
  950. else if ( !strcmp( path, "/nsm/gui/session/name" ) &&
  951. !strcmp( types, "ss" ))
  952. {
  953. controller->session_name( &argv[0]->s );
  954. if ( !strcmp( &argv[0]->s, "" ) )
  955. {
  956. controller->session_browser->deselect_all();
  957. }
  958. else
  959. {
  960. Fl_Tree_Item *o = controller->session_browser->find_item( &argv[1]->s );
  961. if ( o )
  962. {
  963. controller->session_browser->select_only( o, 0 );
  964. controller->session_browser->show_item( o, 0 );
  965. }
  966. }
  967. }
  968. else if (!strcmp( path, "/error" ) &&
  969. !strcmp( types, "sis" ) )
  970. {
  971. int err = argv[1]->i;
  972. if ( err != 0 )
  973. {
  974. char *s;
  975. asprintf( &s, "Command %s failed with:\n\n%s", &argv[0]->s, &argv[2]->s );
  976. Fl::awake(fl_awake_alert, s);
  977. }
  978. }
  979. else if (!strcmp( path, "/reply" ) && argc && 's' == *types )
  980. {
  981. if ( !strcmp( &argv[0]->s, "/nsm/server/list" ) )
  982. {
  983. controller->add_session_to_list( &argv[1]->s );
  984. }
  985. else if ( !strcmp( &argv[0]->s, "/osc/ping" ) )
  986. {
  987. last_ping_response = time( NULL );
  988. }
  989. else if ( ! strcmp( types, "ss" ) )
  990. {
  991. MESSAGE( "%s says %s", &argv[0]->s, &argv[1]->s);
  992. controller->log_status( &argv[1]->s );
  993. }
  994. }
  995. if ( !strncmp( path, "/nsm/gui/client/", strlen( "/nsm/gui/client/" ) ) )
  996. {
  997. if ( !strcmp( path, "/nsm/gui/client/new" ) &&
  998. !strcmp( types, "ss" ) )
  999. {
  1000. controller->client_new( &argv[0]->s, &argv[1]->s );
  1001. }
  1002. else
  1003. {
  1004. NSM_Client *c = controller->client_by_id( &argv[0]->s );
  1005. if ( c )
  1006. {
  1007. if ( !strcmp( path, "/nsm/gui/client/status" ) &&
  1008. !strcmp( types, "ss" ))
  1009. {
  1010. controller->client_pending_command( c, &argv[1]->s );
  1011. }
  1012. else if ( !strcmp( path, "/nsm/gui/client/progress" ) &&
  1013. !strcmp( types, "sf" ))
  1014. {
  1015. c->progress( argv[1]->f );
  1016. }
  1017. else if ( !strcmp( path, "/nsm/gui/client/dirty" ) &&
  1018. !strcmp( types, "si" ))
  1019. {
  1020. c->dirty( argv[1]->i );
  1021. }
  1022. else if ( !strcmp( path, "/nsm/gui/client/gui_visible" ) &&
  1023. !strcmp( types, "si" ))
  1024. {
  1025. c->gui_visible( argv[1]->i );
  1026. }
  1027. else if ( !strcmp( path, "/nsm/gui/client/label" ) &&
  1028. !strcmp( types, "ss" ))
  1029. {
  1030. c->client_label( &argv[1]->s );
  1031. }
  1032. else if ( !strcmp( path, "/nsm/gui/client/has_optional_gui" ) &&
  1033. !strcmp( types, "s" ))
  1034. {
  1035. c->has_optional_gui();
  1036. }
  1037. else if ( !strcmp( path, "/nsm/gui/client/switch" ) &&
  1038. !strcmp( types, "ss" ))
  1039. {
  1040. c->client_id( &argv[1]->s );
  1041. }
  1042. }
  1043. else
  1044. MESSAGE( "Got message %s from unknown client", path );
  1045. }
  1046. }
  1047. Fl::unlock();
  1048. Fl::awake();
  1049. return 0;
  1050. }
  1051. };
  1052. static NSM_Controller *controller;
  1053. void
  1054. ping ( void * )
  1055. {
  1056. controller->ping();
  1057. Fl::repeat_timeout( 1.0, ping, NULL );
  1058. }
  1059. void
  1060. cb_main ( Fl_Widget *, void * )
  1061. {
  1062. if ( Fl::event_key() != FL_Escape )
  1063. {
  1064. int children = 0;
  1065. foreach_daemon ( d )
  1066. {
  1067. if ( (*d)->is_child )
  1068. ++children;
  1069. }
  1070. if ( children )
  1071. {
  1072. if ( strlen( controller->session_name() ) )
  1073. {
  1074. fl_message( "%s", "You have to close the session before you can quit." );
  1075. return;
  1076. }
  1077. }
  1078. while ( Fl::first_window() ) Fl::first_window()->hide();
  1079. }
  1080. }
  1081. int
  1082. main (int argc, char **argv )
  1083. {
  1084. fl_register_images();
  1085. Fl::lock();
  1086. Fl_Double_Window *main_window;
  1087. {
  1088. Fl_Double_Window *o = main_window = new Fl_Double_Window( 800, 600, APP_TITLE );
  1089. {
  1090. main_window->xclass( APP_NAME );
  1091. Fl_Widget *o = controller = new NSM_Controller( 0, 0, main_window->w(), main_window->h(), NULL );
  1092. controller->session_name( "" );
  1093. Fl_Group::current()->resizable(o);
  1094. }
  1095. o->end();
  1096. o->size_range( main_window->w(), controller->min_h(), 0, 0 );
  1097. o->callback( (Fl_Callback*)cb_main, main_window );
  1098. o->show( 0, NULL );
  1099. }
  1100. static struct option long_options[] =
  1101. {
  1102. { "nsm-url", required_argument, 0, 'n' },
  1103. { "help", no_argument, 0, 'h' },
  1104. { 0, 0, 0, 0 }
  1105. };
  1106. int option_index = 0;
  1107. int c = 0;
  1108. while ( ( c = getopt_long_only( argc, argv, "", long_options, &option_index ) ) != -1 )
  1109. {
  1110. switch ( c )
  1111. {
  1112. case 'n':
  1113. {
  1114. DMESSAGE( "Adding %s to daemon list", optarg );
  1115. Daemon *d = new Daemon;
  1116. d->url = optarg;
  1117. d->addr = lo_address_new_from_url( optarg );
  1118. daemon_list.push_back( d );
  1119. break;
  1120. }
  1121. case 'h':
  1122. printf( "Usage: %s [--nsm-url...] [-- server options ]\n\n", argv[0] );
  1123. exit(0);
  1124. break;
  1125. }
  1126. }
  1127. const char *nsm_url = getenv( "NSM_URL" );
  1128. if ( nsm_url )
  1129. {
  1130. MESSAGE( "Found NSM URL of \"%s\" in environment, attempting to connect.", nsm_url );
  1131. Daemon *d = new Daemon;
  1132. d->url = nsm_url;
  1133. d->addr = lo_address_new_from_url( nsm_url );
  1134. daemon_list.push_back( d );
  1135. }
  1136. if ( controller->init_osc() )
  1137. FATAL( "Could not create OSC server" );
  1138. if ( daemon_list.size() )
  1139. {
  1140. foreach_daemon ( d )
  1141. {
  1142. controller->announce( (*d)->url );
  1143. }
  1144. }
  1145. else
  1146. {
  1147. /* start a new daemon... */
  1148. MESSAGE( "Starting daemon..." );
  1149. char *url = osc->url();
  1150. if ( ! fork() )
  1151. {
  1152. /* pass non-option arguments on to daemon */
  1153. char *args[4 + argc - optind];
  1154. int i = 0;
  1155. args[i++] = strdup("nsmd");
  1156. args[i++] = strdup("--gui-url");
  1157. args[i++] = url;
  1158. for ( ; optind < argc; i++, optind++ )
  1159. {
  1160. DMESSAGE( "Passing argument: %s", argv[optind] );
  1161. args[i] = argv[optind];
  1162. }
  1163. args[i] = 0;
  1164. if ( -1 == execvp( "nsmd", args ) )
  1165. {
  1166. FATAL( "Error starting process: %s", strerror( errno ) );
  1167. }
  1168. }
  1169. free(url);
  1170. }
  1171. Fl::add_timeout( 1.0, ping, NULL );
  1172. Fl::run();
  1173. foreach_daemon ( d )
  1174. {
  1175. if ( (*d)->is_child )
  1176. {
  1177. MESSAGE( "Telling server to quit" );
  1178. osc->send( (*d)->addr, "/nsm/server/quit" );
  1179. }
  1180. }
  1181. return 0;
  1182. }