aggressive screensaver [de]activation
authorozzloy <ozzloy@zero7.(none)>
Sat, 6 Feb 2010 22:55:53 +0000 (14:55 -0800)
committerozzloy <ozzloy@zero7.(none)>
Sat, 6 Feb 2010 23:05:20 +0000 (15:05 -0800)
ditching [Un]Inhibit, SimulateUserActivity in favor of gnome-screensaver-command
which seems to work better.

bugs:
  * mouse and keyboard activity don't delay oble's gnome-screensaver activation
toggle.  sometimes oble will activate the screensaver while the mouse is
moving.
* face recognition is not recognized by gnome-screensaver as user activity.
sometimes gnome-screensaver will activate due to timeout even while oble
clearly shows a face.

.gitignore
Makefile
hello-world.cpp [deleted file]
oble.cpp [new file with mode: 0644]

index b33fd92599e97bdbdd4b4bce7c2ecbb0a3fee380..a32d98a06e83d59d51096698a0219500d563358d 100644 (file)
@@ -1,3 +1,4 @@
 .*.sw?
 hello-world
+gnome-screensaver/
 
index dc5f06f4b554961232a806320c91a5222db05cb2..3f6e743dbd6ef72c4337639d02df530e986f1a58 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,17 +1,19 @@
 
-all:
-       gcc `pkg-config --cflags opencv dbus-glib-1` -o hello-world hello-world.cpp `pkg-config --libs dbus-glib-1 opencv`
+all:oble
 
-dbushello: dbushello.c
-       gcc `pkg-config --cflags dbus-glib-1` -o dbushello dbushello.c `pkg-config --libs dbus-glib-1`
+oble:oble.cpp
+       gcc `pkg-config --cflags opencv dbus-glib-1` -o oble oble.cpp `pkg-config --libs dbus-glib-1 opencv`
 
+gnome-screensaver-poke: gnome-screensaver-poke.c
+       gcc `pkg-config --cflags dbus-glib-1` -o gnome-screensaver-poke gnome-screensaver-poke.c `pkg-config --libs dbus-glib-1`
 
 test: all
-       ./hello-world /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml
+       ./oble /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml
 
 clean:
-       rm -rf hello-world tags dbushello
+       rm -rf oble gnome-screensaver-poke
 
 new: clean all
 
 .PHONY: clean
