paned.c 2.79 KB
Newer Older
Victor Ananjesky's avatar
Victor Ananjesky committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * This file is part of YAD.
 *
 * YAD 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 3 of the License, or
 * (at your option) any later version.
 *
 * YAD 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with YAD. If not, see <http://www.gnu.org/licenses/>.
 *
17
 * Copyright (C) 2008-2019, Victor Ananjevsky <ananasik@gmail.com>
Victor Ananjesky's avatar
Victor Ananjesky committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/ipc.h>
#include <sys/shm.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <gdk/gdk.h>

#include "yad.h"

static GtkWidget *paned;

GtkWidget *
paned_create_widget (GtkWidget * dlg)
{
  GtkWidget *w, *s;

  /* get shared memory */
  tabs = get_tabs (options.common_data.key, TRUE);
  if (!tabs)
    exit (-1);

  /* create widget */
Victor Ananjesky's avatar
Victor Ananjesky committed
49
  paned = w = gtk_paned_new (options.paned_data.orient);
Victor Ananjesky's avatar
Victor Ananjesky committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  gtk_widget_set_name (w, "yad-paned-widget");

  gtk_paned_set_position (GTK_PANED (w), options.paned_data.splitter);

  s = gtk_socket_new ();
  gtk_paned_add1 (GTK_PANED (w), s);
  g_object_set_data (G_OBJECT (w), "s1", s);

  s = gtk_socket_new ();
  gtk_paned_add2 (GTK_PANED (w), s);
  g_object_set_data (G_OBJECT (w), "s2", s);

  return w;
}

void
paned_swallow_childs (void)
{
  GtkWidget *s1, *s2;

  s1 = GTK_WIDGET (g_object_get_data (G_OBJECT (paned), "s1"));
  s2 = GTK_WIDGET (g_object_get_data (G_OBJECT (paned), "s2"));

73 74
  /* wait until all children are registered */
  while (!tabs[1].xid || !tabs[2].xid)
Victor Ananjesky's avatar
Victor Ananjesky committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    usleep (1000);

  if (tabs[1].pid != -1)
    gtk_socket_add_id (GTK_SOCKET (s1), tabs[1].xid);
  if (tabs[2].pid != -1)
    gtk_socket_add_id (GTK_SOCKET (s2), tabs[2].xid);
}

void
paned_print_result (void)
{
  if (tabs[1].pid != -1)
    kill (tabs[1].pid, SIGUSR1);
  if (tabs[2].pid != -1)
    kill (tabs[2].pid, SIGUSR1);
}

void
paned_close_childs (void)
{
  guint i;
  struct shmid_ds buf;
  gboolean is_running = TRUE;

  if (tabs[1].pid != -1)
    kill (tabs[1].pid, SIGUSR2);
  if (tabs[2].pid != -1)
    kill (tabs[2].pid, SIGUSR2);

  /* wait for stop subprocesses */
  while (is_running)
    {
      is_running = FALSE;
108
      for (i = 1; i < 3; i++)
Victor Ananjesky's avatar
Victor Ananjesky committed
109 110 111 112 113 114 115
        {
          if (tabs[i].pid != -1 && kill (tabs[i].pid, 0) == 0)
            {
              is_running = TRUE;
              break;
            }
        }
116 117
      if (is_running)
        usleep (1000);
Victor Ananjesky's avatar
Victor Ananjesky committed
118 119 120 121 122 123
    }

  /* cleanup shared memory */
  shmctl (tabs[0].pid, IPC_RMID, &buf);
  shmdt (tabs);
}