summaryrefslogtreecommitdiff
path: root/src/msm/main.c
blob: 0280a3794278336c7e04ddab60832c4a7cd503ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* msm main() */

/* 
 * Copyright (C) 2001 Havoc Pennington
 * 
 * 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 of the
 * License, or (at your option) any later version.
 *
 * 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

#include "server.h"
#include "util.h"

static GMainLoop *main_loop = NULL;

static void
usage (void)
{
  g_print ("msm [--choose-session=NAME] [--failsafe]\n");
  exit (1);
}

static void
shutdown_cleanly_on_signal (int signo)
{
  if (main_loop && g_main_is_running (main_loop))
    g_main_quit (main_loop);
}

void
msm_quit (void)
{
  if (main_loop && g_main_is_running (main_loop))
    g_main_quit (main_loop);
}

int
main (int argc, char **argv)
{
  int i;
  const char *prev_arg;
  char *session_name;
  gboolean failsafe;
  struct sigaction act;
  sigset_t empty_mask;
  MsmServer *server;
  
  sigemptyset (&empty_mask);
  act.sa_handler = SIG_IGN;
  act.sa_mask    = empty_mask;
  act.sa_flags   = 0;
  sigaction (SIGPIPE, &act, 0);

  act.sa_handler = shutdown_cleanly_on_signal;
  sigaction (SIGHUP, &act, 0);
  sigaction (SIGINT, &act, 0);
  
  /* connect to display */
  gtk_init (&argc, &argv);

  /* Parse options lamely */
  session_name = NULL;
  failsafe = FALSE;
  prev_arg = NULL;
  i = 1;
  while (i < argc)
    {
      const char *arg = argv[i];
      
      if (strcmp (arg, "--help") == 0 ||
          strcmp (arg, "-h") == 0 ||
          strcmp (arg, "-?") == 0)
        usage ();
      else if (strcmp (arg, "--failsafe") == 0)
        failsafe = TRUE; 
      else if (strstr (arg, "--choose-session=") == arg)
        {
          const char *name;

          if (session_name != NULL)
            msm_fatal ("Can't specify session name twice\n");
          
          name = strchr (arg, '=');
          ++name;
          
          session_name = g_strdup (name);
        }
      else if (prev_arg &&
               strcmp (prev_arg, "--choose-session") == 0)
        {
          if (session_name != NULL)
            msm_fatal ("Can't specify session name twice\n");

          session_name = g_strdup (arg);
        }
      else if (strcmp (arg, "--choose-session") == 0)
        ; /* wait for next arg */
      else
        usage ();
      
      prev_arg = arg;
      
      ++i;
    }

  if (failsafe)
    server = msm_server_new_failsafe ();
  else
    server = msm_server_new (session_name);

  msm_server_launch_session (server);
  
  main_loop = g_main_loop_new (NULL, FALSE);
  
  g_main_run (main_loop);
  
  return 0;
}