Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <gtk/gtk.h>
  4. #include <glib.h>
  5. #include <math.h>
  6. #define INTERVAL 100
  7.  
  8. typedef struct
  9. {
  10.   gint last_x, last_y;
  11.   gdouble distance;
  12. } MotionRecord;
  13.  
  14. gdouble distance (gint dx, gint dy)
  15. {
  16.   static gboolean initialized = FALSE;
  17.   static gdouble dx_mm, dy_mm;
  18.   if (!initialized)
  19.     {
  20.       gdouble width_meter, width_pixel, height_meter, height_pixel;
  21.       GdkScreen *screen = gdk_screen_get_default ();
  22.       width_meter = (gdouble)gdk_screen_get_width_mm (screen) / 1000;
  23.       width_pixel = gdk_screen_get_width (screen);
  24.       height_meter = (gdouble)gdk_screen_get_height_mm (screen) / 1000;
  25.       height_pixel = gdk_screen_get_height (screen);
  26.      
  27.       dx_mm = (gdouble)width_meter / width_pixel;
  28.       dy_mm = (gdouble)height_meter / height_pixel;
  29.       initialized = TRUE;
  30.     }
  31.    
  32.   return sqrt (pow (dx_mm*dx, 2) + pow (dy_mm*dy, 2));
  33. }
  34.  
  35. gboolean record_motion (gpointer data)
  36. {
  37.   static gboolean initialized = FALSE;
  38.   MotionRecord *record = (MotionRecord *)data;
  39.   GdkWindow *root_window;
  40.   gint x, y;  
  41.  
  42.   root_window = gdk_screen_get_root_window (gdk_screen_get_default ());
  43.   gdk_window_get_pointer (root_window, &x, &y, NULL);
  44.  
  45.   if (initialized)
  46.     {
  47.       gint dx, dy;
  48.       dx = record->last_x - x;
  49.       dy = record->last_y - y;
  50.       record->distance += distance (dx, dy);
  51.     }
  52.   else
  53.     initialized = TRUE;
  54.    
  55.   record->last_x = x;
  56.   record->last_y = y;
  57.   g_print ("distance: %.2fm\n", record->distance);
  58.   return TRUE;
  59. }
  60.  
  61. int main (int argc, char **argv)
  62. {
  63.   MotionRecord record = {0};
  64.  
  65.   gtk_init (&argc, &argv);
  66.   g_timeout_add (INTERVAL, record_motion, &record);
  67.   gtk_main ();
  68.  
  69.   return EXIT_SUCCESS;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement