On Wed, 2007-04-25 at 23:23 +0200, [EMAIL PROTECTED] wrote:

> > How does that sound? I'll give you all the help you need to implement
> > this. 
>       Thanks, I will probably need some help with making button events.

My resident X hacker (& boss) has told me that faking these kind of
mouse events is pretty horrible and the resident GTK hacker says they'll
be filtered anyway. Instead we can just simulate the keyboard up/down
arrows.

Code is attached. Compile with: 

gcc -o scroll-magic `pkg-config --cflags --libs gtk+-2.0 libfakekey`
scroll-magic.c

Enjoy.

Cheers,

Rob
/* 
 *  Author: Rob Bradford <[EMAIL PROTECTED]>
 *
 *  Copyright (c) 2007 OpenedHand Ltd - http://o-hand.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 2, 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.
 *
 */

/* 
 * Compile with: 
 * gcc -o scroll-magic `pkg-config --cflags --libs gtk+-2.0 libfakekey` scroll-magic.c 
 *
 */

#include <gtk/gtk.h>
#include <X11/Xlib.h>
#include <gdk/gdkx.h>
#include <fakekey/fakekey.h>

static guint timer = 0;
static FakeKey *fk = NULL;

static void repeat_timeout_cb (gpointer user_data)
{
  fakekey_repeat (fk);
}

static void 
button_up_button_press_cb (GtkWidget *button, GdkEventButton *event, gpointer user_data)
{
  fakekey_press_keysym (fk, XK_Up, 0);
  timer = g_timeout_add (100, (GSourceFunc) repeat_timeout_cb, NULL);
}

static void 
button_down_button_press_cb (GtkWidget *button, GdkEventButton *event, gpointer user_data)
{
  fakekey_press_keysym (fk, XK_Down, 0);
  timer = g_timeout_add (100, (GSourceFunc) repeat_timeout_cb, NULL);
}

static void
button_release_cb (GtkWidget *button, GdkEventButton *event, gpointer user_data)
{
  g_source_remove (timer);
  fakekey_release (fk);
}

int 
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *button_up, *arrow_up;
  GtkWidget *button_down, *arrow_down;
  GtkWidget *frame;
  Display *xdisplay;

  gtk_init (&argc, &argv);

  xdisplay = gdk_x11_get_default_xdisplay ();
  fk = fakekey_init (xdisplay);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  vbox = gtk_vbox_new (FALSE, 6);

  arrow_up = gtk_arrow_new (GTK_ARROW_UP, GTK_SHADOW_NONE);
  gtk_widget_show (arrow_up);

  arrow_down = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
  gtk_widget_show (arrow_down);

  button_up = gtk_button_new ();
  gtk_widget_show (button_up);

  button_down = gtk_button_new ();
  gtk_widget_show (button_down);

  gtk_container_add (GTK_CONTAINER (button_up), arrow_up);
  gtk_container_add (GTK_CONTAINER (button_down), arrow_down);

  gtk_box_pack_start (GTK_BOX (vbox), button_up, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), button_down, TRUE, TRUE, 0);

  gtk_widget_show (vbox);

  frame = gtk_frame_new (NULL);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (frame);

  gtk_container_add (GTK_CONTAINER (window), frame);

  gtk_window_set_type_hint (GTK_WINDOW (window), 
      GDK_WINDOW_TYPE_HINT_TOOLBAR);

  gtk_window_set_accept_focus (GTK_WINDOW (window), FALSE);

  gtk_window_set_default_size (GTK_WINDOW (window), 40, 80);

  gtk_widget_show (window);
  
  g_signal_connect (button_up, "button-press-event", G_CALLBACK (button_up_button_press_cb), NULL);
  g_signal_connect (button_up, "button-release-event", G_CALLBACK (button_release_cb), NULL);

  g_signal_connect (button_down, "button-press-event", G_CALLBACK (button_down_button_press_cb), NULL);
  g_signal_connect (button_down, "button-release-event", G_CALLBACK (button_release_cb), NULL);

  gtk_main ();

  return 0;
}

Reply via email to