PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/arduino-core/src/processing/app/windows/Platform.java

https://github.com/akafugu/Xmegaduino
Java | 231 lines | 138 code | 42 blank | 51 comment | 15 complexity | fbca7c8a7c6b7918522ba9ecfaf0b760 MD5 | raw file
  1. /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
  2. /*
  3. Part of the Processing project - http://processing.org
  4. Copyright (c) 2008-2009 Ben Fry and Casey Reas
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. package processing.app.windows;
  18. import org.apache.commons.exec.CommandLine;
  19. import org.apache.commons.exec.DefaultExecutor;
  20. import org.apache.commons.exec.Executor;
  21. import org.apache.commons.exec.PumpStreamHandler;
  22. import processing.app.debug.TargetPackage;
  23. import processing.app.legacy.PApplet;
  24. import processing.app.legacy.PConstants;
  25. import java.io.ByteArrayOutputStream;
  26. import java.io.File;
  27. import java.io.IOException;
  28. import java.util.LinkedList;
  29. import java.util.List;
  30. import java.util.Map;
  31. public class Platform extends processing.app.Platform {
  32. private File settingsFolder;
  33. private File defaultSketchbookFolder;
  34. public void init() throws IOException {
  35. super.init();
  36. checkPath();
  37. recoverSettingsFolderPath();
  38. recoverDefaultSketchbookFolder();
  39. }
  40. private void recoverSettingsFolderPath() throws IOException {
  41. String path = getFolderPathFromRegistry("AppData");
  42. this.settingsFolder = new File(path, "Arduino15");
  43. }
  44. private void recoverDefaultSketchbookFolder() throws IOException {
  45. String path = getFolderPathFromRegistry("Personal");
  46. this.defaultSketchbookFolder = new File(path, "Arduino");
  47. }
  48. private String getFolderPathFromRegistry(String folderType) throws IOException {
  49. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  50. Executor executor = new DefaultExecutor();
  51. executor.setStreamHandler(new PumpStreamHandler(baos, null));
  52. CommandLine toDevicePath = CommandLine.parse("reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v \"" + folderType + "\"");
  53. executor.execute(toDevicePath);
  54. return new RegQueryParser(new String(baos.toByteArray())).getValueOfKey();
  55. }
  56. /**
  57. * Remove extra quotes, slashes, and garbage from the Windows PATH.
  58. */
  59. protected void checkPath() {
  60. String path = System.getProperty("java.library.path");
  61. String[] pieces = PApplet.split(path, File.pathSeparatorChar);
  62. String[] legit = new String[pieces.length];
  63. int legitCount = 0;
  64. for (String item : pieces) {
  65. if (item.startsWith("\"")) {
  66. item = item.substring(1);
  67. }
  68. if (item.endsWith("\"")) {
  69. item = item.substring(0, item.length() - 1);
  70. }
  71. if (item.endsWith(File.separator)) {
  72. item = item.substring(0, item.length() - File.separator.length());
  73. }
  74. File directory = new File(item);
  75. if (!directory.exists()) {
  76. continue;
  77. }
  78. if (item.trim().length() == 0) {
  79. continue;
  80. }
  81. legit[legitCount++] = item;
  82. }
  83. legit = PApplet.subset(legit, 0, legitCount);
  84. String newPath = PApplet.join(legit, File.pathSeparator);
  85. if (!newPath.equals(path)) {
  86. System.setProperty("java.library.path", newPath);
  87. }
  88. }
  89. public File getSettingsFolder() {
  90. return settingsFolder;
  91. }
  92. public File getDefaultSketchbookFolder() throws Exception {
  93. return defaultSketchbookFolder;
  94. }
  95. public void openURL(String url) throws Exception {
  96. // this is not guaranteed to work, because who knows if the
  97. // path will always be c:\progra~1 et al. also if the user has
  98. // a different browser set as their default (which would
  99. // include me) it'd be annoying to be dropped into ie.
  100. //Runtime.getRuntime().exec("c:\\progra~1\\intern~1\\iexplore "
  101. // + currentDir
  102. // the following uses a shell execute to launch the .html file
  103. // note that under cygwin, the .html files have to be chmodded +x
  104. // after they're unpacked from the zip file. i don't know why,
  105. // and don't understand what this does in terms of windows
  106. // permissions. without the chmod, the command prompt says
  107. // "Access is denied" in both cygwin and the "dos" prompt.
  108. //Runtime.getRuntime().exec("cmd /c " + currentDir + "\\reference\\" +
  109. // referenceFile + ".html");
  110. if (url.startsWith("http")) {
  111. // open dos prompt, give it 'start' command, which will
  112. // open the url properly. start by itself won't work since
  113. // it appears to need cmd
  114. Runtime.getRuntime().exec("cmd /c start " + url);
  115. } else {
  116. // just launching the .html file via the shell works
  117. // but make sure to chmod +x the .html files first
  118. // also place quotes around it in case there's a space
  119. // in the user.dir part of the url
  120. Runtime.getRuntime().exec("cmd /c \"" + url + "\"");
  121. }
  122. }
  123. public boolean openFolderAvailable() {
  124. return true;
  125. }
  126. public void openFolder(File file) throws Exception {
  127. String folder = file.getAbsolutePath();
  128. // doesn't work
  129. //Runtime.getRuntime().exec("cmd /c \"" + folder + "\"");
  130. // works fine on winxp, prolly win2k as well
  131. Runtime.getRuntime().exec("explorer \"" + folder + "\"");
  132. // not tested
  133. //Runtime.getRuntime().exec("start explorer \"" + folder + "\"");
  134. }
  135. // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  136. @Override
  137. public String getName() {
  138. return PConstants.platformNames[PConstants.WINDOWS];
  139. }
  140. @Override
  141. public Map<String, Object> resolveDeviceAttachedTo(String serial, Map<String, TargetPackage> packages, String devicesListOutput) {
  142. assert packages != null;
  143. if (devicesListOutput == null) {
  144. return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput);
  145. }
  146. try {
  147. String vidPid = new ListComPortsParser().extractVIDAndPID(devicesListOutput, serial);
  148. if (vidPid == null) {
  149. return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput);
  150. }
  151. return super.resolveDeviceByVendorIdProductId(packages, vidPid);
  152. } catch (IOException e) {
  153. return super.resolveDeviceAttachedTo(serial, packages, devicesListOutput);
  154. }
  155. }
  156. @Override
  157. public String preListAllCandidateDevices() {
  158. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  159. Executor executor = new DefaultExecutor();
  160. executor.setStreamHandler(new PumpStreamHandler(baos, null));
  161. try {
  162. String listComPorts = new File(System.getProperty("user.dir"), "hardware/tools/listComPorts.exe").getCanonicalPath();
  163. CommandLine toDevicePath = CommandLine.parse(listComPorts);
  164. executor.execute(toDevicePath);
  165. return new String(baos.toByteArray());
  166. } catch (Throwable e) {
  167. return super.preListAllCandidateDevices();
  168. }
  169. }
  170. @Override
  171. public void fixPrefsFilePermissions(File prefsFile) throws IOException {
  172. //noop
  173. }
  174. public List<File> postInstallScripts(File folder) {
  175. List<File> scripts = new LinkedList<File>();
  176. scripts.add(new File(folder, "post_install.bat"));
  177. return scripts;
  178. }
  179. public void symlink(File something, File somewhere) throws IOException, InterruptedException {
  180. }
  181. public void link(File something, File somewhere) throws IOException, InterruptedException {
  182. }
  183. public void chmod(File file, int mode) throws IOException, InterruptedException {
  184. }
  185. }