summaryrefslogtreecommitdiff
path: root/Source/FusionDale/src/fusiondale.c
blob: b5e3a0ce4707054e5af14bfc972f10fd967d98ff (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
   (c) Copyright 2006-2007  directfb.org

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>.

   This library 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 of the License, or (at your option) any later version.

   This library 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.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#include <fusiondale.h>
#include <fusiondale_version.h>

#include <direct/debug.h>
#include <direct/interface.h>
#include <direct/log.h>
#include <direct/messages.h>
#include <direct/util.h>

#include <ifusiondale.h>

#include <misc/dale_config.h>


IFusionDale *ifusiondale_singleton = NULL;

/**************************************************************************************************/

static DirectResult CreateRemote( const char *host, int session, IFusionDale **ret_interface );

/**********************************************************************************************************************/

/*
 * Version checking
 */
const unsigned int fusiondale_major_version = FUSIONDALE_MAJOR_VERSION;
const unsigned int fusiondale_minor_version = FUSIONDALE_MINOR_VERSION;
const unsigned int fusiondale_micro_version = FUSIONDALE_MICRO_VERSION;
const unsigned int fusiondale_binary_age    = FUSIONDALE_BINARY_AGE;
const unsigned int fusiondale_interface_age = FUSIONDALE_INTERFACE_AGE;

const char *
FusionDaleCheckVersion( unsigned int required_major,
                         unsigned int required_minor,
                         unsigned int required_micro )
{
     if (required_major > FUSIONDALE_MAJOR_VERSION)
          return "FusionDale version too old (major mismatch)";
     if (required_major < FUSIONDALE_MAJOR_VERSION)
          return "FusionDale version too new (major mismatch)";
     if (required_minor > FUSIONDALE_MINOR_VERSION)
          return "FusionDale version too old (minor mismatch)";
     if (required_minor < FUSIONDALE_MINOR_VERSION)
          return "FusionDale version too new (minor mismatch)";
     if (required_micro < FUSIONDALE_MICRO_VERSION - FUSIONDALE_BINARY_AGE)
          return "FusionDale version too new (micro mismatch)";
     if (required_micro > FUSIONDALE_MICRO_VERSION)
          return "FusionDale version too old (micro mismatch)";

     return NULL;
}

const char *
FusionDaleUsageString( void )
{
     return fd_config_usage();
}

DirectResult
FusionDaleInit( int *argc, char **argv[] )
{
#ifdef DSLINUX
     IFusionDale_Requestor_ctor();
#endif

     return fd_config_init( argc, argv );
}

DirectResult
FusionDaleSetOption( const char *name, const char *value )
{
     if (fusiondale_config == NULL) {
          D_ERROR( "FusionDaleSetOption: FusionDaleInit has to be called first!\n" );
          return DR_INIT;
     }

     if (ifusiondale_singleton) {
          D_ERROR( "FusionDaleSetOption: FusionDaleCreate has already been called!\n" );
          return DR_INIT;
     }

     if (!name)
          return DR_INVARG;

     return fd_config_set( name, value );
}

DirectResult
FusionDaleCreate( IFusionDale **ret_interface )
{
     DirectResult ret;
     
     if (!fusiondale_config) {
          D_ERROR( "FusionDaleCreate: FusionDaleInit has to be called first!\n" );
          return DR_INIT;
     }

     if (!ret_interface)
          return DR_INVARG;
          
     if (ifusiondale_singleton) {
          ifusiondale_singleton->AddRef( ifusiondale_singleton );
          *ret_interface = ifusiondale_singleton;
          return DR_OK;
     }

#ifndef DIRECTFB_PURE_VOODOO
     if (fusiondale_config->remote.host)
          return CreateRemote( fusiondale_config->remote.host, fusiondale_config->remote.session, ret_interface );

     if (!(direct_config->quiet & DMT_BANNER) && fusiondale_config->banner) {
          direct_log_printf( NULL,
               "\n"
               "     *--------------) FusionDale v%d.%d.%d (--------------*\n"
               "                (c) 2006-2007  directfb.org\n"
               "       -----------------------------------------------\n"
               "\n",
               FUSIONDALE_MAJOR_VERSION, FUSIONDALE_MINOR_VERSION, FUSIONDALE_MICRO_VERSION );
     }

     DIRECT_ALLOCATE_INTERFACE( ifusiondale_singleton, IFusionDale );
     
     ret = IFusionDale_Construct( ifusiondale_singleton );
     if (ret != DR_OK)
          ifusiondale_singleton = NULL;
          
     *ret_interface = ifusiondale_singleton;

     return DR_OK;
#else
     return CreateRemote( fusiondale_config->remote.host ?: "", fusiondale_config->remote.session, ret_interface );
#endif
}

DirectResult
FusionDaleError( const char *msg, DirectResult error )
{
     if (msg)
          fprintf( stderr, "(#) FusionDale Error [%s]: %s\n", msg, DirectResultString( error ) );
     else
          fprintf( stderr, "(#) FusionDale Error: %s\n", DirectResultString( error ) );

     return error;
}

DirectResult
FusionDaleErrorFatal( const char *msg, DirectResult error )
{
     FusionDaleError( msg, error );
     
     exit( error );
}

const char *
FusionDaleErrorString( DirectResult error )
{
     return DirectResultString( error );
}

/**********************************************************************************************************************/

static DirectResult
CreateRemote( const char *host, int session, IFusionDale **ret_interface )
{
     DirectResult          ret;
     DirectInterfaceFuncs *funcs;
     void                 *interface;

     D_ASSERT( host != NULL );
     D_ASSERT( ret_interface != NULL );

     ret = DirectGetInterface( &funcs, "IFusionDale", "Requestor", NULL, NULL );
     if (ret)
          return ret;

     ret = funcs->Allocate( &interface );
     if (ret)
          return ret;

     ret = funcs->Construct( interface, host, session );
     if (ret)
          return ret;

     *ret_interface = interface;

     return DR_OK;
}