xfsm-startup.c 33 KB
Newer Older
1 2
/* $Id$ */
/*-
3
 * Copyright (c) 2003-2006 Benedikt Meurer <benny@xfce.org>
4
 * Copyright (c) 2008 Brian Tarricone <bjt23@cornell.edu>
5 6
 * All rights reserved.
 *
7 8 9 10
 * This program 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 2, or (at your option)
 * any later version.
Jérôme Guelfucci's avatar
Jérôme Guelfucci committed
11
 *
12 13 14 15
 * This program 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.
Jérôme Guelfucci's avatar
Jérôme Guelfucci committed
16
 *
17 18
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Jérôme Guelfucci's avatar
Jérôme Guelfucci committed
19 20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
21 22 23 24 25 26
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

27 28 29
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
30 31 32
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
33
#ifdef HAVE_STDIO_H
34
#include <stdio.h>
35
#endif
36 37 38 39 40 41 42 43 44 45 46 47
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
48 49 50 51 52 53
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
54

55
#include <glib/gstdio.h>
56
#include <gdk/gdkx.h>
57
#include <libxfce4ui/libxfce4ui.h>
58

59 60
#include <libxfsm/xfsm-util.h>

61 62
#include <xfce4-session/xfsm-compat-gnome.h>
#include <xfce4-session/xfsm-compat-kde.h>
63 64 65 66
#include <xfce4-session/xfsm-global.h>
#include <xfce4-session/xfsm-manager.h>
#include <xfce4-session/xfsm-splash-screen.h>

67
#include <xfce4-session/xfsm-startup.h>
68

69

70 71 72 73
typedef struct
{
  XfsmManager    *manager;
  XfsmProperties *properties;
74
} XfsmStartupData;
75 76 77 78 79

static void     xfsm_startup_failsafe                (XfsmManager *manager);

static gboolean xfsm_startup_session_next_prio_group (XfsmManager *manager);

80
static void     xfsm_startup_data_free               (XfsmStartupData *sdata);
81 82 83
static void     xfsm_startup_child_watch             (GPid         pid,
                                                      gint         status,
                                                      gpointer     user_data);
84 85
static gboolean xfsm_startup_timeout                 (gpointer     data);

86
static void     xfsm_startup_handle_failed_startup   (XfsmProperties *properties,
87
                                                      XfsmManager    *manager);
88

89 90 91 92 93 94 95 96 97

static gchar *running_sshagent = NULL;



static void
xfsm_startup_init_sshagent (const gchar *cmd,
                            const gchar *agent)
{
Nick Schermer's avatar
Nick Schermer committed
98
  gchar     *cmdoutput = NULL;
99 100 101 102 103 104
  GError    *error = NULL;
  gchar    **lines;
  guint      i;
  gchar     *p, *t;
  gchar     *variable, *value;

Nick Schermer's avatar
Nick Schermer committed
105
  if (g_spawn_command_line_sync (cmd, &cmdoutput, NULL, NULL, &error))
106
    {
Nick Schermer's avatar
Nick Schermer committed
107
      if (G_UNLIKELY (cmdoutput == NULL))
108 109 110 111 112
        {
          g_message ("%s returned no variables to stdout", agent);
          return;
        }

Nick Schermer's avatar
Nick Schermer committed
113
      lines = g_strsplit (cmdoutput, "\n", -1);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      g_assert (lines != NULL);
      for (i = 0; lines[i] != NULL; i++)
        {
          p = strchr (lines[i], '=');
          if (G_UNLIKELY (p == NULL))
            continue;
          t = strchr (p + 1, ';');
          if (G_UNLIKELY (t == NULL))
            continue;

          variable = g_strndup (lines[i], p - lines[i]);
          value = g_strndup (p + 1, t - p - 1);

          g_setenv (variable, value, TRUE);

          g_free (variable);
          g_free (value);
        }
      g_strfreev (lines);

      /* keep this around for shutdown */
      running_sshagent = g_strdup (agent);
    }
  else
    {
      g_warning ("Failed to spawn %s: %s", agent, error->message);
      g_error_free (error);
    }

Nick Schermer's avatar
Nick Schermer committed
143
  g_free (cmdoutput);
144 145 146
}


