import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
public class main {
public static void main(String[] args) throws Exception {
// Send information to whoever is listening on port 4989
String bashCmd = "echo -e 'windows key pressed\\n' | telnet localhost 4989";
File scriptFile = getProgramFile("converse.sh");
saveTextFile(scriptFile, bashCmd);
String cmdToRun = "/bin/bash " + scriptFile.getPath();
assignWindowsKeyTo(bashQuote(cmdToRun));
}
static void assignWindowsKeyTo(String cmdToRun) { try {
if (!isLinux())
fail("This function is only implemented for Linux");
// Install xbindkeys if not there
if (!new File("/usr/bin/xbindkeys").isFile()) {
fail("xbindkeys not installed");
/*
//print("xbindkeys not found, installing.");
String sudoPassword = TinyBrainUtils.showPasswordDialog(new MiniDB(), null,
"Please enter sudo password (for apt-get install xbindkeys)", "");
if (sudoPassword == null)
return; // No password, interrupt
Logger oldLogger = Log.getLogger();
Log.setLoggerForThread(new NonLogger()); // don't log sudo password
try {
backtick("echo " + sudoPassword + " | sudo -S apt-get -y install xbindkeys");
} finally {
Log.setLoggerForThread(oldLogger);
}
*/
} else {
print("Good: xbindkeys is installed.");
}
if (!new File("/usr/bin/xbindkeys").isFile())
throw new RuntimeException("Could not install xbindkeys");
print("xbindkeys there.");
print("Creating ~/.xbindkeysrc");
String homedir = userHome();
print("home dir: " + homedir);
// We just overwrite .xbindkeysrc... hopefully no other keys were bound by user :)
File dot_xbindkeysrc = new File(homedir, ".xbindkeysrc");
print("writing: " + dot_xbindkeysrc);
saveTextFile(dot_xbindkeysrc,
"# This file was created by " + getProgramURL() + "\n" +
"\n" +
cmdToRun + "\n" +
" c:133\n" // It's the Windows key!
);
print("Adding xbindkeys to your autostart");
// make xbindkeys.desktop (autostart xbindkeys for local user)
File autostart_dir = new File(homedir, ".config/autostart");
print("Creating " + autostart_dir);
autostart_dir.mkdirs();
File autostart_xbindkeys = new File(autostart_dir, "xbindkeys.desktop");
print("Writing " + autostart_xbindkeys);
saveTextFile(autostart_xbindkeys,
" [Desktop Entry]\n" +
" Version=1.0\n" +
" Name=xbindkeys\n" +
" Name[en_US]=xbindkeys\n" +
" Comment=Binds hotkeys to actions\n" +
" Exec=xbindkeys\n" +
" Icon=\n" +
" Terminal=false\n" +
" Type=Application\n" +
" Encoding=UTF-8\n" +
" Categories=Accessories\n");
print("Starting xbindkeys");
String processes = backtick("ps --no-heading -C xbindkeys");
if (!processes.isEmpty()) {
print(" xbindkeys already running - restarting.");
backtick("killall xbindkeys && xbindkeys");
} else {
backtick("xbindkeys");
print(" xbindkeys started.");
}
print("All done, Windows key assigned.");
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static File getProgramFile(String progID, String fileName) {
return new File(getProgramDir(progID), fileName);
}
static File getProgramFile(String fileName) {
return getProgramFile(getProgramID(), fileName);
}
static boolean isLinux() {
return !isWindows() && !isMac() && !isAndroid();
}
static RuntimeException fail() {
throw new RuntimeException("fail");
}
static RuntimeException fail(Object msg) {
throw new RuntimeException(String.valueOf(msg));
}
static RuntimeException fail(String msg) {
throw new RuntimeException(unnull(msg));
}
static RuntimeException fail(String msg, Object... args) {
throw new RuntimeException(format(msg, args));
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static void print() {
print("");
}
// slightly overblown signature to return original object...
static A print(A o) {
String s = String.valueOf(o) + "\n";
StringBuffer loc = local_log;
StringBuffer buf = print_log;
int loc_max = print_log_max;
if (buf != loc && buf != null) {
print_append(buf, s, print_log_max);
loc_max = local_log_max;
}
if (loc != null)
print_append(loc, s, loc_max);
System.out.print(s);
return o;
}
static void print(long l) {
print(String.valueOf(l));
}
static void print(char c) {
print(String.valueOf(c));
}
static void print_append(StringBuffer buf, String s, int max) {
synchronized(buf) {
buf.append(s);
max /= 2;
if (buf.length() > max) try {
int newLength = max/2;
int ofs = buf.length()-newLength;
String newString = buf.substring(ofs);
buf.setLength(0);
buf.append("[...] ").append(newString);
} catch (Exception e) {
buf.setLength(0);
}
}
}
static String _userHome;
static String userHome() {
if (_userHome == null) {
if (isAndroid())
_userHome = "/storage/sdcard0/";
else
_userHome = System.getProperty("user.home");
//System.out.println("userHome: " + _userHome);
}
return _userHome;
}
static String getProgramURL() {
return shortSnippetLink(getProgramID());
}
/** writes safely (to temp file, then rename) */
public static void saveTextFile(String fileName, String contents) throws IOException {
File file = new File(fileName);
File parentFile = file.getParentFile();
if (parentFile != null)
parentFile.mkdirs();
String tempFileName = fileName + "_temp";
if (contents != null) {
FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.print(contents);
printWriter.close();
}
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + fileName);
if (contents != null)
if (!new File(tempFileName).renameTo(file))
throw new IOException("Can't rename " + tempFileName + " to " + fileName);
}
public static void saveTextFile(File fileName, String contents) {
try {
saveTextFile(fileName.getPath(), contents);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/** possibly improvable */
public static String bashQuote(String text) {
if (text == null) return null;
return "\"" + text
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r") + "\"";
}
static int backtick_exitValue;
static boolean backtick_verbose;
public static String backtick(String cmd) { try {
File outFile = File.createTempFile("_backtick", "");
File scriptFile = File.createTempFile("_backtick", isWindows() ? ".bat" : "");
String command = cmd + " >" + bashQuote(outFile.getPath()) + " 2>&1";
//Log.info("[Backtick] " + command);
try {
if (backtick_verbose)
print("backtick: command " + command);
saveTextFile(scriptFile.getPath(), command);
String[] command2;
if (isWindows())
command2 = new String[] { scriptFile.getPath() };
else
command2 = new String[] { "/bin/bash", scriptFile.getPath() };
if (backtick_verbose)
print("backtick: command2 " + structure(command2));
Process process = Runtime.getRuntime().exec(command2);
try {
process.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
backtick_exitValue = process.exitValue();
if (backtick_verbose)
System.out.println("Process return code: " + backtick_exitValue);
return loadTextFile(outFile.getPath(), "");
} finally {
scriptFile.delete();
}
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
public static String loadTextFile(String fileName) {
try {
return loadTextFile(fileName, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String loadTextFile(String fileName, String defaultContents) throws IOException {
if (!new File(fileName).exists())
return defaultContents;
FileInputStream fileInputStream = new FileInputStream(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
return loadTextFile(inputStreamReader);
}
public static String loadTextFile(File fileName) {
try {
return loadTextFile(fileName, null);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String loadTextFile(File fileName, String defaultContents) throws IOException {
try {
return loadTextFile(fileName.getPath(), defaultContents);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String loadTextFile(Reader reader) throws IOException {
StringBuilder builder = new StringBuilder();
try {
char[] buffer = new char[1024];
int n;
while (-1 != (n = reader.read(buffer)))
builder.append(buffer, 0, n);
} finally {
reader.close();
}
return builder.toString();
}
static String programID;
static String getProgramID() {
return nempty(programID) ? formatSnippetID(programID) : "?";
}
// TODO: ask JavaX instead
static String getProgramID(Class c) {
String id = (String) getOpt(c, "programID");
if (nempty(id))
return formatSnippetID(id);
return "?";
}
static String getProgramID(Object o) {
return getProgramID(getMainClass(o));
}
static String unnull(String s) {
return s == null ? "" : s;
}
static List unnull(List l) {
return l == null ? emptyList() : l;
}
static Object[] unnull(Object[] a) {
return a == null ? new Object[0] : a;
}
static String format(String pat, Object... args) {
return format3(pat, args);
}
static File getProgramDir() {
return programDir();
}
static File getProgramDir(String snippetID) {
return programDir(snippetID);
}
static String structure(Object o) {
HashSet refd = new HashSet();
return structure_2(structure_1(o, 0, new IdentityHashMap(), refd), refd);
}
// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
static String structure_1(Object o, int stringSizeLimit, IdentityHashMap