| 1 |
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
|
| 3 |
/* |
| 4 |
Part of the Processing project - http://processing.org |
| 5 |
|
| 6 |
Copyright (c) 2008 Ben Fry and Casey Reas |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software Foundation, |
| 20 |
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
*/ |
| 22 |
|
| 23 |
package processing.app; |
| 24 |
|
| 25 |
import cc.arduino.packages.BoardPort; |
| 26 |
import processing.app.debug.TargetBoard; |
| 27 |
import processing.app.debug.TargetPackage; |
| 28 |
import processing.app.debug.TargetPlatform; |
| 29 |
import processing.app.legacy.PConstants; |
| 30 |
|
| 31 |
import javax.swing.*; |
| 32 |
import java.io.File; |
| 33 |
import java.io.IOException; |
| 34 |
import java.util.*; |
| 35 |
|
| 36 |
import static processing.app.I18n.tr; |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Used by Base for platform-specific tweaking, for instance finding the |
| 41 |
* sketchbook location using the Windows registry, or OS X event handling. |
| 42 |
* <p/> |
| 43 |
* The methods in this implementation are used by default, and can be |
| 44 |
* overridden by a subclass, if loaded by Base.main(). |
| 45 |
* <p/> |
| 46 |
* These methods throw vanilla-flavored Exceptions, so that error handling |
| 47 |
* occurs inside Base. |
| 48 |
* <p/> |
| 49 |
* There is currently no mechanism for adding new platforms, as the setup is |
| 50 |
* not automated. We could use getProperty("os.arch") perhaps, but that's |
| 51 |
* debatable (could be upper/lowercase, have spaces, etc.. basically we don't |
| 52 |
* know if name is proper Java package syntax.) |
| 53 |
*/ |
| 54 |
public class Platform { |
| 55 |
/** |
| 56 |
* Set the default L & F. While I enjoy the bounty of the sixteen possible |
| 57 |
* exception types that this UIManager method might throw, I feel that in |
| 58 |
* just this one particular case, I'm being spoiled by those engineers |
| 59 |
* at Sun, those Masters of the Abstractionverse. It leaves me feeling sad |
| 60 |
* and overweight. So instead, I'll pretend that I'm not offered eleven dozen |
| 61 |
* ways to report to the user exactly what went wrong, and I'll bundle them |
| 62 |
* all into a single catch-all "Exception". Because in the end, all I really |
| 63 |
* care about is whether things worked or not. And even then, I don't care. |
| 64 |
* |
| 65 |
* @throws Exception Just like I said. |
| 66 |
*/ |
| 67 |
public void setLookAndFeel() throws Exception { |
| 68 |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
public void init() throws Exception { |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
public File getSettingsFolder() throws Exception { |
| 77 |
// otherwise make a .processing directory int the user's home dir |
| 78 |
File home = new File(System.getProperty("user.home")); |
| 79 |
File dataFolder = new File(home, ".arduino15"); |
| 80 |
return dataFolder; |
| 81 |
|
| 82 |
/* |
| 83 |
try { |
| 84 |
Class clazz = Class.forName("processing.app.macosx.ThinkDifferent"); |
| 85 |
Method m = clazz.getMethod("getLibraryFolder", new Class[] { }); |
| 86 |
String libraryPath = (String) m.invoke(null, new Object[] { }); |
| 87 |
//String libraryPath = BaseMacOS.getLibraryFolder(); |
| 88 |
File libraryFolder = new File(libraryPath); |
| 89 |
dataFolder = new File(libraryFolder, "Processing"); |
| 90 |
|
| 91 |
} catch (Exception e) { |
| 92 |
showError("Problem getting data folder", |
| 93 |
"Error getting the Processing data folder.", e); |
| 94 |
} |
| 95 |
*/ |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* @return null if not overridden, which will cause a prompt to show instead. |
| 101 |
* @throws Exception |
| 102 |
*/ |
| 103 |
public File getDefaultSketchbookFolder() throws Exception { |
| 104 |
return null; |
| 105 |
} |
| 106 |
|
| 107 |
public void openURL(File folder, String url) throws Exception { |
| 108 |
if (!url.startsWith("file://./")) { |
| 109 |
openURL(url); |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
url = url.replaceAll("file://./", folder.getCanonicalFile().toURI().toASCIIString()); |
| 114 |
openURL(url); |
| 115 |
} |
| 116 |
|
| 117 |
public void openURL(String url) throws Exception { |
| 118 |
String launcher = PreferencesData.get("launcher"); |
| 119 |
if (launcher != null) { |
| 120 |
Runtime.getRuntime().exec(new String[]{launcher, url}); |
| 121 |
} else { |
| 122 |
showLauncherWarning(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
public boolean openFolderAvailable() { |
| 128 |
return PreferencesData.get("launcher") != null; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
public void openFolder(File file) throws Exception { |
| 133 |
String launcher = PreferencesData.get("launcher"); |
| 134 |
if (launcher != null) { |
| 135 |
String folder = file.getAbsolutePath(); |
| 136 |
Runtime.getRuntime().exec(new String[]{launcher, folder}); |
| 137 |
} else { |
| 138 |
showLauncherWarning(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
static { |
| 143 |
System.loadLibrary("listSerialsj"); |
| 144 |
} |
| 145 |
|
| 146 |
private static void loadLib(File lib) { |
| 147 |
try { |
| 148 |
System.load(lib.getAbsolutePath()); |
| 149 |
} catch (UnsatisfiedLinkError e) { |
| 150 |
e.printStackTrace(); |
| 151 |
System.out.println(e.getMessage()); |
| 152 |
System.out.println("Cannot load native library " + lib.getAbsolutePath()); |
| 153 |
System.out.println("The program has terminated!"); |
| 154 |
System.exit(1); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
private native String resolveDeviceAttachedToNative(String serial); |
| 159 |
|
| 160 |
private native String[] listSerialsNative(); |
| 161 |
|
| 162 |
public String preListAllCandidateDevices() { |
| 163 |
return null; |
| 164 |
} |
| 165 |
|
| 166 |
public List<String> listSerials() { |
| 167 |
return new ArrayList<>(Arrays.asList(listSerialsNative())); |
| 168 |
} |
| 169 |
|
| 170 |
public List<String> listSerialsNames() { |
| 171 |
List<String> list = new LinkedList<>(); |
| 172 |
for (String port : listSerialsNative()) { |
| 173 |
list.add(port.split("_")[0]); |
| 174 |
} |
| 175 |
return list; |
| 176 |
} |
| 177 |
|
| 178 |
public synchronized Map<String, Object> resolveDeviceByVendorIdProductId(String serial, Map<String, TargetPackage> packages) { |
| 179 |
String vid_pid_iSerial = resolveDeviceAttachedToNative(serial); |
| 180 |
for (TargetPackage targetPackage : packages.values()) { |
| 181 |
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) { |
| 182 |
for (TargetBoard board : targetPlatform.getBoards().values()) { |
| 183 |
List<String> vids = new LinkedList<>(board.getPreferences().subTree("vid", 1).values()); |
| 184 |
if (!vids.isEmpty()) { |
| 185 |
List<String> pids = new LinkedList<>(board.getPreferences().subTree("pid", 1).values()); |
| 186 |
List<String> descriptors = new LinkedList<>(board.getPreferences().subTree("descriptor", 1).values()); |
| 187 |
for (int i = 0; i < vids.size(); i++) { |
| 188 |
String vidPid = vids.get(i) + "_" + pids.get(i); |
| 189 |
if (vid_pid_iSerial.toUpperCase().contains(vidPid.toUpperCase())) { |
| 190 |
if (!descriptors.isEmpty()) { |
| 191 |
boolean matched = false; |
| 192 |
for (int j = 0; j < descriptors.size(); j++) { |
| 193 |
if (vid_pid_iSerial.toUpperCase().contains(descriptors.get(j).toUpperCase())) { |
| 194 |
matched = true; |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
if (matched == false) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
} |
| 202 |
Map<String, Object> boardData = new HashMap<>(); |
| 203 |
boardData.put("board", board); |
| 204 |
// remove 0x from VID / PID to keep them as reported by liblistserial |
| 205 |
boardData.put("vid", vids.get(i).replaceAll("0x", "")); |
| 206 |
boardData.put("pid", pids.get(i).replaceAll("0x", "")); |
| 207 |
String extrafields = vid_pid_iSerial.substring(vidPid.length() + 1); |
| 208 |
String[] parts = extrafields.split("_"); |
| 209 |
boardData.put("iserial", parts[0]); |
| 210 |
return boardData; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
return null; |
| 218 |
} |
| 219 |
|
| 220 |
public String resolveDeviceByBoardID(Map<String, TargetPackage> packages, String boardId) { |
| 221 |
assert packages != null; |
| 222 |
assert boardId != null; |
| 223 |
for (TargetPackage targetPackage : packages.values()) { |
| 224 |
for (TargetPlatform targetPlatform : targetPackage.getPlatforms().values()) { |
| 225 |
for (TargetBoard board : targetPlatform.getBoards().values()) { |
| 226 |
if (boardId.equals(board.getId())) { |
| 227 |
return board.getName(); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
return null; |
| 233 |
} |
| 234 |
|
| 235 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
| 236 |
|
| 237 |
public String getName() { |
| 238 |
return PConstants.platformNames[PConstants.OTHER]; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
| 243 |
|
| 244 |
|
| 245 |
protected void showLauncherWarning() { |
| 246 |
BaseNoGui.showWarning(tr("No launcher available"), |
| 247 |
tr("Unspecified platform, no launcher available.\nTo enable opening URLs or folders, add a \n\"launcher=/path/to/app\" line to preferences.txt"), |
| 248 |
null); |
| 249 |
} |
| 250 |
|
| 251 |
public List<BoardPort> filterPorts(List<BoardPort> ports, boolean aBoolean) { |
| 252 |
return new LinkedList<>(ports); |
| 253 |
} |
| 254 |
|
| 255 |
public void fixPrefsFilePermissions(File prefsFile) throws IOException, InterruptedException { |
| 256 |
Process process = Runtime.getRuntime().exec(new String[]{"chmod", "600", prefsFile.getAbsolutePath()}, null, null); |
| 257 |
process.waitFor(); |
| 258 |
} |
| 259 |
|
| 260 |
public List<File> postInstallScripts(File folder) { |
| 261 |
List<File> scripts = new LinkedList<>(); |
| 262 |
scripts.add(new File(folder, "install_script.sh")); |
| 263 |
scripts.add(new File(folder, "post_install.sh")); |
| 264 |
return scripts; |
| 265 |
} |
| 266 |
|
| 267 |
public List<File> preUninstallScripts(File folder) { |
| 268 |
List<File> scripts = new LinkedList<>(); |
| 269 |
scripts.add(new File(folder, "pre_uninstall.sh")); |
| 270 |
return scripts; |
| 271 |
} |
| 272 |
|
| 273 |
public String getOsName() { |
| 274 |
return System.getProperty("os.name"); |
| 275 |
} |
| 276 |
|
| 277 |
public String getOsArch() { |
| 278 |
return System.getProperty("os.arch"); |
| 279 |
} |
| 280 |
|
| 281 |
public void symlink(String something, File somewhere) throws IOException, InterruptedException { |
| 282 |
Process process = Runtime.getRuntime().exec(new String[]{"ln", "-s", something, somewhere.getAbsolutePath()}, null, somewhere.getParentFile()); |
| 283 |
process.waitFor(); |
| 284 |
} |
| 285 |
|
| 286 |
public void link(File something, File somewhere) throws IOException, InterruptedException { |
| 287 |
Process process = Runtime.getRuntime().exec(new String[]{"ln", something.getAbsolutePath(), somewhere.getAbsolutePath()}, null, null); |
| 288 |
process.waitFor(); |
| 289 |
} |
| 290 |
|
| 291 |
public void chmod(File file, int mode) throws IOException, InterruptedException { |
| 292 |
Process process = Runtime.getRuntime().exec(new String[]{"chmod", Integer.toOctalString(mode), file.getAbsolutePath()}, null, null); |
| 293 |
process.waitFor(); |
| 294 |
} |
| 295 |
|
| 296 |
public void fixSettingsLocation() throws Exception { |
| 297 |
//noop |
| 298 |
} |
| 299 |
|
| 300 |
public int getSystemDPI() { |
| 301 |
return 96; |
| 302 |
} |
| 303 |
|
| 304 |
} |