Ticket #970: binding-win32-2012-01-07.diff

File binding-win32-2012-01-07.diff, 16.2 KB (added by tryagain, 7 years ago)

First version of binding::win32 feature

  • navit/graphics/win32/graphics_win32.c

     
    545545    case WM_KEYDOWN:
    546546        HandleKeyDown( gra_priv, wParam);
    547547        break;
     548   case WM_COPYDATA:
     549        dbg(0,"got WM_COPYDATA\n");
     550        callback_list_call_attr_2(gra_priv->cbl, attr_wm_copydata, (void *)wParam, (void*)lParam);
     551        break;
    548552#ifdef HAVE_API_WIN32_CE
    549553    case WM_SETFOCUS:
    550554        if (fullscr) {
     
    710714        dbg(0, "Window creation failed: %d\n",  GetLastError());
    711715        return NULL;
    712716    }
     717// TODO: For Vista, we need here ChangeWindowMessageFilter(WM_COPYDATA,MSGFLT_ADD);, since Win7 we need above one or ChangeWindowMessageFilterEx (MSDN), both are
     718// not avail for earlier Win and not present in my mingw :(. Welcome late binding?
     719 
    713720    gr->wnd_handle = hwnd;
    714721
    715722    callback_list_call_attr_2(gr->cbl, attr_resize, (void *)gr->width, (void *)gr->height);
  • navit/binding/win32/binding_win32.h

     
     1/**
     2 * Navit, a modular navigation system.
     3 * Copyright (C) 2005-2008 Navit Team
     4 *
     5 * This program is free software; you can redistribute it and/or
     6 * modify it under the terms of the GNU General Public License
     7 * version 2 as published by the Free Software Foundation.
     8 *
     9 * This program is distributed in the hope that it will be useful,
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 * GNU General Public License for more details.
     13 *
     14 * You should have received a copy of the GNU General Public License
     15 * along with this program; if not, write to the
     16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17 * Boston, MA  02110-1301, USA.
     18 */
     19
     20#ifndef BINDING_WIN32_H
     21#define BINDING_WIN32_H
     22
     23#define NAVIT_BINDING_W32_DWDATA 1
     24#define NAVIT_BINDING_W32_MAGIC "NavIt"
     25#define NAVIT_BINDING_W32_VERSION 1
     26
     27struct navit_binding_w32_msg {
     28        /* Structure version number, should be equal to NAVIT_BINDING_W32_VERSION */
     29        int version;
     30        /* Magic code to filter out packets directed to other applications and [mistakely] sent to us or broadcasted.
     31         * should be equal to NAVIT_BINDING_W32_MAGIC  */
     32        char magic[6];
     33        /* Command to be executed by Navit */
     34        char text[1];
     35};
     36
     37#endif
  • navit/binding/win32/tell_navit.c

     
     1/**
     2 * Navit, a modular navigation system.
     3 * Copyright (C) 2005-2008 Navit Team
     4 *
     5 * This program is free software; you can redistribute it and/or
     6 * modify it under the terms of the GNU General Public License
     7 * version 2 as published by the Free Software Foundation.
     8 *
     9 * This program is distributed in the hope that it will be useful,
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 * GNU General Public License for more details.
     13 *
     14 * You should have received a copy of the GNU General Public License
     15 * along with this program; if not, write to the
     16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17 * Boston, MA  02110-1301, USA.
     18 */
     19
     20#include "config.h"
     21#include <windows.h>
     22#include <stdio.h>
     23#include <stdarg.h>
     24#ifdef HAVE_GETOPT_H
     25#include <getopt.h>
     26#else
     27#include <XGetopt.h>
     28#endif
     29#include <glib.h>
     30#include "binding_win32.h"
     31
     32static LRESULT CALLBACK message_handler( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
     33{
     34        switch(uMsg) {
     35                case WM_CREATE:
     36                        return 0;
     37        }
     38        return TRUE;
     39}
     40
     41int errormode=1;
     42
     43void err(char *fmt, ...)
     44{
     45        va_list ap;
     46        char buf[1024];
     47        va_start(ap, fmt);
     48        vsnprintf(buf, sizeof(buf), fmt, ap);
     49        va_end(ap);
     50        switch(errormode) {
     51                case 0: /* be silent */
     52                        break;
     53                case 1:
     54                        MessageBox(NULL, buf, "tell_navit", MB_ICONERROR|MB_OK);
     55                        break;
     56                case 2:
     57                        fprintf(stderr,"%s",buf);
     58                        break;
     59        }
     60}
     61
     62void print_usage(void)
     63{
     64  err(
     65    "tell_navit usage:\n"
     66    "tell_navit [options] navit_command\n"
     67    "\t-h this help\n"
     68    "\t-e <way>: set way to report error messages:\n"
     69    "\t\t0 - suppress messages\n"
     70    "\t\t1 - use messagebox (default)\n"
     71    "\t\t2 - print to stderr\n"
     72  );
     73}
     74
     75int main(int argc, char **argv)
     76{
     77    HWND navitWindow;
     78    COPYDATASTRUCT cd;
     79    char opt;
     80   
     81    TCHAR *g_szClassName  = TEXT("TellNavitWND");
     82    WNDCLASS wc;
     83    HWND hwnd;
     84    HWND hWndParent=NULL;
     85   
     86   
     87    if(argc>0) {
     88        while((opt = getopt(argc, argv, ":hvc:d:e:s:")) != -1) {
     89                switch(opt){
     90                case 'h':
     91                        print_usage();
     92                        exit(0);
     93                        break;
     94                case 'e':
     95                        errormode=atoi(optarg);
     96                        break;
     97                default:
     98                        err("Unknown option %c\n", opt);
     99                        exit(1);
     100                        break;
     101                }
     102        }
     103    } else {
     104        print_usage();
     105        exit(1);
     106    }
     107    if(optind==argc) {
     108        err("Navit command to execute is needed.");
     109        exit(1);
     110    }
     111 
     112
     113    memset(&wc, 0 , sizeof(WNDCLASS));
     114    wc.lpfnWndProc      = message_handler;
     115    wc.hInstance        = GetModuleHandle(NULL);
     116    wc.lpszClassName = g_szClassName;
     117
     118    if (!RegisterClass(&wc))
     119    {
     120        err(TEXT("Window class registration failed\n"));
     121        return 1;
     122    } else {
     123            hwnd = CreateWindow(
     124                                g_szClassName,
     125                                TEXT("Tell Navit"),
     126                                0,
     127                                0,
     128                                0,
     129                                0,
     130                                0,
     131                                hWndParent,
     132                                NULL,
     133                                GetModuleHandle(NULL),
     134                                NULL);
     135            if(!hwnd) {
     136                err(TEXT("Can't create hidden window\n"));
     137                UnregisterClass(g_szClassName,NULL);
     138                return 1;
     139            }
     140    }
     141   
     142    navitWindow=FindWindow( TEXT("NAVGRA"), NULL );
     143    if(!navitWindow) {
     144        err(TEXT("Navit window not found\n"));
     145        DestroyWindow(hwnd);
     146        UnregisterClass(g_szClassName,NULL);
     147        return 1;
     148    } else {
     149        int rv;
     150        char *command=g_strjoinv(" ",argv+optind);
     151        struct navit_binding_w32_msg *msg;
     152        int sz=sizeof(*msg)+strlen(command);
     153
     154        cd.dwData=NAVIT_BINDING_W32_DWDATA;
     155        msg=g_malloc0(sz);
     156        msg->version=NAVIT_BINDING_W32_VERSION;
     157        g_strlcpy(msg->magic,NAVIT_BINDING_W32_MAGIC,sizeof(msg->magic));
     158        g_strlcpy(msg->text,command,sz-sizeof(*msg)+1);
     159        cd.cbData=sz;
     160        cd.lpData=msg;
     161        rv=SendMessage( navitWindow, WM_COPYDATA, (WPARAM)hwnd, (LPARAM) (LPVOID) &cd );
     162        g_free(command);
     163        g_free(msg);
     164        if(rv!=0) {
     165                err(TEXT("Error %d sending message, SendMessage return value is %d\n"), GetLastError(), rv);
     166                DestroyWindow(hwnd);
     167                UnregisterClass(g_szClassName,NULL);
     168                return 1;
     169        }
     170    }
     171    DestroyWindow(hwnd);
     172    UnregisterClass(g_szClassName,NULL);
     173    return 0;
     174}
     175
     176
  • navit/binding/win32/Makefile.am

     
     1include $(top_srcdir)/Makefile.inc
     2AM_CPPFLAGS = -I$(top_srcdir)/navit -I$(top_srcdir)/navit/binding/win32 @NAVIT_CFLAGS@ -DMODULE=binding_win32
     3if PLUGINS
     4modulebinding_LTLIBRARIES = libbinding_win32.la
     5else
     6noinst_LTLIBRARIES = libbinding_win32.la
     7endif
     8libbinding_win32_la_SOURCES = binding_win32.c binding_win32.h
     9libbinding_win32_la_LIBADD =
     10libbinding_win32_la_LDFLAGS = -module -avoid-version
     11bin_PROGRAMS=tell_navit
     12tell_navit_LDADD = @NAVIT_LIBS@
  • navit/binding/win32/binding_win32.c

     
     1/**
     2 * Navit, a modular navigation system.
     3 * Copyright (C) 2005-2008 Navit Team
     4 *
     5 * This program is free software; you can redistribute it and/or
     6 * modify it under the terms of the GNU General Public License
     7 * version 2 as published by the Free Software Foundation.
     8 *
     9 * This program is distributed in the hope that it will be useful,
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 * GNU General Public License for more details.
     13 *
     14 * You should have received a copy of the GNU General Public License
     15 * along with this program; if not, write to the
     16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17 * Boston, MA  02110-1301, USA.
     18 */
     19
     20#include <string.h>
     21#include <glib.h>
     22#include <windows.h>
     23#include "config.h"
     24#include "config_.h"
     25#include "navit.h"
     26#include "coord.h"
     27#include "point.h"
     28#include "plugin.h"
     29#include "debug.h"
     30#include "item.h"
     31#include "attr.h"
     32#include "layout.h"
     33#include "navigation.h"
     34#include "command.h"
     35#include "callback.h"
     36#include "graphics.h"
     37#include "track.h"
     38#include "vehicle.h"
     39#include "vehicleprofile.h"
     40#include "map.h"
     41#include "mapset.h"
     42#include "osd.h"
     43#include "route.h"
     44#include "search.h"
     45#include "callback.h"
     46#include "gui.h"
     47#include "util.h"
     48#include "binding_win32.h"
     49
     50struct win32_binding_private {
     51        struct navit* navit;
     52};
     53
     54
     55
     56/* TODO: do something meaningful here
     57 *
     58 */
     59static int
     60win32_cmd_send_signal(struct navit *navit, char *command, struct attr **in, struct attr ***out)
     61{
     62        dbg(0,"this function is a stub\n");
     63        if (in) {
     64                while (*in) {
     65                        dbg(0,"another attribute to be sent\n");
     66                        in++;
     67                }
     68        }
     69        return 0;
     70}
     71
     72
     73static struct command_table commands[] = {
     74        {"win32_send",command_cast(win32_cmd_send_signal)},
     75};
     76
     77
     78static void
     79win32_wm_copydata(struct win32_binding_private *this, int *hwndSender, COPYDATASTRUCT *cpd)
     80{
     81        struct attr navit;
     82        struct navit_binding_w32_msg *msg;
     83        navit.type=attr_navit;
     84        navit.u.navit=this->navit;
     85        if(cpd->dwData!=NAVIT_BINDING_W32_DWDATA) {
     86                dbg(0,"COPYDATA message came with wrong DWDATA value, expected %d, got %d.\n",NAVIT_BINDING_W32_DWDATA,cpd->dwData);
     87                return;
     88        }
     89        if(cpd->cbData<sizeof(*msg)) {
     90                dbg(0,"COPYDATA message too short, expected >=%d, got %d.\n",sizeof(*msg),cpd->cbData);
     91                return;
     92        }
     93        msg=cpd->lpData;
     94        if(cpd->dwData!=NAVIT_BINDING_W32_VERSION) {
     95                dbg(0,"Got request with wrong version number, expected %d, got %d.\n",NAVIT_BINDING_W32_VERSION,msg->version);
     96                return;
     97        }
     98        if(strcmp(NAVIT_BINDING_W32_MAGIC,msg->magic)) {
     99                dbg(0,"Got request with wrong MAGIC, expected %s, got %*s.\n",NAVIT_BINDING_W32_MAGIC, msg->magic,sizeof(msg->magic));
     100                return;
     101        }
     102        dbg(0,"Running command %s\n", msg->text);
     103        command_evaluate(&navit, msg->text);
     104}
     105
     106static void
     107win32_cb_graphics_ready(struct win32_binding_private *this, struct navit *navit)
     108{
     109        struct graphics *gra;
     110        struct callback *gcb;
     111
     112        gcb=callback_new_attr_1(callback_cast(win32_wm_copydata),attr_wm_copydata, this);
     113        gra=navit_get_graphics(navit);
     114        dbg_assert(gra);
     115        graphics_add_callback(gra, gcb);
     116}
     117
     118static void
     119win32_main_navit(struct win32_binding_private *this, struct navit *navit, int added)
     120{
     121        struct attr attr;
     122        dbg(0,"enter");
     123        if (added==1) {
     124                dbg(0,"enter2");
     125                this->navit=navit;
     126                command_add_table_attr(commands, sizeof(commands)/sizeof(struct command_table), navit, &attr);
     127                navit_add_attr(navit, &attr);
     128                navit_add_callback(navit,callback_new_attr_1(callback_cast(win32_cb_graphics_ready),attr_graphics_ready, this));
     129        }
     130
     131}
     132
     133
     134
     135void plugin_init(void)
     136{
     137        struct attr callback;
     138        struct win32_binding_private *this=g_new0(struct win32_binding_private,1);
     139        callback.type=attr_callback;
     140        callback.u.callback=callback_new_attr_1(callback_cast(win32_main_navit),attr_navit,this);
     141        config_add_attr(config, &callback);
     142}
     143
  • navit/binding/win32/CMakeLists.txt

     
     1if (USE_LIBGNUINTL AND NOT HAVE_GLIB)
     2   ADD_DEPENDENCIES(support_glib intl_cmake)
     3endif()
     4
     5module_add_library(binding_win32 binding_win32.c)
     6
     7add_executable(tell_navit tell_navit.c)
     8target_link_libraries(tell_navit  ${MODULES_NAME} ${NAVIT_SUPPORT_LIBS} fib ${NAVIT_LIBS} )
     9
  • navit/binding/Makefile.am

     
    66  SUBDIRS+=dbus
    77endif
    88
    9 DIST_SUBDIRS=python dbus
     9if BINDING_WIN32
     10  SUBDIRS+=win32
     11endif
    1012
     13
     14DIST_SUBDIRS=python dbus win32
     15
  • navit/attr_def.h

     
    443443ATTR(log_textfile)
    444444ATTR(graphics_ready)
    445445ATTR(destroy)
     446ATTR(wm_copydata)
    446447ATTR2(0x000bffff,type_callback_end)
    447448ATTR2(0x000c0000,type_int64_begin)
    448449ATTR(osm_nodeid)
  • configure.in

     
    3939binding_dbus=yes; binding_dbus_reason=default
    4040binding_dbus_use_system_bus=no
    4141binding_python=yes; binding_python_reason=default
     42binding_win32=no; binding_win32_reason=default
    4243font_freetype=yes; font_freetype_reason=default
    4344fontconfig=yes; fontconfig_reason=default
    4445fribidi=yes; fribidi_reason=default
     
    9697        AC_DEFINE(HAVE_API_WIN32_BASE, 1, [Have Windows Base API])
    9798        AC_DEFINE(HAVE_API_WIN32_CE, 1, [Have Windows CE API])
    9899        AC_DEFINE(HAVE_PRAGMA_PACK, 1, [Have pragma pack])
     100        binding_win32=yes; binding_win32_reason="host_os is wince"
    99101        gui_win32=yes; gui_win32_reason="host_os is wince"
    100102        graphics_win32=yes; graphics_win32_reason="host_os is wince"
    101103        vehicle_wince=yes; vehcile_wince_reason="host_os is wince"
     
    107109        win32=yes
    108110        AC_DEFINE(HAVE_API_WIN32_BASE, 1, [Have Windows Base API])
    109111        AC_DEFINE(HAVE_API_WIN32, 1, [Have Windows API])
     112        binding_win32=yes; binding_win32_reason="host_os is mingw32"
    110113        gui_win32=yes; gui_win32_reason="host_os is mingw32"
    111114        graphics_win32=yes; graphics_win32_reason="host_os is mingw32"
    112115        speech_espeak=yes; speech_espeak_reason="host_os is mingw32"
     
    683686AC_ARG_WITH(dbus-service-dir, [ --with-dbus-service-dir   specify where the dbus service dir resides], DBUS_SERVICE_DIR=$withval, DBUS_SERVICE_DIR="$datarootdir/dbus-1/services")
    684687AC_SUBST(DBUS_SERVICE_DIR)
    685688
     689# win32 binding
     690AC_ARG_ENABLE(binding-win32,   [  --disable-binding-win32               don't create binding win32], binding_win32=$enableval;binding_win32_reason="configure parameter")
     691if test "x${binding_win32}" = "xyes" ; then
     692        AC_DEFINE(USE_BINDING_WIN32, 1, [Build with binding win32])
     693fi
     694AM_CONDITIONAL(BINDING_WIN32, test "x${binding_win32}" = "xyes")
     695
    686696# svg
    687697AC_ARG_ENABLE(svg, [  --disable-svg        disable Scalable Vector Graphics], enable_svg=$enableval, enable_svg=yes)
    688698AC_ARG_ENABLE(svg2png, [  --disable-svg2png        disable conversion of svgs to pngs], enable_svg2png=$enableval, enable_svg2png=yes)
     
    11271137navit/binding/Makefile
    11281138navit/binding/python/Makefile
    11291139navit/binding/dbus/Makefile
     1140navit/binding/win32/Makefile
    11301141navit/map/Makefile
    11311142navit/map/mg/Makefile
    11321143navit/map/textfile/Makefile
  • CMakeLists.txt

     
    6262add_module(vehicle/gpsd "gpsd lib not found" FALSE)
    6363add_module(vehicle/gypsy "gypsy lib not found" FALSE)
    6464add_module(vehicle/maemo "Default" FALSE)
     65add_module(binding/win32 "Default" FALSE)
    6566add_module(binding/dbus "dbus-glib-1 not found" FALSE)
    6667add_module(speech/dbus "dbus-glib-1 not found" FALSE)
    6768add_module(speech/cmdline "neither system() nor CreateProcess() found" FALSE)
     
    388389   set_with_reason(support/ezxml "Windows detected" TRUE)
    389390   set_with_reason(speech/espeak "Windows detected" TRUE)
    390391   set_with_reason(support/espeak "Windows detected" TRUE)
     392   set_with_reason(binding/win32 "Windows detected" TRUE)
    391393
    392394   # vehicle_file is broken for windows. use vehicle_wince instead
    393395   # whicle_wince isn't buildable on non-CE windows ssytems