Prevent to exec second copy of application

Helo, I try to force my application to be only one on display,
I mean if somebody do

There is another copy. Use it

My algorithm is follow: create atom, check if it selected by somebody, if it so another copy of application 
running, if it not so we are alone, select atom and work.

Code is follows:
$ cat main.cpp
#include <gdk/gdk.h>

#include <gtk/gtk.h>
#include <cstdlib>
#include <cstring>

gint OnDelete(GtkWidget *widget, GdkEvent *event, gpointer data);
gint OnDestroy(GtkWidget *widget, GdkEvent *event, gpointer data);

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

  GtkWidget *topLevelWindow=gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_signal_connect(GTK_OBJECT(topLevelWindow), "delete_event", 
                     GTK_SIGNAL_FUNC(OnDelete), NULL);
  gtk_signal_connect(GTK_OBJECT(topLevelWindow), "destroy", 
                     GTK_SIGNAL_FUNC(OnDestroy), NULL);
  GdkAtom atom=gdk_atom_intern("MY_PRIVATE_ATOM_GTK_EXAMPLE", FALSE);
  if(gdk_selection_owner_get_for_display(gdk_display_get_default(), atom)!=NULL)
        g_print("There is another copy\n");
                                                   topLevelWindow, atom, 0);
  return EXIT_SUCCESS;

gint OnDelete(GtkWidget *widget, GdkEvent *event, gpointer data)
  return FALSE;

gint OnDestroy(GtkWidget *widget, GdkEvent *event, gpointer data)
  g_print("Shutting down\n");
  return 0;
but it didn't work, what wrong?
May be there is another algorithm to prevent starting second copy of application in the same dipslay?

With regards, Jan.

  I don't want to use bonobo.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]