How to detect an active application using C / java?

I want to record the active application and save the history of my active applications. I say an active application, because if I run the application and it is minimized, etc., I would not consider it active. To make my question more understandable, consider this example. I open Firefox and search the web for 20 minutes. Then I open a text editor and start writing within 5 minutes (Firefox works, but I do not use it, so Firefox should not be considered an active application). Therefore, I should be able to record the following information:

Firefox -> 20 minutes Text editor -> 5 minutes 

I want to keep track of every application I use while the OS is running (preferably Linux), and make an open source application that says how much you use each application.

UPDATE: the application that I want to record is the application that the user sees on the display and works. For example, you can resize the window so that you can see both the Firefox editor and the Text editor (along with another workspace, cascade, etc.), but you are printing a text editor, so the text editor is the active application. In other words, the application is active if you interact with the last interacting application that you are viewing to read something in PDF, text, etc. Interaction with the application: I mean clicking, entering text, scrolling, etc.

NODE: The only thing I can’t understand is how to get an active application with these conditions. I think that if I get the best display application in the current workspace that solves the problem, then we have the ability to install on windows (always on top) that may have to do without this property. I found this question that could help answer.

Using Fedora 26,

Thanks in advance.

+5
source share
2 answers
  • Regardless of whether the application is "minimized", it is NOT a property of the Linux process. Rather, it is managed by your "desktop manager: software (such as Gnome), which, in turn, sits on top of X Windows.

  • In other words, to find which applications are "minimized" and which do not, you usually have to query X Windows. For instance:

How can you check if a window is minimized through a terminal in Linux

 if xwininfo -all -id $windowIdGoHere |grep "Hidden"; then echo "is hidden" fi 
  1. If "xwininfo" works for you ... then you can, of course, call it from Java, for example, using Process p = Runtime.getRuntime().exec(...) .
+2
source

Thanks to the contributors, I found a solution to do what I asked. I found this github project that does a similar job in C, and then implemented the solution in java (I prefer it because of maven and JavaFX).

 import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.TimeUnit; public class Main { private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); public static void main(String[] args) throws IOException, InterruptedException { System.out.println("Start :"); while(true) { Date date = new Date(); Process proc = Runtime.getRuntime().exec("xdotool getactivewindow getwindowname"); java.io.InputStream is = proc.getInputStream(); java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); String val = ""; if (s.hasNext()) { val = s.next(); } else { val = ""; } System.out.print(val +" " + "at:"); System.out.println(sdf.format(date)); TimeUnit.SECONDS.sleep(10); } } } 

It returns the name of the active window every 10 seconds and prints the title of the application in use:

 Start : ApplineBuilder - NetBeans IDE 8.2 at:2017/10/12 02:58:58 ApplineBuilder - NetBeans IDE 8.2 at:2017/10/12 02:59:08 GoldenDict at:2017/10/12 02:59:18 at:2017/10/12 02:59:28 How to detect the active application using C/java? - Qaru - Mozilla Firefox at:2017/10/12 02:59:38 ApplineBuilder - NetBeans IDE 8.2 at:2017/10/12 02:59:48 Cancel Running Task at:2017/10/12 02:59:58 
+1
source

Source: https://habr.com/ru/post/1272426/


All Articles