This commit adds all necessary code and files to make this work.
This commit is contained in:
Bea 2020-10-18 17:00:57 +02:00
parent c0c8dd1815
commit b642661863
14 changed files with 461 additions and 0 deletions

76
.gitignore vendored Normal file
View File

@ -0,0 +1,76 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
target/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
# Ignore self-extracted stuff
config.yml
restart.sh

View File

@ -0,0 +1,8 @@
<component name="ArtifactManager">
<artifact type="jar" name="ScreenScalingFixer:jar">
<output-path>$PROJECT_DIR$/out/artifacts/ScreenScalingFixer_jar</output-path>
<root id="archive" name="ScreenScalingFixer.jar">
<element id="module-output" name="ScreenScalingFixer" />
</root>
</artifact>
</component>

16
.idea/compiler.xml generated Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="ScreenScalingFixer" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="ScreenScalingFixer" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

1
.idea/description.html generated Normal file
View File

@ -0,0 +1 @@
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

8
.idea/encodings.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

20
.idea/jarRepositories.xml generated Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

16
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/ScreenScalingFixer.iml" filepath="$PROJECT_DIR$/ScreenScalingFixer.iml" />
</modules>
</component>
</project>

3
.idea/project-template.xml generated Normal file
View File

@ -0,0 +1,3 @@
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>

16
ScreenScalingFixer.iml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.21" level="project" />
</component>
</module>

3
buildNumber.properties Normal file
View File

@ -0,0 +1,3 @@
#maven.buildNumber.plugin properties file
#Sun Oct 18 16:52:03 CEST 2020
buildNumber=43

100
pom.xml Normal file
View File

@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>ScreenScalingFixer</artifactId>
<version>1.0-SNAPSHOT</version>
<scm>
<connection>scm:svn:http://127.0.0.1/dummy</connection>
<developerConnection>scm:svn:https://127.0.0.1/dummy</developerConnection>
<tag>HEAD</tag>
<url>http://127.0.0.1/dummy</url>
</scm>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>buildnumber</id>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<format>{0,number}</format>
<items>
<item>buildNumber</item>
</items>
<doCheck>false</doCheck>
<doUpdate>false</doUpdate>
<revisionOnScmFailure>unknownbuild</revisionOnScmFailure>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>net.mindoverflow.tools.ssf.Main</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<!-- Filter for version name in properties -->
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.yml</include>
<include>*.sh</include>
</includes>
</resource>
</resources>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<versionName>${project.version}</versionName>
</properties>
<dependencies>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.21</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,183 @@
package net.mindoverflow.tools.ssf;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.yaml.snakeyaml.Yaml;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.nio.file.Files;
import java.util.Map;
public class Main {
private static int restartDelay;
private static String monitorConnector;
private static double monitorScale;
private static boolean enable;
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException, InterruptedException {
File monitorConfig = new File(System.getProperty("user.home") + "/.config/monitors.xml");
if(!monitorConfig.exists())
{
System.out.println("File \"monitors.xml\" does not exist! Quitting...");
System.exit(1);
}
if(!loadConfig())
{
System.out.println("Error loading \"config.yml\"! Quitting...");
System.exit(1);
}
if(!enable)
{
System.out.println("Script disabled! Enable it by setting \"enabled: true\" in config.yml!");
System.exit(0);
}
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(monitorConfig);
Element rootElement = doc.getDocumentElement();
System.out.println("Main element: " + rootElement.getNodeName());
NodeList rootNodes = rootElement.getElementsByTagName("configuration");
Node configuration = rootNodes.item(0);
System.out.println("1 - Current node: " + configuration.getNodeName());
NodeList logicalMonitors = configuration.getChildNodes();
for(int pos = 0; pos < logicalMonitors.getLength(); pos++)
{
Node logicalMonitor = logicalMonitors.item(pos);
if(!logicalMonitor.getNodeName().equalsIgnoreCase("logicalmonitor")) continue;
System.out.println(" 2 - Current node: " + logicalMonitor.getNodeName());
Node scale = getNodeByName("scale", logicalMonitor.getChildNodes());
System.out.println(" 3A - current node: " + scale.getNodeName());
System.out.println(" Current scale: " + scale.getTextContent());
Node monitor = getNodeByName("monitor", logicalMonitor.getChildNodes());
System.out.println(" 3B - current node: " + monitor.getNodeName());
Node monitorspec = getNodeByName("monitorspec", monitor.getChildNodes());
System.out.println(" 4 - current node: " + monitorspec.getNodeName());
Node connector = getNodeByName("connector", monitorspec.getChildNodes());
System.out.println(" 5 - current node: " + connector.getNodeName());
System.out.println(" Current connector: " + connector.getTextContent());
String connectorValue = connector.getTextContent();
if(connectorValue.equalsIgnoreCase(monitorConnector))
{
scale.setTextContent("1");
saveFile(doc, monitorConfig);
restartGnome();
Thread.sleep(restartDelay);
scale.setTextContent(monitorScale + "");
saveFile(doc, monitorConfig);
restartGnome();
return;
}
}
}
private static Node getNodeByName(String name, NodeList list)
{
for(int pos = 0; pos < list.getLength(); pos++)
{
Node current = list.item(pos);
if(current.getNodeName().equalsIgnoreCase(name)) return current;
}
return null;
}
private static void restartGnome() throws IOException, InterruptedException {
String s;
InputStream stream = Main.class.getResourceAsStream("/restart.sh");
File script = new File("restart.sh");
if(!script.exists())
{
System.out.println("Extracting restart script...");
Files.copy(stream, script.getAbsoluteFile().toPath());
System.out.println("Done!");
}
System.out.println("Restarting GNOME...");
script.setExecutable(true);
Process process = Runtime.getRuntime().exec(script.getAbsolutePath());
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
while((s = br.readLine()) != null)
{
System.out.println("line: " + s);
}
process.waitFor();
System.out.println ("exit: " + process.exitValue());
process.destroy();
}
private static void saveFile(Document doc, File file) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(doc), new StreamResult(file));
}
private static boolean loadConfig()
{
InputStream stream = Main.class.getResourceAsStream("/config.yml");
File config = new File("config.yml");
if(!config.exists())
{
System.out.println("Extracting config...");
try
{
Files.copy(stream, config.getAbsoluteFile().toPath());
} catch (IOException e)
{
e.printStackTrace();
return false;
}
System.out.println("Done!");
}
System.out.println("Loading config...");
Yaml configYaml = new Yaml();
Map<String, Object> contents;
try
{
contents = configYaml.load(new FileInputStream(config));
} catch (FileNotFoundException e)
{
e.printStackTrace();
return false;
}
enable = (boolean) contents.get("enable");
restartDelay = (int) contents.get("delay");
monitorScale = (double) contents.get("scale");
monitorConnector = (String) contents.get("monitor-connector");
System.out.println("Done!");
System.out.println("Enabled: " + enable);
System.out.println("Delay: " + restartDelay);
System.out.println("Monitor name: " + monitorConnector);
System.out.println("Monitor scale: " + monitorScale);
return true;
}
}

View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: net.mindoverflow.tools.ssf.Main