147
void
148
xfsm_startup_init (XfconfChannel *channel)
149
{
150 151 152 153 154 155
  gchar       *agent;
  gchar       *path = NULL;
  gchar       *envfile;
  gchar       *cmd;
  const gchar *ssh_agent_pid;
  pid_t        pid;
156
  gboolean     gnome_keyring_found;
157 158 159

  if (xfconf_channel_get_bool (channel, "/startup/ssh-agent/enabled", TRUE))
    {
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
      /* if GNOME compatibility is enabled and gnome-keyring-daemon
       * is found, skip the gpg/ssh agent startup and wait for
       * gnome-keyring, which is probably what the user wants */
      if (xfconf_channel_get_bool (channel, "/compat/LaunchGNOME", FALSE))
        {
          cmd = g_find_program_in_path ("gnome-keyring-daemon");
          gnome_keyring_found = (cmd != NULL);
          g_free (cmd);

          if (gnome_keyring_found)
            {
              g_print ("xfce4-session: %s\n",
                       "GNOME compatibility is enabled and gnome-keyring-daemon is "
                       "found on the system. Skipping gpg/ssh-agent startup.");
              return;
            }
        }

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      agent = xfconf_channel_get_string (channel, "/startup/ssh-agent/type", NULL);
      if (g_strcmp0 (agent, "gpg-agent") == 0
          || g_strcmp0 (agent, "ssh-agent") == 0)
        {
          path = g_find_program_in_path (agent);
        }
      else if (agent == NULL)
        {
          /* lookup gpg- or ssh-agent */
          path = g_find_program_in_path ("gpg-agent");
          if (G_UNLIKELY (path != NULL))
            {
              agent = g_strdup ("gpg-agent");
            }
          else
            {
              path = g_find_program_in_path ("ssh-agent");
              if (path != NULL)
                agent = g_strdup ("ssh-agent");
            }
        }
      else
        {
          g_message ("Unknown authentication agent \"%s\" set", agent);

          /* avoid more errors */
          g_free (agent);
          return;
        }

      if (G_LIKELY (path != NULL))
        {
          ssh_agent_pid = g_getenv ("SSH_AGENT_PID");
          if (ssh_agent_pid != NULL && *ssh_agent_pid == '\0')
            ssh_agent_pid = NULL;

          if (ssh_agent_pid != NULL)
            {
              /* check if the pid is still responding (ie not stale) */
              pid = atoi (ssh_agent_pid);
              if (pid > 0 && kill (pid, 0) != 0)
                {
                  g_unsetenv ("SSH_AGENT_PID");
                  g_unsetenv ("SSH_AUTH_SOCK");

                  ssh_agent_pid = NULL;
                }
            }

          if (g_strcmp0 (agent, "gpg-agent") == 0)
            {
229
              envfile = xfce_resource_save_location (XFCE_RESOURCE_CACHE, "gpg-agent-info", FALSE);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

              if (ssh_agent_pid == NULL)
                {
                  cmd = g_strdup_printf ("%s --sh --daemon --enable-ssh-support "
                                         "--write-env-file '%s'", path, envfile);
                  xfsm_startup_init_sshagent (cmd, agent);
                  g_free (cmd);
                }
              else if (g_getenv ("GPG_AGENT_INFO") == NULL)
                {
                  g_message ("ssh-agent is already running; starting gpg-agent without ssh support");
                  cmd = g_strdup_printf ("%s --sh --daemon --write-env-file '%s'", path, envfile);
                  xfsm_startup_init_sshagent (cmd, agent);
                  g_free (cmd);
                }
              else
                {
                  g_message ("%s is already running", agent);
                }

              g_free (envfile);
            }
          else if (g_strcmp0 (agent, "ssh-agent") == 0)
            {
              if (ssh_agent_pid == NULL)
                {
                  cmd = g_strdup_printf ("%s -s", path);
                  xfsm_startup_init_sshagent (cmd, agent);
                  g_free (cmd);
                }
              else
                {
                  g_message ("%s is already running", agent);
                }
            }
        }
      else
        {
          g_printerr ("xfce4-session: %s\n",
                      "No gpg or ssh authentication agent found");
        }

      g_free (agent);
      g_free (path);
    }
}


void
xfsm_startup_shutdown (void)
{
  gchar       *envfile;
  const gchar *agentpid;
  pid_t        pid;
  gboolean     is_gpg_agent;

  if (running_sshagent == NULL)
    return;

  agentpid = g_getenv ("SSH_AGENT_PID");
  if (G_UNLIKELY (agentpid == NULL))
    {
      g_warning ("%s was started, but SSH_AGENT_PID is not set, nothing to kill", running_sshagent);
    }
  else
    {
      is_gpg_agent = g_strcmp0 (running_sshagent, "gpg-agent") == 0;

      /* kill the process (gpg-agent uses SIGINT, ssh-agent SIGTERM) */
      pid = atoi (agentpid);
      if (pid < 1
          || kill (pid, is_gpg_agent ? SIGINT : SIGTERM) != 0)
        {
          g_warning ("Failed to kill %s with pid %s", running_sshagent, agentpid);
        }

      /* drop the info file from gpg-agent */
      if (is_gpg_agent)
        {
          envfile = xfce_resource_lookup (XFCE_RESOURCE_CACHE, "gpg-agent-info");
          if (G_LIKELY (envfile != NULL))
            g_unlink (envfile);
          g_free (envfile);
        }
    }

  /* make sure the env values are unset */
  g_unsetenv ("SSH_AGENT_PID");
  g_unsetenv ("SSH_AUTH_SOCK");

  g_free (running_sshagent);
321 322 323 324 325 326
}


