Archive

Archive for March, 2011

Single Instance Applications

March 18, 2011 Leave a comment

Hello! Here ‘s a fairly simple example of how to create single instance Applications with LibUnique and Gtk +. I took the example in the documentation and changed it to demonstrate how to pass a parameter to the active instance.

How to compile:

gcc -o uniquesample uniquesample.c 
  `pkg-config --cflags gtk+-2.0 unique-1.0 --libs gtk+-2.0 unique-1.0`

How to run it, first launch first instance:

# ./uniquesample

Then, launch the others:

# ./uniquesample "One App to rule them All"

Here’s the code:

#include gtk/gtk.h
#include unique/unique.h

static UniqueResponse 
message_received_cb (UniqueApp *app, 
                     UniqueCommand command, 
                     UniqueMessageData *message, 
                     guint time_, 
                     gpointer user_data)
{
  UniqueResponse res;

  /* get text payload from the unique message data*/
  gchar *texto = unique_message_data_get_text (message);
	
  /* Get the label widget */
  GtkLabel *label = GTK_LABEL (user_data);

  switch (command)
  {		
    case UNIQUE_ACTIVATE:
      {
        gtk_label_set_text (GTK_LABEL (label), texto);
        res = UNIQUE_RESPONSE_OK;
        break;
      }
    default:
      res = UNIQUE_RESPONSE_OK;
      break;
  }

  return res;
}

static gboolean 
delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
{
  gtk_main_quit ();
}

int
main (int argc, char *argv[])
{
  gtk_init (&argc, &argv);

  /* Create the UniqueApp Instance */
  UniqueApp *app = unique_app_new ("home.myapp", NULL);
	
  /* check if there already is an instance running */
  if (unique_app_is_running (app))
  {		
    if (argc > 1)
    {						
      /* build a message with the first command line argument */
      UniqueMessageData *message = unique_message_data_new();
      unique_message_data_set_text (message, argv[1], -1);
	
      /* send the message, we don't care about the response */
      unique_app_send_message (app, UNIQUE_ACTIVATE, message);

      unique_message_data_free (message);		
      g_object_unref (app);
    }
  }
  else
  {
    /* this is the first instance */
    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);	
    GtkWidget *label = gtk_label_new ("UNIQUE INSTANCE");

    /* Makes app "watch" a window. 
     * Every watched window will receive startup notification changes automatically
     */
    unique_app_watch_window (app, GTK_WINDOW (window));

    /* connect to the signal so we can handle commands and responses
     * We are passing the label widget here as user data */
    g_signal_connect (G_OBJECT (app), "message-received", 
                      G_CALLBACK (message_received_cb), label);

    g_signal_connect (G_OBJECT (window), "delete_event",
                      G_CALLBACK (delete_event_cb), NULL);

    gtk_widget_set_size_request (GTK_WIDGET (window), 300, 200);
		
    gtk_container_add (GTK_CONTAINER (window), label);	
    gtk_widget_show_all(window);

    gtk_main ();

    g_object_unref (app);
  }

  return 0;
}
Categories: Gtk+, linux

Gtk+ 3.0 Client Side Theme

March 12, 2011 3 comments

How to override user’s theme or I want my app with green buttons.

#include 

static gboolean
delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
{
	gtk_main_quit();
}

int
main (int argc, char* argv[])
{
  gtk_init(&argc, &argv);

  GtkCssProvider *provider = gtk_css_provider_new ();

  gtk_css_provider_load_from_data (
    provider, 
    "GtkButton {font: Monospace 10; background-color: rgba(0%, 76%, 0%, 0.6);}", 
    -1, NULL);

  GdkDisplay *display = gdk_display_get_default ();
  GdkScreen *screen = gdk_display_get_default_screen (display);

  gtk_style_context_add_provider_for_screen (
    screen, GTK_STYLE_PROVIDER (provider),
    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  
  g_object_unref (provider);

  GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL);

  GtkWidget *button = gtk_button_new_with_label("GtkCssProvider load from data");
  gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (button));
  gtk_widget_show_all (GTK_WIDGET (window));

  gtk_main ();	
  return 0;
}
Categories: Gtk+

ogg to mp3

March 10, 2011 Leave a comment

If you have a device in your home that does not reproduce ogg files. :(

#!/bin/sh

find . -name '* *' | sort | while read FILE
do
	NEWFILE=`echo ${FILE} | sed 's/ /_/g;'`
	mv "${FILE}" "${NEWFILE}"
done

for file in *.ogg
do
	ogg123 -d wav -f $file.wav $file 
	lame -m s -V 0 $file.wav
	rm $file
	rm $file.wav
done

find . -name '*ogg.wav.mp3*' | sort | while read FILE
do
	NEWFILE=`echo ${FILE} | sed 's/ogg.wav.mp3/mp3/g;'`
	mv "${FILE}" "${NEWFILE}"
done
Categories: Uncategorized

wma to mp3 shell script

March 10, 2011 Leave a comment

This shell script will convert wma files to variable rate mp3.

#!/bin/sh

find . -name '* *' | sort | while read FILE
do
    NEWFILE=`echo ${FILE} | sed 's/ /_/g;'`
    mv "${FILE}" "${NEWFILE}"
done

for file in *.wma
do
    mplayer $file -ao pcm
    mv audiodump.wav $file.wav
    lame -m s -V 0 $file.wav
    rm $file.wav
    rm $file
done

find . -name '*wma.wav.mp3*' | sort | while read FILE
do
    NEWFILE=`echo ${FILE} | sed 's/wma.wav.mp3/mp3/g;'`
    mv "${FILE}" "${NEWFILE}"
done
Categories: linux, shell script
Follow

Get every new post delivered to your Inbox.