Skip to content

Commit

Permalink
Added screen monitor feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
CBenoit committed Mar 30, 2016
1 parent 6a7e39d commit 582fd91
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/main/java/fr/utbm/rngames/controller/MainWindowController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import fr.utbm.rngames.event.EventDispatcher;
import fr.utbm.rngames.keyboard.KeyboardWriter;
import fr.utbm.rngames.mouse.MouseWriter;
import fr.utbm.rngames.screen.ScreenMonitor;
import fr.utbm.rngames.screen.ScreenWriter;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.fxml.FXML;
Expand All @@ -46,16 +48,22 @@
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;

public class MainWindowController implements Initializable, CloseEventListener {

private final Logger logger = Logger.getLogger(MainWindowController.class.getName());
private static final Logger LOG = Logger.getLogger(MainWindowController.class.getName());

private KeyboardWriter kWriter;
private MouseWriter mWriter;
private ScreenWriter sWriter;
private ScreenMonitor screenMonitor;
private final BooleanProperty startDisabled = new SimpleBooleanProperty(false);

private ExecutorService executorService = Executors.newSingleThreadExecutor();

@FXML
private TextField textAreaSaveDirectory;

Expand Down Expand Up @@ -148,7 +156,7 @@ private void handleStartRecording() {

this.kWriter.start();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
LOG.severe(exception.getMessage());
}
}

Expand All @@ -160,7 +168,20 @@ private void handleStartRecording() {

this.mWriter.start();
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
LOG.severe(exception.getMessage());
}

try {
this.sWriter = new ScreenWriter(new URL("file:///" + System.getProperty("java.io.tmpdir")
+ File.separator
+ Locale.getString(ScreenWriter.class, "screen.file.name")));

this.sWriter.start();

this.screenMonitor = new ScreenMonitor();
this.executorService.execute(this.screenMonitor);
} catch (IOException exception) {
LOG.severe(exception.getMessage());
}
}

Expand All @@ -178,6 +199,8 @@ public void handleCloseEvent() {
if (this.startDisabled.get()) {
stopAndZip();
}

this.executorService.shutdown();
}

/**
Expand All @@ -204,9 +227,16 @@ private void stopAndZip() {
this.fullRecordName
+ Locale.getString("logfile.mouse.end") //$NON-NLS-1$
+ logfileExtension);

this.sWriter.stop();
zipper.addFile(this.sWriter.getFileLocation(),
this.fullRecordName
+ Locale.getString("logfile.other.end") //$NON-NLS-1$
+ logfileExtension);
this.screenMonitor.stop();
}
} catch (IOException exception) {
this.logger.severe(exception.getMessage());
LOG.severe(exception.getMessage());
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/fr/utbm/rngames/screen/ScreenEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**************************************************************************
* RNGames, a software to record your inputs while playing.
* Copyright (C) 2016 CORTIER Benoît, BOULMIER Jérôme
*
* 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/>.
*************************************************************************/

package fr.utbm.rngames.screen;

import fr.utbm.rngames.event.Event;

public class ScreenEvent implements Event<ScreenEventListener> {
private final double newWidth;

private final double newHeight;

public ScreenEvent(double newWidth, double newHeight) {
this.newWidth = newWidth;
this.newHeight = newHeight;
}

@Override
public void notify(ScreenEventListener listener) {
listener.handleScreenResizeEvent(newWidth, newHeight);
}
}
25 changes: 25 additions & 0 deletions src/main/java/fr/utbm/rngames/screen/ScreenEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**************************************************************************
* RNGames, a software to record your inputs while playing.
* Copyright (C) 2016 CORTIER Benoît, BOULMIER Jérôme
*
* 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/>.
*************************************************************************/

package fr.utbm.rngames.screen;

import java.util.EventListener;

public interface ScreenEventListener extends EventListener {
void handleScreenResizeEvent(double newWidth, double newHeight);
}
73 changes: 73 additions & 0 deletions src/main/java/fr/utbm/rngames/screen/ScreenMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**************************************************************************
* RNGames, a software to record your inputs while playing.
* Copyright (C) 2016 CORTIER Benoît, BOULMIER Jérôme
*
* 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/>.
*************************************************************************/