static gboolean
destroy_splash (gpointer user_data)
{
327 328 329 330 331
  if (G_LIKELY (splash_screen != NULL))
    {
      xfsm_splash_screen_free (splash_screen);
      splash_screen = NULL;
    }
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

  return FALSE;
}


static const gchar*
figure_app_name (const gchar *program_path)
{
  static char progbuf[256];
  gchar *prog;

  prog = g_path_get_basename (program_path);

  /* Xfce applications */
  if (strcmp (prog, "xfce4-mixer") == 0)
    return _("Starting the Volume Controller");
  else if (strcmp (prog, "xfce4-panel") == 0)
    return _("Starting the Panel");
  else if (strcmp (prog, "xfdesktop") == 0)
    return _("Starting the Desktop Manager");
352
  else if (strcmp (prog, "xftaskbar4") == 0)
353 354 355 356 357 358 359 360
    return _("Starting the Taskbar");
  else if (strcmp (prog, "xfwm4") == 0)
    return _("Starting the Window Manager");

  /* Gnome applications */
  if (strcmp (prog, "gnome-terminal") == 0)
    return _("Starting the Gnome Terminal Emulator");

361 362 363 364 365 366 367 368 369 370 371 372 373 374
  /* KDE applications */
  if (strcmp (prog, "kate") == 0)
    return _("Starting the KDE Advanced Text Editor");
  else if (strcmp (prog, "klipper") == 0)
    return _("Starting the KDE Clipboard Manager");
  else if (strcmp (prog, "kmail") == 0)
    return _("Starting the KDE Mail Reader");
  else if (strcmp (prog, "knews") == 0)
    return _("Starting the KDE News Reader");
  else if (strcmp (prog, "konqueror") == 0)
    return _("Starting the Konqueror");
  else if (strcmp (prog, "konsole") == 0)
    return _("Starting the KDE Terminal Emulator");

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  /* 3rd party applications */
  if (strcmp (prog, "beep-media-player") == 0)
    return _("Starting the Beep Media Player");
  else if (strncmp (prog, "gimp", 4) == 0)
    return _("Starting The Gimp");
  else if (strcmp (prog, "gvim") == 0)
    return _("Starting the VI Improved Editor");
  else if (strcmp (prog, "smproxy") == 0)
    return _("Starting the Session Management Proxy");
  else if (strcmp (prog, "xchat") == 0 || strcmp (prog, "xchat2") == 0)
    return _("Starting the X-Chat IRC Client");
  else if (strcmp (prog, "xmms") == 0)
    return _("Starting the X Multimedia System");
  else if (strcmp (prog, "xterm") == 0)
    return _("Starting the X Terminal Emulator");

  g_snprintf (progbuf, 256, _("Starting %s"), prog);

  return progbuf;
}


397 398 399

static gboolean
xfsm_check_valid_exec (const gchar *exec)
Benedikt Meurer's avatar
Benedikt Meurer committed
400
{
401 402 403 404 405
  gboolean result = TRUE;
  gchar   *tmp;
  gchar   *p;

  if (*exec == '/')
Benedikt Meurer's avatar
Benedikt Meurer committed
406
    {
407 408 409 410 411 412 413 414
      result = (access (exec, X_OK) == 0);
    }
  else
    {
      tmp = g_strdup (exec);
      p = strchr (tmp, ' ');
      if (G_UNLIKELY (p != NULL))
        *p = '\0';
Benedikt Meurer's avatar
Benedikt Meurer committed
415

416 417 418 419 420 421 422 423
      p = g_find_program_in_path (tmp);
      g_free (tmp);

      if (G_UNLIKELY (p == NULL))
        {
          result = FALSE;
        }
      else
Benedikt Meurer's avatar
Benedikt Meurer committed
424
        {
425 426 427 428 429 430 431 432
          result = (access (p, X_OK) == 0);
          g_free (p);
        }
    }

  return result;
}

Benedikt Meurer's avatar
Benedikt Meurer committed
433

