2
* Copyright (C) 2003-2010 The Music Player Daemon Project
3
* http://www.musicpd.org
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.
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.
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.
25
#include "mpd_error.h"
26
#include "event_pipe.h"
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;
40
service_main(DWORD argc, CHAR *argv[]);
42
static SERVICE_TABLE_ENTRY service_registry[] = {
43
{service_name, service_main},
48
service_notify_status(DWORD status_code)
50
SERVICE_STATUS current_status;
52
current_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
53
current_status.dwControlsAccepted = status_code == SERVICE_START_PENDING
55
: SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
57
current_status.dwCurrentState = status_code;
58
current_status.dwWin32ExitCode = NO_ERROR;
59
current_status.dwCheckPoint = 0;
60
current_status.dwWaitHint = 1000;
62
SetServiceStatus(service_handle, ¤t_status);
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)
70
case SERVICE_CONTROL_SHUTDOWN:
71
case SERVICE_CONTROL_STOP:
72
event_pipe_emit(PIPE_EVENT_SHUTDOWN);
80
service_main(G_GNUC_UNUSED DWORD argc, G_GNUC_UNUSED CHAR *argv[])
86
RegisterServiceCtrlHandlerEx(service_name,
87
service_dispatcher, NULL);
89
if (service_handle == 0) {
90
error_code = GetLastError();
91
error_message = g_win32_error_message(error_code);
92
MPD_ERROR("RegisterServiceCtrlHandlerEx() failed: %s",
96
service_notify_status(SERVICE_START_PENDING);
97
mpd_main(service_argc, service_argv);
98
service_notify_status(SERVICE_STOPPED);
102
console_handler(DWORD event)
106
case CTRL_CLOSE_EVENT:
107
if (!ignore_console_events)
108
event_pipe_emit(PIPE_EVENT_SHUTDOWN);
115
int win32_main(int argc, char *argv[])
118
gchar* error_message;
123
if (StartServiceCtrlDispatcher(service_registry))
124
return 0; /* run as service successefully */
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);
135
error_message = g_win32_error_message(error_code);
136
MPD_ERROR("StartServiceCtrlDispatcher() failed: %s", error_message);
139
void win32_app_started()
141
if (service_handle != 0)
142
service_notify_status(SERVICE_RUNNING);
144
ignore_console_events = FALSE;
147
void win32_app_stopping()
149
if (service_handle != 0)
150
service_notify_status(SERVICE_STOP_PENDING);
152
ignore_console_events = TRUE;