1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim 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 GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
25 #include "core/GP_Debug.h"
27 #include "GP_Backends.h"
28 #include "GP_BackendInit.h"
30 static void backend_sdl_help(FILE *help
, const char *err
)
36 fprintf(help
, "ERROR: %s\n", err
);
38 fprintf(help
, "libSDL backend\n"
41 " FS - Full Screen mode\n"
42 " WxH - Display Size\n");
45 static int sdl_params_to_flags(const char *param
, GP_Size
*w
, GP_Size
*h
,
46 uint8_t *flags
, FILE *help
)
48 if (!strcasecmp(param
, "FS")) {
49 *flags
|= GP_SDL_FULLSCREEN
;
54 * Accepts only string with format "intxint" or "intXint"
59 if (sscanf(param
, "%i%*[xX]%i%n", &sw
, &sh
, &n
) == 2 && n
== strlen(param
)) {
65 backend_sdl_help(help
, "SDL: Invalid parameters");
69 static GP_Backend
*backend_sdl_init(char *params
, const char *caption
,
73 return GP_BackendSDLInit(0, 0, 0, 0, caption
);
76 uint8_t flags
= GP_SDL_RESIZABLE
;
84 if (sdl_params_to_flags(params
, &w
, &h
, &flags
, help
))
90 if (sdl_params_to_flags(params
, &w
, &h
, &flags
, help
))
93 return GP_BackendSDLInit(w
, h
, 0, flags
, caption
);
100 static void backend_fb_help(FILE *help
, const char *err
)
106 fprintf(help
, "ERROR: %s\n", err
);
108 fprintf(help
, "LinuxFB backend\n"
113 static GP_Backend
*backend_fb_init(char *params
, const char *caption
,
116 const char *fb
= "/dev/fb0";
124 return GP_BackendLinuxFBInit(fb
, 1);
127 static void backend_x11_help(FILE *help
, const char *err
)
133 fprintf(help
, "ERROR: %s\n", err
);
135 fprintf(help
, "X11 backend\n"
137 "X11:WxH:[ROOT_WIN]\n\n"
138 "ROOT_WIN - starts the backend in the root window\n"
139 " (w and h, if set, are ignored)\n");
142 static int x11_params_to_flags(const char *param
, GP_Size
*w
, GP_Size
*h
,
143 enum GP_BackendX11Flags
*flags
, FILE *help
)
145 if (!strcasecmp(param
, "ROOT_WIN")) {
146 *flags
|= GP_X11_USE_ROOT_WIN
;
151 * Accepts only string with format "intxint" or "intXint"
156 if (sscanf(param
, "%i%*[xX]%i%n", &sw
, &sh
, &n
) == 2 && n
== strlen(param
)) {
162 backend_sdl_help(help
, "X11: Invalid parameters");
167 static GP_Backend
*backend_x11_init(char *params
, const char *caption
,
170 GP_Size w
= 640, h
= 480;
171 enum GP_BackendX11Flags flags
= 0;
174 return GP_BackendX11Init(NULL
, 0, 0, w
, h
, caption
, 0);
182 if (x11_params_to_flags(params
, &w
, &h
, &flags
, help
))
188 if (x11_params_to_flags(params
, &w
, &h
, &flags
, help
))
191 return GP_BackendX11Init(NULL
, 0, 0, w
, h
, caption
, flags
);
198 static const char *backend_names
[] = {
200 "FB", /* Linux Framebuffer */
201 "X11", /* X11 window system */
205 static GP_Backend
*(*backend_inits
[])(char *, const char *, FILE *) = {
212 static void (*backend_helps
[])(FILE *help
, const char *err
) = {
219 static void print_help(FILE *help
, char *err
)
227 fprintf(help
, "ERROR: %s\n", err
);
231 fprintf(help
, "Backends usage\n"
232 "--------------\n\n");
234 for (i
= 0; backend_helps
[i
] != NULL
; i
++) {
235 backend_helps
[i
](help
, NULL
);
240 static int get_backend(const char *name
)
244 for (i
= 0; backend_names
[i
] != 0; i
++)
245 if (!strcasecmp(name
, backend_names
[i
]))
251 static GP_Backend
*init_backend(const char *name
, char *params
,
252 const char *caption
, FILE *help
)
254 int i
= get_backend(name
);
257 GP_DEBUG(1, "Invalid backend name '%s'", name
);
258 print_help(help
, "Invalid backend name");
262 return backend_inits
[i
](params
, caption
, help
);
265 GP_Backend
*GP_BackendInit(const char *params
, const char *caption
, FILE *help
)
267 if (params
== NULL
) {
268 print_help(help
, NULL
);
272 /* parse backend name */
273 int i
, len
= strlen(params
);
274 char buf
[len
+1], *backend_params
= NULL
;
278 for (i
= 0; i
< len
; i
++) {
281 backend_params
= buf
+ i
+ 1;
286 GP_DEBUG(1, "Have backend name '%s' params '%s'", buf
, backend_params
);
288 return init_backend(buf
, backend_params
, caption
, help
);