434

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
static void
xfsm_startup_autostart_migrate (void)
{
  const gchar *entry;
  gchar        source_path[4096];
  gchar        target_path[4096];
  gchar       *source;
  gchar       *target;
  FILE        *fp;
  GDir        *dp;

  /* migrate the content */
  source = xfce_get_homefile ("Desktop", "Autostart/", NULL);
  dp = g_dir_open (source, 0, NULL);
  if (G_UNLIKELY (dp != NULL))
    {
      /* check if the LOCATION-CHANGED.txt file exists and the target can be opened */
      g_snprintf (source_path, 4096, "%s/LOCATION-CHANGED.txt", source);
      target = xfce_resource_save_location (XFCE_RESOURCE_CONFIG, "autostart/", TRUE);
      if (G_LIKELY (target != NULL && !g_file_test (source_path, G_FILE_TEST_IS_REGULAR)))
        {
          g_message ("Trying to migrate autostart items from %s to %s...", source, target);
Benedikt Meurer's avatar
Benedikt Meurer committed
457

458
          for (;;)
Benedikt Meurer's avatar
Benedikt Meurer committed
459
            {
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
              entry = g_dir_read_name (dp);
              if (entry == NULL)
                break;

              /* determine full source and dest paths */
              g_snprintf (source_path, 4096, "%s%s", source, entry);
              g_snprintf (target_path, 4096, "%s%s", target, entry);

              /* try to move the file */
              if (rename (source_path, target_path) < 0)
                {
                  g_warning ("Failed to rename %s to %s: %s",
                              source_path, target_path,
                              g_strerror (errno));
                  continue;
                }

              /* check if the file is executable */
              if (!g_file_test (target_path, G_FILE_TEST_IS_EXECUTABLE))
                continue;

              /* generate a .desktop file for the executable file */
              g_snprintf (source_path, 4096, "%s.desktop", target_path);
              if (!g_file_test (source_path, G_FILE_TEST_IS_REGULAR))
                {
                  fp = fopen (source_path, "w");
                  if (G_LIKELY (fp != NULL))
                    {
                      fprintf (fp,
                               "# This file was automatically generated for the autostart\n"
                               "# item %s\n"
                               "[Desktop Entry]\n"
                               "Type=Application\n"
                               "Exec=%s\n"
                               "Hidden=False\n"
                               "Terminal=False\n"
                               "StartupNotify=False\n"
                               "Version=0.9.4\n"
                               "Name=%s\n",
                               entry, target_path, entry);
                      fclose (fp);
                    }
                  else
                    {
                      g_warning ("Failed to create a .desktop file for %s: %s",
                                 target_path, g_strerror (errno));
                    }
                }
            }
509

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
          /* create the LOCATION-CHANGED.txt file to let the user know */
          g_snprintf (source_path, 4096, "%s/LOCATION-CHANGED.txt", source);
          fp = fopen (source_path, "w");
          if (G_LIKELY (fp != NULL))
            {
              g_fprintf (fp, _("The location and the format of the autostart directory has changed.\n"
                               "The new location is\n"
                               "\n"
                               "  %s\n"
                               "\n"
                               "where you can place .desktop files to, that describe the applications\n"
                               "to start when you login to your Xfce desktop. The files in your old\n"
                               "autostart directory have been successfully migrated to the new\n"
                               "location.\n"
                               "You should delete this directory now.\n"), target);
              fclose (fp);
526
            }
527 528 529 530 531 532 533 534 535 536 537

          g_free (target);
        }

      g_dir_close (dp);
    }
}



static gint
538 539
xfsm_startup_autostart_xdg (XfsmManager *manager,
                            gboolean     start_at_spi)
