Adjust the Mouse Gestures coding style

This commit is contained in:
Jérôme Geulfucci 2009-05-19 19:10:13 +02:00 committed by Christian Dywan
parent e1268d3dea
commit c9550d36e3

View file

@ -13,10 +13,10 @@
#include "mouse-gestures.h" #include "mouse-gestures.h"
#define MOUSE_GESTURES_VERSION "0.1" #define MOUSE_GESTURES_VERSION "0.1"
#define DEVIANCE 20 // the deviance to determine if a line is straight or not #define DEVIANCE 20
#define MINLENGTH 50 // the minimal length of a line to be treated as a gesture #define MINLENGTH 50
// #define __MOUSE_GESTURES_DEBUG__ // uncomment for debugging purposes /* #define __MOUSE_GESTURES_DEBUG__ */
MouseGesture *gesture; MouseGesture *gesture;
@ -29,35 +29,42 @@ void mouse_gesture_clear(MouseGesture *g)
g->end.x = 0; g->end.x = 0;
g->end.y = 0; g->end.y = 0;
g->last = MOUSE_BUTTON_UNSET; g->last = MOUSE_BUTTON_UNSET;
return;
} }
MouseGesture* mouse_gesture_new (void) MouseGesture* mouse_gesture_new (void)
{ {
MouseGesture *g = g_new (MouseGesture, 1); MouseGesture *g = g_new (MouseGesture, 1);
mouse_gesture_clear (g); mouse_gesture_clear (g);
return(g);
return g;
} }
static gboolean mouse_gestures_handle_events(GtkWidget *widget, GdkEvent *event, MidoriBrowser *browser) static gboolean mouse_gestures_handle_events (GtkWidget *widget,
GdkEvent *event,
MidoriBrowser *browser)
{ {
if(event->type == GDK_BUTTON_PRESS) // a button was pressed /* A button was pressed */
if (event->type == GDK_BUTTON_PRESS)
{ {
if(gesture->last == MOUSE_BUTTON_UNSET) // if the gesture was previously cleaned -> new gesture -> new start coordinates /* If the gesture was previously cleaned, start a new gesture and coordinates */
if (gesture->last == MOUSE_BUTTON_UNSET)
{ {
gesture->start.x = event->button.x; gesture->start.x = event->button.x;
gesture->start.y = event->button.y; gesture->start.y = event->button.y;
gesture->last = event->button.button; gesture->last = event->button.button;
} }
return(TRUE);
return TRUE;
} }
else if(event->type == GDK_MOTION_NOTIFY) // the mouse was moved else if (event->type == GDK_MOTION_NOTIFY)
{ {
if (gesture->last != MOUSE_BUTTON_UNSET) if (gesture->last != MOUSE_BUTTON_UNSET)
{ {
guint x, y; guint x, y;
x = event->motion.x; x = event->motion.x;
y = event->motion.y; y = event->motion.y;
if ((gesture->start.x - x < DEVIANCE && gesture->start.x - x > -DEVIANCE) || if ((gesture->start.x - x < DEVIANCE && gesture->start.x - x > -DEVIANCE) ||
(gesture->start.y - y < DEVIANCE && gesture->start.y - y > -DEVIANCE)) (gesture->start.y - y < DEVIANCE && gesture->start.y - y > -DEVIANCE))
{ {
@ -71,152 +78,176 @@ static gboolean mouse_gestures_handle_events(GtkWidget *widget, GdkEvent *event,
gesture->end.y = y; gesture->end.y = y;
} }
} }
return(TRUE);
return TRUE;
} }
else if (event->type == GDK_BUTTON_RELEASE) else if (event->type == GDK_BUTTON_RELEASE)
{ {
if(gesture->last == MOUSE_BUTTON_MIDDLE) // all mouse gestures will use the middle mouse button /* All mouse gestures will use the middle mouse button */
if (gesture->last == MOUSE_BUTTON_MIDDLE)
{ {
// middle mouse button has been released /* The initial horizontal move is between the bounds */
if(gesture->middle.x - gesture->start.x < DEVIANCE && gesture->middle.x - gesture->start.x > -DEVIANCE) if ((gesture->middle.x - gesture->start.x < DEVIANCE) &&
(gesture->middle.x - gesture->start.x > -DEVIANCE))
{ {
// StartNode to MiddleNode is a straight vertical line /* We initially moved down more than MINLENGTH pixels */
if (gesture->middle.y > gesture->start.y + MINLENGTH) if (gesture->middle.y > gesture->start.y + MINLENGTH)
{ {
// StartNode to MiddleNode is drawn downwards /* Then we the final vertical move is between the bounds and
if(gesture->middle.y - gesture->end.y < DEVIANCE && gesture->middle.y - gesture->end.y > -DEVIANCE && gesture->end.x > gesture->middle.x + MINLENGTH) we moved right more than MINLENGTH pixels */
if ((gesture->middle.y - gesture->end.y < DEVIANCE) &&
(gesture->middle.y - gesture->end.y > -DEVIANCE) &&
(gesture->end.x > gesture->middle.x + MINLENGTH))
{ {
// MiddleNode to EndNode is a straight horizontal line (to the right) -> close tab /* We moved down then right: close the tab */
midori_browser_activate_action (browser, "TabClose"); midori_browser_activate_action (browser, "TabClose");
#ifdef __MOUSE_GESTURES_DEBUG__
g_print("TabClose gesture\n");
#endif
} }
else if(gesture->middle.y - gesture->end.y < DEVIANCE && gesture->middle.y - gesture->end.y > -DEVIANCE && gesture->end.x < gesture->middle.x - MINLENGTH) /* Then we the final vertical move is between the bounds and
we moved left more than MINLENGTH pixels */
else if ((gesture->middle.y - gesture->end.y < DEVIANCE) &&
(gesture->middle.y - gesture->end.y > -DEVIANCE) &&
(gesture->end.x + MINLENGTH < gesture->middle.x))
{ {
// MiddleNode to EndNode is a straight horizontal line (to the left) -> reload /* We moved down then left: reload */
midori_browser_activate_action (browser, "Reload"); midori_browser_activate_action (browser, "Reload");
#ifdef __MOUSE_GESTURES_DEBUG__
g_print("Reload gesture\n");
#endif
} }
/* The end node was never updated, we only did a vertical move */
else if(gesture->end.y == 0 && gesture->end.x == 0) else if(gesture->end.y == 0 && gesture->end.x == 0)
{ {
// no EndNode, just a vertical line -> new tab /* We moved down then: create a new tab */
midori_browser_activate_action (browser, "TabNew"); midori_browser_activate_action (browser, "TabNew");
#ifdef __MOUSE_GESTURES_DEBUG__
g_print("TabNew gesture\n");
#endif
} }
} }
if(gesture->middle.y < gesture->start.y - MINLENGTH) /* We initially moved up more than MINLENGTH pixels */
else if (gesture->middle.y + MINLENGTH < gesture->start.y)
{ {
// StartNode to MiddleNode is drawn upwards /* The end node was never updated, we only did a vertical move */
if (gesture->end.y == 0 && gesture->end.x == 0) if (gesture->end.y == 0 && gesture->end.x == 0)
{ {
// no EndNode, just a vertical line -> stop /* We moved up: stop */
midori_browser_activate_action (browser, "Stop"); midori_browser_activate_action (browser, "Stop");
#ifdef __MOUSE_GESTURES_DEBUG__
g_print("Stop gesture\n");
#endif
} }
} }
} }
else if(gesture->middle.y - gesture->start.y < DEVIANCE && gesture->middle.y - gesture->start.y > -DEVIANCE) /* The initial horizontal move is between the bounds */
else if ((gesture->middle.y - gesture->start.y < DEVIANCE) &&
(gesture->middle.y - gesture->start.y > -DEVIANCE))
{ {
// Start Node to MiddleNode is a straight horizontal line /* We initially moved right more than MINLENGTH pixels */
if (gesture->middle.x > gesture->start.x + MINLENGTH) if (gesture->middle.x > gesture->start.x + MINLENGTH)
{ {
// the line was drawn from left to right /* The end node was never updated, we only did an horizontal move */
if (gesture->end.x == 0 && gesture->end.y == 0) if (gesture->end.x == 0 && gesture->end.y == 0)
{ {
// we only have one horizontal line from left to right -> forward gesture /* We moved right: forward */
midori_browser_activate_action (browser, "Forward"); midori_browser_activate_action (browser, "Forward");
#ifdef __MOUSE_GESTURES_DEBUG__
g_print("Forward gesture\n");
#endif
} }
} }
else if(gesture->middle.x < gesture->start.x - MINLENGTH) /* We initially moved left more than MINLENGTH pixels */
else if (gesture->middle.x + MINLENGTH < gesture->start.x)
{ {
// the line was drawn from right to left /* The end node was never updated, we only did an horizontal move */
if (gesture->end.x == 0 && gesture->end.y == 0) if (gesture->end.x == 0 && gesture->end.y == 0)
{ {
// we only have one horizontal line from right to left -> backwards gesture /* We moved left: back */
midori_browser_activate_action (browser, "Back"); midori_browser_activate_action (browser, "Back");
#ifdef __MOUSE_GESTURES_DEBUG__
g_print("Back gesture\n");
#endif
} }
} }
} }
} }
mouse_gesture_clear(gesture); // gesture finished, clear it
return(TRUE); mouse_gesture_clear (gesture);
return TRUE;
} }
else else
return(FALSE); // this event is supposed to be handled again (by someone else's code) since it was irrelevant to us return FALSE;
} }
static void mouse_gestures_tab_cb(MidoriBrowser* browser, GtkWidget *view) // this is performed when a new tab is created static void mouse_gestures_tab_cb (MidoriBrowser* browser, GtkWidget *view)
{ {
g_signal_connect(view, "event", G_CALLBACK(mouse_gestures_handle_events), browser); // perform the above callback when an event from the view is received g_signal_connect (view, "event", G_CALLBACK (mouse_gestures_handle_events), browser);
return;
} }
static void mouse_gestures_browser_cb(MidoriApp *app, MidoriBrowser *browser) // this is performed when ever a new window is created static void mouse_gestures_browser_cb (MidoriApp *app, MidoriBrowser *browser)
{ {
g_signal_connect(browser, "add-tab", G_CALLBACK(mouse_gestures_tab_cb), NULL); // connect the above callback to the "add-tab" signal of browser g_signal_connect (browser, "add-tab", G_CALLBACK (mouse_gestures_tab_cb), NULL);
return;
} }
/* this is performed when the extension is deactivated.
disconnect all signal handlers, so that mouse gestures are no longer handled */
static void mouse_gestures_deactivate (MidoriExtension *extension, MidoriApp *app) static void mouse_gestures_deactivate (MidoriExtension *extension, MidoriApp *app)
{ {
gulong signal_id = g_signal_handler_find(app, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, mouse_gestures_browser_cb, NULL); // get the signal handler id gulong signal_id;
if(signal_id != 0) // if that id is valid KatzeArray *browsers;
g_signal_handler_disconnect(app, signal_id); // disconnect the signal guint i;
KatzeArray *browsers = katze_object_get_object(app, "browsers"); // get the array that contains all browsers gint j;
guint i; // our counter variable :) GtkWidget *notebook;
for(i = 0; i < katze_array_get_length(browsers); i++) // from the first to the last browser
{
MidoriBrowser *browser = katze_array_get_nth_item(browsers, i); // get a pointer on the current browser
signal_id = g_signal_handler_find(browser, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, mouse_gestures_tab_cb, NULL); // search forthe signal handler id
if(signal_id != 0) // and if its not invalid..
g_signal_handler_disconnect(browser, signal_id); // disconnect it
GtkWidget *notebook = katze_object_get_object(browser, "notebook"); // get a pointer on the notebook signal_id =
gint j; // another counter g_signal_handler_find (app, G_SIGNAL_MATCH_FUNC,
for(j = 0; j < gtk_notebook_get_n_pages(GTK_NOTEBOOK(notebook)); j++) // from the first to the last tab 0, 0, NULL,
mouse_gestures_browser_cb, NULL);
if(signal_id != 0)
g_signal_handler_disconnect (app, signal_id);
browsers = katze_object_get_object (app, "browsers");
for (i = 0; i < katze_array_get_length (browsers); i++)
{ {
GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(notebook), j); // get a pointer on the tab's view MidoriBrowser *browser;
signal_id = g_signal_handler_find(page, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, mouse_gestures_handle_events, NULL); // find the signal id of the event handler
if(signal_id != 0) // if the id is valid browser = katze_array_get_nth_item (browsers, i);
g_signal_handler_disconnect(page, signal_id); // disconnect the handler
signal_id =
g_signal_handler_find (browser, G_SIGNAL_MATCH_FUNC,
0, 0, NULL,
mouse_gestures_tab_cb, NULL);
if (signal_id != 0)
g_signal_handler_disconnect (browser, signal_id);
notebook = katze_object_get_object (browser, "notebook");
for (j = 0; j < gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook)); j++)
{
GtkWidget *page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), j);
signal_id =
g_signal_handler_find (page, G_SIGNAL_MATCH_FUNC,
0, 0, NULL,
mouse_gestures_handle_events, NULL);
if (signal_id != 0)
g_signal_handler_disconnect (page, signal_id);
} }
} }
g_signal_handlers_disconnect_by_func (extension, mouse_gestures_deactivate, app); g_signal_handlers_disconnect_by_func (extension, mouse_gestures_deactivate, app);
g_free(gesture); // free the structure that contains the gesture information g_free (gesture);
return;
} }
static void mouse_gestures_activate(MidoriExtension *extension, MidoriApp *app) // this is performed on extension-activation static void mouse_gestures_activate (MidoriExtension *extension, MidoriApp *app)
{ {
gesture = mouse_gesture_new (); gesture = mouse_gesture_new ();
g_signal_connect(app, "add-browser", G_CALLBACK(mouse_gestures_browser_cb), NULL); // connect the above callback to the "add-browser" signal of app
g_signal_connect(extension, "deactivate", G_CALLBACK(mouse_gestures_deactivate), app); // connect the deactivate callback to the extension g_signal_connect (app, "add-browser",
return; G_CALLBACK (mouse_gestures_browser_cb), NULL);
g_signal_connect (extension, "deactivate",
G_CALLBACK (mouse_gestures_deactivate), app);
} }
MidoriExtension* extension_init (void) MidoriExtension* extension_init (void)
{ {
MidoriExtension* extension = NULL; MidoriExtension* extension;
extension = g_object_new (MIDORI_TYPE_EXTENSION, extension = g_object_new (MIDORI_TYPE_EXTENSION,
"name", _("Mouse Gestures"), "name", _("Mouse Gestures"),
"description", _("Control Midori by moving the mouse"), "description", _("Control Midori by moving the mouse"),
"version", MOUSE_GESTURES_VERSION, "version", MOUSE_GESTURES_VERSION,
"authors", "Matthias Kruk <mkruk@matthiaskruk.de>", NULL); "authors", "Matthias Kruk <mkruk@matthiaskruk.de>", NULL);
g_signal_connect(extension, "activate", G_CALLBACK(mouse_gestures_activate), NULL); // connect the callback that's executed on activation
return(extension); g_signal_connect (extension, "activate",
G_CALLBACK (mouse_gestures_activate), NULL);
return extension;
} }