+
diff --git a/hello-world.cpp b/hello-world.cpp
deleted file mode 100644 (file)
index 12c666e..0000000
+++ /dev/null
@@ -1,179 +0,0 @@
-//use the camera to aid the decision to sleep.
-//Copyright 2009 Daniel Watson
-/*
-    use a camera to prevent screensaver
-    Copyright (C) 2009  daniel watson, ozzloy@gmail.com
-
-    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/>.
-*/
-/*
-        This file is part of oble.
-
-        oble 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.
-
-        oble 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 oble.  If not, see <http://www.gnu.org/licenses/>.
-
-*/
-#include <cv.h>
-#include <highgui.h>
-#include <stdio.h>
-#include <iostream>
-//#include <dbus/dbus.h>
-//#include <dbus/dbus-glib.h>
-
-using namespace std;
-
-//DBusGConnection *connection;
-//GError *error;
-//DBusGProxy *proxy;
-//char **name_list;
-//char **name_list_ptr;
-
-
-// argument for cvFlip(src, dest, FLIP_TYPE)
-#define MIRROR 1
-
-CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
-{
-       return (CvHaarClassifierCascade*)cvLoad(cascade_path);
-}
-
-void detect_and_draw_objects(IplImage* image,
-               CvHaarClassifierCascade* cascade,
-               int do_pyramids)
-{
-       IplImage* small_image = image;
-       CvMemStorage* storage = cvCreateMemStorage(0);
-       CvSeq* faces;
-       int i, scale = 1;
-
-       /* if the flag is specified, down-scale the input image to get a
-                performance boost w/o loosing quality (perhaps) */
-       if(do_pyramids)
-       {
-               small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
-                               IPL_DEPTH_8U, 3);
-               cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
-               scale = 2;
-       }
-
-       /* use the fastest variant */
-       faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
-                       CV_HAAR_DO_CANNY_PRUNING);
-
-       /*
-       if(0 < faces->total)
-       {
-               dbus_g_proxy_call_no_reply(proxy, "SimulateUserActivity", G_TYPE_INVALID);
-       }
-       */
-
-       /* draw all the rectangles */
-       for(i = 0; i < faces->total; i++)
-       {
-               /* extract the rectangles only */
-               CvRect face = *(CvRect*)cvGetSeqElem(faces, i);
-               CvPoint upperLeft = cvPoint(face.x * scale, face.y * scale);
-               CvPoint bottomRight = cvPoint((face.x + face.width) * scale,
-                               (face.y + face.height) * scale);
-               cvRectangle(image, upperLeft, bottomRight, CV_RGB(255,0,0), 3);
-       }
-
-       if(small_image != image)
-               cvReleaseImage(&small_image);
-       cvReleaseMemStorage(&storage);
-}
-
-// A Simple Camera Capture Framework.
-int main(int argc, char** argv)
-{
-//     g_type_init ();
-
-       //error = NULL;
-       //connection = dbus_g_bus_get (DBUS_BUS_SESSION,
-       //              &error);
-       /*if (connection == NULL)
-       {
-               g_printerr ("Failed to open connection to bus: %s\n",
-                               error->message);
-               g_error_free (error);
-               exit (1);
-       }*/
-
-       /* Create a proxy object for the "bus driver" (name "org.freedesktop.DBus") */
-
-       /*
-       proxy = dbus_g_proxy_new_for_name (connection,
-                       "org.gnome.ScreenSaver",
-                       "/org/gnome/ScreenSaver",
-                       "org.gnome.ScreenSaver");
-                       */
-
-       CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
-
-       CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
-       if(!capture) {
-               cerr << "ERROR: capture is NULL " << endl;
-               getchar();
-               return -1;
-       }
-
-       // Create a window in which the captured images will be presented
-       cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
-
-       IplImage* frame = cvQueryFrame(capture);
-       if(!frame) {
-               cerr << "ERROR: frame is null..." << endl;
-               getchar();
-               return -1;
-       }
-       IplImage* mirrored =
-               cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
-       // Show the image captured from the camera in the window and repeat
-       while(1) {
-               // Get one frame
-               frame = cvQueryFrame(capture);
-               if(!frame) {
-                       cerr << "ERROR: frame is null..." << endl;
-                       getchar();
-                       break;
-               }
-
-               //flip the image so displayed right/left corresponds to physical right/left
-               cvFlip(frame, mirrored, MIRROR);
-
-               detect_and_draw_objects(mirrored, cascade, 1);
-               cvShowImage("mywindow", mirrored);
-               // Do not release the frame!
-
-               //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
-               //remove higher bits using AND operator
-               if((cvWaitKey(10) & 255) == 27) break;
-       }
-
-       // Release the capture device housekeeping
-       cvReleaseCapture(&capture);
-       cvDestroyWindow("mywindow");
-       cvReleaseImage(&mirrored);
-       return 0;
-}
diff --git a/oble.cpp b/oble.cpp
new file mode 100644 (file)
index 0000000..c710b4e
--- /dev/null
+++ b/oble.cpp
@@ -0,0 +1,178 @@
+//use the camera to aid the decision to sleep.
+//Copyright 2009 Daniel Watson
+/*
+    use a camera to prevent screensaver
+    Copyright (C) 2009  daniel watson, ozzloy@gmail.com
+
+    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/>.
+*/
+/*
+        This file is part of oble.
+
+        oble 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.
+
+        oble 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 oble.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+#include <cv.h>
+#include <highgui.h>
+#include <stdio.h>
+#include <iostream>
+#include <time.h>
+
+static time_t t_last = time(NULL);
+static time_t t_current = time(NULL);
+static time_t t_tmp = time(NULL);
+
+using namespace std;
+
+// argument for cvFlip(src, dest, FLIP_TYPE)
+#define MIRROR 1
+
+CvHaarClassifierCascade* load_object_detector(const char* cascade_path)
+{
+       return (CvHaarClassifierCascade*)cvLoad(cascade_path);
+}
+
+void detect_and_draw_objects(IplImage* image,
+               CvHaarClassifierCascade* cascade,
+               int do_pyramids)
+{
+       IplImage* small_image = image;
+       CvMemStorage* storage = cvCreateMemStorage(0);
+       CvSeq* faces;
+       int i, scale = 1;
+
+       /* if the flag is specified, down-scale the input image to get a
+                performance boost w/o loosing quality (perhaps) */
+       if(do_pyramids)
+       {
+               small_image = cvCreateImage(cvSize(image->width/2,image->height/2),
+                               IPL_DEPTH_8U, 3);
+               cvPyrDown(image, small_image, CV_GAUSSIAN_5x5);
+               scale = 2;
+       }
+
+       /* use the fastest variant */
+       faces = cvHaarDetectObjects(small_image, cascade, storage, 1.2, 2,
+                       CV_HAAR_DO_CANNY_PRUNING);
+
+       if(0 < faces->total)
+       {
+               t_current = time(NULL);
+       }
+
+       /* draw all the rectangles */
+       for(i = 0; i < faces->total; i++)
+       {
+               /* extract the rectangles only */
+               CvRect face = *(CvRect*)cvGetSeqElem(faces, i);
+               CvPoint upperLeft = cvPoint(face.x * scale, face.y * scale);
+               CvPoint bottomRight = cvPoint((face.x + face.width) * scale,
+                               (face.y + face.height) * scale);
+               cvRectangle(image, upperLeft, bottomRight, CV_RGB(255,0,0), 3);
+       }
+
+       if(small_image != image)
+               cvReleaseImage(&small_image);
+       cvReleaseMemStorage(&storage);
+}
+
+void screensave(time_t t_current)
+{
+       static int last_elapse = 0;
+       static int activated = 0;
+       int elapse = difftime(time(NULL), t_current);
+       int timeout = 10;
+       if(elapse > timeout && elapse != last_elapse)
+       {
+               last_elapse = elapse;
+               printf("elapse = %d\n", elapse);
+               if(!activated)
+               {
+                       printf("activated\n");
+                       activated = 1;
+                       system("gnome-screensaver-command -a");
+               }
+       }
+       if(elapse < timeout && activated)
+       {
+               printf("deactivated\n");
+               activated = 0;
+               system("gnome-screensaver-command -d");
+       }
+}
+
+// A Simple Camera Capture Framework.
+int main(int argc, char** argv)
+{
+       CvHaarClassifierCascade* cascade = load_object_detector(argv[1]);
+
+       CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
+       if(!capture) {
+               cerr << "ERROR: capture is NULL " << endl;
+               getchar();
+               return -1;
+       }
+
+       // Create a window in which the captured images will be presented
+       cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
+
+       IplImage* frame = cvQueryFrame(capture);
+       if(!frame) {
+               cerr << "ERROR: frame is null..." << endl;
+               getchar();
+               return -1;
+       }
+       IplImage* mirrored =
+               cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
+       t_current = time(NULL);
+       // Show the image captured from the camera in the window and repeat
+       while(1) {
+               // Get one frame
+               frame = cvQueryFrame(capture);
+               if(!frame) {
+                       cerr << "ERROR: frame is null..." << endl;
+                       getchar();
+                       break;
+               }
+
+               //flip the image so displayed right/left corresponds to physical right/left
+               cvFlip(frame, mirrored, MIRROR);
+
+               detect_and_draw_objects(mirrored, cascade, 1);
+               cvShowImage("mywindow", mirrored);
+               // Do not release the frame!
+               screensave(t_current);
+
+               //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
+               //remove higher bits using AND operator
+               if((cvWaitKey(100) & 255) == 27) break;
+       }
+
+       // Release the capture device housekeeping
+       cvReleaseCapture(&capture);
+       cvDestroyWindow("mywindow");
+       cvReleaseImage(&mirrored);
+       return 0;
+}