540 541 542 543 544 545 546 547 548 549 550 551 552 553
{
  const gchar *try_exec;
  const gchar *type;
  const gchar *exec;
  gboolean     startup_notify;
  gboolean     terminal;
  gboolean     skip;
  GError      *error = NULL;
  XfceRc      *rc;
  gchar      **files;
  gchar      **only_show_in;
  gchar      **not_show_in;
  gint         started = 0;
  gint         n, m;
554 555
  gchar       *filename;
  const gchar *pattern;
556 557 558 559

  /* migrate the old autostart location (if still present) */
  xfsm_startup_autostart_migrate ();

560 561 562 563 564 565 566
  /* pattern for only at-spi desktop files or everything */
  if (start_at_spi)
    pattern = "autostart/at-spi-*.desktop";
  else
    pattern = "autostart/*.desktop";

  files = xfce_resource_match (XFCE_RESOURCE_CONFIG, pattern, TRUE);
567 568 569 570 571 572 573 574 575 576 577 578
  for (n = 0; files[n] != NULL; ++n)
    {
      rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, files[n], TRUE);
      if (G_UNLIKELY (rc == NULL))
        continue;

      xfce_rc_set_group (rc, "Desktop Entry");

      /* check the Hidden key */
      skip = xfce_rc_read_bool_entry (rc, "Hidden", FALSE);
      if (G_LIKELY (!skip))
        {
579
          if (xfce_rc_read_bool_entry (rc, "X-XFCE-Autostart-Override", FALSE))
580
            {
581 582 583 584 585 586 587 588
              /* override the OnlyShowIn check */
              skip = FALSE;
            }
          else
            {
              /* check the OnlyShowIn setting */
              only_show_in = xfce_rc_read_list_entry (rc, "OnlyShowIn", ";");
              if (G_UNLIKELY (only_show_in != NULL))
589
                {
590 591
                  /* check if "XFCE" is specified */
                  for (m = 0, skip = TRUE; only_show_in[m] != NULL; ++m)
592
                    {
593 594 595 596 597
                      if (g_ascii_strcasecmp (only_show_in[m], "XFCE") == 0)
                        {
                          skip = FALSE;
                          break;
                        }
598
                    }
599

600 601
                  g_strfreev (only_show_in);
                }
Benedikt Meurer's avatar
Benedikt Meurer committed
602
            }
603 604 605 606

          /* check the NotShowIn setting */
          not_show_in = xfce_rc_read_list_entry (rc, "NotShowIn", ";");
          if (G_UNLIKELY (not_show_in != NULL))
607
            {
608 609 610 611 612 613 614 615 616
              /* check if "Xfce" is not specified */
              for (m = 0; not_show_in[m] != NULL; ++m)
                if (g_ascii_strcasecmp (not_show_in[m], "XFCE") == 0)
                  {
                    skip = TRUE;
                    break;
                  }

              g_strfreev (not_show_in);
617
            }
618 619 620 621 622 623 624 625

          /* skip at-spi launchers if not in at-spi mode or don't skip
           * them no matter what the OnlyShowIn key says if only
           * launching at-spi */
          filename = g_path_get_basename (files[n]);
          if (g_str_has_prefix (filename, "at-spi-"))
            skip = !start_at_spi;
          g_free (filename);
626 627 628 629 630 631 632 633 634 635 636
        }

      /* check the "Type" key */
      type = xfce_rc_read_entry (rc, "Type", NULL);
      if (G_UNLIKELY (!skip && type != NULL && g_ascii_strcasecmp (type, "Application") != 0))
        skip = TRUE;

      /* check the "TryExec" key */
      try_exec = xfce_rc_read_entry (rc, "TryExec", NULL);
      if (G_UNLIKELY (!skip && try_exec != NULL))
        skip = !xfsm_check_valid_exec (try_exec);
Benedikt Meurer's avatar
Benedikt Meurer committed
637

638 639 640 641 642 643 644 645 646
      /* execute the item */
      exec = xfce_rc_read_entry (rc, "Exec", NULL);
      if (G_LIKELY (!skip && exec != NULL))
        {
          /* query launch parameters */
          startup_notify = xfce_rc_read_bool_entry (rc, "StartupNotify", FALSE);
          terminal = xfce_rc_read_bool_entry (rc, "Terminal", FALSE);

          /* try to launch the command */
647
          xfsm_verbose ("Autostart: running command \"%s\"\n", exec);
648 649 650 651 652
          if (!xfce_spawn_command_line_on_screen (gdk_screen_get_default (),
                                                  exec,
                                                  terminal,
                                                  startup_notify,
                                                  &error))
653
            {
654 655 656
              g_warning ("Unable to launch \"%s\" (specified by %s): %s", exec, files[n], error->message);
              g_error_free (error);
              error = NULL;
657 658 659
            }
          else
            {
660
              ++started;
661
            }
Benedikt Meurer's avatar
Benedikt Meurer committed
662 663
        }

664 665
      /* cleanup */
      xfce_rc_close (rc);
Benedikt Meurer's avatar
Benedikt Meurer committed
666
    }
667 668 669 670 671 672 673 674
  g_strfreev (files);

  return started;
}



static void
675
xfsm_startup_autostart (XfsmManager *manager)
676 677 678
{
  gint n;

679
  n = xfsm_startup_autostart_xdg (manager, FALSE);
Benedikt Meurer's avatar
Benedikt Meurer committed
680 681

  if (n > 0)
682 683 684
    {
      if (G_LIKELY (splash_screen != NULL))
        xfsm_splash_screen_next (splash_screen, _("Performing Autostart..."));
Benedikt Meurer's avatar
Benedikt Meurer committed
685

686 687 688 689 690 691
      g_timeout_add (2000, destroy_splash, NULL);
    }
  else
    {
      g_timeout_add (1000, destroy_splash, NULL);
    }
Benedikt Meurer's avatar
Benedikt Meurer committed
692 693 694
}


695

696
void
697
xfsm_startup_foreign (XfsmManager *manager)
698
{
699
  if (xfsm_manager_get_compat_startup(manager, XFSM_MANAGER_COMPAT_KDE))
700
    xfsm_compat_kde_startup (splash_screen);
701

702
  if (xfsm_manager_get_compat_startup(manager, XFSM_MANAGER_COMPAT_GNOME))
703
    xfsm_compat_gnome_startup (splash_screen);
704 705 706
}