package fr.utbm.rngames.screen;

import fr.utbm.rngames.controller.CloseEvent;
import fr.utbm.rngames.controller.CloseEventListener;
import fr.utbm.rngames.event.EventDispatcher;
import javafx.stage.Screen;

import java.util.logging.Logger;

public class ScreenMonitor implements CloseEventListener, Runnable {
private static final Logger LOG = Logger.getLogger(ScreenMonitor.class.getName());

private boolean running;

@Override
public void run() {
EventDispatcher.getInstance().addListener(CloseEvent.class, this);

double lastWidth = 0;
double lastHeight = 0;
double newWidth;
double newHeight;

this.running = true;
while (this.running) {
Screen screen = Screen.getPrimary();
newWidth = screen.getBounds().getWidth();
newHeight = screen.getBounds().getHeight();
if (newWidth != lastWidth || newHeight != lastHeight) {
EventDispatcher.getInstance().fire(new ScreenEvent(newWidth, newHeight));
lastWidth = newWidth;
lastHeight = newHeight;
}

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOG.severe(e.getMessage());
}
}

EventDispatcher.getInstance().removeListener(CloseEvent.class, this);
}

/**
* Stop the screen monitor.
*/
public void stop() {
this.running = false;
}

public void handleCloseEvent() {
this.running = false;
}
}
111 changes: 111 additions & 0 deletions src/main/java/fr/utbm/rngames/screen/ScreenWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**************************************************************************
* RNGames, a software to record your inputs while playing.
* Copyright (C) 2016 CORTIER Benoît, BOULMIER Jérôme
*
* 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/>.
*************************************************************************/

package fr.utbm.rngames.screen;

import fr.utbm.rngames.event.EventDispatcher;
import fr.utbm.rngames.mouse.MouseWriter;
import org.arakhne.afc.vmutil.locale.Locale;
import org.jnativehook.GlobalScreen;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.mouse.NativeMouseWheelEvent;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;

public class ScreenWriter implements ScreenEventListener {
private static final String CSV_SEPARATOR;

static {
CSV_SEPARATOR = Locale.getString("screen.csv.separator"); //$NON-NLS-1$
}

private static final Logger LOG = Logger.getLogger(MouseWriter.class.getName());

private final long startTime;
private final URL fileLocation;
private final BufferedWriter writer;

public ScreenWriter(URL fileLocation) throws IOException {
this.startTime = System.currentTimeMillis();
this.fileLocation = fileLocation;
File file = new File(getFileLocation().getPath());

if (file.createNewFile()) {
file.deleteOnExit();
}

this.writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),
StandardCharsets.UTF_8));

this.writer.write(Locale.getString("screen.file.header"));
this.writer.newLine();
}

private void register() {
EventDispatcher.getInstance().addListener(ScreenEvent.class, this);
}

private void unregister() {
EventDispatcher.getInstance().removeListener(ScreenEvent.class, this);
}

public final void start() {
register();
}

public final void stop() {
unregister();
close();
}

public final URL getFileLocation() {
return this.fileLocation;
}

@Override
public void handleScreenResizeEvent(double newWidth, double newHeight) {
try {
this.writer.write(generateScreenResize(newWidth, newHeight, System.currentTimeMillis()));
this.writer.newLine();
} catch (IOException exception) {
LOG.severe(exception.getMessage());
}
}

private String generateScreenResize(double newWidth, double newHeight, long timestampMillis) {
return Locale.getString("screen.event.resize") + CSV_SEPARATOR //$NON-NLS-1$
+ newWidth + CSV_SEPARATOR
+ newHeight + CSV_SEPARATOR
+ (timestampMillis - this.startTime);
}

private void close() {
try {
this.writer.close();
} catch (IOException exception) {
LOG.severe(exception.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ logfile.extension=.csv
logfile.keyboard.end=.K
logfile.mouse.end=.M
logfile.gamepad.end=.P
logfile.other.end=.O

directory.chooser.title = Select directory where records are saved

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
screen.file.header = Event;Width;Height;Time
screen.csv.separator = ;
screen.file.name = nrgamestmp.screen.csv
screen.event.resize = screen resize

0 comments on commit 582fd91

Please sign in to comment.