📜 ⬆️ ⬇️

Little about libunique

Today I would like to talk about one of the ways to create an application that has one instance - the library LibUnique .

Why do you need it?

In my case, this was needed when writing a small calendar applet for the tint2 panel. I wanted the calendar to appear when I clicked on the clock, and when I clicked again, I would hide it, as in the gnome-panel. At first, I was hampered by the fact that tint2 did not have such functionality. But this is open source! An evening to read the code, an evening to write the patch ... Now in tint2 there is such a functionality.

Then I began to look for what could serve as such an applet, but did not find anything suitable. Orage was too heavy, zenity - calendar jumped out in the center of the screen, and it could not be closed by clicking again on the clock. In the end, I decided to write my little calendars: gsimplecal . Moreover, I wanted to try to write something under Linux.
')
On his example, and consider the use of LibUnique.

Let's write the code


To start the link to the full listing .

In fact, everything is very simple, and the essence of this post is rather to let know about the existence of such a solution. And with this knowledge you will need to complete only a few steps:
  1. To begin with, of course, we need to include the header file unique.h .
    #include <unique/unique.h>

  2. Then, somewhere at the very beginning of the main function, you need to call the function unique_app_new , passing it the application identifier in the form of a domain name, like “ru.habrahabr.singletone_app” .

    int main( int argc, char *argv[])
    {
    UniqueApp *app;
    app = unique_app_new( "org.dmedvinsky.gsimplecal" , NULL );


    If you want to use your own commands (from the predefined ones there is ACTIVATE , NEW , OPEN , CLOSE ), then you can call unique_app_new_with_commands immediately instead of unique_app_new (commands can be added separately by calling unique_app_add_command ).

  3. After that, call the function unique_app_is_running , and act depending on its result:
    • true : send an already existing instance of one of the predefined or custom messages.

      if (unique_app_is_running(app)) {
      unique_app_send_message(app, UNIQUE_CLOSE, NULL );
      }

    • false : the program is started for the first time; you need to create our window and attach a libunique message handler to it.

      else {
      create_main_window();
      unique_app_watch_window(app, GTK_WINDOW(main_window));
      g_signal_connect(app, "message-received" , G_CALLBACK(message_received_cb), NULL );
      }
  4. Actually, write the message handler itself.

    static UniqueResponse message_received_cb(UniqueApp *app, UniqueCommand command,
    UniqueMessageData *message, guint time_, gpointer user_data)
    {
    if (command == UNIQUE_CLOSE) {
    gtk_signal_emit_by_name(GTK_OBJECT(main_window), "destroy" );
    }

    return UNIQUE_RESPONSE_OK;
    }

  5. Do not forget to clean up for yourself.
    g_object_unref(app);

  6. ...
  7. Profit!

Source: https://habr.com/ru/post/88314/


All Articles