707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
static void
xfsm_startup_at_set_gtk_modules (void)
{
  const gchar  *old;
  gchar       **modules;
  guint         i;
  gboolean      found_gail = FALSE;
  gboolean      found_atk_bridge = FALSE;
  GString      *new;

  old = g_getenv ("GTK_MODULES");
  if (old != NULL && *old != '\0')
    {
      /* check which modules are already loaded */
      modules = g_strsplit (old, ":", -1);
      for (i = 0; modules[i] != NULL; i++)
        {
          if (strcmp (modules[i], "gail") == 0)
            found_gail = TRUE;
          else if (strcmp (modules[i], "atk-bridge") == 0)
            found_atk_bridge = TRUE;
        }
      g_strfreev (modules);

      if (!found_gail || !found_atk_bridge)
        {
          /* append modules to old value */
          new = g_string_new (old);
          if (!found_gail)
            new = g_string_append (new, ":gail");
          if (!found_atk_bridge)
            new = g_string_append (new, ":atk-bridge");

          g_setenv ("GTK_MODULES", new->str, TRUE);
          g_string_free (new, TRUE);
        }
    }
  else
    {
      g_setenv ("GTK_MODULES", "gail:atk-bridge", TRUE);
    }
}


static gboolean
xfsm_startup_at_spi_ior_set (void)
{
  Atom        AT_SPI_IOR;
  GdkDisplay *display;
  Atom        actual_type;
  gint        actual_format;
  guchar    *data = NULL;
  gulong     nitems;
  gulong     leftover;

  display = gdk_display_get_default ();
  AT_SPI_IOR = XInternAtom (GDK_DISPLAY_XDISPLAY (display), "AT_SPI_IOR", False);
  XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
                      XDefaultRootWindow (GDK_DISPLAY_XDISPLAY (display)),
                      AT_SPI_IOR, 0L,
                      (long) BUFSIZ, False,
                      (Atom) 31, &actual_type, &actual_format,
                      &nitems, &leftover, &data);

  if (data == NULL)
    return FALSE;
  XFree (data);

  return TRUE;
}


static void
xfsm_startup_at (XfsmManager *manager)
{
  gint n, i;

  /* start at-spi-dbus-bus and/or at-spi-registryd */
  n = xfsm_startup_autostart_xdg (manager, TRUE);

  if (n > 0)
    {
      if (G_LIKELY (splash_screen != NULL))
        xfsm_splash_screen_next (splash_screen, _("Starting Assistive Technologies"));

      xfsm_startup_at_set_gtk_modules ();

      /* wait for 2 seconds until the at-spi registered, not very nice
       * but required to properly start an accessible desktop */
      for (i = 0; i < 10; i++)
        {
          if (xfsm_startup_at_spi_ior_set ())
            break;
          xfsm_verbose ("Waiting for at-spi to register...\n");
          g_usleep (G_USEC_PER_SEC / 5);
        }
    }
  else
    {
      g_warning ("No assistive technology service provider was started!");
    }
}


811
void
812
xfsm_startup_begin (XfsmManager *manager)
813
{
814 815 816 817
  /* start assistive technology before anything else */
  if (xfsm_manager_get_start_at (manager))
    xfsm_startup_at (manager);

818
  if (xfsm_manager_get_use_failsafe_mode (manager))
819
    {
820
      xfsm_startup_failsafe (manager);
821
      xfsm_startup_autostart (manager);
822
      xfsm_manager_signal_startup_done (manager);
823
    }
824
  else
825
    {
826
      xfsm_startup_session_continue (manager);
827
    }
828 829 830
}


831
static void
832
xfsm_startup_failsafe (XfsmManager *manager)
833
{
834 835
  GQueue *failsafe_clients = xfsm_manager_get_queue (manager, XFSM_MANAGER_QUEUE_FAILSAFE_CLIENTS);
  FailsafeClient *fclient;
836

837
  while ((fclient = g_queue_pop_head (failsafe_clients)))
838
    {
839
      /* FIXME: splash */
840
      /* let the user know whats going on */
841 842 843 844 845
      if (G_LIKELY (splash_screen != NULL))
        {
          xfsm_splash_screen_next (splash_screen,
                                   figure_app_name (fclient->command[0]));
        }
846 847 848 849

      /* start the application */
      xfsm_start_application (fclient->command, NULL, fclient->screen,
                              NULL, NULL, NULL);
850
      xfsm_failsafe_client_free (fclient);
851 852 853 854
    }
}


855 856 857
gboolean
xfsm_startup_start_properties (XfsmProperties *properties,
                               XfsmManager    *manager)
858
{
859 860
  XfsmStartupData *child_watch_data;
  XfsmStartupData *startup_timeout_data;
861
  gchar          **restart_command;
862 863 864
  gchar          **argv;
  gint             argc;
  gint             n;
865
  const gchar     *current_directory;
866
  GPid             pid;
867
  GError          *error = NULL;
868 869 870

  /* release any possible old resources related to a previous startup */
  xfsm_properties_set_default_child_watch (properties);
871 872

  /* generate the argument vector for the application (expanding variables) */
873 874
  restart_command = xfsm_properties_get_strv (properties, SmRestartCommand);
  argc = g_strv_length (restart_command);
875 876
  argv = g_new (gchar *, argc + 1);
  for (n = 0; n < argc; ++n)
877
    argv[n] = xfce_expand_variables (restart_command[n], NULL);
878 879
  argv[n] = NULL;

880 881
  current_directory = xfsm_properties_get_string (properties, SmCurrentDirectory);

882 883 884 885 886
  if (!g_spawn_async (current_directory,
                      argv, NULL,
                      G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
                      NULL, NULL,
                      &pid, &error))
887
    {
888 889 890 891
      g_warning ("Unable to launch \"%s\": %s",
                 *argv, error->message);
      g_error_free (error);
      g_strfreev (argv);
892

893
      return FALSE;
894 895
    }

896 897 898 899
  xfsm_verbose ("Launched command \"%s\" with PID %dn", *argv, (gint) pid);

  g_strfreev (argv);

900 901
  properties->pid = pid;

902
  /* set a watch to make sure the child doesn't quit before registering */
903
  child_watch_data = g_new0 (XfsmStartupData, 1);
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
  child_watch_data->manager = g_object_ref (manager);
  child_watch_data->properties = properties;
  g_child_watch_add_full (G_PRIORITY_LOW, properties->pid,
                          xfsm_startup_child_watch, child_watch_data,
                          (GDestroyNotify) xfsm_startup_data_free);

  /* set a timeout -- client must register in a a certain amount of time
   * or it's assumed to be broken/have issues. */
  startup_timeout_data = g_new (XfsmStartupData, 1);
  startup_timeout_data->manager = g_object_ref (manager);
  startup_timeout_data->properties = properties;
  properties->startup_timeout_id = g_timeout_add_full (G_PRIORITY_DEFAULT,
                                                       STARTUP_TIMEOUT,
                                                       xfsm_startup_timeout,
                                                       startup_timeout_data,
                                                       (GDestroyNotify) xfsm_startup_data_free);

  return TRUE;
922 923 924
}


925
void
926
xfsm_startup_session_continue (XfsmManager *manager)
927
{
928
  GQueue  *pending_properties = xfsm_manager_get_queue (manager, XFSM_MANAGER_QUEUE_PENDING_PROPS);
929 930 931 932 933 934
  gboolean client_started = FALSE;

  /* try to start some clients.  if we fail to start anything in the current
   * priority group, move right to the next one.  if we *did* start something,
   * the failed/registered handlers will take care of moving us on to the
   * next priority group */
935 936
  while (client_started == FALSE && g_queue_peek_head (pending_properties) != NULL)
    client_started = xfsm_startup_session_next_prio_group (manager);
937

938
  if (G_UNLIKELY (client_started == FALSE && g_queue_peek_head (pending_properties) == NULL))
939 940 941 942
    {
      /* we failed to start anything, and we don't have anything else,
       * to start, so just move on to the autostart items and signal
       * the manager that we're finished */
943
      xfsm_startup_autostart (manager);
944
      xfsm_manager_signal_startup_done (manager);
945 946 947 948 949
    }
}


