Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <gtk/gtk.h>
- #include <glib.h>
- #include <math.h>
- #define INTERVAL 100
- typedef struct
- {
- gint last_x, last_y;
- gdouble distance;
- } MotionRecord;
- gdouble distance (gint dx, gint dy)
- {
- static gboolean initialized = FALSE;
- static gdouble dx_mm, dy_mm;
- if (!initialized)
- {
- gdouble width_meter, width_pixel, height_meter, height_pixel;
- GdkScreen *screen = gdk_screen_get_default ();
- width_meter = (gdouble)gdk_screen_get_width_mm (screen) / 1000;
- width_pixel = gdk_screen_get_width (screen);
- height_meter = (gdouble)gdk_screen_get_height_mm (screen) / 1000;
- height_pixel = gdk_screen_get_height (screen);
- dx_mm = (gdouble)width_meter / width_pixel;
- dy_mm = (gdouble)height_meter / height_pixel;
- initialized = TRUE;
- }
- return sqrt (pow (dx_mm*dx, 2) + pow (dy_mm*dy, 2));
- }
- gboolean record_motion (gpointer data)
- {
- static gboolean initialized = FALSE;
- MotionRecord *record = (MotionRecord *)data;
- GdkWindow *root_window;
- gint x, y;
- root_window = gdk_screen_get_root_window (gdk_screen_get_default ());
- gdk_window_get_pointer (root_window, &x, &y, NULL);
- if (initialized)
- {
- gint dx, dy;
- dx = record->last_x - x;
- dy = record->last_y - y;
- record->distance += distance (dx, dy);
- }
- else
- initialized = TRUE;
- record->last_x = x;
- record->last_y = y;
- g_print ("distance: %.2fm\n", record->distance);
- return TRUE;
- }
- int main (int argc, char **argv)
- {
- MotionRecord record = {0};
- gtk_init (&argc, &argv);
- g_timeout_add (INTERVAL, record_motion, &record);
- gtk_main ();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement