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.

2570 lines
67KB

  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. #define __MODULE__ "nsmd"
  21. //debug.c has only one function that gets used multiple times by debug.h and for logging and printing
  22. #include "debug.h"
  23. #ifndef _GNU_SOURCE
  24. #define _GNU_SOURCE
  25. #endif
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <list>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <sys/types.h>
  33. #include <signal.h>
  34. #include <sys/signalfd.h>
  35. #include <sys/stat.h>
  36. #include <sys/wait.h>
  37. #include <unistd.h>
  38. #include <time.h>
  39. #include <libgen.h>
  40. #include <dirent.h>
  41. #include <ftw.h>
  42. #include <list>
  43. #include <getopt.h>
  44. #include <sys/time.h>
  45. #include "Endpoint.hpp"
  46. /* for locking */
  47. #include "file.h"
  48. #include <map>
  49. #include <string>
  50. #include <algorithm>
  51. #pragma GCC diagnostic ignored "-Wunused-parameter"
  52. #pragma GCC diagnostic ignored "-Wunused-result"
  53. static OSC::Endpoint *osc_server;
  54. static lo_address gui_addr;
  55. static bool gui_is_active = false;
  56. static int signal_fd;
  57. static int session_lock_fd = 0;
  58. static char *session_root;
  59. #define NSM_API_VERSION_MAJOR 1
  60. #define NSM_API_VERSION_MINOR 1
  61. #define NSM_API_VERSION_PATCH 0
  62. #define VERSION_STRING "1.4.0"
  63. #define ERR_OK 0
  64. #define ERR_GENERAL_ERROR -1
  65. #define ERR_INCOMPATIBLE_API -2
  66. #define ERR_BLACKLISTED -3
  67. #define ERR_LAUNCH_FAILED -4
  68. #define ERR_NO_SUCH_FILE -5
  69. #define ERR_NO_SESSION_OPEN -6
  70. #define ERR_UNSAVED_CHANGES -7
  71. #define ERR_NOT_NOW -8
  72. #define ERR_BAD_PROJECT -9
  73. #define ERR_CREATE_FAILED -10
  74. #define ERR_SESSION_LOCKED -11
  75. #define ERR_OPERATION_PENDING -12
  76. #define APP_TITLE "New Session Manager"
  77. enum {
  78. COMMAND_NONE = 0,
  79. COMMAND_QUIT,
  80. COMMAND_KILL,
  81. COMMAND_SAVE,
  82. COMMAND_OPEN,
  83. COMMAND_START,
  84. COMMAND_CLOSE,
  85. COMMAND_DUPLICATE,
  86. COMMAND_NEW
  87. };
  88. static int pending_operation = COMMAND_NONE;
  89. static void wait ( long );
  90. #define GUIMSG( fmt, args... ) \
  91. { \
  92. MESSAGE( fmt, ## args ); \
  93. if ( gui_is_active ) \
  94. { \
  95. char *s;\
  96. asprintf( &s, fmt, ## args );\
  97. osc_server->send( gui_addr, "/nsm/gui/server/message", s);\
  98. free(s);\
  99. }\
  100. }
  101. struct Client
  102. {
  103. private:
  104. int _reply_errcode;
  105. char *_reply_message;
  106. int _pending_command;
  107. struct timeval _command_sent_time;
  108. bool _gui_visible;
  109. char *_label;
  110. public:
  111. lo_address addr=0; /* */
  112. char *name; /* First this is the basename of client executable, later it becomes the client-reported name which must be treated as if unrelated. */
  113. char *executable_path; /* Contrary to the name this is basename(executable) */
  114. int pid; /* PID of client process */
  115. float progress; /* */
  116. bool active; /* NSM capable: client has registered via announce */
  117. //bool stopped; /* the client quit, but not because we told it to--user still has to decide to remove it from the session */
  118. char *client_id; /* short part of client ID */
  119. char *capabilities; /* client capabilities... will be null for dumb clients */
  120. bool dirty; /* flag for client self-reported dirtiness */
  121. bool pre_existing;
  122. const char *status;
  123. int launch_error; /* v1.4, leads to status for executable not found, permission denied etc. */
  124. char *name_with_id; /* v1.4, client.nABC */
  125. const char *label ( void ) const { return _label; }
  126. void label ( const char *l )
  127. {
  128. if ( _label )
  129. free( _label );
  130. if ( l )
  131. _label = strdup( l );
  132. else
  133. _label = NULL;
  134. }
  135. bool gui_visible ( void ) const
  136. {
  137. return _gui_visible;
  138. }
  139. void gui_visible ( bool b )
  140. {
  141. _gui_visible = b;
  142. }
  143. bool
  144. has_error ( void ) const
  145. {
  146. return _reply_errcode != 0;
  147. }
  148. int
  149. error_code ( void ) const
  150. {
  151. return _reply_errcode;
  152. }
  153. const char * message ( void )
  154. {
  155. return _reply_message;
  156. }
  157. void
  158. set_reply ( int errcode, const char *message )
  159. {
  160. if ( _reply_message )
  161. free( _reply_message );
  162. _reply_message = strdup( message );
  163. _reply_errcode = errcode;
  164. }
  165. bool reply_pending ( void )
  166. {
  167. return _pending_command != COMMAND_NONE;
  168. }
  169. bool is_dumb_client ( void )
  170. {
  171. return capabilities == NULL;
  172. }
  173. void pending_command ( int command )
  174. {
  175. gettimeofday( &_command_sent_time, NULL );
  176. _pending_command = command;
  177. }
  178. double milliseconds_since_last_command ( void ) const
  179. {
  180. struct timeval now;
  181. gettimeofday( &now, NULL );
  182. double elapsedms = ( now.tv_sec - _command_sent_time.tv_sec ) * 1000.0;
  183. elapsedms += ( now.tv_usec - _command_sent_time.tv_usec ) / 1000.0;
  184. return elapsedms;
  185. }
  186. int pending_command ( void )
  187. {
  188. return _pending_command;
  189. }
  190. // capability should be enclosed in colons. I.e. ":switch:"
  191. bool
  192. is_capable_of ( const char *capability ) const
  193. {
  194. return capabilities &&
  195. strstr( capabilities, capability );
  196. }
  197. Client ( )
  198. {
  199. _label = 0;
  200. _gui_visible = true;
  201. _reply_errcode = 0;
  202. _reply_message = 0;
  203. pid = 0;
  204. progress = -0;
  205. _pending_command = 0;
  206. active = false;
  207. client_id = 0;
  208. capabilities = 0;
  209. name = 0;
  210. executable_path = 0;
  211. pre_existing = false;
  212. launch_error = 0;
  213. dirty = 0;
  214. status = 0;
  215. name_with_id = 0;
  216. }
  217. ~Client ( )
  218. {
  219. if ( name )
  220. free(name);
  221. if (executable_path)
  222. free(executable_path);
  223. if (client_id)
  224. free(client_id);
  225. if (capabilities)
  226. free(capabilities);
  227. if (name_with_id)
  228. free(name_with_id);
  229. name = executable_path = client_id = capabilities = name_with_id = NULL;
  230. }
  231. };
  232. static std::list< Client* > client;
  233. /* helper macros for defining OSC handlers */
  234. #define OSC_NAME( name ) osc_ ## name
  235. // #define OSCDMSG() DMESSAGE( "Got OSC message: %s", path );
  236. #define OSC_HANDLER( name ) static int OSC_NAME( name ) ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  237. static char *session_path = NULL;
  238. static char *session_name = NULL;
  239. bool
  240. clients_have_errors ( )
  241. {
  242. for ( std::list<Client*>::const_iterator i = client.begin();
  243. i != client.end();
  244. ++i )
  245. if ( (*i)->active && (*i)->has_error() )
  246. return true;
  247. return false;
  248. }
  249. Client *
  250. get_client_by_pid ( int pid )
  251. {
  252. std::list<Client*> *cl = &client;
  253. for ( std::list<Client*>::const_iterator i = cl->begin();
  254. i != cl->end();
  255. ++i )
  256. if ( (*i)->pid == pid )
  257. return *i;
  258. return NULL;
  259. }
  260. void clear_clients ( void )
  261. {
  262. std::list<Client*> *cl = &client;
  263. for ( std::list<Client*>::iterator i = cl->begin();
  264. i != cl->end();
  265. ++i )
  266. {
  267. delete *i;
  268. i = cl->erase( i );
  269. }
  270. }
  271. void
  272. handle_client_process_death ( int pid )
  273. {
  274. Client *c = get_client_by_pid( (int)pid );
  275. if ( c )
  276. {
  277. //There is a difference if a client quit on its own, e.g. via a menu or window manager,
  278. //or if the server send SIGTERM as quit signal. Both cases are equally valid.
  279. //We only check the case to print a different log message
  280. bool dead_because_we_said = ( c->pending_command() == COMMAND_KILL ||
  281. c->pending_command() == COMMAND_QUIT );
  282. if ( dead_because_we_said )
  283. {
  284. GUIMSG( "Client %s terminated by server instruction.", c->name_with_id );
  285. }
  286. else
  287. {
  288. GUIMSG( "Client %s terminated itself.", c->name_with_id );
  289. }
  290. //Decide if the client terminated or if removed from the session
  291. if ( c->pending_command() == COMMAND_QUIT )
  292. {
  293. c->status = "removed";
  294. if ( gui_is_active )
  295. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  296. client.remove(c); //This will not remove the clients save data
  297. delete c;
  298. }
  299. else
  300. {
  301. if ( c->launch_error )
  302. /* NSM API treats the stopped status as switch. You can only remove stopped.
  303. * Furthermore the GUI will change its client-buttons.
  304. * In consequence we cannot add an arbitrary "launch-error" status.
  305. * Compatible compromise is to use the label field to relay info the user,
  306. * which was the goal. There is nothing we can do about a failed launch anyway.
  307. */
  308. c->label( "launch error!" );
  309. else
  310. c->label( "" );
  311. c->status = "stopped";
  312. if ( gui_is_active )
  313. {
  314. osc_server->send( gui_addr, "/nsm/gui/client/label", c->client_id, c->label() );
  315. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  316. }
  317. }
  318. c->pending_command( COMMAND_NONE );
  319. c->active = false;
  320. c->pid = 0;
  321. }
  322. }
  323. void handle_sigchld ( )
  324. {
  325. // compare waitpid(2)
  326. for ( ;; )
  327. {
  328. int status = 1; // make it not NULL to enable information storage in status
  329. pid_t pid = waitpid(-1, &status, WNOHANG); //-1 meaning wait for any child process. pid_t is signed integer
  330. if (pid <= 0)
  331. {
  332. break; // no child process has ended this loop. Check again.
  333. }
  334. else
  335. {
  336. //One child process has stopped. Find which and figure out the stop-conditions
  337. Client *c;
  338. c = get_client_by_pid( pid );
  339. if ( c )
  340. {
  341. //The following will not trigger with normal crashes, e.g. segfaults or python tracebacks
  342. if ( WIFEXITED( status ) ) // returns true if the child terminated normally
  343. if ( WEXITSTATUS( status ) == 255 ) // as given by exit(-1) in launch()
  344. c->launch_error = true;
  345. }
  346. // Call even if Client was already null. This will check itself again and was expected
  347. // to be called for the majority of nsmds development
  348. handle_client_process_death( pid );
  349. }
  350. }
  351. }
  352. int
  353. path_is_valid ( const char *path )
  354. {
  355. char *s;
  356. asprintf( &s, "/%s/", path );
  357. int r = strstr( s, "/../" ) == NULL;
  358. free( s );
  359. return r;
  360. }
  361. int
  362. mkpath ( const char *path, bool create_final_directory )
  363. {
  364. char *p = strdup( path );
  365. char *i = p + 1;
  366. while ( ( i = index( i, '/' ) ) )
  367. {
  368. *i = 0;
  369. struct stat st;
  370. if ( stat( p, &st ) )
  371. {
  372. if ( mkdir( p, 0711 ) )
  373. {
  374. free( p );
  375. return -1;
  376. }
  377. }
  378. *i = '/';
  379. i++;
  380. }
  381. if ( create_final_directory )
  382. {
  383. if ( mkdir( p, 0711 ) )
  384. {
  385. free( p );
  386. return -1;
  387. }
  388. }
  389. free( p );
  390. return 0;
  391. }
  392. void
  393. set_name ( const char *name )
  394. {
  395. if ( session_name )
  396. free( session_name );
  397. char *s = strdup( name );
  398. session_name = strdup( basename( s ) );
  399. free( s );
  400. }
  401. bool
  402. address_matches ( lo_address addr1, lo_address addr2 )
  403. {
  404. /* char *url1 = lo_address_get_url( addr1 ); */
  405. /* char *url2 = lo_address_get_url( addr2 ); */
  406. char *url1 = strdup( lo_address_get_port( addr1 ) );
  407. char *url2 = strdup(lo_address_get_port( addr2 ) );
  408. bool r = !strcmp( url1, url2 );
  409. free( url1 );
  410. free( url2 );
  411. return r;
  412. }
  413. Client *
  414. get_client_by_id ( std::list<Client*> *cl, const char *id )
  415. {
  416. for ( std::list<Client*>::const_iterator i = cl->begin();
  417. i != cl->end();
  418. ++i )
  419. if ( !strcmp( (*i)->client_id, id ) )
  420. return *i;
  421. return NULL;
  422. }
  423. Client *
  424. get_client_by_name_and_id ( std::list<Client*> *cl, const char *name, const char *id )
  425. {
  426. for ( std::list<Client*>::const_iterator i = cl->begin();
  427. i != cl->end();
  428. ++i )
  429. if ( !strcmp( (*i)->client_id, id ) &&
  430. ! strcmp( (*i)->name, name ) )
  431. return *i;
  432. return NULL;
  433. }
  434. Client *
  435. get_client_by_address ( lo_address addr )
  436. {
  437. for ( std::list<Client*>::iterator i = client.begin();
  438. i != client.end();
  439. ++i )
  440. if ( (*i)->addr && address_matches( (*i)->addr, addr ) )
  441. return *i;
  442. return NULL;
  443. }
  444. char *
  445. generate_client_id ( void )
  446. {
  447. /* Before v1.4 this returned "n" + 4 random upper-case letters, which could lead to collisions.
  448. We changed behaviour to still generate 4 letters, but check for collision with existing IDs.
  449. Loaded client IDs are not checked, just copied from session.nsm because loading happens before
  450. any generation of new clients. Loaded clients are part of further checks of course.
  451. There is a theoretical limit when all 26^4 IDs are in use which will lead to an infinite loop
  452. of generation. We risk to leave this unhandled. */
  453. char id_str[6];
  454. id_str[0] = 'n';
  455. id_str[5] = 0;
  456. while ( true )
  457. {
  458. for ( int i = 1; i < 5; i++ )
  459. id_str[i] = 'A' + (rand() % 25);
  460. if ( get_client_by_id(&client, id_str )==NULL ) // found a free id
  461. break;
  462. }
  463. return strdup(id_str);
  464. }
  465. bool
  466. replies_still_pending ( void )
  467. {
  468. for ( std::list<Client*>::const_iterator i = client.begin();
  469. i != client.end();
  470. ++i )
  471. if ( (*i)->active && (*i)->reply_pending() )
  472. return true;
  473. return false;
  474. }
  475. int
  476. number_of_reponsive_clients ( void )
  477. {
  478. /* This was renamed from number_of_active_clients in version 1.4 to reflect
  479. * that not only active==true clients are in a state where waiting has ended, but also clients
  480. * that never started. It is used in wait_for_announce only, which added a 5000ms delay to startup
  481. *
  482. * We are sadly unable to distinguish between a client that has a slow announce and a client
  483. * without NSM-support. However, this is mitgated by nsm-proxy which is a reliable indicator
  484. * that this program will never announce (or rather nsm-proxy announces normally).
  485. */
  486. int responsive = 0;
  487. for ( std::list<Client*>::const_iterator i = client.begin(); i != client.end(); ++i )
  488. {
  489. //Optimisation: Clients that never launched (e.g. file not found) will be checked many times/seconds here. We skip them by counting them
  490. if ( (*i)->active || (*i)->launch_error )
  491. responsive++;
  492. }
  493. return responsive;
  494. }
  495. void
  496. wait_for_announce ( void )
  497. {
  498. GUIMSG( "Waiting for announce messages from clients" );
  499. int n = 5 * 1000;
  500. long unsigned int active;
  501. while ( n > 0 )
  502. {
  503. n -= 100;
  504. wait(100);
  505. active = number_of_reponsive_clients();
  506. if ( client.size() == active )
  507. break;
  508. }
  509. GUIMSG( "Done. %lu out of %lu clients announced (or failed to launch) within the initialization grace period",
  510. active, (long unsigned)client.size() );
  511. }
  512. void
  513. wait_for_replies ( void )
  514. {
  515. GUIMSG( "Waiting for clients to reply to commands" );
  516. int n = 60 * 1000; /* 60 seconds */
  517. while ( n )
  518. {
  519. n -= 100;
  520. wait(100);
  521. if ( ! replies_still_pending() )
  522. break;
  523. }
  524. GUIMSG( "Done waiting" );
  525. /* FIXME: do something about unresponsive clients */
  526. }
  527. char *
  528. get_client_project_path ( const char *session_path, Client *c )
  529. {
  530. char *client_project_path;
  531. asprintf( &client_project_path, "%s/%s.%s", session_path, c->name, c->client_id );
  532. return client_project_path;
  533. }
  534. bool
  535. launch ( const char *executable, const char *client_id )
  536. {
  537. Client *c;
  538. if ( !client_id || !( c = get_client_by_id( &client, client_id ) ) )
  539. {
  540. c = new Client();
  541. c->executable_path = strdup( executable );
  542. {
  543. char *s = strdup( c->executable_path );
  544. c->name = strdup( basename( s ) );
  545. free( s );
  546. }
  547. if ( client_id )
  548. c->client_id = strdup( client_id );
  549. else
  550. c->client_id = generate_client_id();
  551. asprintf( &c->name_with_id, "%s.%s", c->name, c->client_id );
  552. client.push_back( c );
  553. }
  554. char * url = osc_server->url();
  555. int pid;
  556. if ( ! (pid = fork()) )
  557. {
  558. //This is code of the child process. It will be executed after launch() has finished
  559. GUIMSG( "Launching %s", executable );
  560. char *args[] = { strdup( executable ), NULL };
  561. setenv( "NSM_URL", url, 1 );
  562. /* Ensure the launched process can receive SIGCHLD */
  563. /* Unblocking SIGCHLD here does NOT unblock it for nsmd itself */
  564. sigset_t mask;
  565. sigemptyset( &mask );
  566. sigaddset( &mask, SIGCHLD );
  567. sigprocmask(SIG_UNBLOCK, &mask, NULL );
  568. if ( -1 == execvp( executable, args ) )
  569. {
  570. /* The program was not started. Causes: not installed on the current system, and the
  571. * session was transferred from another system, or permission denied (no executable flag)
  572. * Since we are running in a forked child process Client c does exist, but points to
  573. * a memory copy, not the real client. So we can't set any error code or status in the
  574. * client object. Instead we check the exit return code in handle_sigchld() and set the
  575. * bool client->launch_error to true.
  576. */
  577. WARNING( "Error starting process %s: %s", executable, strerror( errno ) );
  578. exit(-1); //-1 later parsed as 255
  579. }
  580. }
  581. //This is code of the parent process. It is executed right at this point, before the child.
  582. c->pending_command( COMMAND_START );
  583. c->pid = pid;
  584. MESSAGE( "Process %s has pid: %i", executable, pid ); //We do not have a name yet, use executable
  585. //Normal launch. Setting launch_error to false is not redundant:
  586. //A previous launch-error fixed by the user, and then resume, needs this reset.
  587. c->launch_error = false;
  588. c->status = "launch";
  589. if ( gui_is_active )
  590. {
  591. //At this point we do not know if launched program will start or fail
  592. //And we do not know if it has nsm-support or not. This will be decided if it announces.
  593. osc_server->send( gui_addr, "/nsm/gui/client/new", c->client_id, c->executable_path ); // a second message may get send with c->name, if the client sends announce()
  594. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  595. osc_server->send( gui_addr, "/nsm/gui/client/label", c->client_id, "" );
  596. }
  597. return true;
  598. }
  599. void
  600. command_client_to_save ( Client *c )
  601. {
  602. if ( c->active )
  603. {
  604. MESSAGE( "Telling %s to save", c->name_with_id );
  605. osc_server->send( c->addr, "/nsm/client/save" );
  606. c->pending_command( COMMAND_SAVE );
  607. c->status = "save";
  608. if ( gui_is_active )
  609. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  610. }
  611. else if ( c->is_dumb_client() && c->pid )
  612. {
  613. // this is a dumb client...
  614. c->status = "noop";
  615. if ( gui_is_active )
  616. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  617. }
  618. }
  619. void command_client_to_switch ( Client *c, const char *new_client_id )
  620. {
  621. char *old_client_id = c->client_id;
  622. c->client_id = strdup( new_client_id );
  623. char *client_project_path = get_client_project_path( session_path, c );
  624. MESSAGE( "Commanding %s to switch \"%s\"", c->name_with_id, client_project_path );
  625. char *full_client_id;
  626. asprintf( &full_client_id, "%s.%s", c->name, c->client_id );
  627. osc_server->send( c->addr, "/nsm/client/open", client_project_path, session_name, full_client_id );
  628. free( full_client_id );
  629. free( client_project_path );
  630. c->pending_command( COMMAND_OPEN );
  631. c->status = "switch";
  632. if ( gui_is_active )
  633. {
  634. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  635. osc_server->send( gui_addr, "/nsm/gui/client/switch", old_client_id, c->client_id );
  636. }
  637. free( old_client_id );
  638. }
  639. void
  640. purge_inactive_clients ( )
  641. {
  642. for ( std::list<Client*>::iterator i = client.begin();
  643. i != client.end();
  644. ++i )
  645. {
  646. if ( ! (*i)->active )
  647. {
  648. (*i)->status = "removed";
  649. if ( gui_is_active )
  650. osc_server->send( gui_addr, "/nsm/gui/client/status", (*i)->client_id, (*i)->status );
  651. delete *i;
  652. i = client.erase( i );
  653. }
  654. }
  655. }
  656. bool
  657. process_is_running ( int pid )
  658. {
  659. if ( 0 == kill( pid, 0 ) )
  660. {
  661. return true;
  662. }
  663. else if ( ESRCH == errno )
  664. {
  665. return false;
  666. }
  667. return false;
  668. }
  669. void
  670. purge_dead_clients ( )
  671. {
  672. std::list<Client*> tmp( client );
  673. for ( std::list<Client*>::const_iterator i = tmp.begin();
  674. i != tmp.end();
  675. ++i )
  676. {
  677. const Client *c = *i;
  678. if ( c->pid )
  679. {
  680. if ( ! process_is_running( c->pid ) )
  681. handle_client_process_death( c->pid );
  682. }
  683. }
  684. }
  685. /************************/
  686. /* OSC Message Handlers */
  687. /************************/
  688. OSC_HANDLER( add )
  689. {
  690. if ( ! session_path )
  691. {
  692. osc_server->send( lo_message_get_source( msg ), "/error", path,
  693. ERR_NO_SESSION_OPEN,
  694. "Cannot add to session because no session is loaded." );
  695. return 0;
  696. }
  697. if ( strchr( &argv[0]->s, '/' ) )
  698. {
  699. osc_server->send( lo_message_get_source( msg ), "/error", path,
  700. ERR_LAUNCH_FAILED,
  701. "Absolute paths are not permitted. Clients must be in $PATH" );
  702. return 0;
  703. }
  704. if ( ! launch( &argv[0]->s, NULL ) )
  705. {
  706. osc_server->send( lo_message_get_source( msg ), "/error", path,
  707. ERR_LAUNCH_FAILED,
  708. "Failed to launch process!" );
  709. }
  710. else
  711. {
  712. osc_server->send( lo_message_get_source( msg ), "/reply", path, "Launched." );
  713. }
  714. return 0;
  715. }
  716. OSC_HANDLER( announce )
  717. {
  718. /* A client announces itself which identifies it as real nsm-capable client, internally represented by the c->active bool.
  719. If nsmd started the client itself (e.g. through a GUI) at this point the program is already
  720. part of the session and registered with c->name=basename(executable). For these clients a
  721. second client/new message is sent, indicating an upgrade of the formerly dumb client. Through
  722. this c->name changes from executable to the self-reported client name from this announce
  723. message.
  724. Before v1.4 clients that announce themselves (started with NSM URL ENV present) never triggered
  725. the first client/new which sends an executable. This created a problem with attaching GUIs to a
  726. running nsmd never were able to infer any data from executables, like icons. Changed so that
  727. every new client scenario sends basename(executable) first.
  728. */
  729. const char *client_name = &argv[0]->s;
  730. const char *capabilities = &argv[1]->s;
  731. const char *executable_path = &argv[2]->s;
  732. int major = argv[3]->i;
  733. int minor = argv[4]->i;
  734. int pid = argv[5]->i;
  735. GUIMSG( "Got announce from %s", client_name );
  736. if ( ! session_path )
  737. {
  738. osc_server->send( lo_message_get_source( msg ), "/error",
  739. path,
  740. ERR_NO_SESSION_OPEN,
  741. "Sorry, but there's no session open for this application to join." );
  742. return 0;
  743. }
  744. bool expected_client = false;
  745. Client *c = NULL;
  746. for ( std::list<Client*>::iterator i = client.begin();
  747. i != client.end();
  748. ++i )
  749. {
  750. if ( ! strcmp( (*i)->executable_path, executable_path ) &&
  751. ! (*i)->active &&
  752. (*i)->pending_command() == COMMAND_START )
  753. {
  754. // I think we've found the slot we were looking for.
  755. MESSAGE( "Client %s was expected.", (*i)->name );
  756. c = *i;
  757. break;
  758. }
  759. }
  760. if ( ! c )
  761. {
  762. c = new Client();
  763. c->executable_path = strdup( executable_path );
  764. c->client_id = generate_client_id();
  765. }
  766. else
  767. expected_client = true;
  768. if ( major > NSM_API_VERSION_MAJOR )
  769. {
  770. MESSAGE( "Client %s is using incompatible and more recent API version %i.%i", c->name_with_id, major, minor );
  771. osc_server->send( lo_message_get_source( msg ), "/error",
  772. path,
  773. ERR_INCOMPATIBLE_API,
  774. "Server is using an incompatible API version." );
  775. return 0;
  776. }
  777. c->pid = pid;
  778. c->capabilities = strdup( capabilities );
  779. c->addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ));
  780. c->name = strdup( client_name ); //replace executable with clients self-reported pretty name
  781. c->active = true;
  782. asprintf( &c->name_with_id, "%s.%s", c->name, c->client_id );
  783. MESSAGE( "Process %s has pid: %i", c->name_with_id, pid );
  784. if ( ! expected_client )
  785. client.push_back( c );
  786. MESSAGE( "The client \"%s\" at \"%s\" informs us it's ready to receive commands.", &argv[0]->s, lo_address_get_url( c->addr ) );
  787. osc_server->send( lo_message_get_source( msg ), "/reply",
  788. path,
  789. expected_client ?
  790. "Howdy, what took you so long?" :
  791. "Well hello, stranger. Welcome to the party.",
  792. APP_TITLE,
  793. ":server-control:broadcast:optional-gui:" );
  794. c->status = "open";
  795. if ( gui_is_active )
  796. {
  797. osc_server->send( gui_addr, "/nsm/gui/client/new", c->client_id, c->name ); //pretty-name. not exectuable
  798. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  799. if ( c->is_capable_of( ":optional-gui:" ) )
  800. osc_server->send( gui_addr, "/nsm/gui/client/has_optional_gui", c->client_id );
  801. }
  802. {
  803. char *full_client_id;
  804. asprintf( &full_client_id, "%s.%s", c->name, c->client_id );
  805. char *client_project_path = get_client_project_path( session_path, c );
  806. osc_server->send( lo_message_get_source( msg ), "/nsm/client/open", client_project_path, session_name, full_client_id );
  807. c->pending_command( COMMAND_OPEN );
  808. free( full_client_id );
  809. free( client_project_path );
  810. }
  811. return 0;
  812. }
  813. void
  814. save_session_file ( )
  815. {
  816. char *session_file = NULL;
  817. asprintf( &session_file, "%s/session.nsm", session_path );
  818. FILE *fp = fopen( session_file, "w+" );
  819. free( session_file );
  820. /* FIXME: handle errors. */
  821. for ( std::list<Client*>::iterator i = client.begin();
  822. i != client.end();
  823. ++i )
  824. {
  825. fprintf( fp, "%s:%s:%s\n", (*i)->name, (*i)->executable_path, (*i)->client_id );
  826. }
  827. fclose( fp );
  828. }
  829. Client *
  830. client_by_name ( const char *name,
  831. std::list<Client*> *cl )
  832. {
  833. for ( std::list<Client*>::iterator i = cl->begin();
  834. i != cl->end();
  835. ++i )
  836. {
  837. if ( !strcmp( name, (*i)->name ) )
  838. return *i;
  839. }
  840. return NULL;
  841. }
  842. bool
  843. dumb_clients_are_alive ( )
  844. {
  845. std::list<Client*> *cl = &client;
  846. for ( std::list<Client*>::iterator i = cl->begin();
  847. i != cl->end();
  848. ++i )
  849. {
  850. if ( (*i)->is_dumb_client() && (*i)->pid > 0 )
  851. {
  852. MESSAGE( "Waiting for %s", (*i)->name_with_id ); //This replaced the Loop 1, Loop 2 ... 60 message from wait_for_dumb_clients_to_die where you couldn't see which client actually was hanging
  853. return true;
  854. }
  855. }
  856. return false;
  857. }
  858. void
  859. wait_for_dumb_clients_to_die ( )
  860. {
  861. struct signalfd_siginfo fdsi;
  862. GUIMSG( "Waiting for any dumb clients to die." );
  863. for ( int i = 0; i < 6; i++ )
  864. {
  865. if ( ! dumb_clients_are_alive() )
  866. break;
  867. ssize_t s = read(signal_fd, &fdsi, sizeof(struct signalfd_siginfo));
  868. if (s == sizeof(struct signalfd_siginfo))
  869. {
  870. if (fdsi.ssi_signo == SIGCHLD)
  871. handle_sigchld();
  872. }
  873. usleep( 50000 );
  874. }
  875. GUIMSG( "Done waiting" );
  876. /* FIXME: give up on remaining clients and purge them */
  877. }
  878. bool
  879. killed_clients_are_alive ( )
  880. {
  881. std::list<Client*> *cl = &client;
  882. for ( std::list<Client*>::iterator i = cl->begin();
  883. i != cl->end();
  884. ++i )
  885. {
  886. if ( ( (*i)->pending_command() == COMMAND_QUIT ||
  887. (*i)->pending_command() == COMMAND_KILL ) &&
  888. (*i)->pid > 0 )
  889. {
  890. MESSAGE( "Waiting for %s", (*i)->name_with_id ); //This replaced the Loop 1, Loop 2 ... 60 message from wait_for_killed_clients_to_die where you couldn't see which client actually was hanging
  891. return true;
  892. }
  893. }
  894. return false;
  895. }
  896. void
  897. wait_for_killed_clients_to_die ( )
  898. {
  899. struct signalfd_siginfo fdsi;
  900. MESSAGE( "Waiting for killed clients to die." );
  901. for ( int i = 0; i < 60; i++ )
  902. {
  903. if ( ! killed_clients_are_alive() )
  904. goto done;
  905. ssize_t s = read(signal_fd, &fdsi, sizeof(struct signalfd_siginfo));
  906. if (s == sizeof(struct signalfd_siginfo))
  907. {
  908. if (fdsi.ssi_signo == SIGCHLD)
  909. handle_sigchld();
  910. }
  911. purge_dead_clients();
  912. /* check OSC so we can get /progress messages. */
  913. osc_server->check();
  914. sleep(1);
  915. }
  916. WARNING( "Killed clients are still alive" );
  917. return;
  918. done:
  919. MESSAGE( "All clients have died." );
  920. }
  921. void
  922. command_all_clients_to_save ( )
  923. {
  924. if ( session_path )
  925. {
  926. GUIMSG( "Commanding attached clients to save." );
  927. for ( std::list<Client*>::iterator i = client.begin();
  928. i != client.end();
  929. ++i )
  930. {
  931. command_client_to_save( *i );
  932. }
  933. wait_for_replies();
  934. save_session_file();
  935. }
  936. }
  937. void
  938. command_client_to_stop ( Client *c )
  939. {
  940. GUIMSG( "Stopping client %s", c->name_with_id );
  941. if ( c->pid > 0 )
  942. {
  943. c->pending_command( COMMAND_KILL );
  944. kill( c->pid, SIGTERM );
  945. c->status = "stopped";
  946. if ( gui_is_active )
  947. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  948. }
  949. }
  950. void
  951. command_client_to_quit ( Client *c )
  952. {
  953. MESSAGE( "Commanding %s to quit", c->name_with_id );
  954. if ( c->active )
  955. {
  956. c->pending_command( COMMAND_QUIT );
  957. kill( c->pid, SIGTERM );
  958. c->status = "quit";
  959. if ( gui_is_active )
  960. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  961. }
  962. else if ( c->is_dumb_client() )
  963. {
  964. if ( c->pid > 0 )
  965. {
  966. c->status = "quit";
  967. if ( gui_is_active )
  968. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  969. /* should be kill? */
  970. c->pending_command( COMMAND_QUIT );
  971. // this is a dumb client... try and kill it
  972. kill( c->pid, SIGTERM );
  973. }
  974. else
  975. {
  976. c->status = "removed";
  977. if ( gui_is_active )
  978. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  979. }
  980. }
  981. }
  982. void
  983. close_session ( )
  984. {
  985. if ( ! session_path )
  986. return;
  987. for ( std::list<Client*>::iterator i = client.begin();
  988. i != client.end();
  989. ++i )
  990. {
  991. command_client_to_quit( *i );
  992. }
  993. wait_for_killed_clients_to_die();
  994. purge_inactive_clients();
  995. clear_clients();
  996. if ( session_path )
  997. {
  998. char *session_lock;
  999. asprintf( &session_lock, "%s/.lock", session_path );
  1000. release_lock( &session_lock_fd, session_lock );
  1001. free(session_lock);
  1002. free(session_path);
  1003. session_path = NULL;
  1004. free(session_name);
  1005. session_name = NULL;
  1006. }
  1007. if ( gui_is_active )
  1008. {
  1009. osc_server->send( gui_addr, "/nsm/gui/session/name", "", "" );
  1010. }
  1011. }
  1012. void
  1013. tell_client_session_is_loaded( Client *c )
  1014. {
  1015. if ( c->active )
  1016. //!c->is_dumb_client() )
  1017. {
  1018. MESSAGE( "Telling client %s that session is loaded.", c->name_with_id );
  1019. osc_server->send( c->addr, "/nsm/client/session_is_loaded" );
  1020. }
  1021. }
  1022. void
  1023. tell_all_clients_session_is_loaded ( void )
  1024. {
  1025. MESSAGE( "Telling all clients that session is loaded..." );
  1026. for ( std::list<Client*>::iterator i = client.begin();
  1027. i != client.end();
  1028. ++i )
  1029. {
  1030. tell_client_session_is_loaded( *i );
  1031. }
  1032. }
  1033. int
  1034. load_session_file ( const char * path )
  1035. {
  1036. char *session_file = NULL;
  1037. asprintf( &session_file, "%s/session.nsm", path );
  1038. char *session_lock = NULL;
  1039. asprintf( &session_lock, "%s/.lock", path );
  1040. if ( ! acquire_lock( &session_lock_fd, session_lock ) )
  1041. {
  1042. free( session_file );
  1043. free( session_lock );
  1044. WARNING( "Session is locked by another process" );
  1045. return ERR_SESSION_LOCKED;
  1046. }
  1047. FILE *fp;
  1048. if ( ! ( fp = fopen( session_file, "r" ) ) )
  1049. {
  1050. free( session_file );
  1051. return ERR_CREATE_FAILED;
  1052. }
  1053. free( session_file );
  1054. session_path = strdup( path );
  1055. set_name( path );
  1056. std::list<Client*> new_clients;
  1057. {
  1058. char * client_name = NULL;
  1059. char * client_executable = NULL;
  1060. char * client_id = NULL;
  1061. // load the client list
  1062. while ( fscanf( fp, "%m[^:]:%m[^:]:%m[^:\n]\n", &client_name, &client_executable, &client_id ) > 0 )
  1063. {
  1064. Client *c = new Client();
  1065. c->name = client_name;
  1066. c->executable_path = client_executable;
  1067. c->client_id = client_id;
  1068. asprintf( &c->name_with_id, "%s.%s", c->name, c->client_id );
  1069. new_clients.push_back( c );
  1070. }
  1071. }
  1072. fclose(fp);
  1073. MESSAGE( "Commanding unneeded and dumb clients to quit" );
  1074. std::map<std::string,int> client_map;
  1075. /* count how many instances of each client are needed in the new session */
  1076. for ( std::list<Client*>::iterator i = new_clients.begin();
  1077. i != new_clients.end();
  1078. ++i )
  1079. {
  1080. if ( client_map.find( (*i)->name) != client_map.end() )
  1081. client_map[(*i)->name]++;
  1082. else
  1083. client_map[(*i)->name] = 1;
  1084. }
  1085. for ( std::list<Client*>::iterator i = client.begin();
  1086. i != client.end();
  1087. ++i )
  1088. {
  1089. if ( ! (*i)->is_capable_of( ":switch:" ) || client_map.find((*i)->name ) == client_map.end() )
  1090. {
  1091. /* client is not capable of switch, or is not wanted in the new session */
  1092. command_client_to_quit( *i );
  1093. }
  1094. else
  1095. {
  1096. /* client is switch capable and may be wanted in the new session */
  1097. if ( client_map[ (*i)->name ]-- <= 0 )
  1098. /* nope,, we already have as many as we need, stop this one */
  1099. command_client_to_quit( *i );
  1100. }
  1101. }
  1102. // wait_for_replies();
  1103. wait_for_killed_clients_to_die();
  1104. // wait_for_dumb_clients_to_die();
  1105. purge_inactive_clients();
  1106. for ( std::list<Client*>::iterator i = client.begin();
  1107. i != client.end();
  1108. ++i )
  1109. {
  1110. (*i)->pre_existing = true;
  1111. }
  1112. MESSAGE( "Commanding smart clients to switch" );
  1113. for ( std::list<Client*>::iterator i = new_clients.begin();
  1114. i != new_clients.end();
  1115. ++i )
  1116. {
  1117. Client *c = NULL;
  1118. /* in a duplicated session, clients will have the same
  1119. * IDs, so be sure to pick the right one to avoid race
  1120. * conditions in JACK name registration. */
  1121. c = get_client_by_name_and_id( &client, (*i)->name, (*i)->client_id );
  1122. if ( ! c )
  1123. c = client_by_name( (*i)->name, &client );
  1124. if ( c && c->pre_existing && !c->reply_pending() )
  1125. {
  1126. // since we already shutdown clients not capable of 'switch', we can assume that these are.
  1127. command_client_to_switch( c, (*i)->client_id );
  1128. }
  1129. else
  1130. {
  1131. /* sleep a little bit because liblo derives its sequence
  1132. * of port numbers from the system time (second
  1133. * resolution) and if too many clients start at once they
  1134. * won't be able to find a free port. */
  1135. usleep( 100 * 1000 );
  1136. launch( (*i)->executable_path, (*i)->client_id );
  1137. }
  1138. }
  1139. /* this part is a little tricky... the clients need some time to
  1140. * send their 'announce' messages before we can send them 'open'
  1141. * and know that a reply is pending and we should continue waiting
  1142. * until they finish. wait_for_replies() must check for OSC
  1143. * messages immediately, even if no replies seem to be pending
  1144. * yet. */
  1145. /* dumb clients will never send an 'announce message', so we need
  1146. * to give up waiting on them fairly soon. */
  1147. wait_for_announce();
  1148. wait_for_replies();
  1149. tell_all_clients_session_is_loaded();
  1150. MESSAGE( "Loaded." );
  1151. new_clients.clear();
  1152. if ( gui_is_active )
  1153. { //This is not the case when --load-session was used. GUI announce will come later.
  1154. //Send two parameters to signal that the session was loaded. First is the direct session name,
  1155. //second is the full filepath.
  1156. //See function announce_gui for a full description where /nsm/gui/session/name is also send from
  1157. osc_server->send( gui_addr, "/nsm/gui/session/name", session_name, session_path + strlen( session_root ));
  1158. }
  1159. return ERR_OK;
  1160. }
  1161. OSC_HANDLER( save )
  1162. {
  1163. lo_address sender_addr;
  1164. sender_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ));
  1165. if ( pending_operation != COMMAND_NONE )
  1166. {
  1167. osc_server->send( sender_addr, "/error", path,
  1168. ERR_OPERATION_PENDING,
  1169. "An operation pending." );
  1170. return 0;
  1171. }
  1172. if ( ! session_path )
  1173. {
  1174. osc_server->send( sender_addr, "/error", path,
  1175. ERR_NO_SESSION_OPEN,
  1176. "No session to save.");
  1177. goto done;
  1178. }
  1179. command_all_clients_to_save();
  1180. MESSAGE( "Done." );
  1181. osc_server->send( sender_addr, "/reply", path, "Saved." );
  1182. done:
  1183. pending_operation = COMMAND_NONE;
  1184. return 0;
  1185. }
  1186. OSC_HANDLER( duplicate )
  1187. {
  1188. lo_address sender_addr;
  1189. sender_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ));
  1190. if ( pending_operation != COMMAND_NONE )
  1191. {
  1192. osc_server->send( sender_addr, "/error", path,
  1193. ERR_OPERATION_PENDING,
  1194. "An operation pending." );
  1195. return 0;
  1196. }
  1197. pending_operation = COMMAND_DUPLICATE;
  1198. if ( ! session_path )
  1199. {
  1200. osc_server->send( sender_addr, "/error", path,
  1201. ERR_NO_SESSION_OPEN,
  1202. "No session to duplicate.");
  1203. goto done;
  1204. }
  1205. if ( ! path_is_valid( &argv[0]->s ) )
  1206. {
  1207. osc_server->send( sender_addr, "/error", path,
  1208. ERR_CREATE_FAILED,
  1209. "Invalid session name." );
  1210. goto done;
  1211. }
  1212. command_all_clients_to_save();
  1213. if ( clients_have_errors() )
  1214. {
  1215. osc_server->send( sender_addr, "/error", path,
  1216. ERR_GENERAL_ERROR,
  1217. "Some clients could not save" );
  1218. goto done;
  1219. }
  1220. // save_session_file();
  1221. char *spath;
  1222. asprintf( &spath, "%s/%s", session_root, &argv[0]->s );
  1223. mkpath( spath, false );
  1224. /* FIXME: code a recursive copy instead of calling the shell */
  1225. char *cmd;
  1226. asprintf( &cmd, "cp -R \"%s\" \"%s\"", session_path, spath);
  1227. system( cmd );
  1228. free( cmd );
  1229. osc_server->send( gui_addr, "/nsm/gui/session/session", &argv[0]->s );
  1230. MESSAGE( "Attempting to open %s", spath );
  1231. if ( !load_session_file( spath ) )
  1232. {
  1233. MESSAGE( "Loaded" );
  1234. osc_server->send( sender_addr, "/reply", path,
  1235. "Loaded." );
  1236. }
  1237. else
  1238. {
  1239. MESSAGE( "Failed" );
  1240. osc_server->send( sender_addr, "/error", path,
  1241. ERR_NO_SUCH_FILE,
  1242. "No such file." );
  1243. free(spath);
  1244. return -1;
  1245. }
  1246. free( spath );
  1247. MESSAGE( "Done" );
  1248. osc_server->send( sender_addr, "/reply", path, "Duplicated." );
  1249. done:
  1250. pending_operation = COMMAND_NONE;
  1251. return 0;
  1252. }
  1253. OSC_HANDLER( new )
  1254. {
  1255. lo_address sender_addr;
  1256. sender_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ));
  1257. if ( pending_operation != COMMAND_NONE )
  1258. {
  1259. osc_server->send( sender_addr, "/error", path,
  1260. ERR_OPERATION_PENDING,
  1261. "An operation pending." );
  1262. return 0;
  1263. }
  1264. pending_operation = COMMAND_NEW;
  1265. if ( ! path_is_valid( &argv[0]->s ) )
  1266. {
  1267. osc_server->send( sender_addr, "/error", path,
  1268. ERR_CREATE_FAILED,
  1269. "Invalid session name." );
  1270. pending_operation = COMMAND_NONE;
  1271. return 0;
  1272. }
  1273. if ( session_path )
  1274. {
  1275. command_all_clients_to_save();
  1276. close_session();
  1277. }
  1278. GUIMSG( "Creating new session \"%s\"", &argv[0]->s );
  1279. char *spath;
  1280. asprintf( &spath, "%s/%s", session_root, &argv[0]->s );
  1281. if ( mkpath( spath, true ) )
  1282. {
  1283. osc_server->send( sender_addr, "/error", path,
  1284. ERR_CREATE_FAILED,
  1285. "Could not create the session directory" );
  1286. free(spath);
  1287. pending_operation = COMMAND_NONE;
  1288. return 0;
  1289. }
  1290. session_path = strdup( spath );
  1291. set_name( session_path );
  1292. osc_server->send( sender_addr, "/reply", path, "Created." );
  1293. if ( gui_is_active )
  1294. {
  1295. osc_server->send( gui_addr, "/nsm/gui/session/session", &argv[0]->s );
  1296. osc_server->send( gui_addr, "/nsm/gui/session/name", &argv[0]->s, &argv[0]->s );
  1297. }
  1298. save_session_file();
  1299. free( spath );
  1300. osc_server->send( sender_addr, "/reply", path, "Session created" );
  1301. pending_operation = COMMAND_NONE;
  1302. return 0;
  1303. }
  1304. static lo_address list_response_address;
  1305. int
  1306. list_file ( const char *fpath, const struct stat *sb, int tflag )
  1307. {
  1308. if ( tflag == FTW_F )
  1309. {
  1310. char *s;
  1311. s = strdup( fpath );
  1312. if ( ! strcmp( "session.nsm", basename( s ) ) )
  1313. {
  1314. free( s );
  1315. s = strdup( fpath );
  1316. s = dirname( s );
  1317. memmove( s, s + strlen( session_root ) + 1, (strlen( s ) - strlen( session_root )) + 1);
  1318. osc_server->send( list_response_address, "/reply", "/nsm/server/list", s );
  1319. free( s );
  1320. }
  1321. else
  1322. free( s );
  1323. }
  1324. return 0;
  1325. }
  1326. OSC_HANDLER( list )
  1327. {
  1328. GUIMSG( "Listing sessions" );
  1329. list_response_address = lo_message_get_source( msg );
  1330. ftw( session_root, list_file, 20 );
  1331. // osc_server->send( lo_message_get_source( msg ), path, ERR_OK, "Done." );
  1332. // As marker that all sessions were sent reply with an empty string, which is impossible to conflict with a session name
  1333. osc_server->send( list_response_address, "/reply", "/nsm/server/list", "" );
  1334. return 0;
  1335. }
  1336. OSC_HANDLER( open )
  1337. {
  1338. lo_address sender_addr;
  1339. sender_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ));
  1340. GUIMSG( "Opening session %s", &argv[0]->s );
  1341. if ( pending_operation != COMMAND_NONE )
  1342. {
  1343. osc_server->send( sender_addr, "/error", path,
  1344. ERR_OPERATION_PENDING,
  1345. "An operation pending." );
  1346. return 0;
  1347. }
  1348. pending_operation = COMMAND_OPEN;
  1349. if ( session_path )
  1350. {
  1351. command_all_clients_to_save();
  1352. if ( clients_have_errors() )
  1353. {
  1354. osc_server->send( sender_addr, "/error", path,
  1355. ERR_GENERAL_ERROR,
  1356. "Some clients could not save" );
  1357. pending_operation = COMMAND_NONE;
  1358. return 0;
  1359. }
  1360. // save_session_file();
  1361. }
  1362. char *spath;
  1363. asprintf( &spath, "%s/%s", session_root, &argv[0]->s );
  1364. MESSAGE( "Attempting to open %s", spath );
  1365. int err = load_session_file( spath );
  1366. if ( ! err )
  1367. {
  1368. MESSAGE( "Loaded" );
  1369. osc_server->send( sender_addr, "/reply", path,
  1370. "Loaded." );
  1371. }
  1372. else
  1373. {
  1374. MESSAGE( "Failed" );
  1375. const char *m = NULL;
  1376. switch ( err )
  1377. {
  1378. case ERR_CREATE_FAILED:
  1379. m = "Could not create session file!";
  1380. break;
  1381. case ERR_SESSION_LOCKED:
  1382. m = "Session is locked by another process!";
  1383. break;
  1384. case ERR_NO_SUCH_FILE:
  1385. m = "The named session does not exist.";
  1386. break;
  1387. default:
  1388. m = "Unknown error";
  1389. }
  1390. osc_server->send( sender_addr, "/error", path,
  1391. err,
  1392. m );
  1393. }
  1394. free( spath );
  1395. MESSAGE( "Done" );
  1396. pending_operation = COMMAND_NONE;
  1397. return 0;
  1398. }
  1399. OSC_HANDLER( quit )
  1400. {
  1401. close_session();
  1402. exit(0);
  1403. return 0;
  1404. }
  1405. OSC_HANDLER( abort )
  1406. {
  1407. lo_address sender_addr;
  1408. sender_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ));
  1409. if ( pending_operation != COMMAND_NONE )
  1410. {
  1411. osc_server->send( sender_addr, "/error", path,
  1412. ERR_OPERATION_PENDING,
  1413. "An operation pending." );
  1414. return 0;
  1415. }
  1416. pending_operation = COMMAND_CLOSE;
  1417. if ( ! session_path )
  1418. {
  1419. osc_server->send( sender_addr, "/error", path,
  1420. ERR_NO_SESSION_OPEN,
  1421. "No session to abort." );
  1422. goto done;
  1423. }
  1424. GUIMSG( "Commanding attached clients to quit." );
  1425. close_session();
  1426. osc_server->send( sender_addr, "/reply", path,
  1427. "Aborted." );
  1428. MESSAGE( "Done" );
  1429. done:
  1430. pending_operation = COMMAND_NONE;
  1431. return 0;
  1432. }
  1433. OSC_HANDLER( close )
  1434. {
  1435. lo_address sender_addr;
  1436. sender_addr = lo_address_new_from_url( lo_address_get_url( lo_message_get_source( msg ) ));
  1437. if ( pending_operation != COMMAND_NONE )
  1438. {
  1439. osc_server->send( sender_addr, "/error", path,
  1440. ERR_OPERATION_PENDING,
  1441. "An operation pending." );
  1442. return 0;
  1443. }
  1444. pending_operation = COMMAND_CLOSE;
  1445. if ( ! session_path )
  1446. {
  1447. osc_server->send( sender_addr, "/error", path,
  1448. ERR_NO_SESSION_OPEN,
  1449. "No session to close." );
  1450. goto done;
  1451. }
  1452. command_all_clients_to_save();
  1453. GUIMSG( "Commanding attached clients to quit." );
  1454. close_session();
  1455. osc_server->send( sender_addr, "/reply", path, "Closed." );
  1456. MESSAGE( "Done" );
  1457. done:
  1458. pending_operation = COMMAND_NONE;
  1459. return 0;
  1460. }
  1461. OSC_HANDLER( broadcast )
  1462. {
  1463. const char *to_path = &argv[0]->s;
  1464. /* don't allow clients to broadcast NSM commands */
  1465. if ( ! strncmp( to_path, "/nsm/", strlen( "/nsm/" ) ) )
  1466. return 0;
  1467. std::list<OSC::OSC_Value> new_args;
  1468. for ( int i = 1; i < argc; ++i )
  1469. {
  1470. switch ( types[i] )
  1471. {
  1472. case 's':
  1473. new_args.push_back( OSC::OSC_String( &argv[i]->s ) );
  1474. break;
  1475. case 'i':
  1476. new_args.push_back( OSC::OSC_Int( argv[i]->i ) );
  1477. break;
  1478. case 'f':
  1479. new_args.push_back( OSC::OSC_Float( argv[i]->f ) );
  1480. break;
  1481. }
  1482. }
  1483. char *sender_url = lo_address_get_url( lo_message_get_source( msg ) );
  1484. for ( std::list<Client*>::iterator i = client.begin();
  1485. i != client.end();
  1486. ++i )
  1487. {
  1488. if ( ! (*i)->addr )
  1489. continue;
  1490. char *url = lo_address_get_url( (*i)->addr );
  1491. if ( strcmp( sender_url, url ) )
  1492. {
  1493. osc_server->send( (*i)->addr, to_path, new_args );
  1494. }
  1495. free( url );
  1496. }
  1497. /* also relay to attached GUI so that the broadcast can be
  1498. * propagated to another NSMD instance */
  1499. if ( gui_is_active )
  1500. {
  1501. char *u1 = lo_address_get_url( gui_addr );
  1502. if ( strcmp( u1, sender_url ) )
  1503. {
  1504. new_args.push_front( OSC::OSC_String( to_path ) );
  1505. osc_server->send( gui_addr, path, new_args );
  1506. }
  1507. free(u1);
  1508. }
  1509. free( sender_url );
  1510. return 0;
  1511. }
  1512. /*********************************/
  1513. /* Client Informational Messages */
  1514. /*********************************/
  1515. OSC_HANDLER( progress )
  1516. {
  1517. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1518. if ( c )
  1519. {
  1520. c->progress = argv[0]->f;
  1521. /* MESSAGE( "%s progress: %i%%", c->name, (int)(c->progress * 100.0f) ); */
  1522. if ( gui_is_active )
  1523. {
  1524. osc_server->send( gui_addr, "/nsm/gui/client/progress", c->client_id, (float)c->progress );
  1525. }
  1526. }
  1527. return 0;
  1528. }
  1529. OSC_HANDLER( is_dirty )
  1530. {
  1531. MESSAGE( "Client sends dirty" );
  1532. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1533. if ( ! c )
  1534. return 0;
  1535. c->dirty = 1;
  1536. if ( gui_is_active )
  1537. osc_server->send( gui_addr, "/nsm/gui/client/dirty", c->client_id, c->dirty );
  1538. return 0;
  1539. }
  1540. OSC_HANDLER( is_clean )
  1541. {
  1542. MESSAGE( "Client sends clean" );
  1543. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1544. if ( ! c )
  1545. return 0;
  1546. c->dirty = 0;
  1547. if ( gui_is_active )
  1548. osc_server->send( gui_addr, "/nsm/gui/client/dirty", c->client_id, c->dirty );
  1549. return 0;
  1550. }
  1551. OSC_HANDLER( gui_is_hidden )
  1552. {
  1553. MESSAGE( "Client sends gui hidden" );
  1554. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1555. if ( ! c )
  1556. return 0;
  1557. c->gui_visible( false );
  1558. if ( gui_is_active )
  1559. osc_server->send( gui_addr, "/nsm/gui/client/gui_visible", c->client_id, c->gui_visible() );
  1560. return 0;
  1561. }
  1562. OSC_HANDLER( gui_is_shown )
  1563. {
  1564. MESSAGE( "Client sends gui shown" );
  1565. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1566. if ( ! c )
  1567. return 0;
  1568. c->gui_visible( true );
  1569. if ( gui_is_active )
  1570. osc_server->send( gui_addr, "/nsm/gui/client/gui_visible", c->client_id, c->gui_visible() );
  1571. return 0;
  1572. }
  1573. OSC_HANDLER( message )
  1574. {
  1575. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1576. if ( ! c )
  1577. return 0;
  1578. if ( gui_is_active )
  1579. osc_server->send( gui_addr, "/nsm/gui/client/message", c->client_id, argv[0]->i, &argv[1]->s );
  1580. return 0;
  1581. }
  1582. OSC_HANDLER( label )
  1583. {
  1584. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1585. if ( ! c )
  1586. return 0;
  1587. if ( strcmp( types, "s" ) )
  1588. return -1;
  1589. c->label( &argv[0]->s );
  1590. if ( gui_is_active )
  1591. osc_server->send( gui_addr, "/nsm/gui/client/label", c->client_id, c->label() );
  1592. return 0;
  1593. }
  1594. /**********************/
  1595. /* Response Handlers */
  1596. /**********************/
  1597. OSC_HANDLER( error )
  1598. {
  1599. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1600. if ( ! c )
  1601. {
  1602. WARNING( "Error from unknown client" );
  1603. return 0;
  1604. }
  1605. // const char *rpath = &argv[0]->s;
  1606. int err_code = argv[1]->i;
  1607. const char *message = &argv[2]->s;
  1608. c->set_reply( err_code, message );
  1609. MESSAGE( "Client \"%s\" replied with error: %s (%i) in %fms", c->name_with_id, message, err_code, c->milliseconds_since_last_command() );
  1610. c->pending_command( COMMAND_NONE );
  1611. c->status = "error";
  1612. if ( gui_is_active )
  1613. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  1614. return 0;
  1615. }
  1616. OSC_HANDLER( reply )
  1617. {
  1618. Client *c = get_client_by_address( lo_message_get_source( msg ) );
  1619. // const char *rpath = &argv[0]->s;
  1620. const char *message = &argv[1]->s;
  1621. if ( c )
  1622. {
  1623. c->set_reply( ERR_OK, message );
  1624. MESSAGE( "Client \"%s\" replied with: %s in %fms", c->name_with_id, message, c->milliseconds_since_last_command() );
  1625. c->pending_command( COMMAND_NONE );
  1626. c->status = "ready";
  1627. if ( gui_is_active )
  1628. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  1629. }
  1630. else
  1631. MESSAGE( "Reply from unknown client" );
  1632. return 0;
  1633. }
  1634. /******************/
  1635. /* GUI operations */
  1636. /******************/
  1637. OSC_HANDLER( stop )
  1638. {
  1639. Client *c = get_client_by_id( &client, &argv[0]->s );
  1640. if ( c )
  1641. {
  1642. command_client_to_stop( c );
  1643. if ( gui_is_active )
  1644. osc_server->send( gui_addr, "/reply", "Client stopped." );
  1645. }
  1646. else
  1647. {
  1648. if ( gui_is_active )
  1649. osc_server->send( gui_addr, "/error", -10, "No such client." );
  1650. }
  1651. return 0;
  1652. }
  1653. OSC_HANDLER( remove )
  1654. {
  1655. Client *c = get_client_by_id( &client, &argv[0]->s );
  1656. if ( c )
  1657. {
  1658. if ( c->pid == 0 &&
  1659. ! c->active )
  1660. {
  1661. c->status = "removed";
  1662. if ( gui_is_active )
  1663. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  1664. client.remove( c );
  1665. delete c;
  1666. if ( gui_is_active )
  1667. osc_server->send( gui_addr, "/reply", "Client removed." );
  1668. }
  1669. }
  1670. else
  1671. {
  1672. if ( gui_is_active )
  1673. osc_server->send( gui_addr, "/error", -10, "No such client." );
  1674. }
  1675. return 0;
  1676. }
  1677. OSC_HANDLER( resume )
  1678. {
  1679. Client *c = get_client_by_id( &client, &argv[0]->s );
  1680. /* FIXME: return error if no such client? */
  1681. if ( c )
  1682. {
  1683. if ( c->pid == 0 &&
  1684. ! c->active )
  1685. {
  1686. if ( ! launch( c->executable_path, c->client_id ) )
  1687. {
  1688. }
  1689. }
  1690. }
  1691. return 0;
  1692. }
  1693. OSC_HANDLER( client_save )
  1694. {
  1695. Client *c = get_client_by_id( &client, &argv[0]->s );
  1696. /* FIXME: return error if no such client? */
  1697. if ( c )
  1698. {
  1699. if ( c->active )
  1700. {
  1701. command_client_to_save( c );
  1702. }
  1703. }
  1704. return 0;
  1705. }
  1706. OSC_HANDLER( client_show_optional_gui )
  1707. {
  1708. Client *c = get_client_by_id( &client, &argv[0]->s );
  1709. /* FIXME: return error if no such client? */
  1710. if ( c )
  1711. {
  1712. if ( c->active )
  1713. {
  1714. osc_server->send( c->addr, "/nsm/client/show_optional_gui" );
  1715. }
  1716. }
  1717. return 0;
  1718. }
  1719. OSC_HANDLER( client_hide_optional_gui )
  1720. {
  1721. Client *c = get_client_by_id( &client, &argv[0]->s );
  1722. /* FIXME: return error if no such client? */
  1723. if ( c )
  1724. {
  1725. if ( c->active )
  1726. {
  1727. osc_server->send( c->addr, "/nsm/client/hide_optional_gui" );
  1728. }
  1729. }
  1730. return 0;
  1731. }
  1732. void
  1733. announce_gui( const char *url, bool is_reply )
  1734. {
  1735. // This is send for a new and empty nsmd as well as already running, headless, ones.
  1736. // If a GUI connects to an existing server with a running session this will trigger a list of
  1737. // clients send to the new GUI.
  1738. MESSAGE ( "A GUI announced to us from the URL %s", url );
  1739. gui_addr = lo_address_new_from_url( url );
  1740. gui_is_active = true; //global state
  1741. if ( is_reply )
  1742. // the default case. A GUI starts its own nsmd or connects to a running one
  1743. osc_server->send( gui_addr, "/nsm/gui/gui_announce", "hi" );
  1744. else
  1745. //The server was started directly and instructed to connect to a running GUI.
  1746. osc_server->send( gui_addr, "/nsm/gui/server_announce", "hi" );
  1747. osc_server->send( gui_addr, "/nsm/gui/session/root", session_root );
  1748. // Send a list of clients to the newly registered GUI in case there was already a session open
  1749. MESSAGE ( "Informing GUI about %li already running clients", client.size() );
  1750. for ( std::list<Client*>::iterator i = client.begin();
  1751. i != client.end();
  1752. ++i )
  1753. {
  1754. Client *c = *i;
  1755. osc_server->send( gui_addr, "/nsm/gui/client/new", c->client_id, c->executable_path ); // we send new twice. see announce() comment
  1756. if ( c->status )
  1757. osc_server->send( gui_addr, "/nsm/gui/client/status", c->client_id, c->status );
  1758. if ( c->is_capable_of( ":optional-gui:" ) )
  1759. osc_server->send( gui_addr, "/nsm/gui/client/has_optional_gui", c->client_id );
  1760. if ( c->label() ) // could be NULL
  1761. osc_server->send( gui_addr, "/nsm/gui/client/label", c->client_id, c->label() );
  1762. if ( c->active )
  1763. osc_server->send( gui_addr, "/nsm/gui/client/new", c->client_id, c->name ); // upgrade to pretty-name
  1764. }
  1765. //Send two parameters. The first one is the short session name, which is the directory name.
  1766. //The second parameter is the full file path.
  1767. //If both are empty it signals that no session is currently open, which is the default state if
  1768. //a GUI started nsmd.
  1769. DMESSAGE( "Informing GUI about potentially running session name: %s", session_name );
  1770. osc_server->send( gui_addr, "/nsm/gui/session/name", session_name ? session_name : "", session_path ? session_path : "" );
  1771. DMESSAGE( "Registration with GUI complete\n" );
  1772. }
  1773. OSC_HANDLER( gui_announce )
  1774. {
  1775. announce_gui( lo_address_get_url( lo_message_get_source( msg ) ), true );
  1776. return 0;
  1777. }
  1778. OSC_HANDLER( ping )
  1779. {
  1780. osc_server->send( lo_message_get_source( msg ), "/reply", path );
  1781. return 0;
  1782. }
  1783. OSC_HANDLER( null )
  1784. {
  1785. WARNING( "Unrecognized message with type signature \"%s\" at path \"%s\"", types, path );
  1786. return 0;
  1787. }
  1788. static void
  1789. wait ( long timeout )
  1790. {
  1791. struct signalfd_siginfo fdsi;
  1792. ssize_t s = read(signal_fd, &fdsi, sizeof(struct signalfd_siginfo));
  1793. if (s == sizeof(struct signalfd_siginfo))
  1794. {
  1795. if (fdsi.ssi_signo == SIGCHLD)
  1796. handle_sigchld();
  1797. }
  1798. osc_server->wait( timeout );
  1799. purge_dead_clients();
  1800. }
  1801. int main(int argc, char *argv[])
  1802. {
  1803. sigset_t mask;
  1804. sigemptyset( &mask );
  1805. sigaddset( &mask, SIGCHLD );
  1806. sigprocmask(SIG_BLOCK, &mask, NULL );
  1807. signal_fd = signalfd( -1, &mask, SFD_NONBLOCK );
  1808. /* generate random seed for client ids */
  1809. {
  1810. time_t seconds;
  1811. time(&seconds);
  1812. srand( (unsigned int) seconds );
  1813. }
  1814. //Command line parameters
  1815. char *osc_port = NULL;
  1816. const char *gui_url = NULL;
  1817. const char *load_session = NULL;
  1818. static struct option long_options[] =
  1819. {
  1820. { "detach", no_argument, 0, 'd' },
  1821. { "session-root", required_argument, 0, 's' },
  1822. { "osc-port", required_argument, 0, 'p' },
  1823. { "gui-url", required_argument, 0, 'g' },
  1824. { "help", no_argument, 0, 'h' },
  1825. { "version", no_argument, 0, 'v' },
  1826. { "load-session", required_argument, 0, 'l'},
  1827. { 0, 0, 0, 0 }
  1828. };
  1829. int option_index = 0;
  1830. int c = 0;
  1831. bool detach = false;
  1832. while ( ( c = getopt_long_only( argc, argv, "", long_options, &option_index ) ) != -1 )
  1833. {
  1834. switch ( c )
  1835. {
  1836. case 'd':
  1837. detach = true;
  1838. break;
  1839. case 's':
  1840. {
  1841. session_root = optarg;
  1842. /* get rid of trailing slash */
  1843. char *s = rindex(session_root,'/');
  1844. if ( s == &session_root[strlen(session_root) - 1] )
  1845. *s = '\0';
  1846. break;
  1847. }
  1848. case 'p':
  1849. DMESSAGE( "Using OSC port %s", optarg );
  1850. osc_port = optarg;
  1851. break;
  1852. case 'g':
  1853. DMESSAGE( "Going to connect to GUI at: %s", optarg );
  1854. gui_url = optarg;
  1855. break;
  1856. case 'l':
  1857. DMESSAGE( "Loading existing session file %s", optarg);
  1858. load_session = optarg;
  1859. break;
  1860. case 'v':
  1861. printf( "%s " VERSION_STRING "\n", argv[0] );
  1862. exit(0);
  1863. break;
  1864. case 'h':
  1865. //Print usage message according to POSIX.1-2017
  1866. const char *usage =
  1867. "nsmd - Daemon and server for the 'New Session Manager'\n\n"
  1868. "Usage:\n"
  1869. " nsmd\n"
  1870. " nsmd --help\n"
  1871. " nsmd --version\n"
  1872. "\n"
  1873. "Options:\n"
  1874. " --help Show this screen\n"
  1875. " --version Show version\n"
  1876. " --osc-port portnum OSC port number [Default: provided by system].\n"
  1877. " --session-root path Base path for sessions [Default: ~/NSM Sessions].\n"
  1878. " --load-session name Load existing session [Example: \"My Song\"].\n"
  1879. " --gui-url url Connect to running legacy-gui [Example: osc.udp://mycomputer.localdomain:38356/].\n"
  1880. " --detach Detach from console.\n"
  1881. "\n\n"
  1882. "nsmd can be run headless with existing sessions. To create new ones it is recommended to use a GUI\n"
  1883. "such as nsm-legacy-gui (included) or Argodejo (separate package)\n"
  1884. "";
  1885. printf ( usage );
  1886. exit(0);
  1887. break;
  1888. }
  1889. }
  1890. if ( !session_root )
  1891. asprintf( &session_root, "%s/%s", getenv( "HOME" ), "NSM Sessions" );
  1892. struct stat st;
  1893. if ( stat( session_root, &st ) )
  1894. {
  1895. if ( mkdir( session_root, 0771 ) )
  1896. {
  1897. FATAL( "Failed to create session directory: %s", strerror( errno ) );
  1898. }
  1899. }
  1900. MESSAGE( "Session root is: %s", session_root );
  1901. osc_server = new OSC::Endpoint();
  1902. if ( osc_server->init( LO_UDP, osc_port ) )
  1903. {
  1904. FATAL( "Failed to create OSC server." );
  1905. }
  1906. printf( "NSM_URL=%s\n", osc_server->url() );
  1907. if ( gui_url )
  1908. {
  1909. //The server was started directly and instructed to connect to a running GUI.
  1910. announce_gui( gui_url, false );
  1911. }
  1912. /* */
  1913. osc_server->add_method( "/nsm/server/announce", "sssiii", OSC_NAME( announce ), NULL, "client_name,capabilities,executable,api_version_major,api_version_minor,client_pid" );
  1914. /* response handlers */
  1915. osc_server->add_method( "/reply", "ss", OSC_NAME( reply ), NULL, "err_code,msg" );
  1916. osc_server->add_method( "/error", "sis", OSC_NAME( error ), NULL, "err_code,msg" );
  1917. osc_server->add_method( "/nsm/client/progress", "f", OSC_NAME( progress ), NULL, "progress" );
  1918. osc_server->add_method( "/nsm/client/is_dirty", "", OSC_NAME( is_dirty ), NULL, "dirtiness" );
  1919. osc_server->add_method( "/nsm/client/is_clean", "", OSC_NAME( is_clean ), NULL, "dirtiness" );
  1920. osc_server->add_method( "/nsm/client/message", "is", OSC_NAME( message ), NULL, "message" );
  1921. osc_server->add_method( "/nsm/client/gui_is_hidden", "", OSC_NAME( gui_is_hidden ), NULL, "message" );
  1922. osc_server->add_method( "/nsm/client/gui_is_shown", "", OSC_NAME( gui_is_shown ), NULL, "message" );
  1923. osc_server->add_method( "/nsm/client/label", "s", OSC_NAME( label ), NULL, "message" );
  1924. /* */
  1925. osc_server->add_method( "/nsm/gui/gui_announce", "", OSC_NAME( gui_announce ), NULL, "" );
  1926. osc_server->add_method( "/nsm/gui/client/stop", "s", OSC_NAME( stop ), NULL, "client_id" );
  1927. osc_server->add_method( "/nsm/gui/client/remove", "s", OSC_NAME( remove ), NULL, "client_id" );
  1928. osc_server->add_method( "/nsm/gui/client/resume", "s", OSC_NAME( resume ), NULL, "client_id" );
  1929. osc_server->add_method( "/nsm/gui/client/save", "s", OSC_NAME( client_save ), NULL, "client_id" );
  1930. osc_server->add_method( "/nsm/gui/client/show_optional_gui", "s", OSC_NAME( client_show_optional_gui ), NULL, "client_id" );
  1931. osc_server->add_method( "/nsm/gui/client/hide_optional_gui", "s", OSC_NAME( client_hide_optional_gui ), NULL, "client_id" );
  1932. osc_server->add_method( "/osc/ping", "", OSC_NAME( ping ), NULL, "" );
  1933. osc_server->add_method( "/nsm/server/broadcast", NULL, OSC_NAME( broadcast ), NULL, "" );
  1934. osc_server->add_method( "/nsm/server/duplicate", "s", OSC_NAME( duplicate ), NULL, "" );
  1935. osc_server->add_method( "/nsm/server/abort", "", OSC_NAME( abort ), NULL, "" );
  1936. osc_server->add_method( "/nsm/server/list", "", OSC_NAME( list ), NULL, "" );
  1937. osc_server->add_method( "/nsm/server/add", "s", OSC_NAME( add ), NULL, "executable_name" );
  1938. osc_server->add_method( "/nsm/server/new", "s", OSC_NAME( new ), NULL, "name" );
  1939. osc_server->add_method( "/nsm/server/save", "", OSC_NAME( save ), NULL, "" );
  1940. osc_server->add_method( "/nsm/server/open", "s", OSC_NAME( open ), NULL, "name" );
  1941. osc_server->add_method( "/nsm/server/close", "", OSC_NAME( close ), NULL, "" );
  1942. osc_server->add_method( "/nsm/server/quit", "", OSC_NAME( quit ), NULL, "" );
  1943. osc_server->add_method( NULL, NULL, OSC_NAME( null ),NULL, "" );
  1944. if ( load_session )
  1945. {
  1946. char *spath;
  1947. asprintf( &spath, "%s/%s", session_root, load_session); // Build the session path. --load-session works with --session-root
  1948. MESSAGE( "LOAD SESSION %s", spath);
  1949. load_session_file( spath );
  1950. free ( spath );
  1951. }
  1952. if ( detach )
  1953. {
  1954. MESSAGE( "Detaching from console" );
  1955. if ( fork() )
  1956. {
  1957. exit( 0 );
  1958. }
  1959. else
  1960. {
  1961. fclose( stdin );
  1962. fclose( stdout );
  1963. fclose( stderr );
  1964. }
  1965. }
  1966. /* listen for sigchld signals and process OSC messages forever */
  1967. for ( ;; )
  1968. {
  1969. wait( 1000 );
  1970. }
  1971. // osc_server->run();
  1972. return 0;
  1973. }