osxmain.cpp 5.58 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2019 Savoir-faire Linux Inc.
3
 *
4 5 6 7 8 9
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
 *
 *  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
10
 *  the Free Software Foundation; either version 3 of the License, or
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *  (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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
#include <iostream>
#include <thread>
#include <cstring>
#include <signal.h>
#include <getopt.h>
27
#include <string>
28
#include <chrono>
29

30
#include "dring.h"
31 32 33
#include "callmanager_interface.h"
#include "configurationmanager_interface.h"
#include "presencemanager_interface.h"
Adrien Béraud's avatar
Adrien Béraud committed
34
#ifdef ENABLE_VIDEO
35 36
#include "videomanager_interface.h"
#endif
37 38
#include "fileutils.h"

39
static int ringFlags = 0;
40

41 42
static void
print_title()
43
{
44
    std::cout
Philippe Gorley's avatar
Philippe Gorley committed
45
        << "Jami Daemon " << DRing::version()
46
        << ", by Savoir-faire Linux 2004-2019" << std::endl
47
        << "https://jami.net/" << std::endl
Adrien Béraud's avatar
Adrien Béraud committed
48
#ifdef ENABLE_VIDEO
49 50 51
        << "[Video support enabled]" << std::endl
#endif
        << std::endl;
52 53
}

54 55
static void
print_usage()
56 57 58 59 60
{
    std::cout << std::endl <<
    "-c, --console \t- Log in console (instead of syslog)" << std::endl <<
    "-d, --debug \t- Debug mode (more verbose)" << std::endl <<
    "-p, --persistent \t- Stay alive after client quits" << std::endl <<
61
    "--auto-answer \t- Force automatic answer to incoming calls" << std::endl <<
62 63 64 65 66 67
    "-h, --help \t- Print help" << std::endl;
}

// Parse command line arguments, setting debug options or printing a help
// message accordingly.
// returns true if we should quit (i.e. help was printed), false otherwise
68 69
static bool
parse_args(int argc, char *argv[], bool& persistent)
70
{
71 72 73 74 75 76 77
    int consoleFlag = false;
    int debugFlag = false;
    int helpFlag = false;
    int versionFlag = false;
    int autoAnswer = false;

    const struct option long_options[] = {
78 79 80 81 82 83
        /* These options set a flag. */
        {"debug", no_argument, NULL, 'd'},
        {"console", no_argument, NULL, 'c'},
        {"persistent", no_argument, NULL, 'p'},
        {"help", no_argument, NULL, 'h'},
        {"version", no_argument, NULL, 'v'},
84
        {"auto-answer", no_argument, &autoAnswer, true},
85 86 87 88 89 90 91
        {0, 0, 0, 0} /* Sentinel */
    };

    while (true) {
        /* getopt_long stores the option index here. */
        int option_index = 0;

92 93 94 95 96 97 98
        auto c = getopt_long(argc, argv, "dcphv", long_options, &option_index);

        // end of the options
        if (c == -1)
            break;

        switch (c) {
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
            case 'd':
                debugFlag = true;
                break;

            case 'c':
                consoleFlag = true;
                break;

            case 'p':
                persistent = true;
                break;

            case 'h':
            case '?':
                helpFlag = true;
                break;

            case 'v':
                versionFlag = true;
                break;

            default:
                break;
        }
    }

    if (helpFlag) {
        print_usage();
127 128 129 130
        return true;
    }

    if (versionFlag) {
131
        // We've always print the title/version, so we can just exit
132
        return true;
133 134
    }

135 136
    if (consoleFlag)
        ringFlags |= DRing::DRING_FLAG_CONSOLE_LOG;
137

138 139
    if (debugFlag)
        ringFlags |= DRing::DRING_FLAG_DEBUG;
140

141 142 143
    if (autoAnswer)
        ringFlags |= DRing::DRING_FLAG_AUTOANSWER;

144 145
    return false;
}
146

147 148 149 150
static int
osxTests()
{
    using SharedCallback = std::shared_ptr<DRing::CallbackWrapperBase>;
151

152 153
    DRing::init(static_cast<DRing::InitFlag>(ringFlags));

154 155
    registerSignalHandlers(std::map<std::string, std::shared_ptr<DRing::CallbackWrapperBase>>());

156 157
    if (!DRing::start())
            return -1;
158 159

    while (true) {
160
        std::this_thread::sleep_for(std::chrono::seconds(1));
161
    }
162

163
    DRing::fini();
164 165
}

166 167
static int
run()
168
{
169
    osxTests();
170
    return 0;
171 172
}

173 174 175
static void
interrupt()
{}
176

177 178
static void
signal_handler(int code)
179 180
{
    std::cerr << "Caught signal " << strsignal(code)
181
              << ", terminating..." << std::endl;
182 183 184 185 186 187 188 189 190

    // Unset signal handlers
    signal(SIGHUP, SIG_DFL);
    signal(SIGINT, SIG_DFL);
    signal(SIGTERM, SIG_DFL);

    interrupt();
}

191 192
int
main(int argc, char *argv [])
193 194 195
{
    // make a copy as we don't want to modify argv[0], copy it to a vector to
    // guarantee that memory is correctly managed/exception safe
196
    std::string programName {argv[0]};
197 198 199
    std::vector<char> writable(programName.size() + 1);
    std::copy(programName.begin(), programName.end(), writable.begin());

Adrien Béraud's avatar
Adrien Béraud committed
200
    jami::fileutils::set_program_dir(writable.data());
201 202 203

#ifdef TOP_BUILDDIR
    if (!getenv("CODECS_PATH"))
204
        setenv("CODECS_PATH", TOP_BUILDDIR "/src/media/audio/codecs", 1);
205 206 207 208 209 210 211 212 213 214 215 216 217 218
#endif

    print_title();

    bool persistent = false;
    if (parse_args(argc, argv, persistent))
        return 0;

    // TODO: Block signals for all threads but the main thread, decide how/if we should
    // handle other signals
    signal(SIGINT, signal_handler);
    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);

219
    return run();
220
}