浏览代码

Made x11idle more robust

* UTILITIES/x11idle.c (org-clock-idle-time): Added multiple checks to
  functions return values to prevent segfault. Also "fixed" return codes
  to fail unless the value could be printed, in which case the program
  succeeds.

TINYCHANGE
Nicolas Calderon Asselin 12 年之前
父节点
当前提交
49994a4c3e
共有 1 个文件被更改,包括 15 次插入4 次删除
  1. 15 4
      UTILITIES/x11idle.c

+ 15 - 4
UTILITIES/x11idle.c

@@ -8,14 +8,25 @@
  * path
  */
 main() {
+    Status querry = 0;
     XScreenSaverInfo *info = XScreenSaverAllocInfo();
+    //open the display specified by the DISPLAY environment variable
     Display *display = XOpenDisplay(0);
 
-    //check that X11 is running or else you get a segafult/coredump
-    if (display != NULL) {
-	XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
+    //display could be null if there is no X server running
+    if (info == NULL || display == NULL) {
+    return -1;
     }
-    XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
+
+    //X11 is running, retrieve and print idle time
+	querry = XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
+
+    if (querry == 0) {
+    return -1;
+    }
+
+    //idle time was retrieved successfully, print it
     printf("%u\n", info->idle);
     return 0;
 }
+