comparison gtk4/dw.c @ 2759:cd6a306800f5

GTK4: New way of querying the mouse pointer position. The old code was completely bogus and did not work at all. Since there doesn't seem to be a way to actually query the position... Plus with wayland there are no global coordinates... instead... Any event we handle that has pointer coordinates... we convert them to be window relative and save them. When querying the pointer location, we return the latest coordinates that we had handled. I removed the thread safety since there are no API calls, but we may want to put it back so that the data doesn't get changed during the call... although that may not be the most tragic thing.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 03 Jan 2022 16:23:51 +0000
parents e1b5dbec0796
children 8a5131cbbe93
comparison
equal deleted inserted replaced
2758:e1b5dbec0796 2759:cd6a306800f5
472 #ifdef USE_WEBKIT 472 #ifdef USE_WEBKIT
473 static void _dw_html_result_event(GObject *object, GAsyncResult *result, gpointer script_data); 473 static void _dw_html_result_event(GObject *object, GAsyncResult *result, gpointer script_data);
474 static void _dw_html_changed_event(WebKitWebView *web_view, WebKitLoadEvent load_event, gpointer data); 474 static void _dw_html_changed_event(WebKitWebView *web_view, WebKitLoadEvent load_event, gpointer data);
475 #endif 475 #endif
476 static void _dw_signal_disconnect(gpointer data, GClosure *closure); 476 static void _dw_signal_disconnect(gpointer data, GClosure *closure);
477 static void _dw_event_coordinates_to_window(GtkWidget *widget, double *x, double *y);
477 478
478 GObject *_DWObject = NULL; 479 GObject *_DWObject = NULL;
479 GApplication *_DWApp = NULL; 480 GApplication *_DWApp = NULL;
480 GMainLoop *_DWMainLoop = NULL; 481 GMainLoop *_DWMainLoop = NULL;
481 static char _dw_app_id[_DW_APP_ID_SIZE+1] = { 0 }; 482 static char _dw_app_id[_DW_APP_ID_SIZE+1] = { 0 };
482 char *_DWDefaultFont = NULL; 483 char *_DWDefaultFont = NULL;
483 static char _dw_share_path[PATH_MAX+1] = { 0 }; 484 static char _dw_share_path[PATH_MAX+1] = { 0 };
485 static long _dw_mouse_last_x = 0;
486 static long _dw_mouse_last_y = 0;
484 487
485 typedef struct _dw_signal_list 488 typedef struct _dw_signal_list
486 { 489 {
487 void *func; 490 void *func;
488 char name[30]; 491 char name[30];
776 mybutton = DW_BUTTON2_MASK; 779 mybutton = DW_BUTTON2_MASK;
777 else if(mybutton == 2) 780 else if(mybutton == 2)
778 mybutton = DW_BUTTON3_MASK; 781 mybutton = DW_BUTTON3_MASK;
779 782
780 retval = buttonfunc(work.window, (int)x, (int)y, mybutton, work.data); 783 retval = buttonfunc(work.window, (int)x, (int)y, mybutton, work.data);
784
785 _dw_event_coordinates_to_window(work.window, &x, &y);
786
787 _dw_mouse_last_x = (long)x;
788 _dw_mouse_last_y = (long)y;
781 } 789 }
782 return retval; 790 return retval;
783 } 791 }
784 792
785 static gint _dw_button_release_event(GtkGestureSingle *gesture, int n_press, double x, double y, gpointer data) 793 static gint _dw_button_release_event(GtkGestureSingle *gesture, int n_press, double x, double y, gpointer data)
796 mybutton = DW_BUTTON2_MASK; 804 mybutton = DW_BUTTON2_MASK;
797 else if(mybutton == 2) 805 else if(mybutton == 2)
798 mybutton = DW_BUTTON3_MASK; 806 mybutton = DW_BUTTON3_MASK;
799 807
800 retval = buttonfunc(work.window, (int)x, (int)y, mybutton, work.data); 808 retval = buttonfunc(work.window, (int)x, (int)y, mybutton, work.data);
809
810 _dw_event_coordinates_to_window(work.window, &x, &y);
811
812 _dw_mouse_last_x = (long)x;
813 _dw_mouse_last_y = (long)y;
801 } 814 }
802 return retval; 815 return retval;
803 } 816 }
804 817
805 static gint _dw_motion_notify_event(GtkEventControllerMotion *controller, double x, double y, gpointer data) 818 static gint _dw_motion_notify_event(GtkEventControllerMotion *controller, double x, double y, gpointer data)
820 keys |= DW_BUTTON2_MASK; 833 keys |= DW_BUTTON2_MASK;
821 if (state & GDK_BUTTON2_MASK) 834 if (state & GDK_BUTTON2_MASK)
822 keys |= DW_BUTTON3_MASK; 835 keys |= DW_BUTTON3_MASK;
823 836
824 retval = motionfunc(work.window, (int)x, (int)y, keys, work.data); 837 retval = motionfunc(work.window, (int)x, (int)y, keys, work.data);
838
839 _dw_event_coordinates_to_window(work.window, &x, &y);
840
841 _dw_mouse_last_x = (long)x;
842 _dw_mouse_last_y = (long)y;
825 } 843 }
826 return retval; 844 return retval;
827 } 845 }
828 846
829 static gboolean _dw_delete_event(GtkWidget *window, gpointer data) 847 static gboolean _dw_delete_event(GtkWidget *window, gpointer data)
989 } 1007 }
990 return retval; 1008 return retval;
991 } 1009 }
992 1010
993 /* Convert coordinate system from the widget to the window */ 1011 /* Convert coordinate system from the widget to the window */
994 void _dw_event_coordinates_to_window(GtkWidget *widget, double *x, double *y) 1012 static void _dw_event_coordinates_to_window(GtkWidget *widget, double *x, double *y)
995 { 1013 {
996 GtkRoot *root = (widget && GTK_IS_WIDGET(widget)) ? gtk_widget_get_root(widget) : NULL; 1014 GtkRoot *root = (widget && GTK_IS_WIDGET(widget)) ? gtk_widget_get_root(widget) : NULL;
997 1015
998 if(root && GTK_IS_WIDGET(root)) 1016 if(root && GTK_IS_WIDGET(root))
999 { 1017 {
1040 char *text = NULL; 1058 char *text = NULL;
1041 void *itemdata = NULL; 1059 void *itemdata = NULL;
1042 GtkWidget *widget = work.window; 1060 GtkWidget *widget = work.window;
1043 1061
1044 _dw_event_coordinates_to_window(widget, &x, &y); 1062 _dw_event_coordinates_to_window(widget, &x, &y);
1063
1064 _dw_mouse_last_x = (long)x;
1065 _dw_mouse_last_y = (long)y;
1045 1066
1046 /* Containers and trees are inside scrolled window widgets */ 1067 /* Containers and trees are inside scrolled window widgets */
1047 if(GTK_IS_SCROLLED_WINDOW(widget)) 1068 if(GTK_IS_SCROLLED_WINDOW(widget))
1048 widget = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "_dw_user")); 1069 widget = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "_dw_user"));
1049 1070
3296 * Returns the current X and Y coordinates of the mouse pointer. 3317 * Returns the current X and Y coordinates of the mouse pointer.
3297 * Parameters: 3318 * Parameters:
3298 * x: Pointer to variable to store X coordinate. 3319 * x: Pointer to variable to store X coordinate.
3299 * y: Pointer to variable to store Y coordinate. 3320 * y: Pointer to variable to store Y coordinate.
3300 */ 3321 */
3301 DW_FUNCTION_DEFINITION(dw_pointer_query_pos, void, long *x, long *y) 3322 void API dw_pointer_query_pos(long *x, long *y)
3302 DW_FUNCTION_ADD_PARAM2(x, y) 3323 {
3303 DW_FUNCTION_NO_RETURN(dw_pointer_query_pos)
3304 DW_FUNCTION_RESTORE_PARAM2(x, long *, y, long *)
3305 {
3306 GdkSeat *seat = gdk_display_get_default_seat(gdk_display_get_default());
3307 GdkDevice *mouse = gdk_seat_get_pointer(seat);
3308 double dx, dy;
3309
3310 gdk_device_get_surface_at_position(mouse, &dx, &dy);
3311
3312 if(x) 3324 if(x)
3313 *x = (long)dx; 3325 *x = (long)_dw_mouse_last_x;
3314 if(y) 3326 if(y)
3315 *y = (long)dy; 3327 *y = (long)_dw_mouse_last_y;
3316 DW_FUNCTION_RETURN_NOTHING;
3317 } 3328 }
3318 3329
3319 /* 3330 /*
3320 * Sets the X and Y coordinates of the mouse pointer. 3331 * Sets the X and Y coordinates of the mouse pointer.
3321 * Parameters: 3332 * Parameters: