From 3c520cffd8f5cabcdc91adf4d3a3bede8f9441ac Mon Sep 17 00:00:00 2001 From: sletz Date: Sat, 6 Mar 2010 06:54:15 +0000 Subject: [PATCH] Change error return code for functions that return a pointer. git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@3943 0c269be4-1314-0410-8aa9-9f06e86f4224 --- common/JackWeakAPI.cpp | 101 ++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 41 deletions(-) diff --git a/common/JackWeakAPI.cpp b/common/JackWeakAPI.cpp index 4bc89f6a..ca91aac2 100644 --- a/common/JackWeakAPI.cpp +++ b/common/JackWeakAPI.cpp @@ -1,32 +1,36 @@ -/* -Copyright (C) 2009 Grame - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or -(at your option) any later version. +//============================================================================= +// MuseScore +// Linux Music Score Editor +// $Id: +// +// jackWeakAPI based on code from Stéphane Letz (Grame) +// partly based on Julien Pommier (PianoTeq : http://www.pianoteq.com/) code. +// +// Copyright (C) 2002-2007 Werner Schweer and others +// Copyright (C) 2009 Grame -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 Lesser General Public License for more details. +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. -You should have received a copy of the GNU Lesser General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// 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 Lesser General Public License for more details. -*/ - -/* - Completed from Julien Pommier (PianoTeq : http://www.pianoteq.com/) code. -*/ +// You should have received a copy of the GNU Lesser General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include #include #include +#ifndef WIN32 +#include +#endif #include -#include #include /* dynamically load libjack and forward all registered calls to libjack @@ -41,13 +45,12 @@ using std::cerr; int libjack_is_present = 0; // public symbol, similar to what relaytool does. #ifdef WIN32 -#include HMODULE libjack_handle = 0; #else -#include static void *libjack_handle = 0; #endif + static void __attribute__((constructor)) tryload_libjack() { if (getenv("SKIP_LIBJACK") == 0) { // just in case libjack is causing troubles.. @@ -67,7 +70,7 @@ void *load_jack_function(const char *fn_name) { void *fn = 0; if (!libjack_handle) { - std::cerr << "libjack not found, so do not try to load " << fn_name << " ffs !\n"; + fprintf (stderr, "libjack not found, so do not try to load %s ffs !\n", fn_name); return 0; } #ifdef WIN32 @@ -76,7 +79,13 @@ void *load_jack_function(const char *fn_name) fn = dlsym(libjack_handle, fn_name); #endif if (!fn) { - std::cerr << "could not dlsym(" << libjack_handle << "), " << dlerror() << "\n"; +#ifdef WIN32 + char* lpMsgBuf; + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0,NULL ); + fprintf (stderr, "could not GetProcAddress( %s ), %s \n", fn_name, lpMsgBuf) ; +#else + fprintf (stderr, "could not dlsym( %s ), %s \n", fn_name, dlerror()) ; +#endif } return fn; } @@ -90,6 +99,15 @@ void *load_jack_function(const char *fn_name) else return (return_type)-1; \ } +#define DECL_FUNCTION_NULL(return_type, fn_name, arguments_types, arguments) \ + typedef return_type (*fn_name##_ptr_t)arguments_types; \ + return_type fn_name arguments_types { \ + static fn_name##_ptr_t fn = 0; \ + if (fn == 0) { fn = (fn_name##_ptr_t)load_jack_function(#fn_name); } \ + if (fn) return (*fn)arguments; \ + else return (return_type)0; \ + } + #define DECL_VOID_FUNCTION(fn_name, arguments_types, arguments) \ typedef void (*fn_name##_ptr_t)arguments_types; \ void fn_name arguments_types { \ @@ -98,21 +116,22 @@ void *load_jack_function(const char *fn_name) if (fn) (*fn)arguments; \ } + DECL_VOID_FUNCTION(jack_get_version, (int *major_ptr, int *minor_ptr, int *micro_ptr, int *proto_ptr), (major_ptr, minor_ptr, micro_ptr, proto_ptr)); -DECL_FUNCTION(const char *, jack_get_version_string, (), ()); -DECL_FUNCTION(jack_client_t *, jack_client_open, (const char *client_name, jack_options_t options, jack_status_t *status, ...), +DECL_FUNCTION_NULL(const char *, jack_get_version_string, (), ()); +DECL_FUNCTION_NULL(jack_client_t *, jack_client_open, (const char *client_name, jack_options_t options, jack_status_t *status, ...), (client_name, options, status)); DECL_FUNCTION(int, jack_client_close, (jack_client_t *client), (client)); -DECL_FUNCTION(jack_client_t *, jack_client_new, (const char *client_name), (client_name)); +DECL_FUNCTION_NULL(jack_client_t *, jack_client_new, (const char *client_name), (client_name)); DECL_FUNCTION(int, jack_client_name_size, (), ()); -DECL_FUNCTION(char*, jack_get_client_name, (jack_client_t *client), (client)); +DECL_FUNCTION_NULL(char*, jack_get_client_name, (jack_client_t *client), (client)); DECL_FUNCTION(int, jack_internal_client_new, (const char *client_name, const char *load_name, const char *load_init), (client_name, load_name, load_init)); DECL_VOID_FUNCTION(jack_internal_client_close, (const char *client_name), (client_name)); DECL_FUNCTION(int, jack_is_realtime, (jack_client_t *client), (client)); DECL_VOID_FUNCTION(jack_on_shutdown, (jack_client_t *client, JackShutdownCallback shutdown_callback, void *arg), (client, shutdown_callback, arg)); -DECL_VOID_FUNCTION(jack_on_info_shutdown, (jack_client_t* client, JackInfoShutdownCallback shutdown_callback, void* arg), (client, shutdown_callback, arg)); +//DECL_VOID_FUNCTION(jack_on_info_shutdown, (jack_client_t* client, JackInfoShutdownCallback shutdown_callback, void* arg), (client, shutdown_callback, arg)); DECL_FUNCTION(int, jack_set_process_callback, (jack_client_t *client, JackProcessCallback process_callback, void *arg), (client, process_callback, arg)); @@ -158,21 +177,21 @@ DECL_FUNCTION(int, jack_set_xrun_callback, (jack_client_t *client, void *arg), (client, xrun_callback, arg)); DECL_FUNCTION(int, jack_activate, (jack_client_t *client), (client)); DECL_FUNCTION(int, jack_deactivate, (jack_client_t *client), (client)); -DECL_FUNCTION(jack_port_t *, jack_port_register, (jack_client_t *client, const char *port_name, const char *port_type, +DECL_FUNCTION_NULL(jack_port_t *, jack_port_register, (jack_client_t *client, const char *port_name, const char *port_type, unsigned long flags, unsigned long buffer_size), (client, port_name, port_type, flags, buffer_size)); DECL_FUNCTION(int, jack_port_unregister, (jack_client_t *client, jack_port_t* port), (client, port)); -DECL_FUNCTION(void *, jack_port_get_buffer, (jack_port_t *port, jack_nframes_t nframes), (port, nframes)); -DECL_FUNCTION(const char*, jack_port_name, (const jack_port_t *port), (port)); -DECL_FUNCTION(const char*, jack_port_short_name, (const jack_port_t *port), (port)); +DECL_FUNCTION_NULL(void *, jack_port_get_buffer, (jack_port_t *port, jack_nframes_t nframes), (port, nframes)); +DECL_FUNCTION_NULL(const char*, jack_port_name, (const jack_port_t *port), (port)); +DECL_FUNCTION_NULL(const char*, jack_port_short_name, (const jack_port_t *port), (port)); DECL_FUNCTION(int, jack_port_flags, (const jack_port_t *port), (port)); -DECL_FUNCTION(const char*, jack_port_type, (const jack_port_t *port), (port)); +DECL_FUNCTION_NULL(const char*, jack_port_type, (const jack_port_t *port), (port)); DECL_FUNCTION(jack_port_type_id_t, jack_port_type_id, (const jack_port_t *port), (port)); DECL_FUNCTION(int, jack_port_is_mine, (const jack_client_t *client, const jack_port_t* port), (client, port)); DECL_FUNCTION(int, jack_port_connected, (const jack_port_t *port), (port)); DECL_FUNCTION(int, jack_port_connected_to, (const jack_port_t *port, const char *port_name), (port, port_name)); -DECL_FUNCTION(const char**, jack_port_get_connections, (const jack_port_t *port), (port)); -DECL_FUNCTION(const char**, jack_port_get_all_connections, (const jack_client_t *client,const jack_port_t *port), (client, port)); +DECL_FUNCTION_NULL(const char**, jack_port_get_connections, (const jack_port_t *port), (port)); +DECL_FUNCTION_NULL(const char**, jack_port_get_all_connections, (const jack_client_t *client,const jack_port_t *port), (client, port)); DECL_FUNCTION(int, jack_port_tie, (jack_port_t *src, jack_port_t *dst), (src, dst)); DECL_FUNCTION(int, jack_port_untie, (jack_port_t *port), (port)); DECL_FUNCTION(jack_nframes_t, jack_port_get_latency, (jack_port_t *port), (port)); @@ -197,10 +216,10 @@ DECL_FUNCTION(int, jack_port_type_size,(),()); DECL_FUNCTION(jack_nframes_t, jack_get_sample_rate, (jack_client_t *client), (client)); DECL_FUNCTION(jack_nframes_t, jack_get_buffer_size, (jack_client_t *client), (client)); -DECL_FUNCTION(const char**, jack_get_ports, (jack_client_t *client, const char *port_name_pattern, const char * type_name_pattern, +DECL_FUNCTION_NULL(const char**, jack_get_ports, (jack_client_t *client, const char *port_name_pattern, const char * type_name_pattern, unsigned long flags), (client, port_name_pattern, type_name_pattern, flags)); -DECL_FUNCTION(jack_port_t *, jack_port_by_name, (jack_client_t * client, const char *port_name), (client, port_name)); -DECL_FUNCTION(jack_port_t *, jack_port_by_id, (jack_client_t *client, jack_port_id_t port_id), (client, port_id)); +DECL_FUNCTION_NULL(jack_port_t *, jack_port_by_name, (jack_client_t * client, const char *port_name), (client, port_name)); +DECL_FUNCTION_NULL(jack_port_t *, jack_port_by_id, (jack_client_t *client, jack_port_id_t port_id), (client, port_id)); DECL_FUNCTION(int, jack_engine_takeover_timebase, (jack_client_t * client), (client)); DECL_FUNCTION(jack_nframes_t, jack_frames_since_cycle_start, (const jack_client_t * client), (client)); @@ -210,7 +229,7 @@ DECL_FUNCTION(jack_time_t, jack_frames_to_time, (const jack_client_t *client, ja DECL_FUNCTION(jack_nframes_t, jack_frame_time, (const jack_client_t *client), (client)); DECL_FUNCTION(jack_nframes_t, jack_last_frame_time, (const jack_client_t *client), (client)); DECL_FUNCTION(float, jack_cpu_load, (jack_client_t *client), (client)); -DECL_FUNCTION(pthread_t, jack_client_thread_id, (jack_client_t *client), (client)); +DECL_FUNCTION_NULL(pthread_t, jack_client_thread_id, (jack_client_t *client), (client)); DECL_VOID_FUNCTION(jack_set_error_function, (print_function fun), (fun)); DECL_VOID_FUNCTION(jack_set_info_function, (print_function fun), (fun)); @@ -268,6 +287,6 @@ DECL_FUNCTION(jack_nframes_t, jack_midi_get_event_count, (void* port_buffer), (p DECL_FUNCTION(int, jack_midi_event_get, (jack_midi_event_t* event, void* port_buffer, jack_nframes_t event_index), (event, port_buffer, event_index)) ; DECL_VOID_FUNCTION(jack_midi_clear_buffer, (void* port_buffer), (port_buffer)); DECL_FUNCTION(size_t, jack_midi_max_event_size, (void* port_buffer), (port_buffer)); -DECL_FUNCTION(jack_midi_data_t*, jack_midi_event_reserve, (void* port_buffer, jack_nframes_t time, size_t data_size), (port_buffer, time, data_size)); +DECL_FUNCTION_NULL(jack_midi_data_t*, jack_midi_event_reserve, (void* port_buffer, jack_nframes_t time, size_t data_size), (port_buffer, time, data_size)); DECL_FUNCTION(int, jack_midi_event_write, (void* port_buffer, jack_nframes_t time, const jack_midi_data_t* data, size_t data_size), (port_buffer, time, data, data_size)); DECL_FUNCTION(jack_nframes_t, jack_midi_get_lost_event_count, (void* port_buffer), (port_buffer));