Permalink
Browse files

initial commit

  • Loading branch information...
rbarazzutti committed Aug 31, 2016
0 parents commit 7ed9c8e8268c32d5d51c0ac14625e6add3a6cc5c
@@ -0,0 +1,5 @@
.*
target
*.iml
hs_err_pid*.log

@@ -0,0 +1,3 @@
language: java
jdk:
- oraclejdk8
@@ -0,0 +1,44 @@
[![build status](https://secure.travis-ci.org/rbarazzutti/insomnia.svg?branch=master)](http://travis-ci.org/rbarazzutti/insomnia)

# insomnia
Insomnia is a simple cross-platform toolkit that allows the programmer to prevent the operating system to enter in sleep mode.

Usage example:

```java
import ch.fever.insomnia.Insomnia;
import ch.fever.insomnia.InsomniaMode;
import java.util.Optional;
public class MyMediaApp {
public void main(String args[]) {
Optional<Insomnia> insomnia = Insomnia.getOptInstance();
insomnia.ifPresent(i -> i.apply(InsomniaMode.SYSTEM_AND_DISPLAY_UP));
// display a nice animation
insomnia.ifPresent(i -> i.apply(InsomniaMode.DISABLED));
System.exit(0);
}
}
```

---

Copyright (C) 2016 Raphaël P. Barazzutti

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
178 pom.xml
@@ -0,0 +1,178 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright (C) 2016 Raphael P. Barazzutti
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>ch.fever</groupId>
<artifactId>insomnia</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>



<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
<version>0.11</version>
<dependencies>
<!-- https://issues.apache.org/jira/browse/RAT-158 -->
<!-- TODO remove when upgrading to 0.12 -->
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-core</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<licenses>
<license implementation="org.apache.rat.analysis.license.SimplePatternBasedLicense">
<licenseFamilyCategory>AL2</licenseFamilyCategory>
<licenseFamilyName>Apache License 2.0</licenseFamilyName>
<notes/>
<patterns>
<pattern>
Licensed under the Apache License, Version 2.0
</pattern>
</patterns>
</license>
</licenses>
<licenseFamilies>
<licenseFamily implementation="org.apache.rat.license.SimpleLicenseFamily">
<familyName>Apache License 2.0</familyName>
</licenseFamily>
</licenseFamilies>

<excludeSubProjects>false</excludeSubProjects>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.2.2</version>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<developers>
<developer>
<id>rbarazzutti</id>
<name>Raphaël P. Barazzutti</name>
<roles>
<role>Owner</role>
</roles>
</developer>
</developers>

</project>
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2015 Raphael P. Barazzutti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ch.fever.insomnia;

import java.util.Optional;

/**
* Insomnia class
*
* Subclasses of this one are targeting various operating systems (up to now, Microsoft Windows and Apple OSX). Obviously
* these methods might behave in a weird manner if accessed in a concurrent way.
*
* Insomnia subclasses give no guarantee in term of synchronization, it's up to the developer to handle this. We expect that
* in normal usage of Insomnia, the application does not face such issues. This framework does not address concurrency,
* if your software might access under some circumstances concurrently Insomnia, please use the appropriate synchronization
* mechanism to avoid any issue.
*/
public interface Insomnia {
/**
* Change the standby behavior of the machine
*
* @param insomniaMode mode to be requested
* @return true if the request has been properly processed by the operating system
*/
default boolean apply(InsomniaMode insomniaMode) {
return apply(insomniaMode, "No reason given");
}

/**
* Change the standby behavior of the machine
*
* @param insomniaMode mode to be requested
* @param reason reason invoked
* @return true if the request has been properly processed by the operating system
*/
boolean apply(InsomniaMode insomniaMode, String reason);

/**
* @return a singleton of the insomnia
* @throws UnsupportedArchitectureException if no support is available on the current architecture
*/
static Insomnia getInstance() throws UnsupportedArchitectureException {
return InsomniaHelper.getNonNullInstance();
}

/**
*
*/
static Optional<Insomnia> getOptInstance(){
return Optional.ofNullable( InsomniaHelper.getInstance());
}
}
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2015 Raphael P. Barazzutti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ch.fever.insomnia;

import ch.fever.insomnia.osx.InsomniaOSX;
import ch.fever.insomnia.windows.InsomniaWindows;

class InsomniaHelper {
private static class StaticHolder {
static final Insomnia INSTANCE = InsomniaHelper.prepareInsomnia();
}


/**
* No need of constructor
*/
private InsomniaHelper() {
}


static Insomnia getNonNullInstance() throws UnsupportedArchitectureException {
if (StaticHolder.INSTANCE != null)
return StaticHolder.INSTANCE;
else
throw new UnsupportedArchitectureException();
}

static Insomnia getInstance() {
return StaticHolder.INSTANCE;
}


static Insomnia prepareInsomnia() {
String os = System.getProperty("os.name").toLowerCase();

if ((os.contains("win")))
return new InsomniaWindows();
else if ((os.contains("mac")))
return new InsomniaOSX();
else
return null;
}


}
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 Raphael P. Barazzutti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ch.fever.insomnia;

/**
* Modes supported by Insomnia
*/
public enum InsomniaMode {
/**
* The standard behavior of the operating system
*/
DISABLED,

/**
* Prevent the sleep idle time-out.
*/
SYSTEM_UP,

/**
* Prevent the sleep idle time-out (and maintain display).
*/
SYSTEM_AND_DISPLAY_UP
}
Oops, something went wrong.

0 comments on commit 7ed9c8e

Please sign in to comment.