Re: Prevent to exec second copy of application



On Sat, 2004-11-13 at 06:41, jakonda jakarta wrote:
Helo, I try to force my application to be only one on display,
I mean if somebody do

$./my-program
OK!
$./my-program
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 
is 
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);
  gtk_widget_show(topLevelWindow);
  
  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");
  gtk_selection_owner_set_for_display(gdk_display_get_default(),
                                                   topLevelWindow, atom, 0);
  

gdk_selection_owner_get_for_display does not give you the right answer
if the selection is owned by another client:

/**
 * gdk_selection_owner_get_for_display:
 * @display: a #GdkDisplay.
 * @selection: an atom indentifying a selection.
 *
 * Determine the owner of the given selection.
 *
 * Note that the return value may be owned by a different 
 * process if a foreign window was previously created for that
 * window, but a new foreign window will never be created by this call. 
 *
 * Returns: if there is a selection owner for this window, and it is a 
 *    window known to the current process, the #GdkWindow that owns the 
 *    selection, otherwise %NULL.
 *
 * Since: 2.2
 */ 

You have to use Xlib directly for this. What you are trying to do here
is basically what the ICCCM calls "Manager Selection", you may want to
read up on that: http://tronche.com/gui/x/icccm/sec-2.html#s-2.8

Matthias






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