commands.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "Application.h"
  2. #include "Pad.h"
  3. #include "Socket.h"
  4. #include "Command.h"
  5. Command::CommandReturn _help(Application& app, Pad& pad, std::vector<Json::Value>& args, CommandLog& io)
  6. {
  7. app.help(io);
  8. return Command::CONTINUE;
  9. }
  10. Command::CommandReturn _(Application& app, Pad& pad, std::vector<Json::Value>& args, CommandLog& io)
  11. {
  12. return Command::CONTINUE;
  13. }
  14. Command::CommandReturn _exit(Application& app, Pad& pad, std::vector<Json::Value>& args, CommandLog& io)
  15. {
  16. return Command::EXIT;
  17. }
  18. Command::CommandReturn _load(Application& app, Pad& pad, std::vector<Json::Value>& args, CommandLog& io)
  19. {
  20. if(!pad){
  21. THROW(NoPadSelectedError, "");
  22. }
  23. if(args.size()<2 || !args[0].isString() || !args[1].isString()){
  24. THROW(ArgumentCommandError, "Pad not loaded");
  25. }
  26. if(args[0].asString()=="conf" || args[0].asString()=="configuration"){
  27. if(app.load_pad_configuration(args[1].asString())){
  28. io << "Configuration '"<< args[0].asString() << "' chargée\n";
  29. }
  30. }
  31. else if(args[0].asString()=="def" || args[0].asString()=="definition"){
  32. if(app.load_pad_definition(args[1].asString())){
  33. io << "Pad '"<< args[0].asString() << "' chargé\n";
  34. }
  35. }else{
  36. THROW(ArgumentCommandError, "Pad not loaded");
  37. }
  38. return Command::CONTINUE;
  39. }
  40. Command::CommandReturn _close(Application& app, Pad& _pad, std::vector<Json::Value>& args, CommandLog& io)
  41. {
  42. return Command::CLOSE;
  43. }
  44. Command::CommandReturn _pad(Application& app, Pad& _pad, std::vector<Json::Value>& args, CommandLog& io)
  45. {
  46. int l = args.size();
  47. if(!l) THROW(ArgumentCommandError, "");
  48. unsigned int i=0;
  49. Json::Value v = args[0];
  50. std::string cmd;
  51. Pad* pad = &_pad;
  52. if(v.isInt()){
  53. pad=&app.get_pad(v.asInt());
  54. i++;
  55. }
  56. cmd=args[i].asString();
  57. i++;
  58. if(cmd=="new" && args.size()>=i+1 && args[i].isString()){
  59. if(app.load_pad_definition(args[i].asString())){
  60. io << "Pad '"<< args[i].asString() << "' chargé\n";
  61. }else{
  62. io << "Impossible de charger le pad '"<< args[i].asString() <<"'\n";
  63. }
  64. }else if(cmd=="load" && args.size()>=i+1 && args[i].isString()){
  65. if(app.load_pad_configuration(args[i].asString())){
  66. io << "Configuration '"<< args[i].asString() << "' chargée\n";
  67. }else{
  68. io << "Impossible de charger la configuration '"<< args[i].asString() <<"'\n";
  69. }
  70. }else if(cmd=="input" && args.size()>=i+1 && args[i].isString() ){
  71. if(!pad)THROW(NoPadSelectedError, "");
  72. pad->input(args[i].asString());
  73. }else if(cmd=="remove" && args.size()>=i+1 && args[i].isInt()){
  74. if(!pad)THROW(NoPadSelectedError, "");
  75. app.remove_pad(args[i].asInt());
  76. }else if(cmd=="list"){
  77. int len = app.pad_count();
  78. io << len << " pads:\n";
  79. for(int i=0; i<len; i++){
  80. Pad& p = app[i];
  81. if(&p){
  82. io << "\t" << ((&p==&_pad)?(">"):(" ")) << app.get_id(p.get_name())
  83. << " : " << p.get_name() <<"\n" ;
  84. }
  85. }
  86. }else if(cmd=="select" && args.size()>=i+1 && (args[i].isInt() || args[i].isString())){
  87. if(args[i].isInt()) app.set_pad(args[i].asInt());
  88. else app.set_pad(args[i].asString());
  89. }else if(cmd=="clear"){
  90. pad->clear();
  91. }else{
  92. THROW(ArgumentCommandError, "Erreur: argmunents non reconnus...")
  93. }
  94. return Command::CONTINUE;
  95. }
  96. void add_commands(Application& app)
  97. {
  98. app.add_command("help", _help, "Affiche cette aide");
  99. app.add_command("close", _close, "Ferme la connexion avec la socket");
  100. app.add_command("load", _load, "Charge une définition ou une configuration\n\
  101. \tUsage: load (conf[figuration] | def[finition] \"FILE_OR_NAME\"\n\
  102. \t\tFILE_OR_NAME: fichier de configuration (chemin) ou nom du pad");
  103. app.add_command("pad", _pad, "Commande de pad.\n"
  104. "\tUsage: pad [ID] (new \"NAME\"|load \"FILE\"|input \"ARGS\"|list|remmove|select ID)\n"
  105. "\t\tID: Préciser l'id du pad pour le sélectionner, la commande suivante s'appliquera sur ce pad\n"
  106. "\t\tnew \"NAME\": Charge et selectionne en pad actif le pad NAME. Si le pad est déja chargé, il ser simplement sélectionné\n"
  107. "\t\tload \"FILE\": Charger la configuration du pad FILE. Si le pad de la configuration n'est pas chargé, il le sera automatiquement "
  108. "sinon il écrasera la configuration précédente.\n"
  109. "\t\tinput \"ARGS\": Simule une entrée du pad. ARGS doit être un Json de MidiMessage ou MidiMEssageList (exemple:"
  110. "[\"noteon\", 1, 54,127] ou [[\"noteon\", 1, 54,127][\"noteon\", 1, 56,127]])\n"
  111. "\t\tremove: supprime le pad actif (ou celui explicitement cité)\n"
  112. "\t\select ID: Change le pad actif par ID\n"
  113. "\t\tlist: Liste les différents pad\n");
  114. app.add_command("exit", _exit, "Ferme l'application");
  115. }