port-connector.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. PortConnector::PortConnector(View* parent) : PanelItem(parent, Size{~0, ~0}) {
  2. setCollapsible().setVisible(false);
  3. renameAction.setIcon(Icon::Application::TextEditor).setText("Rename ...").onActivate([&] { eventRename(); });
  4. removeAction.setIcon(Icon::Action::Remove).setText("Delete ...").onActivate([&] { eventRemove(); });
  5. locationLabel.setMouseCursor(MouseCursor::Hand);
  6. locationLabel.setFont(Font().setBold());
  7. locationLabel.setForegroundColor({0, 0, 240});
  8. locationLabel.onMousePress([&](auto button) {
  9. if(button == Mouse::Button::Left) eventBrowse();
  10. });
  11. peripheralList.onActivate([&] { eventActivate(); });
  12. peripheralList.onChange([&] { eventChange(); });
  13. peripheralList.onContext([&] { eventContext(); });
  14. importButton.setText("Import ...").onActivate([&] { eventImport(); });
  15. acceptButton.setText("Create").onActivate([&] { eventActivate(); });
  16. }
  17. auto PortConnector::show() -> void {
  18. setVisible(true);
  19. }
  20. auto PortConnector::hide() -> void {
  21. setVisible(false);
  22. port = {};
  23. }
  24. auto PortConnector::isMiaType() const -> bool {
  25. if(!port) return false; //should never occur
  26. if(port->type() == "Cartridge") return true;
  27. if(port->type() == "Floppy Disk") return true;
  28. if(port->type() == "Compact Disc") return true;
  29. return false;
  30. }
  31. auto PortConnector::refresh(higan::Node::Port port) -> void {
  32. this->port = port;
  33. importButton.setVisible(isMiaType());
  34. auto path = port->attribute("path");
  35. //is this the first time refreshing this port?
  36. if(!path) {
  37. //see if there's a shared icarus path for it
  38. if(isMiaType()) {
  39. path = {Path::user(), "Emulation/", port->family(), "/"};
  40. }
  41. //if not, use a local path for it
  42. if(!directory::exists(path)) {
  43. path = {emulator.system.data, port->family(), " ", port->type(), "/"};
  44. }
  45. port->setAttribute("path", path);
  46. }
  47. //if this is a user path, replace the home directory with ~ for brevity
  48. if(path.beginsWith(Path::user())) {
  49. locationLabel.setText(string{path}.trimLeft(Path::user(), 1L).prepend("~/"));
  50. } else {
  51. locationLabel.setText(path);
  52. }
  53. peripheralList.reset();
  54. ListViewItem item{&peripheralList};
  55. item.setAttribute("type", "nothing");
  56. item.setIcon(Icon::Action::Remove).setText("Nothing");
  57. if(!port->connected()) item.setSelected().setFocused();
  58. if(string location = {emulator.system.templates, port->type(), "/"}) {
  59. for(auto& name : directory::folders(location)) {
  60. //if(!port->supported().find(name.trimRight("/", 1L))) continue;
  61. ListViewItem item{&peripheralList};
  62. item.setAttribute("type", "template");
  63. item.setAttribute("location", {location, name, "/"});
  64. item.setAttribute("path", location);
  65. item.setAttribute("name", name.trimRight("/", 1L));
  66. item.setIcon(Icon::Action::Add).setText(name);
  67. }
  68. }
  69. //for(auto& name : port->supported()) {
  70. // ListViewItem item{&peripheralList};
  71. // item.setAttribute("type", "template");
  72. // item.setAttribute("name", name);
  73. // item.setIcon(Icon::Action::Add).setText(name);
  74. //}
  75. for(auto& name : directory::folders(path)) {
  76. ListViewItem item{&peripheralList};
  77. item.setAttribute("type", "peripheral");
  78. item.setAttribute("location", {path, name});
  79. item.setAttribute("path", path);
  80. item.setAttribute("name", name.trimRight("/", 1L));
  81. item.setIcon(Icon::Emblem::Folder).setText(name);
  82. //set a currently connected peripheral as the active item
  83. if(auto peripheral = port->connected()) {
  84. if(name == peripheral->attribute("name")) {
  85. item.setSelected().setFocused();
  86. }
  87. }
  88. }
  89. peripheralList.doChange();
  90. }
  91. auto PortConnector::eventImport() -> void {
  92. if(auto location = execute("icarus", "--system", interface->name(), "--import").output) {
  93. refresh(port);
  94. }
  95. }
  96. auto PortConnector::eventActivate() -> void {
  97. if(auto item = peripheralList.selected()) {
  98. if(!port->hotSwappable() && emulator.system.power) {
  99. auto response = MessageDialog()
  100. .setText("The peripheral currently connected to this port isn't hot-swappable.\n"
  101. "Removing it anyway may crash the emulated system.\n"
  102. "What would you like to do?")
  103. .setTitle("Warning").setAlignment(program).question({"Force", "Power Off", "Cancel"});
  104. if(response == "Cancel") return;
  105. if(response == "Power Off") emulator.power(false);
  106. }
  107. auto name = item.attribute("name");
  108. if(item.attribute("type") == "nothing") {
  109. port->disconnect();
  110. program.refreshPanelList();
  111. }
  112. else if(item.attribute("type") == "template") {
  113. auto label = NameDialog()
  114. .setTitle({"Create New ", name})
  115. .setAlignment(program)
  116. .create(item.text());
  117. if(!label) return;
  118. auto source = item.attribute("location");
  119. auto target = string{emulator.system.data, port->family(), " ", port->type(), "/", label, "/"};
  120. if(directory::exists(target)) return (void)MessageDialog()
  121. .setText("A directory by this name already exists.")
  122. .setTitle("Error").setAlignment(program).error();
  123. //if(directory::create(target)) {
  124. if(directory::copy(source, target)) {
  125. if(auto peripheral = port->allocate(name)) {
  126. peripheral->setAttribute("location", target);
  127. peripheral->setAttribute("name", label);
  128. port->connect();
  129. inputManager.bind(); //bind any inputs this peripheral may contain
  130. program.refreshPanelList();
  131. refresh(port);
  132. }
  133. }
  134. }
  135. else if(item.attribute("type") == "peripheral") {
  136. auto location = item.attribute("location");
  137. auto connected = emulator.connected(location);
  138. //treat selecting the already-connected device as a no-op that aborts the port connection dialog
  139. if(connected && connected == port) return;
  140. if(connected) return (void)MessageDialog()
  141. .setText({"This peripheral is already connected to another port:\n\n", connected->name()})
  142. .setTitle("Error").setAlignment(program).error();
  143. if(auto markup = file::read({location, "settings.bml"})) {
  144. auto node = higan::Node::unserialize(markup);
  145. //update the location and name here, in case the folder moved since it was last connected
  146. node->setAttribute("location", location);
  147. node->setAttribute("name", item.attribute("name"));
  148. if(auto peripheral = port->allocate(node->name())) {
  149. peripheral->copy(node);
  150. port->connect();
  151. peripheral->copy(node);
  152. }
  153. } else {
  154. if(auto peripheral = port->allocate()) {
  155. peripheral->setAttribute("location", location);
  156. peripheral->setAttribute("name", item.attribute("name"));
  157. port->connect();
  158. }
  159. }
  160. inputManager.bind(); //bind any inputs this peripheral may contain
  161. program.refreshPanelList();
  162. }
  163. }
  164. }
  165. auto PortConnector::eventChange() -> void {
  166. acceptButton.setVisible(false);
  167. acceptButton.setEnabled(true);
  168. if(auto item = peripheralList.selected()) {
  169. if(item.attribute("type") == "nothing") {
  170. if(!port->connected()) {
  171. acceptButton.setText("Disconnect").setEnabled(false).setVisible();
  172. } else {
  173. acceptButton.setText("Disconnect").setVisible();
  174. }
  175. }
  176. if(item.attribute("type") == "template") {
  177. acceptButton.setText("Create").setVisible();
  178. }
  179. if(item.attribute("type") == "peripheral") {
  180. auto peripheral = port->connected();
  181. if(peripheral && peripheral->attribute("name") == item.text()) {
  182. acceptButton.setText("Connect").setEnabled(false).setVisible();
  183. } else {
  184. acceptButton.setText("Connect").setVisible();
  185. }
  186. }
  187. }
  188. controlLayout.resize();
  189. }
  190. auto PortConnector::eventContext() -> void {
  191. if(auto item = peripheralList.selected()) {
  192. if(item.attribute("type") == "peripheral") {
  193. contextMenu.setVisible();
  194. }
  195. }
  196. }
  197. auto PortConnector::eventBrowse() -> void {
  198. auto path = port->attribute("path");
  199. if(auto location = BrowserDialog()
  200. .setTitle({port->name(), " Location"})
  201. .setPath(path ? path : Path::user())
  202. .setAlignment(program).selectFolder()
  203. ) {
  204. port->setAttribute("path", location);
  205. refresh(port);
  206. }
  207. }
  208. auto PortConnector::eventRename() -> void {
  209. if(auto item = peripheralList.selected()) {
  210. if(item.attribute("type") == "peripheral") {
  211. auto location = item.attribute("location");
  212. auto path = item.attribute("path");
  213. auto name = item.attribute("name");
  214. if(auto rename = NameDialog()
  215. .setAlignment(program)
  216. .rename(name)
  217. ) {
  218. if(name == rename) return;
  219. if(directory::exists({path, rename})) return (void)MessageDialog()
  220. .setText("A directory by this name already exists.")
  221. .setTitle("Error").setAlignment(program).error();
  222. if(!directory::rename({path, name}, {path, rename})) return (void)MessageDialog()
  223. .setText("Failed to rename directory.")
  224. .setTitle("Error").setAlignment(program).error();
  225. //the location must be updated if this peripheral is already connected ...
  226. if(auto connector = emulator.connected(location)) {
  227. if(auto connected = connector->connected()) {
  228. connected->setAttribute("location", {path, rename, "/"});
  229. connected->setAttribute("name", rename);
  230. //the name will be updated in the system tree, so it must be refreshed:
  231. program.refreshPanelList();
  232. }
  233. }
  234. return refresh(port);
  235. }
  236. }
  237. }
  238. }
  239. auto PortConnector::eventRemove() -> void {
  240. if(auto item = peripheralList.selected()) {
  241. if(item.attribute("type") == "peripheral") {
  242. auto location = item.attribute("location");
  243. auto connected = emulator.connected(location);
  244. if(!port->hotSwappable() && emulator.system.power && emulator.connected(location)) return (void)MessageDialog()
  245. .setText({"This peripheral is not hot-swappable and is already connected to a port:\n\n", connected->name()})
  246. .setTitle("Error").setAlignment(program).error();
  247. if(MessageDialog()
  248. .setText("Are you really sure you want to delete this peripheral?\n"
  249. "All data will be permanently lost!")
  250. .setTitle("Warning").setAlignment(program).question() == "No"
  251. ) return;
  252. //must disconnect device before removing it, as disconnect will attempt to save data to disk
  253. if(connected) {
  254. port->disconnect();
  255. program.refreshPanelList();
  256. }
  257. if(!directory::remove(location)) return (void)MessageDialog()
  258. .setText("Failed to remove directory.")
  259. .setTitle("Error").setAlignment(program).error();
  260. refresh(port);
  261. }
  262. }
  263. }