/* returns TRUE if we started anything, FALSE if we didn't */
950
static gboolean
951
xfsm_startup_session_next_prio_group (XfsmManager *manager)
952
{
953 954
  GQueue         *pending_properties = xfsm_manager_get_queue (manager, XFSM_MANAGER_QUEUE_PENDING_PROPS);
  GQueue         *starting_properties = xfsm_manager_get_queue (manager, XFSM_MANAGER_QUEUE_STARTING_PROPS);
955
  XfsmProperties *properties;
956 957 958
  gint            cur_prio_group;
  gboolean        client_started = FALSE;

959 960
  properties = (XfsmProperties *) g_queue_peek_head (pending_properties);
  if (properties == NULL)
961 962
    return FALSE;

963
  cur_prio_group = xfsm_properties_get_uchar (properties, GsmPriority, 50);
964 965 966

  xfsm_verbose ("Starting apps in prio group %d\n", cur_prio_group);

967
  while ((properties = g_queue_pop_head (pending_properties)))
968
    {
969
      /* quit if we've hit all the clients in the current prio group */
970
      if (xfsm_properties_get_uchar (properties, GsmPriority, 50) != cur_prio_group)
971
        {
972
          /* we're not starting this one yet; put it back */
973 974 975
          g_queue_push_head (pending_properties, properties);
          break;
        }
976 977

      /* FIXME: splash */
978 979
      if (G_LIKELY (splash_screen != NULL))
        {
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
          const gchar *app_name = NULL;
          const gchar *desktop_file;
          XfceRc      *rcfile = NULL;

          desktop_file = xfsm_properties_get_string (properties, GsmDesktopFile);

          if (desktop_file)
            {
              rcfile = xfce_rc_simple_open (desktop_file, TRUE);
              if (rcfile)
                {
                  xfce_rc_set_group (rcfile, "Desktop Entry");
                  app_name = xfce_rc_read_entry (rcfile, "Name", NULL);
                }
            }

          if (!app_name)
            app_name = figure_app_name (xfsm_properties_get_string (properties,
                                                                    SmProgram));

          xfsm_splash_screen_next (splash_screen, app_name);

          if (rcfile)
            {
              /* delay closing because app_name belongs to the rcfile
               * if we found it in the file */
              xfce_rc_close (rcfile);
            }
1008
        }
1009

1010
      if (G_LIKELY (xfsm_startup_start_properties (properties, manager)))
1011
        {
1012
          g_queue_push_tail (starting_properties, properties);
1013
          client_started = TRUE;
1014 1015 1016
        }
      else
        {
1017 1018 1019
          /* if starting the app failed, let the manager handle it */
          if (xfsm_manager_handle_failed_properties (manager, properties) == FALSE)
            xfsm_properties_free (properties);
1020
        }
1021
    }
1022

1023
  return client_started;
1024 1025
}

1026

1027 1028 1029 1030 1031 1032 1033 1034
static void
xfsm_startup_data_free (XfsmStartupData *sdata)
{
  g_object_unref (sdata->manager);
  g_free (sdata);
}


1035 1036 1037 1038 1039
static void
xfsm_startup_child_watch (GPid     pid,
                          gint     status,
                          gpointer user_data)
{
1040 1041
  XfsmStartupData *cwdata = user_data;
  GQueue          *starting_properties;
1042

1043 1044
  xfsm_verbose ("Client Id = %s, PID %d exited with status %d\n",
                cwdata->properties->client_id, (gint)pid, status);
1045

1046 1047 1048
  cwdata->properties->child_watch_id = 0;
  cwdata->properties->pid = -1;

1049 1050
  starting_properties = xfsm_manager_get_queue (cwdata->manager, XFSM_MANAGER_QUEUE_STARTING_PROPS);
  if (g_queue_find (starting_properties, cwdata->properties) != NULL)
1051
    {
1052 1053 1054
      xfsm_verbose ("Client Id = %s died while starting up\n",
                    cwdata->properties->client_id);
      xfsm_startup_handle_failed_startup (cwdata->properties, cwdata->manager);
1055 1056
    }

1057 1058 1059
  /* NOTE: cwdata->properties could have been freed by
   * xfsm_startup_handle_failed_startup() above, so don't try to access
   * any of its members here. */
1060

1061
  g_spawn_close_pid (pid);
1062 1063 1064
}


1065
static gboolean
1066 1067
xfsm_startup_timeout (gpointer data)
{
1068 1069 1070 1071
  XfsmStartupData *stdata = data;

  xfsm_verbose ("Client Id = %s failed to register in time\n",
                stdata->properties->client_id);
1072 1073

  stdata->properties->startup_timeout_id = 0;
1074
  xfsm_startup_handle_failed_startup (stdata->properties, stdata->manager);
1075 1076 1077 1078 1079 1080

  return FALSE;
}


static void
1081 1082
xfsm_startup_handle_failed_startup (XfsmProperties *properties,
                                    XfsmManager    *manager)
1083 1084 1085
{
  GQueue *starting_properties = xfsm_manager_get_queue (manager, XFSM_MANAGER_QUEUE_STARTING_PROPS);

1086 1087 1088 1089 1090 1091 1092 1093
  /* if our timer hasn't run out yet, kill it */
  if (properties->startup_timeout_id > 0)
    {
      g_source_remove (properties->startup_timeout_id);
      properties->startup_timeout_id = 0;
    }

  xfsm_properties_set_default_child_watch (properties);
1094

1095 1096
  /* not starting anymore, so remove it from the list.  tell the manager
   * it failed, and let it do its thing. */
1097
  g_queue_remove (starting_properties, properties);
1098 1099
  if (xfsm_manager_handle_failed_properties (manager, properties) == FALSE)
      xfsm_properties_free (properties);
1100

1101 1102
  if (g_queue_peek_head (starting_properties) == NULL
      && xfsm_manager_get_state (manager) == XFSM_MANAGER_STARTUP)
1103 1104
    {
      /* everything has finished starting or failed; continue startup */
1105
      xfsm_startup_session_continue (manager);
1106 1107
    }
}
1108 1109