summaryrefslogtreecommitdiff
path: root/navit/binding/win32/tell_navit.c
blob: e06f7325881b73b71c04c5f6cb2c038d0203f6b6 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
 * Navit, a modular navigation system.
 * Copyright (C) 2005-2008 Navit Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * 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 "config.h"
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#include <XGetopt.h>
#endif
#include <glib.h>
#include "binding_win32.h"

static LRESULT CALLBACK message_handler( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch(uMsg) {
		case WM_CREATE: 
			return 0;
	}
	return TRUE;
}

int errormode=1;

void err(char *fmt, ...)
{
	va_list ap;
	char buf[1024];
#if defined HAVE_API_WIN32_CE
#define vsnprintf _vsnprintf
#endif
	va_start(ap, fmt);
	vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);
	switch(errormode) {
		case 0: /* be silent */
			break;
		case 1: 
			MessageBox(NULL, buf, "tell_navit", MB_ICONERROR|MB_OK);
			break;
		case 2:
			fprintf(stderr,"%s",buf);
			break;
	}
}

void print_usage(void)
{
	err(
	"tell_navit usage:\n"
	"tell_navit [options] navit_command\n"
	"\t-h this help\n"
	"\t-e <way>: set way to report error messages:\n"
	"\t\t0 - suppress messages\n"
	"\t\t1 - use messagebox (default)\n"
	"\t\t2 - print to stderr\n"
  );
}

int main(int argc, char **argv)
{
	HWND navitWindow;
	COPYDATASTRUCT cd;
	char opt;

	TCHAR *g_szClassName  = TEXT("TellNavitWND");
	WNDCLASS wc;
	HWND hwnd;
	HWND hWndParent=NULL;


	if(argc>0) {
		while((opt = getopt(argc, argv, ":hvc:d:e:s:")) != -1) {
			switch(opt){
			case 'h':
				print_usage();
				exit(0);
				break;
			case 'e':
				errormode=atoi(optarg);
				break;
			default:
				err("Unknown option %c\n", opt);
				exit(1);
				break;
			}
		}
	} else {
	print_usage();
	exit(1);
	}
	if(optind==argc) {
		err("Navit command to execute is needed.");
		exit(1);
	}


	memset(&wc, 0 , sizeof(WNDCLASS));
	wc.lpfnWndProc	= message_handler;
	wc.hInstance	= GetModuleHandle(NULL);
	wc.lpszClassName = g_szClassName;

	if (!RegisterClass(&wc))
	{
		err(TEXT("Window class registration failed\n"));
		return 1;
	} else {
		hwnd = CreateWindow(
				g_szClassName,
				TEXT("Tell Navit"),
				0,
				0,
				0,
				0,
				0,
				hWndParent,
				NULL,
				GetModuleHandle(NULL),
				NULL);
		if(!hwnd) {
			err(TEXT("Can't create hidden window\n"));
			UnregisterClass(g_szClassName,NULL);
			return 1;
		}
	}
    
	navitWindow=FindWindow( TEXT("NAVGRA"), NULL );
	if(!navitWindow) {
		err(TEXT("Navit window not found\n"));
		DestroyWindow(hwnd);
		UnregisterClass(g_szClassName,NULL);
		return 1;
	} else {
		int rv;
		char *command=g_strjoinv(" ",argv+optind);
		struct navit_binding_w32_msg *msg;
		int sz=sizeof(*msg)+strlen(command);

		cd.dwData=NAVIT_BINDING_W32_DWDATA;
		msg=g_malloc0(sz);
		msg->version=NAVIT_BINDING_W32_VERSION;
		g_strlcpy(msg->magic,NAVIT_BINDING_W32_MAGIC,sizeof(msg->magic));
		g_strlcpy(msg->text,command,sz-sizeof(*msg)+1);
		cd.cbData=sz;
		cd.lpData=msg;
		rv=SendMessage( navitWindow, WM_COPYDATA, (WPARAM)hwnd, (LPARAM) (LPVOID) &cd );
		g_free(command);
		g_free(msg);
		if(rv!=0) {
			err(TEXT("Error %d sending message, SendMessage return value is %d\n"), GetLastError(), rv);
			DestroyWindow(hwnd);
			UnregisterClass(g_szClassName,NULL);
			return 1;
		}
	}
	DestroyWindow(hwnd);
	UnregisterClass(g_szClassName,NULL);
	return 0;
}