~ubuntu-branches/ubuntu/trusty/mpd/trusty

« back to all changes in this revision

Viewing changes to src/main_win32.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-02-02 12:26:30 UTC
  • mfrom: (1.5.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202122630-bdyx8w4k94doz4fs
Tags: 0.16.1-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/control:
    + Don't build-depend on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
  - debian/control, debian/rules:
    + Add libmp3lame-dev to the build dependencies and enable lame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
 
3
 * http://www.musicpd.org
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include "main.h"
 
22
 
 
23
#ifdef WIN32
 
24
 
 
25
#include "mpd_error.h"
 
26
#include "event_pipe.h"
 
27
 
 
28
#include <glib.h>
 
29
 
 
30
#define WINVER 0x0501
 
31
#include <windows.h>
 
32
 
 
33
static int service_argc;
 
34
static char **service_argv;
 
35
static char service_name[] = "";
 
36
static BOOL ignore_console_events;
 
37
static SERVICE_STATUS_HANDLE service_handle;
 
38
 
 
39
static void WINAPI
 
40
service_main(DWORD argc, CHAR *argv[]);
 
41
 
 
42
static SERVICE_TABLE_ENTRY service_registry[] = {
 
43
        {service_name, service_main},
 
44
        {NULL, NULL}
 
45
};
 
46
 
 
47
static void
 
48
service_notify_status(DWORD status_code)
 
49
{
 
50
        SERVICE_STATUS current_status;
 
51
 
 
52
        current_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
 
53
        current_status.dwControlsAccepted = status_code == SERVICE_START_PENDING
 
54
                ? 0
 
55
                : SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
 
56
 
 
57
        current_status.dwCurrentState = status_code;
 
58
        current_status.dwWin32ExitCode = NO_ERROR;
 
59
        current_status.dwCheckPoint = 0;
 
60
        current_status.dwWaitHint = 1000;
 
61
 
 
62
        SetServiceStatus(service_handle, &current_status);
 
63
}
 
64
 
 
65
static DWORD WINAPI
 
66
service_dispatcher(G_GNUC_UNUSED DWORD control, G_GNUC_UNUSED DWORD event_type,
 
67
                   G_GNUC_UNUSED void *event_data, G_GNUC_UNUSED void *context)
 
68
{
 
69
        switch (control) {
 
70
        case SERVICE_CONTROL_SHUTDOWN:
 
71
        case SERVICE_CONTROL_STOP:
 
72
                event_pipe_emit(PIPE_EVENT_SHUTDOWN);
 
73
                return NO_ERROR;
 
74
        default:
 
75
                return NO_ERROR;
 
76
        }
 
77
}
 
78
 
 
79
static void WINAPI
 
80
service_main(G_GNUC_UNUSED DWORD argc, G_GNUC_UNUSED CHAR *argv[])
 
81
{
 
82
        DWORD error_code;
 
83
        gchar* error_message;
 
84
 
 
85
        service_handle =
 
86
                RegisterServiceCtrlHandlerEx(service_name,
 
87
                                             service_dispatcher, NULL);
 
88
 
 
89
        if (service_handle == 0) {
 
90
                error_code = GetLastError();
 
91
                error_message = g_win32_error_message(error_code);
 
92
                MPD_ERROR("RegisterServiceCtrlHandlerEx() failed: %s",
 
93
                          error_message);
 
94
        }
 
95
 
 
96
        service_notify_status(SERVICE_START_PENDING);
 
97
        mpd_main(service_argc, service_argv);
 
98
        service_notify_status(SERVICE_STOPPED);
 
99
}
 
100
 
 
101
static BOOL WINAPI
 
102
console_handler(DWORD event)
 
103
{
 
104
        switch (event) {
 
105
        case CTRL_C_EVENT:
 
106
        case CTRL_CLOSE_EVENT:
 
107
                if (!ignore_console_events)
 
108
                        event_pipe_emit(PIPE_EVENT_SHUTDOWN);
 
109
                return TRUE;
 
110
        default:
 
111
                return FALSE;
 
112
        }
 
113
}
 
114
 
 
115
int win32_main(int argc, char *argv[])
 
116
{
 
117
        DWORD error_code;
 
118
        gchar* error_message;
 
119
 
 
120
        service_argc = argc;
 
121
        service_argv = argv;
 
122
 
 
123
        if (StartServiceCtrlDispatcher(service_registry))
 
124
                return 0; /* run as service successefully */
 
125
 
 
126
        error_code = GetLastError();
 
127
        if (error_code == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
 
128
                /* running as console app */
 
129
                SetConsoleTitle("Music Player Daemon");
 
130
                ignore_console_events = TRUE;
 
131
                SetConsoleCtrlHandler(console_handler, TRUE);
 
132
                return mpd_main(argc, argv);
 
133
        }
 
134
 
 
135
        error_message = g_win32_error_message(error_code);
 
136
        MPD_ERROR("StartServiceCtrlDispatcher() failed: %s", error_message);
 
137
}
 
138
 
 
139
void win32_app_started()
 
140
{
 
141
        if (service_handle != 0)
 
142
                service_notify_status(SERVICE_RUNNING);
 
143
        else
 
144
                ignore_console_events = FALSE;
 
145
}
 
146
 
 
147
void win32_app_stopping()
 
148
{
 
149
        if (service_handle != 0)
 
150
                service_notify_status(SERVICE_STOP_PENDING);
 
151
        else
 
152
                ignore_console_events = TRUE;
 
153
}
 
154
 
 
155
#endif