Author: echatellier Date: 2012-02-08 18:16:37 +0100 (Wed, 08 Feb 2012) New Revision: 525 Url: http://nuiton.org/repositories/revision/sandbox/525 Log: Initial import Added: jnijnajntest/compjni.sh jnijnajntest/org_test_testjni_IdleJNI.c jnijnajntest/org_test_testjni_IdleJNI.h jnijnajntest/pom.xml jnijnajntest/src/ jnijnajntest/src/main/ jnijnajntest/src/main/java/ jnijnajntest/src/main/java/org/ jnijnajntest/src/main/java/org/test/ jnijnajntest/src/main/java/org/test/testjna/ jnijnajntest/src/main/java/org/test/testjna/TestIdleJNA.java jnijnajntest/src/main/java/org/test/testjna/TestNotifyJNA.java jnijnajntest/src/main/java/org/test/testjna/notify/ jnijnajntest/src/main/java/org/test/testjna/notify/Notify.java jnijnajntest/src/main/java/org/test/testjna/unix/ jnijnajntest/src/main/java/org/test/testjna/unix/UnixSystemInfo.java jnijnajntest/src/main/java/org/test/testjna/unix/Xss.java jnijnajntest/src/main/java/org/test/testjni/ jnijnajntest/src/main/java/org/test/testjni/IdleJNI.java jnijnajntest/src/main/java/org/test/testjni/TestIdleJNI.java jnijnajntest/src/main/resources/ jnijnajntest/src/main/resources/logback.xml Added: jnijnajntest/compjni.sh =================================================================== --- jnijnajntest/compjni.sh (rev 0) +++ jnijnajntest/compjni.sh 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,8 @@ +#/bin/bash + +pushd target/classes +javah org.test.testjni.IdleJNI +popd + +gcc -I /usr/include -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -c org_test_testjni_IdleJNI.c -fPIC +gcc -I /usr/include -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -shared org_test_testjni_IdleJNI.o -o libtestidle.so -L/usr/lib -rdynamic -lX11 -lXss -lXext Added: jnijnajntest/org_test_testjni_IdleJNI.c =================================================================== --- jnijnajntest/org_test_testjni_IdleJNI.c (rev 0) +++ jnijnajntest/org_test_testjni_IdleJNI.c 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,62 @@ +/* /usr/include/X11/extensions/scrnsaver.h */ +#include <X11/extensions/scrnsaver.h> +#include <string.h> /* pour strcmp() */ +#include "org_test_testjni_IdleJNI.h" + +/** + * Major thanks to Dan Price for helping to rewrite this library + * to work correctly under Solaris and VNC. Thanks also for + * plugging the memory leaks and error detection. + */ +static int get_x_idle_time(long *idle) { + XScreenSaverInfo *ss_info = NULL; + Display *display; + static int firsttime = 1; //Is this the first time we've run this method? + + if((display = XOpenDisplay(NULL)) == NULL) { + printf("Couldn't open display\n"); + return(-1); + } + + if(firsttime) { + int evb, erb; + if(XScreenSaverQueryExtension(display, &evb, &erb) == False) { + printf("No screen saver extension\n"); + XCloseDisplay(display); + return(-2); + } + firsttime = 0; + } + + if((ss_info = XScreenSaverAllocInfo()) == NULL) { + printf("Could not alloc screen saver info\n"); + XCloseDisplay(display); + return(-1); + } + + if(XScreenSaverQueryInfo(display, DefaultRootWindow(display), ss_info) == 0) { + printf("Could not query root window\n"); + XFree(ss_info); + XCloseDisplay(display); + return(-1); + } + *idle = ss_info->idle; + + XFree(ss_info); + XCloseDisplay(display); + return(0); +} + +JNIEXPORT jlong JNICALL Java_org_test_testjni_IdleJNI_getIdleTime ( + JNIEnv* env, /* Environnement JNI */ + jobject thiz /* Pointeur "this" de l'instance courante */ +) { + int ret; + long time = 0; + + ret = get_x_idle_time(&time); + + if(ret == 0) return(time); + return (ret); +} + Added: jnijnajntest/org_test_testjni_IdleJNI.h =================================================================== --- jnijnajntest/org_test_testjni_IdleJNI.h (rev 0) +++ jnijnajntest/org_test_testjni_IdleJNI.h 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class org_test_testjni_IdleJNI */ + +#ifndef _Included_org_test_testjni_IdleJNI +#define _Included_org_test_testjni_IdleJNI +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_test_testjni_IdleJNI + * Method: getIdleTime + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_org_test_testjni_IdleJNI_getIdleTime + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif Added: jnijnajntest/pom.xml =================================================================== --- jnijnajntest/pom.xml (rev 0) +++ jnijnajntest/pom.xml 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,39 @@ +<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>org.test</groupId> + <artifactId>jnijnajntest</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <dependencies> + <dependency> + <groupId>net.java.dev.jna</groupId> + <artifactId>jna</artifactId> + <version>3.4.0</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>net.java.dev.jna</groupId> + <artifactId>platform</artifactId> + <version>3.4.0</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>1.6.4</version> + <scope>compile</scope> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>jcl-over-slf4j</artifactId> + <version>1.6.4</version> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <version>1.0.0</version> + <scope>runtime</scope> + </dependency> + </dependencies> +</project> \ No newline at end of file Added: jnijnajntest/src/main/java/org/test/testjna/TestIdleJNA.java =================================================================== --- jnijnajntest/src/main/java/org/test/testjna/TestIdleJNA.java (rev 0) +++ jnijnajntest/src/main/java/org/test/testjna/TestIdleJNA.java 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,52 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.test.testjna; + +import org.test.testjna.unix.UnixSystemInfo; +import org.test.testjni.IdleJNI; + +/** + * TODO add comment here. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class TestIdleJNA { + + public static void main(String... args) throws InterruptedException { + + UnixSystemInfo idleJNA = new UnixSystemInfo(); + while (true) { + Thread.sleep(1000); + + long idle = idleJNA.getIdleTime(); + System.out.println("Idle since " + idle + "ms"); + } + } +} Property changes on: jnijnajntest/src/main/java/org/test/testjna/TestIdleJNA.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: jnijnajntest/src/main/java/org/test/testjna/TestNotifyJNA.java =================================================================== --- jnijnajntest/src/main/java/org/test/testjna/TestNotifyJNA.java (rev 0) +++ jnijnajntest/src/main/java/org/test/testjna/TestNotifyJNA.java 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,49 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.test.testjna; + +import org.test.testjna.notify.Notify; +import org.test.testjna.notify.Notify.NotifyNotification; + +/** + * TODO add comment here. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class TestNotifyJNA { + + public static void main(String... args) throws InterruptedException { + + Notify notify = Notify.INSTANCE; + notify.notify_init("test"); + NotifyNotification notif = notify.notify_notification_new("Test", "hello réunion dev", null, null); + notify.notify_notification_show(notif, null); + } +} Property changes on: jnijnajntest/src/main/java/org/test/testjna/TestNotifyJNA.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: jnijnajntest/src/main/java/org/test/testjna/notify/Notify.java =================================================================== --- jnijnajntest/src/main/java/org/test/testjna/notify/Notify.java (rev 0) +++ jnijnajntest/src/main/java/org/test/testjna/notify/Notify.java 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,54 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.test.testjna.notify; + +import com.sun.jna.Library; +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import com.sun.jna.Structure; + +/** + * TODO add comment here. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public interface Notify extends Library { + + Notify INSTANCE = (Notify) Native.loadLibrary("notify", Notify.class); + + public static class NotifyNotification extends Structure { + public Pointer parent_object; + public Pointer priv; + } + + public boolean notify_init(String appName); + public NotifyNotification notify_notification_new(String summary, String body, String icon, Structure attach); + public boolean notify_notification_show(NotifyNotification notification, Structure error); +} Property changes on: jnijnajntest/src/main/java/org/test/testjna/notify/Notify.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: jnijnajntest/src/main/java/org/test/testjna/unix/UnixSystemInfo.java =================================================================== --- jnijnajntest/src/main/java/org/test/testjna/unix/UnixSystemInfo.java (rev 0) +++ jnijnajntest/src/main/java/org/test/testjna/unix/UnixSystemInfo.java 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,145 @@ +/* + * #%L + * jTimer + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2007 - 2011 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +package org.test.testjna.unix; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.sun.jna.platform.unix.X11; +import com.sun.jna.ptr.IntByReference; + +/** + * Linux system info. + * + * Based on + * https://jdic.dev.java.net/source/browse/jdic/src/incubator/systeminfo + * /src/unix/native/jni/systeminfo.c?rev=1.1.1.1&view=markup source code. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class UnixSystemInfo { + + /** log. */ + private static Logger logger = LoggerFactory.getLogger(UnixSystemInfo.class); + + /** + * Constructor. + * + * Package visibility + */ + public UnixSystemInfo() { + } + + public long getIdleTime() { + + long idleTime = 0; + + try { + idleTime = getXssIdleTime(); + } catch (Exception e) { + // TODO catch more specific exception + if (logger.isErrorEnabled()) { + logger.error("Can't detect idle time : ", e); + } + idleTime = 0; + } + + return idleTime; + } + + /** + * Get idle time via Xss. + * + * @return idle time + */ + protected long getXssIdleTime() { + + long idleTime = 0; + + // get X11 display + X11.Display display = X11.INSTANCE.XOpenDisplay(null); + if (display == null) { + logger.error("Can't open X11 display"); + } else { + // Display is opened + + // check if screensaver extension is enabled + IntByReference eventBase = new IntByReference(); + IntByReference errorBase = new IntByReference(); + + if (!Xss.INSTANCE.XScreenSaverQueryExtension(display, eventBase, + errorBase)) { + if (logger.isErrorEnabled()) { + logger.error("Can't find xscreensaver extension, " + + "idle time can't be detected"); + } + } else { + // screensaver is enabled + + Xss.XScreenSaverInfo screenSaverInfo = Xss.INSTANCE + .XScreenSaverAllocInfo(); + + if (screenSaverInfo == null) { + logger.error("Could not alloc screen saver info"); + } else { + // get root window + X11.Window rootWindow = X11.INSTANCE + .XDefaultRootWindow(display); + + if (rootWindow == null) { + logger.error("Could not query root window"); + } else { + + // FIX : without this, cause random NPE + screenSaverInfo.window = rootWindow; + + Xss.INSTANCE.XScreenSaverQueryInfo(display, rootWindow, + screenSaverInfo); + if (screenSaverInfo.idle == null) { + logger.error("screenSaverInfo.idle is null"); + } else { + idleTime = screenSaverInfo.idle.longValue(); + } + } + } + + // free resources + // TODO should be freed + //X11.INSTANCE.XFree(screenSaverInfo); + } + + // free resources + X11.INSTANCE.XCloseDisplay(display); + } + + return idleTime; + } + +} Property changes on: jnijnajntest/src/main/java/org/test/testjna/unix/UnixSystemInfo.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: jnijnajntest/src/main/java/org/test/testjna/unix/Xss.java =================================================================== --- jnijnajntest/src/main/java/org/test/testjna/unix/Xss.java (rev 0) +++ jnijnajntest/src/main/java/org/test/testjna/unix/Xss.java 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,96 @@ +/* + * #%L + * jTimer + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2007 - 2010 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +package org.test.testjna.unix; + +import com.sun.jna.Library; +import com.sun.jna.Native; +import com.sun.jna.NativeLong; +import com.sun.jna.Structure; +import com.sun.jna.platform.unix.X11; +import com.sun.jna.ptr.IntByReference; + +/** + * libXss JNA interface. + * + * /usr/include/X11/extensions/scrnsaver.h + * + * Incomplete definition. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public interface Xss extends Library { + + /** Xss Instance */ + Xss INSTANCE = (Xss) Native.loadLibrary("Xss", Xss.class); + + /** + * XScreenSaverInfo struct + */ + public static class XScreenSaverInfo extends Structure { + public X11.Window window; /* screen saver window - may not exist */ + public int state; /* ScreenSaverOff, ScreenSaverOn, ScreenSaverDisabled */ + public int kind; /* ScreenSaverBlanked, ...Internal, ...External */ + public NativeLong til_or_since; /* time til or since screen saver */ + public NativeLong idle; /* total time since last user input */ + public NativeLong eventMask; /* currently selected events for this client */ + } + + /** + * Information sur l'activation de l'ecran de veille de X11. + * + * @param display display + * @param event_base event_base + * @param error_base error_base + * + * @return true if XScreenSaver extension is loaded + * + * @see IntByReference + */ + public boolean XScreenSaverQueryExtension(X11.Display display, + IntByReference event_base, IntByReference error_base); + + /** + * XScreenSaverAllocInfo. + * + * @return XScreenSaverInfo instance + */ + public XScreenSaverInfo XScreenSaverAllocInfo(); + + /** + * XScreenSaverQueryInfo. + * + * @param display + * @param drawable + * @param infos + * @return status flag + */ + public int XScreenSaverQueryInfo(X11.Display display, + X11.Drawable drawable, XScreenSaverInfo infos); +} Property changes on: jnijnajntest/src/main/java/org/test/testjna/unix/Xss.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: jnijnajntest/src/main/java/org/test/testjni/IdleJNI.java =================================================================== --- jnijnajntest/src/main/java/org/test/testjni/IdleJNI.java (rev 0) +++ jnijnajntest/src/main/java/org/test/testjni/IdleJNI.java 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,49 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.test.testjni; + +/** + * A compiler avec "javah org.test.testjni.IdleJNI". + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class IdleJNI { + + /* Bloc static : le code est exécuté une seule fois + * lors du chargement de la classe + */ + static { + /* On charge la librairie en utilisant son nom de base : */ + System.loadLibrary("testidle"); + } + + public native long getIdleTime(); + +} Property changes on: jnijnajntest/src/main/java/org/test/testjni/IdleJNI.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: jnijnajntest/src/main/java/org/test/testjni/TestIdleJNI.java =================================================================== --- jnijnajntest/src/main/java/org/test/testjni/TestIdleJNI.java (rev 0) +++ jnijnajntest/src/main/java/org/test/testjni/TestIdleJNI.java 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,49 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.test.testjni; + +/** + * TODO add comment here. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class TestIdleJNI { + + public static void main(String... args) throws InterruptedException { + + IdleJNI idleJNI = new IdleJNI(); + while (true) { + Thread.sleep(1000); + + long idle = idleJNI.getIdleTime(); + System.out.println("Idle since " + idle + "ms"); + } + } +} Property changes on: jnijnajntest/src/main/java/org/test/testjni/TestIdleJNI.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: jnijnajntest/src/main/resources/logback.xml =================================================================== --- jnijnajntest/src/main/resources/logback.xml (rev 0) +++ jnijnajntest/src/main/resources/logback.xml 2012-02-08 17:16:37 UTC (rev 525) @@ -0,0 +1,15 @@ +<configuration> + + <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> + <layout class="ch.qos.logback.classic.PatternLayout"> + <Pattern>%d{yyyy/MM/dd HH:mm:ss} [%thread] %-5level %logger{56} - %msg%n</Pattern> + </layout> + </appender> + + <logger name="org.test" level="DEBUG" /> + + <root level="WARN"> + <appender-ref ref="stdout" /> + </root> + +</configuration> \ No newline at end of file