1 /* graphics_sdl.c -- barebones sdl graphics layer
3 copyright (c) 2008 bryan rittmeyer <bryanr@bryanr.org>
9 - proper image transparency (libsdl-image xpm does not work)
14 2008-06-15 SDL_DOUBLEBUF+SDL_Flip for linux fb. fix FT leaks.
15 2008-06-18 defer initial resize_callback
16 2008-06-21 linux touchscreen
17 2008-07-04 custom rastering
36 #include "font/freetype/font_freetype.h"
44 # define USE_WEBOS_ACCELEROMETER
54 #undef LINUX_TOUCHSCREEN
68 #define OVERLAY_MAX 32
79 #include <SDL/SDL_gfxPrimitives.h>
83 #include <SDL/SDL_ttf.h>
86 #include FT_FREETYPE_H
87 #include <freetype/ftglyph.h>
92 #include <SDL/SDL_image.h>
95 #ifdef LINUX_TOUCHSCREEN
96 /* we use Linux evdev directly for the touchscreen. */
97 #include <sys/types.h>
100 #include <linux/input.h>
106 #include <sys/time.h>
111 /* TODO: union overlay + non-overlay to reduce size */
112 struct graphics_priv;
113 struct graphics_priv {
117 uint32_t video_flags;
124 struct graphics_priv *overlay_parent;
129 struct graphics_priv *overlay_array[OVERLAY_MAX];
131 enum draw_mode_num draw_mode;
133 int resize_callback_initial;
136 struct callback_list *cbl;
138 #ifdef USE_WEBOS_ACCELEROMETER
139 SDL_Joystick *accelerometer;
144 #ifdef LINUX_TOUCHSCREEN
156 struct timeval draw_begin_tv;
157 unsigned long draw_time_peak;
159 struct font_freetype_methods freetype_methods;
166 # define WEBOS_KEY_SHIFT 0x130
167 # define WEBOS_KEY_SYM 0x131
168 # define WEBOS_KEY_ORANGE 0x133
170 # define WEBOS_KEY_MOD_SHIFT 0x1
171 # define WEBOS_KEY_MOD_ORANGE 0x2
172 # define WEBOS_KEY_MOD_SYM 0x4
174 # define WEBOS_KEY_MOD_SHIFT_STICKY 0x11
175 # define WEBOS_KEY_MOD_ORANGE_STICKY 0x22
176 # define WEBOS_KEY_MOD_SYM_STICKY 0x44
178 # ifdef USE_WEBOS_ACCELEROMETER
179 # define WEBOS_ORIENTATION_PORTRAIT 0x1
180 # define WEBOS_ORIENTATION_LANDSCAPE 0x2
183 # define SDL_USEREVENT_CODE_TIMER 0x1
184 # define SDL_USEREVENT_CODE_CALL_CALLBACK 0x2
185 # define SDL_USEREVENT_CODE_IDLE_EVENT 0x4
186 # define SDL_USEREVENT_CODE_WATCH 0x8
187 # ifdef USE_WEBOS_ACCELEROMETER
188 # define SDL_USEREVENT_CODE_ROTATE 0xA
191 struct event_timeout {
207 static struct graphics_priv* the_graphics = NULL;
208 static int quit_event_loop = 0; // quit the main event loop
209 static int the_graphics_count = 0; // count how many graphics objects are created
210 static GPtrArray *idle_tasks = NULL;
211 static pthread_t sdl_watch_thread = 0;
212 static GPtrArray *sdl_watch_list = NULL;
214 static void event_sdl_watch_thread (GPtrArray *);
215 static void event_sdl_watch_startthread(GPtrArray *watch_list);
216 static void event_sdl_watch_stopthread(void);
217 static struct event_watch *event_sdl_add_watch(void *, enum event_watch_cond, struct callback *);
218 static void event_sdl_remove_watch(struct event_watch *);
219 static struct event_timeout *event_sdl_add_timeout(int, int, struct callback *);
220 static void event_sdl_remove_timeout(struct event_timeout *);
221 static struct event_idle *event_sdl_add_idle(int, struct callback *);
222 static void event_sdl_remove_idle(struct event_idle *);
223 static void event_sdl_call_callback(struct callback_list *);
224 # ifdef USE_WEBOS_ACCELEROMETER
225 static unsigned int sdl_orientation_count = 2^16;
226 static char sdl_next_orientation = 0;
229 unsigned char * ft_buffer = NULL;
230 unsigned int ft_buffer_size = 0;
232 struct graphics_font_priv {
240 struct graphics_gc_priv {
241 struct graphics_priv *gr;
253 struct graphics_image_priv {
258 #ifdef LINUX_TOUCHSCREEN
259 static int input_ts_exit(struct graphics_priv *gr);
263 graphics_destroy(struct graphics_priv *gr)
265 dbg(1, "graphics_destroy %p %u\n", gr, gr->overlay_mode);
269 SDL_FreeSurface(gr->screen);
270 gr->overlay_parent->overlay_array[gr->overlay_idx] = NULL;
278 FT_Done_FreeType(gr->library);
280 #ifdef LINUX_TOUCHSCREEN
283 #ifdef USE_WEBOS_ACCELEROMETER
284 SDL_JoystickClose(gr->accelerometer);
298 gc_destroy(struct graphics_gc_priv *gc)
304 gc_set_linewidth(struct graphics_gc_priv *gc, int w)
307 printf("gc_set_linewidth %p %d\n", gc, w);
313 gc_set_dashes(struct graphics_gc_priv *gc, int w, int offset, unsigned char *dash_list, int n)
319 gc_set_foreground(struct graphics_gc_priv *gc, struct color *c)
322 printf("gc_set_foreground: %p %d %d %d %d\n", gc, c->a, c->r, c->g, c->b);
324 gc->fore_r = c->r/256;
325 gc->fore_g = c->g/256;
326 gc->fore_b = c->b/256;
327 gc->fore_a = c->a/256;
331 gc_set_background(struct graphics_gc_priv *gc, struct color *c)
334 printf("gc_set_background: %p %d %d %d %d\n", gc, c->a, c->r, c->g, c->b);
336 gc->back_r = c->r/256;
337 gc->back_g = c->g/256;
338 gc->back_b = c->b/256;
339 gc->back_a = c->a/256;
342 static struct graphics_gc_methods gc_methods = {
350 static struct graphics_gc_priv *gc_new(struct graphics_priv *gr, struct graphics_gc_methods *meth)
352 struct graphics_gc_priv *gc=g_new0(struct graphics_gc_priv, 1);
355 gc->linewidth=1; /* upper layer should override anyway? */
360 #if 0 /* unused by core? */
361 static void image_destroy(struct graphics_image_priv *gi)
364 SDL_FreeSurface(gi->img);
369 static struct graphics_image_methods gi_methods =
375 static struct graphics_image_priv *
376 image_new(struct graphics_priv *gr, struct graphics_image_methods *meth, char *name, int *w, int *h,
377 struct point *hot, int rotation)
380 struct graphics_image_priv *gi;
382 /* FIXME: meth is not used yet.. so gi leaks. at least xpm is small */
384 gi = g_new0(struct graphics_image_priv, 1);
385 gi->img = IMG_Load(name);
388 /* TBD: improves blit performance? */
389 #if !SDL_VERSION_ATLEAST(1,3,0)
390 SDL_SetColorKey(gi->img, SDL_RLEACCEL, gi->img->format->colorkey);
399 /* TODO: debug "colour parse errors" on xpm */
400 dbg(0,"image_new on '%s' failed: %s\n", name, IMG_GetError());
412 image_free(struct graphics_priv *gr, struct graphics_image_priv * gi)
415 SDL_FreeSurface(gi->img);
421 get_text_bbox(struct graphics_priv *gr, struct graphics_font_priv *font, char *text, int dx, int dy, struct point *ret, int estimate)
426 FT_GlyphSlot slot = font->face->glyph; // a small shortcut
438 bbox.xMin = bbox.yMin = 32000;
439 bbox.xMax = bbox.yMax = -32000;
440 FT_Set_Transform( font->face, &matrix, &pen );
441 len=g_utf8_strlen(text, -1);
442 for ( n = 0; n < len; n++ ) {
444 glyph_index = FT_Get_Char_Index(font->face, g_utf8_get_char(p));
445 p=g_utf8_next_char(p);
446 FT_Load_Glyph(font->face, glyph_index, FT_LOAD_DEFAULT );
447 FT_Get_Glyph(font->face->glyph, &glyph);
448 FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_pixels, &glyph_bbox );
449 FT_Done_Glyph(glyph);
450 glyph_bbox.xMin += x >> 6;
451 glyph_bbox.xMax += x >> 6;
452 glyph_bbox.yMin += y >> 6;
453 glyph_bbox.yMax += y >> 6;
454 x += slot->advance.x;
455 y -= slot->advance.y;
456 if ( glyph_bbox.xMin < bbox.xMin )
457 bbox.xMin = glyph_bbox.xMin;
458 if ( glyph_bbox.yMin < bbox.yMin )
459 bbox.yMin = glyph_bbox.yMin;
460 if ( glyph_bbox.xMax > bbox.xMax )
461 bbox.xMax = glyph_bbox.xMax;
462 if ( glyph_bbox.yMax > bbox.yMax )
463 bbox.yMax = glyph_bbox.yMax;
465 if ( bbox.xMin > bbox.xMax ) {
482 draw_polygon(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int count)
484 if ((gr->overlay_parent && !gr->overlay_parent->overlay_enable) || (gr->overlay_parent && gr->overlay_parent->overlay_enable && !gr->overlay_enable) )
493 vx = alloca(count * sizeof(Sint16));
494 vy = alloca(count * sizeof(Sint16));
496 for(i = 0; i < count; i++)
516 printf("draw_polygon: %p %i %d,%d\n", gc, i, p[i].x, p[i].y);
523 raster_aapolygon(gr->screen, count, vx, vy,
524 SDL_MapRGBA(gr->screen->format,
532 raster_polygon(gr->screen, count, vx, vy,
533 SDL_MapRGBA(gr->screen->format,
542 sge_FilledPolygonAlpha(gr->screen, count, vx, vy,
543 SDL_MapRGB(gr->screen->format,
550 sge_AAFilledPolygon(gr->screen, count, vx, vy,
551 SDL_MapRGB(gr->screen->format,
556 sge_FilledPolygon(gr->screen, count, vx, vy,
557 SDL_MapRGB(gr->screen->format,
564 filledPolygonRGBA(gr->screen, vx, vy, count,
565 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
573 draw_rectangle(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int w, int h)
575 if ((gr->overlay_parent && !gr->overlay_parent->overlay_enable) || (gr->overlay_parent && gr->overlay_parent->overlay_enable && !gr->overlay_enable) )
581 printf("draw_rectangle: %d %d %d %d r=%d g=%d b=%d a=%d\n", p->x, p->y, w, h,
582 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
584 if(w > gr->screen->w)
588 if(h > gr->screen->h)
594 raster_rect(gr->screen, p->x, p->y, w, h,
595 SDL_MapRGBA(gr->screen->format,
603 sge_FilledRectAlpha(gr->screen, p->x, p->y, p->x + w, p->y + h,
604 SDL_MapRGB(gr->screen->format,
610 /* no AA -- should use poly instead for that */
611 sge_FilledRect(gr->screen, p->x, p->y, p->x + w, p->y + h,
612 SDL_MapRGB(gr->screen->format,
618 boxRGBA(gr->screen, p->x, p->y, p->x + w, p->y + h,
619 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
626 draw_circle(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int r)
628 if ((gr->overlay_parent && !gr->overlay_parent->overlay_enable) || (gr->overlay_parent && gr->overlay_parent->overlay_enable && !gr->overlay_enable) )
634 if(gc->fore_a != 0xff)
636 dbg(0, "%d %d %d %u %u:%u:%u:%u\n", p->x, p->y, r, gc->linewidth,
637 gc->fore_a, gc->fore_r, gc->fore_g, gc->fore_b);
641 /* FIXME: does not quite match gtk */
643 /* hack for osd compass.. why is this needed!? */
652 raster_aacircle(gr->screen, p->x, p->y, r,
653 SDL_MapRGBA(gr->screen->format,
661 raster_circle(gr->screen, p->x, p->y, r,
662 SDL_MapRGBA(gr->screen->format,
671 sge_FilledCircleAlpha(gr->screen, p->x, p->y, r,
672 SDL_MapRGB(gr->screen->format,
673 gc->fore_r, gc->fore_g, gc->fore_b),
677 sge_AAFilledCircle(gr->screen, p->x, p->y, r,
678 SDL_MapRGB(gr->screen->format,
679 gc->fore_r, gc->fore_g, gc->fore_b));
681 sge_FilledCircle(gr->screen, p->x, p->y, r,
682 SDL_MapRGB(gr->screen->format,
683 gc->fore_r, gc->fore_g, gc->fore_b));
688 aacircleRGBA(gr->screen, p->x, p->y, r,
689 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
691 filledCircleRGBA(gr->screen, p->x, p->y, r,
692 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
700 draw_lines(struct graphics_priv *gr, struct graphics_gc_priv *gc, struct point *p, int count)
702 if ((gr->overlay_parent && !gr->overlay_parent->overlay_enable) || (gr->overlay_parent && gr->overlay_parent->overlay_enable && !gr->overlay_enable) )
707 /* you might expect lines to be simpler than the other shapes.
708 but, that would be wrong. 1 line can generate 1 polygon + 2 circles
709 and even worse, we have to calculate their parameters!
710 go dust off your trigonometry hat.
713 int i, l, x_inc, y_inc, lw;
717 for(i = 0; i < count-1; i++)
720 printf("draw_lines: %p %d %d,%d->%d,%d %d\n", gc, i, p[i].x, p[i].y, p[i+1].x, p[i+1].y, gc->linewidth);
722 for(l = 0; l < lw; l++)
726 if(p[i].x != p[i+1].x)
735 if(p[i].y != p[i+1].y)
749 aalineRGBA(gr->screen, p[i].x + x_inc, p[i].y + y_inc, p[i+1].x + x_inc, p[i+1].y + y_inc,
750 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
752 lineRGBA(gr->screen, p[i].x + x_inc, p[i].y + y_inc, p[i+1].x + x_inc, p[i+1].y + y_inc,
753 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
758 /* sort of based on graphics_opengl.c::draw_lines */
759 /* FIXME: should honor ./configure flag for no fp.
760 this could be 100% integer code pretty easily,
761 except that i am lazy
763 struct point vert[4];
764 int lw = gc->linewidth;
768 for(i = 0; i < count-1; i++)
770 float dx=p[i+1].x-p[i].x;
771 float dy=p[i+1].y-p[i].y;
774 float cx=(p[i+1].x+p[i].x)/2;
775 float cy=(p[i+1].y+p[i].y)/2;
780 int x_lw_adj, y_lw_adj;
787 raster_aaline(gr->screen, p[i].x, p[i].y, p[i+1].x, p[i+1].y,
788 SDL_MapRGBA(gr->screen->format,
796 raster_line(gr->screen, p[i].x, p[i].y, p[i+1].x, p[i+1].y,
797 SDL_MapRGBA(gr->screen->format,
806 sge_LineAlpha(gr->screen, p[i].x, p[i].y, p[i+1].x, p[i+1].y,
807 SDL_MapRGB(gr->screen->format,
808 gc->fore_r, gc->fore_g, gc->fore_b),
812 sge_AALine(gr->screen, p[i].x, p[i].y, p[i+1].x, p[i+1].y,
813 SDL_MapRGB(gr->screen->format,
814 gc->fore_r, gc->fore_g, gc->fore_b));
816 sge_Line(gr->screen, p[i].x, p[i].y, p[i+1].x, p[i+1].y,
817 SDL_MapRGB(gr->screen->format,
818 gc->fore_r, gc->fore_g, gc->fore_b));
823 aalineRGBA(gr->screen, p[i].x, p[i].y, p[i+1].x, p[i+1].y,
824 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
826 lineRGBA(gr->screen, p[i].x, p[i].y, p[i+1].x, p[i+1].y,
827 gc->fore_r, gc->fore_g, gc->fore_b, gc->fore_a);
834 /* there is probably a much simpler way but this works ok */
836 /* FIXME: float + double mixture */
837 /* FIXME: lrint(round())? */
842 y_lw_adj = round((float)lw/2.0);
847 x_lw_adj = round((float)lw/2.0);
852 angle = (M_PI/2.0) - atan(abs(dx)/abs(dy));
853 x_lw_adj = round(sin(angle)*(float)lw/2.0);
854 y_lw_adj = round(cos(angle)*(float)lw/2.0);
855 if((x_lw_adj < 0) || (y_lw_adj < 0))
858 printf(" %d,%d->%d,%d\n", p[i].x, p[i].y, p[i+1].x, p[i+1].y);
859 printf(" lw=%d angle=%f\n", lw, 180.0 * angle / M_PI);
860 printf(" x_lw_adj=%d y_lw_adj=%d\n", x_lw_adj, y_lw_adj);
864 if(p[i+1].x > p[i].x)
866 x_lw_adj = -x_lw_adj;
868 if(p[i+1].y > p[i].y)
870 y_lw_adj = -y_lw_adj;
874 if(((y_lw_adj*y_lw_adj)+(x_lw_adj*x_lw_adj)) != (lw/2)*(lw/2))
877 printf(" %d,%d->%d,%d\n", p[i].x, p[i].y, p[i+1].x, p[i+1].y);
878 printf(" lw=%d angle=%f\n", lw, 180.0 * angle / M_PI);
879 printf(" x_lw_adj=%d y_lw_adj=%d\n", x_lw_adj, y_lw_adj);
883 /* FIXME: draw a circle/square if p[i]==p[i+1]? */
884 /* FIXME: clipping, check for neg values. hoping sdl-gfx does this */
885 vert[0].x = p[i].x + x_lw_adj;
886 vert[0].y = p[i].y - y_lw_adj;
887 vert[1].x = p[i].x - x_lw_adj;
888 vert[1].y = p[i].y + y_lw_adj;
889 vert[2].x = p[i+1].x - x_lw_adj;
890 vert[2].y = p[i+1].y + y_lw_adj;
891 vert[3].x = p[i+1].x + x_lw_adj;
892 vert[3].y = p[i+1].y - y_lw_adj;
894 draw_polygon(gr, gc, vert, 4);
896 /* draw small circles at the ends. this looks better than nothing, and slightly
897 * better than the triangle used by graphics_opengl, but is more expensive.
898 * should have an ifdef/xml attr?
901 /* FIXME: should just draw a half circle */
903 /* now some circular endcaps, if the width is over 2 */
908 draw_circle(gr, gc, &p[i], lw/2);
910 /* we truncate on the divide on purpose, so we don't go outside the line */
911 draw_circle(gr, gc, &p[i+1], lw/2);
920 set_pixel(SDL_Surface *surface, int x, int y, Uint8 r2, Uint8 g2, Uint8 b2, Uint8 a2)
922 if(x<0 || y<0 || x>=surface->w || y>=surface->h) {
926 void *target_pixel = ((Uint8*)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel);
930 switch(surface->format->BytesPerPixel) {
933 SDL_GetRGBA(*(Uint16 *)target_pixel, surface->format, &r1, &g1, &b1, &a1);
934 *(Uint16 *)target_pixel = SDL_MapRGBA(surface->format,
935 (r1*(0xff-a2)/0xff) + (r2*a2/0xff),
936 (g1*(0xff-a2)/0xff) + (g2*a2/0xff),
937 (b1*(0xff-a2)/0xff) + (b2*a2/0xff),
938 a2 + a1*(0xff-a2)/0xff );
943 SDL_GetRGBA(*(Uint32 *)target_pixel, surface->format, &r1, &g1, &b1, &a1);
944 *(Uint32 *)target_pixel = SDL_MapRGBA(surface->format,
945 (r1*(0xff-a2)/0xff) + (r2*a2/0xff),
946 (g1*(0xff-a2)/0xff) + (g2*a2/0xff),
947 (b1*(0xff-a2)/0xff) + (b2*a2/0xff),
948 a2 + a1*(0xff-a2)/0xff );
956 resize_ft_buffer (unsigned int new_size)
958 if (new_size > ft_buffer_size) {
960 ft_buffer = g_malloc (new_size);
961 dbg(1, "old_size(%i) new_size(%i) ft_buffer(%i)\n", ft_buffer_size, new_size, ft_buffer);
962 ft_buffer_size = new_size;
967 display_text_draw(struct font_freetype_text *text,
968 struct graphics_priv *gr, struct graphics_gc_priv *fg,
969 struct graphics_gc_priv *bg, int color, struct point *p)
972 struct font_freetype_glyph *g, **gp;
973 struct color transparent = { 0x0000, 0x0000, 0x0000, 0x0000 };
974 struct color black = { fg->fore_r * 255, fg->fore_g * 255,
975 fg->fore_b * 255, fg->fore_a * 255 };
976 struct color white = { 0xffff, 0xffff, 0xffff, 0xffff };
979 if (COLOR_IS_WHITE(black) && COLOR_IS_BLACK(white)) {
989 } else if (COLOR_IS_BLACK(black) && COLOR_IS_WHITE(white)) {
1000 white.r = bg->fore_r * 255;
1001 white.g = bg->fore_g * 255;
1002 white.b = bg->fore_b * 255;
1003 white.a = bg->fore_a * 255;
1014 i = text->glyph_count;
1019 if (g->w && g->h && bg) {
1020 stride = (g->w + 2) * 4;
1022 resize_ft_buffer(stride * (g->h + 2));
1023 gr->freetype_methods.get_shadow(g, ft_buffer, 32, stride, &white, &transparent);
1025 SDL_Surface *glyph_surface =
1026 SDL_CreateRGBSurfaceFrom(ft_buffer, g->w + 2, g->h + 2,
1029 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
1030 if (glyph_surface) {
1032 r.x = (x + g->x) >> 6;
1033 r.y = (y + g->y) >> 6;
1037 SDL_BlitSurface(glyph_surface, NULL, gr->screen, &r);
1038 SDL_FreeSurface(glyph_surface);
1047 i = text->glyph_count;
1056 resize_ft_buffer(stride * g->h * 4);
1057 gr->freetype_methods.get_glyph(g, ft_buffer, 32,
1059 &white, &transparent);
1060 SDL_Surface *glyph_surface =
1061 SDL_CreateRGBSurfaceFrom(ft_buffer, g->w, g->h, 32,
1063 0x000000ff,0x0000ff00, 0x00ff0000,0xff000000);
1064 if (glyph_surface) {
1066 r.x = (x + g->x) >> 6;
1067 r.y = (y + g->y) >> 6;
1071 SDL_BlitSurface(glyph_surface, NULL, gr->screen,&r);
1072 SDL_FreeSurface(glyph_surface);
1076 resize_ft_buffer(stride * g->h);
1077 gr->freetype_methods.get_glyph(g, ft_buffer, 32, stride,
1081 unsigned char* pGlyph = ft_buffer;
1082 for (jj = 0; jj < g->h; ++jj) {
1083 for (ii = 0; ii < g->w; ++ii) {
1084 if(*(pGlyph+3) > 0) {
1085 set_pixel(gr->screen,
1086 ii+((x + g->x) >> 6),
1087 jj+((y + g->y) >> 6),
1088 *(pGlyph+2), // Pixels are in BGRA format
1105 draw_text(struct graphics_priv *gr, struct graphics_gc_priv *fg,
1106 struct graphics_gc_priv *bg, struct graphics_font_priv *font,
1107 char *text, struct point *p, int dx, int dy)
1109 if ((gr->overlay_parent && !gr->overlay_parent->overlay_enable)
1110 || (gr->overlay_parent && gr->overlay_parent->overlay_enable
1111 && !gr->overlay_enable)) {
1115 struct font_freetype_text *t;
1119 dbg(0, "no font, returning\n");
1122 t = gr->freetype_methods.text_new(text,
1123 (struct font_freetype_font *) font,
1130 display_text_draw(t, gr, fg, bg, color, &p_eff);
1131 gr->freetype_methods.text_destroy(t);
1135 draw_image(struct graphics_priv *gr, struct graphics_gc_priv *fg, struct point *p, struct graphics_image_priv *img)
1137 if ((gr->overlay_parent && !gr->overlay_parent->overlay_enable) || (gr->overlay_parent && gr->overlay_parent->overlay_enable && !gr->overlay_enable) )
1150 SDL_BlitSurface(img->img, NULL, gr->screen, &r);
1155 draw_image_warp(struct graphics_priv *gr, struct graphics_gc_priv *fg, struct point *p, int count, char *data)
1161 draw_restore(struct graphics_priv *gr, struct point *p, int w, int h)
1164 printf("draw_restore\n");
1169 background_gc(struct graphics_priv *gr, struct graphics_gc_priv *gc)
1172 printf("background_gc\n");
1178 draw_mode(struct graphics_priv *gr, enum draw_mode_num mode)
1182 unsigned long elapsed;
1184 struct graphics_priv *ov;
1188 if(gr->overlay_mode)
1190 /* will be drawn below */
1195 printf("draw_mode: %d\n", mode);
1199 if(mode == draw_mode_begin)
1201 gettimeofday(&gr->draw_begin_tv, NULL);
1205 if(mode == draw_mode_end)
1207 if((gr->draw_mode == draw_mode_begin) && gr->overlay_enable)
1209 for(i = 0; i < OVERLAY_MAX; i++)
1211 ov = gr->overlay_array[i];
1212 if(ov && ov->overlay_enable)
1214 rect.x = ov->overlay_x;
1215 if(rect.x<0) rect.x += gr->screen->w;
1216 rect.y = ov->overlay_y;
1217 if(rect.y<0) rect.y += gr->screen->h;
1218 rect.w = ov->screen->w;
1219 rect.h = ov->screen->h;
1220 SDL_BlitSurface(ov->screen, NULL,
1226 SDL_Flip(gr->screen);
1229 gettimeofday(&now, NULL);
1230 elapsed = 1000000 * (now.tv_sec - gr->draw_begin_tv.tv_sec);
1231 elapsed += (now.tv_usec - gr->draw_begin_tv.tv_usec);
1232 if(elapsed >= gr->draw_time_peak)
1234 dbg(0, "draw elapsed %u usec\n", elapsed);
1235 gr->draw_time_peak = elapsed;
1240 gr->draw_mode = mode;
1244 static void overlay_disable(struct graphics_priv *gr, int disable)
1246 gr->overlay_enable = !disable;
1247 struct graphics_priv *curr_gr = gr;
1248 if(gr->overlay_parent) {
1249 curr_gr = gr->overlay_parent;
1251 draw_mode(curr_gr,draw_mode_end);
1254 static struct graphics_priv *
1255 overlay_new(struct graphics_priv *gr, struct graphics_methods *meth, struct point *p, int w, int h, int alpha, int wraparound);
1257 static int window_fullscreen(struct window *win, int on)
1259 struct graphics_priv *gr=(struct graphics_priv *)win->priv;
1261 /* Update video flags */
1263 gr->video_flags |= SDL_FULLSCREEN;
1265 gr->video_flags &= ~SDL_FULLSCREEN;
1268 /* Update video mode */
1269 gr->screen = SDL_SetVideoMode(gr->screen->w, gr->screen->h, gr->video_bpp, gr->video_flags);
1270 if(gr->screen == NULL) {
1271 navit_destroy(gr->nav);
1274 callback_list_call_attr_2(gr->cbl, attr_resize, (void *)gr->screen->w, (void *)gr->screen->h);
1280 get_data(struct graphics_priv *this, char const *type)
1282 if(strcmp(type, "window") == 0) {
1284 win=g_new(struct window, 1);
1286 win->fullscreen=window_fullscreen;
1287 win->disable_suspend=NULL;
1294 static void draw_drag(struct graphics_priv *gr, struct point *p)
1297 gr->overlay_x = p->x;
1298 gr->overlay_y = p->y;
1302 static struct graphics_methods graphics_methods = {
1308 NULL /*draw_circle*/,
1325 static struct graphics_priv *
1326 overlay_new(struct graphics_priv *gr, struct graphics_methods *meth, struct point *p, int w, int h,int alpha, int wraparound)
1328 struct graphics_priv *ov;
1329 Uint32 rmask, gmask, bmask, amask;
1332 for(i = 0; i < OVERLAY_MAX; i++)
1334 if(gr->overlay_array[i] == NULL)
1339 if(i == OVERLAY_MAX)
1341 dbg(0, "too many overlays! increase OVERLAY_MAX\n");
1345 dbg(1, "overlay_new %d %d %d %u %u (%x, %x, %x ,%x, %d)\n", i,
1350 gr->screen->format->Rmask,
1351 gr->screen->format->Gmask,
1352 gr->screen->format->Bmask,
1353 gr->screen->format->Amask,
1354 gr->screen->format->BitsPerPixel
1357 ov = g_new0(struct graphics_priv, 1);
1359 switch(gr->screen->format->BitsPerPixel) {
1379 rmask = gr->screen->format->Rmask;
1380 gmask = gr->screen->format->Gmask;
1381 bmask = gr->screen->format->Bmask;
1382 amask = gr->screen->format->Amask;
1385 ov->screen = SDL_CreateRGBSurface(SDL_SWSURFACE,
1387 gr->screen->format->BitsPerPixel,
1388 rmask, gmask, bmask, amask);
1390 ov->overlay_mode = 1;
1391 ov->overlay_enable = 1;
1392 ov->overlay_x = p->x;
1393 ov->overlay_y = p->y;
1394 ov->overlay_parent = gr;
1395 ov->overlay_idx = i;
1396 gr->overlay_array[i] = ov;
1399 struct font_priv *(*font_freetype_new) (void *meth);
1400 font_freetype_new = plugin_get_font_type ("freetype");
1402 if (!font_freetype_new)
1408 font_freetype_new (&ov->freetype_methods);
1410 *meth=graphics_methods;
1413 (struct graphics_font_priv *
1414 (*)(struct graphics_priv *, struct graphics_font_methods *, char *, int,
1415 int)) ov->freetype_methods.font_new;
1416 meth->get_text_bbox = (void *)ov->freetype_methods.get_text_bbox;
1425 #ifdef LINUX_TOUCHSCREEN
1427 #define EVFN "/dev/input/eventX"
1429 static int input_ts_init(struct graphics_priv *gr)
1447 fn[sizeof(EVFN)-2] = '0' + n;
1449 fd = open(fn, O_RDONLY);
1453 ret = ioctl(fd, EVIOCGNAME(64), (void *)name);
1456 printf("input_ts: %s\n", name);
1460 ret = ioctl(fd, EVIOCGID, (void *)&ii);
1464 printf("bustype %04x vendor %04x product %04x version %04x\n",
1471 if((ii.bustype == BUS_USB) &&
1472 (ii.vendor == 0x0eef) &&
1473 (ii.product == 0x0001))
1475 ret = fcntl(fd, F_SETFL, O_NONBLOCK);
1494 /* FIXME: should check all 32 minors */
1507 /* returns 0-based display coordinate for the given ts coord */
1508 static void input_ts_map(int *disp_x, int *disp_y,
1509 uint32_t ts_x, uint32_t ts_y)
1511 /* Dynamix 7" (eGalax TS)
1514 bot left = 1986,1897
1517 calibrate your TS using input_event_dump
1518 and touching all four corners. use the most extreme values.
1521 #define INPUT_TS_LEFT 1978
1522 #define INPUT_TS_RIGHT 48
1523 #define INPUT_TS_TOP 115
1524 #define INPUT_TS_BOT 1870
1527 if(ts_x > INPUT_TS_LEFT)
1529 ts_x = INPUT_TS_LEFT;
1531 if(ts_x < INPUT_TS_RIGHT)
1533 ts_x = INPUT_TS_RIGHT;
1536 ts_x = ts_x - INPUT_TS_RIGHT;
1538 *disp_x = ((DISPLAY_W-1) * ts_x) / (INPUT_TS_LEFT - INPUT_TS_RIGHT);
1539 *disp_x = (DISPLAY_W-1) - *disp_x;
1542 if(ts_y > INPUT_TS_BOT)
1544 ts_y = INPUT_TS_BOT;
1546 if(ts_y < INPUT_TS_TOP)
1548 ts_y = INPUT_TS_TOP;
1551 ts_y = ts_y - INPUT_TS_TOP;
1553 *disp_y = ((DISPLAY_H-1) * ts_y) / (INPUT_TS_BOT - INPUT_TS_TOP);
1554 /* *disp_y = (DISPLAY_H-1) - *disp_y; */
1558 static void input_event_dump(struct input_event *ie)
1560 printf("input_event:\n"
1566 (unsigned int)ie->time.tv_sec,
1574 static int input_ts_exit(struct graphics_priv *gr)
1583 #ifdef USE_WEBOS_ACCELEROMETER
1585 sdl_accelerometer_handler(void* param)
1587 struct graphics_priv *gr = (struct graphics_priv *)param;
1588 int xAxis = SDL_JoystickGetAxis(gr->accelerometer, 0);
1589 int yAxis = SDL_JoystickGetAxis(gr->accelerometer, 1);
1590 int zAxis = SDL_JoystickGetAxis(gr->accelerometer, 2);
1591 unsigned char new_orientation;
1593 dbg(2,"x(%d) y(%d) z(%d) c(%d)\n",xAxis, yAxis, zAxis, sdl_orientation_count);
1595 if (zAxis > -30000) {
1596 if (xAxis < -15000 && yAxis > -5000 && yAxis < 5000)
1597 new_orientation = WEBOS_ORIENTATION_LANDSCAPE;
1598 else if (yAxis > 15000 && xAxis > -5000 && xAxis < 5000)
1599 new_orientation = WEBOS_ORIENTATION_PORTRAIT;
1606 if (new_orientation == sdl_next_orientation) {
1607 if (sdl_orientation_count < 3) sdl_orientation_count++;
1610 sdl_orientation_count = 0;
1611 sdl_next_orientation = new_orientation;
1616 if (sdl_orientation_count == 3 || sdl_next_orientation == 0)
1618 sdl_orientation_count++;
1620 if (new_orientation != gr->orientation) {
1621 dbg(1,"x(%d) y(%d) z(%d) o(%d)\n",xAxis, yAxis, zAxis, new_orientation);
1622 gr->orientation = new_orientation;
1625 SDL_UserEvent userevent;
1627 userevent.type = SDL_USEREVENT;
1628 userevent.code = SDL_USEREVENT_CODE_ROTATE;
1629 userevent.data1 = NULL;
1630 userevent.data2 = NULL;
1632 event.type = SDL_USEREVENT;
1633 event.user = userevent;
1635 SDL_PushEvent (&event);
1641 static gboolean graphics_sdl_idle(void *data)
1643 struct graphics_priv *gr = (struct graphics_priv *)data;
1646 #ifdef LINUX_TOUCHSCREEN
1647 struct input_event ie;
1656 if(the_graphics!=NULL) {
1660 dbg(0,"graphics_idle: graphics not set!\n");
1666 /* generate the initial resize callback, so the gui knows W/H
1668 its unsafe to do this directly inside register_resize_callback;
1669 graphics_gtk does it during Configure, but SDL does not have
1670 an equivalent event, so we use our own flag
1672 if(gr->resize_callback_initial != 0)
1674 callback_list_call_attr_2(gr->cbl, attr_resize, (void *)gr->screen->w, (void *)gr->screen->h);
1675 gr->resize_callback_initial = 0;
1678 #ifdef LINUX_TOUCHSCREEN
1681 ss = read(gr->ts_fd, (void *)&ie, sizeof(ie));
1682 if(ss == sizeof(ie))
1684 /* we (usually) get three events on a touchscreen hit:
1686 code =330 [BTN_TOUCH]
1699 once hit, if the contact point changes, we'll get more
1700 EV_ABS (for 1 or both axes), followed by an EV_SYN.
1705 code =330 [BTN_TOUCH]
1714 if(ie.code == BTN_TOUCH)
1716 gr->ts_hit = ie.value;
1726 gr->ts_x = ie.value;
1728 else if(ie.code == 1)
1730 gr->ts_y = ie.value;
1738 input_ts_map(&p.x, &p.y, gr->ts_x, gr->ts_y);
1740 /* always send MOUSE_MOTION (first) */
1741 callback_list_call_attr_1(gr->cbl, attr_motion, (void *)&p);
1744 callback_list_call_attr_3(gr->cbl, attr_button, (void *)1, (void *)SDL_BUTTON_LEFT, (void *)&p);
1746 else if(gr->ts_hit == 0)
1748 callback_list_call_attr_3(gr->cbl, attr_button, (void *)0, (void *)SDL_BUTTON_LEFT, (void *)&p);
1766 #ifdef USE_WEBOS_ACCELEROMETER
1767 struct callback* accel_cb = NULL;
1768 struct event_timeout* accel_to = NULL;
1769 if (PDL_GetPDKVersion() > 100) {
1770 accel_cb = callback_new_1(callback_cast(sdl_accelerometer_handler), gr);
1771 accel_to = event_add_timeout(200, 1, accel_cb);
1775 unsigned int idle_tasks_idx=0;
1776 unsigned int idle_tasks_cur_priority=0;
1777 struct idle_task *task;
1779 while(!quit_event_loop)
1786 if(idle_tasks->len > 0)
1788 while (!(ret = SDL_PollEvent(&ev)) && idle_tasks->len > 0)
1790 if (idle_tasks_idx >= idle_tasks->len)
1793 dbg(3,"idle_tasks_idx(%d)\n",idle_tasks_idx);
1794 task = (struct idle_task *)g_ptr_array_index(idle_tasks,idle_tasks_idx);
1796 if (idle_tasks_idx == 0) // only execute tasks with lowest priority value
1797 idle_tasks_cur_priority = task->priority;
1798 if (task->priority > idle_tasks_cur_priority)
1802 callback_call_0(task->cb);
1807 if (!ret) // If we get here there are no idle_tasks and we have no events pending
1808 ret = SDL_WaitEvent(&ev);
1810 ret = SDL_PollEvent(&ev);
1818 dbg(2,"SDL_Event %d\n", ev.type);
1822 case SDL_MOUSEMOTION:
1826 callback_list_call_attr_1(gr->cbl, attr_motion, (void *)&p);
1832 memset(keybuf, 0, sizeof(keybuf));
1833 switch(ev.key.keysym.sym)
1837 keybuf[0] = NAVIT_KEY_LEFT;
1842 keybuf[0] = NAVIT_KEY_RIGHT;
1845 case SDLK_BACKSPACE:
1847 keybuf[0] = NAVIT_KEY_BACKSPACE;
1852 keybuf[0] = NAVIT_KEY_RETURN;
1857 keybuf[0] = NAVIT_KEY_DOWN;
1862 keybuf[0] = NAVIT_KEY_ZOOM_OUT;
1867 keybuf[0] = NAVIT_KEY_UP;
1872 keybuf[0] = NAVIT_KEY_ZOOM_IN;
1876 case WEBOS_KEY_SHIFT:
1878 if ((key_mod & WEBOS_KEY_MOD_SHIFT_STICKY) == WEBOS_KEY_MOD_SHIFT_STICKY)
1879 key_mod &= ~(WEBOS_KEY_MOD_SHIFT_STICKY);
1880 else if ((key_mod & WEBOS_KEY_MOD_SHIFT) == WEBOS_KEY_MOD_SHIFT)
1881 key_mod |= WEBOS_KEY_MOD_SHIFT_STICKY;
1883 key_mod |= WEBOS_KEY_MOD_SHIFT;
1886 case WEBOS_KEY_ORANGE:
1888 if ((key_mod & WEBOS_KEY_MOD_ORANGE_STICKY) == WEBOS_KEY_MOD_ORANGE_STICKY)
1889 key_mod &= ~(WEBOS_KEY_MOD_ORANGE_STICKY);
1890 else if ((key_mod & WEBOS_KEY_MOD_ORANGE) == WEBOS_KEY_MOD_ORANGE)
1891 key_mod |= WEBOS_KEY_MOD_ORANGE_STICKY;
1893 key_mod |= WEBOS_KEY_MOD_ORANGE;
1898 /* Toggle the on-screen keyboard */
1899 //callback_list_call_attr_1(gr->cbl, attr_keyboard_toggle); // Not implemented yet
1902 case PDLK_GESTURE_BACK:
1904 keybuf[0] = NAVIT_KEY_BACK;
1907 case PDLK_GESTURE_FORWARD:
1908 case PDLK_GESTURE_AREA:
1916 if (ev.key.keysym.unicode < 0x80 && ev.key.keysym.unicode > 0) {
1917 keybuf[0] = (char)ev.key.keysym.unicode;
1918 if ((key_mod & WEBOS_KEY_MOD_ORANGE) == WEBOS_KEY_MOD_ORANGE) {
1920 case 'e': keybuf[0] = '1'; break;
1921 case 'r': keybuf[0] = '2'; break;
1922 case 't': keybuf[0] = '3'; break;
1923 case 'd': keybuf[0] = '4'; break;
1924 case 'f': keybuf[0] = '5'; break;
1925 case 'g': keybuf[0] = '6'; break;
1926 case 'x': keybuf[0] = '7'; break;
1927 case 'c': keybuf[0] = '8'; break;
1928 case 'v': keybuf[0] = '9'; break;
1929 case '@': keybuf[0] = '0'; break;
1930 case ',': keybuf[0] = '-'; break;
1931 case 'u': strncpy(keybuf, "ü", sizeof(keybuf)); break;
1932 case 'a': strncpy(keybuf, "ä", sizeof(keybuf)); break;
1933 case 'o': strncpy(keybuf, "ö", sizeof(keybuf)); break;
1934 case 's': strncpy(keybuf, "ß", sizeof(keybuf)); break;
1937 if ((key_mod & WEBOS_KEY_MOD_SHIFT_STICKY) != WEBOS_KEY_MOD_SHIFT_STICKY)
1938 key_mod &= ~(WEBOS_KEY_MOD_SHIFT_STICKY);
1939 if ((key_mod & WEBOS_KEY_MOD_ORANGE_STICKY) != WEBOS_KEY_MOD_ORANGE_STICKY)
1940 key_mod &= ~(WEBOS_KEY_MOD_ORANGE_STICKY);
1943 dbg(0,"Unknown key sym: %x\n", ev.key.keysym.sym);
1946 /* return unicode chars when they can be converted to ascii */
1947 keybuf[0] = ev.key.keysym.unicode<=127 ? ev.key.keysym.unicode : 0;
1953 dbg(2,"key mod: 0x%x\n", key_mod);
1956 dbg(2,"key: %s 0x%x\n", keybuf, keybuf);
1957 callback_list_call_attr_1(gr->cbl, attr_keypress, (void *)keybuf);
1967 case SDL_MOUSEBUTTONDOWN:
1970 printf("SDL_MOUSEBUTTONDOWN %d %d %d %d %d\n",
1980 callback_list_call_attr_3(gr->cbl, attr_button, (void *)1, (void *)(int)ev.button.button, (void *)&p);
1984 case SDL_MOUSEBUTTONUP:
1987 printf("SDL_MOUSEBUTTONUP %d %d %d %d %d\n",
1997 callback_list_call_attr_3(gr->cbl, attr_button, (void *)0, (void *)(int)ev.button.button, (void *)&p);
2004 quit_event_loop = 1;
2005 navit_destroy(gr->nav);
2010 case SDL_VIDEORESIZE:
2013 gr->screen = SDL_SetVideoMode(ev.resize.w, ev.resize.h, gr->video_bpp, gr->video_flags);
2014 if(gr->screen == NULL)
2016 navit_destroy(gr->nav);
2020 callback_list_call_attr_2(gr->cbl, attr_resize, (void *)gr->screen->w, (void *)gr->screen->h);
2029 SDL_UserEvent userevent = ev.user;
2030 dbg(9,"received SDL_USEREVENT type(%x) code(%x)\n",userevent.type,userevent.code);
2031 if (userevent.type != SDL_USEREVENT)
2034 if (userevent.code == PDL_GPS_UPDATE)
2036 struct attr vehicle_attr;
2038 navit_get_attr(gr->nav, attr_vehicle, &vehicle_attr, NULL);
2039 v = vehicle_attr.u.vehicle;
2042 attr.type = attr_pdl_gps_update;
2043 attr.u.data = userevent.data1;
2044 vehicle_set_attr(v, &attr);
2047 else if(userevent.code == SDL_USEREVENT_CODE_TIMER)
2049 struct callback *cb = (struct callback *)userevent.data1;
2050 dbg(1, "SDL_USEREVENT timer received cb(%p)\n", cb);
2051 callback_call_0(cb);
2053 else if(userevent.code == SDL_USEREVENT_CODE_WATCH)
2055 struct callback *cb = (struct callback *)userevent.data1;
2056 dbg(1, "SDL_USEREVENT watch received cb(%p)\n", cb);
2057 callback_call_0(cb);
2059 else if(userevent.code == SDL_USEREVENT_CODE_CALL_CALLBACK)
2061 struct callback_list *cbl = (struct callback_list *)userevent.data1;
2062 dbg(1, "SDL_USEREVENT call_callback received cbl(%p)\n", cbl);
2063 callback_list_call_0(cbl);
2065 else if(userevent.code == SDL_USEREVENT_CODE_IDLE_EVENT) {
2066 dbg(1, "SDL_USEREVENT idle_event received\n");
2068 #ifdef USE_WEBOS_ACCELEROMETER
2069 else if(userevent.code == SDL_USEREVENT_CODE_ROTATE)
2071 dbg(1, "SDL_USEREVENT rotate received\n");
2072 switch(gr->orientation)
2074 case WEBOS_ORIENTATION_PORTRAIT:
2075 gr->screen = SDL_SetVideoMode(gr->real_w, gr->real_h, gr->video_bpp, gr->video_flags);
2076 PDL_SetOrientation(PDL_ORIENTATION_0);
2078 case WEBOS_ORIENTATION_LANDSCAPE:
2079 gr->screen = SDL_SetVideoMode(gr->real_h, gr->real_w, gr->video_bpp, gr->video_flags);
2080 PDL_SetOrientation(PDL_ORIENTATION_270);
2083 if(gr->screen == NULL)
2085 navit_destroy(gr->nav);
2089 callback_list_call_attr_2(gr->cbl, attr_resize, (void *)gr->screen->w, (void *)gr->screen->h);
2094 dbg(1, "unknown SDL_USEREVENT\n");
2102 printf("SDL_Event %d\n", ev.type);
2110 event_sdl_watch_stopthread();
2113 #ifdef USE_WEBOS_ACCELEROMETER
2114 if (PDL_GetPDKVersion() > 100) {
2115 event_remove_timeout(accel_to);
2116 callback_destroy(accel_cb);
2124 static struct graphics_priv *
2125 graphics_sdl_new(struct navit *nav, struct graphics_methods *meth, struct attr **attrs, struct callback_list *cbl)
2127 struct graphics_priv *this=g_new0(struct graphics_priv, 1);
2130 int w=DISPLAY_W,h=DISPLAY_H;
2132 struct font_priv *(*font_freetype_new) (void *meth);
2133 font_freetype_new = plugin_get_font_type ("freetype");
2135 if (!font_freetype_new)
2140 font_freetype_new (&this->freetype_methods);
2145 dbg(1,"Calling SDL_Init\n");
2147 # ifdef USE_WEBOS_ACCELEROMETER
2148 ret = SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_JOYSTICK);
2150 ret = SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
2153 ret = SDL_Init(SDL_INIT_VIDEO);
2157 dbg(0,"SDL_Init failed %d\n", ret);
2163 dbg(1,"Calling PDL_Init(0)\n");
2167 dbg(0,"PDL_Init failed %d\n", ret);
2172 if (! event_request_system("sdl","graphics_sdl_new")) {
2174 if (! event_request_system("glib","graphics_sdl_new")) {
2176 dbg(0,"event_request_system failed");
2181 this->video_bpp = 0;
2182 this->video_flags = SDL_SWSURFACE | SDL_ANYFORMAT | SDL_RESIZABLE;
2184 this->video_bpp = 16;
2185 this->video_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_RESIZABLE;
2188 if ((attr=attr_search(attrs, NULL, attr_w)))
2190 if ((attr=attr_search(attrs, NULL, attr_h)))
2192 if ((attr=attr_search(attrs, NULL, attr_bpp)))
2193 this->video_bpp=attr->u.num;
2194 if ((attr=attr_search(attrs, NULL, attr_flags))) {
2195 if (attr->u.num & 1)
2196 this->video_flags = SDL_SWSURFACE;
2198 if ((attr=attr_search(attrs, NULL, attr_frame))) {
2200 this->video_flags |= SDL_NOFRAME;
2203 this->screen = SDL_SetVideoMode(w, h, this->video_bpp, this->video_flags);
2205 if(this->screen == NULL)
2207 dbg(0,"SDL_SetVideoMode failed\n");
2216 /* Use screen size instead of requested */
2217 w = this->screen->w;
2218 h = this->screen->h;
2220 dbg(0, "using screen %ix%i@%i\n",
2221 this->screen->w, this->screen->h,
2222 this->screen->format->BytesPerPixel * 8);
2223 #ifdef USE_WEBOS_ACCELEROMETER
2225 this->orientation = WEBOS_ORIENTATION_LANDSCAPE;
2230 this->orientation = WEBOS_ORIENTATION_PORTRAIT;
2234 this->accelerometer = SDL_JoystickOpen(0);
2237 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2239 PDL_SetOrientation(PDL_ORIENTATION_0);
2242 SDL_EnableUNICODE(1);
2243 SDL_WM_SetCaption("navit", NULL);
2245 #ifdef LINUX_TOUCHSCREEN
2246 input_ts_init(this);
2247 if(this->ts_fd >= 0)
2249 /* mouse cursor does not always display correctly in Linux FB.
2250 anyway, it is unnecessary w/ a touch screen
2261 *meth=graphics_methods;
2265 (struct graphics_font_priv *
2266 (*)(struct graphics_priv *, struct graphics_font_methods *, char *, int,
2267 int)) this->freetype_methods.font_new;
2268 meth->get_text_bbox = (void*) this->freetype_methods.get_text_bbox;
2273 if(the_graphics!=NULL) {
2274 dbg(0,"graphics_sdl_new: graphics struct already set: %d!\n", the_graphics_count);
2276 the_graphics = this;
2277 the_graphics_count++;
2279 g_timeout_add(G_PRIORITY_DEFAULT+10, graphics_sdl_idle, this);
2282 this->overlay_enable = 1;
2285 if((attr=attr_search(attrs, NULL, attr_antialias)))
2286 this->aa = attr->u.num;
2288 this->resize_callback_initial=1;
2293 /* ---------- SDL Eventhandling ---------- */
2296 sdl_timer_callback(Uint32 interval, void* param)
2298 struct event_timeout *timeout=(struct event_timeout*)param;
2300 dbg(1,"timer(%p) multi(%d) interval(%d) fired\n", param, timeout->multi, interval);
2303 SDL_UserEvent userevent;
2305 userevent.type = SDL_USEREVENT;
2306 userevent.code = SDL_USEREVENT_CODE_TIMER;
2307 userevent.data1 = timeout->cb;
2308 userevent.data2 = NULL;
2310 event.type = SDL_USEREVENT;
2311 event.user = userevent;
2313 SDL_PushEvent (&event);
2315 if (timeout->multi == 0) {
2317 return 0; // cancel timer
2319 return interval; // reactivate timer
2325 event_sdl_main_loop_run(void)
2327 PDL_ScreenTimeoutEnable(PDL_FALSE);
2328 graphics_sdl_idle(NULL);
2329 PDL_ScreenTimeoutEnable(PDL_TRUE);
2333 event_sdl_main_loop_quit(void)
2335 quit_event_loop = 1;
2341 event_sdl_watch_thread (GPtrArray *watch_list)
2343 struct pollfd *pfds = g_new0 (struct pollfd, watch_list->len);
2344 struct event_watch *ew;
2348 for (idx = 0; idx < watch_list->len; idx++ ) {
2349 ew = g_ptr_array_index (watch_list, idx);
2350 g_memmove (&pfds[idx], ew->pfd, sizeof(struct pollfd));
2353 while ((ret = ppoll(pfds, watch_list->len, NULL, NULL)) > 0) {
2354 for (idx = 0; idx < watch_list->len; idx++ ) {
2355 if (pfds[idx].revents == pfds[idx].events) { /* The requested event happened, notify mainloop! */
2356 ew = g_ptr_array_index (watch_list, idx);
2357 dbg(1,"watch(%p) event(%d) encountered\n", ew, pfds[idx].revents);
2360 SDL_UserEvent userevent;
2362 userevent.type = SDL_USEREVENT;
2363 userevent.code = SDL_USEREVENT_CODE_WATCH;
2364 userevent.data1 = ew->cb;
2365 userevent.data2 = NULL;
2367 event.type = SDL_USEREVENT;
2368 event.user = userevent;
2370 SDL_PushEvent (&event);
2381 event_sdl_watch_startthread(GPtrArray *watch_list)
2384 if (sdl_watch_thread)
2385 event_sdl_watch_stopthread();
2388 ret = pthread_create (&sdl_watch_thread, NULL, (void *)event_sdl_watch_thread, (void *)watch_list);
2390 dbg_assert (ret == 0);
2394 event_sdl_watch_stopthread()
2397 if (sdl_watch_thread) {
2398 /* Notify the watch thread that the list of FDs will change */
2399 pthread_kill(sdl_watch_thread, SIGUSR1);
2400 pthread_join(sdl_watch_thread, NULL);
2401 sdl_watch_thread = 0;
2405 static struct event_watch *
2406 event_sdl_add_watch(void *fd, enum event_watch_cond cond, struct callback *cb)
2408 dbg(1,"fd(%d) cond(%x) cb(%x)\n", fd, cond, cb);
2410 event_sdl_watch_stopthread();
2412 if (!sdl_watch_list)
2413 sdl_watch_list = g_ptr_array_new();
2415 struct event_watch *new_ew = g_new0 (struct event_watch, 1);
2416 struct pollfd *pfd = g_new0 (struct pollfd, 1);
2420 /* Modify watchlist here */
2422 case event_watch_cond_read:
2423 pfd->events = POLLIN;
2425 case event_watch_cond_write:
2426 pfd->events = POLLOUT;
2428 case event_watch_cond_except:
2429 pfd->events = POLLERR|POLLHUP;
2433 new_ew->pfd = (struct pollfd*) pfd;
2436 g_ptr_array_add (sdl_watch_list, (gpointer)new_ew);
2438 event_sdl_watch_startthread(sdl_watch_list);
2444 event_sdl_remove_watch(struct event_watch *ew)
2446 dbg(1,"enter %p\n",ew);
2448 event_sdl_watch_stopthread();
2450 g_ptr_array_remove (sdl_watch_list, ew);
2454 if (sdl_watch_list->len > 0)
2455 event_sdl_watch_startthread(sdl_watch_list);
2460 static struct event_timeout *
2461 event_sdl_add_timeout(int timeout, int multi, struct callback *cb)
2463 struct event_timeout * ret = g_new0(struct event_timeout, 1);
2465 dbg (0,"g_new0 failed\n");
2468 dbg(1,"timer(%p) multi(%d) interval(%d) cb(%p) added\n",ret, multi, timeout, cb);
2471 ret->id = SDL_AddTimer(timeout, sdl_timer_callback, ret);
2477 event_sdl_remove_timeout(struct event_timeout *to)
2479 dbg(2,"enter %p\n", to);
2482 /* do not SDL_RemoveTimer if oneshot timer has already fired */
2483 int ret = to->id == 0 ? SDL_TRUE : SDL_RemoveTimer(to->id);
2485 if (ret == SDL_FALSE)
2486 dbg(0,"SDL_RemoveTimer (%p) failed\n", to->id);
2489 dbg(1,"timer(%p) removed\n", to);
2495 /* sort ptr_array by priority, increasing order */
2497 sdl_sort_idle_tasks(gconstpointer parama, gconstpointer paramb)
2499 struct idle_task *a = (struct idle_task *)parama;
2500 struct idle_task *b = (struct idle_task *)paramb;
2501 if (a->priority < b->priority)
2503 if (a->priority > b->priority)
2508 static struct event_idle *
2509 event_sdl_add_idle(int priority, struct callback *cb)
2511 dbg(1,"add idle priority(%d) cb(%p)\n", priority, cb);
2513 struct idle_task *task = g_new0(struct idle_task, 1);
2514 task->priority = priority;
2517 g_ptr_array_add(idle_tasks, (gpointer)task);
2519 if (idle_tasks->len < 2)
2522 SDL_UserEvent userevent;
2524 dbg(1,"poking eventloop because of new idle_events\n");
2526 userevent.type = SDL_USEREVENT;
2527 userevent.code = SDL_USEREVENT_CODE_IDLE_EVENT;
2528 userevent.data1 = NULL;
2529 userevent.data2 = NULL;
2531 event.type = SDL_USEREVENT;
2532 event.user = userevent;
2534 SDL_PushEvent (&event);
2536 else // more than one entry => sort the list
2537 g_ptr_array_sort(idle_tasks, sdl_sort_idle_tasks);
2539 return (struct event_idle *)task;
2543 event_sdl_remove_idle(struct event_idle *task)
2545 dbg(1,"remove task(%p)\n", task);
2546 g_ptr_array_remove(idle_tasks, (gpointer)task);
2552 event_sdl_call_callback(struct callback_list *cbl)
2554 dbg(1,"call_callback cbl(%p)\n",cbl);
2556 SDL_UserEvent userevent;
2558 userevent.type = SDL_USEREVENT;
2559 userevent.code = SDL_USEREVENT_CODE_CALL_CALLBACK;
2560 userevent.data1 = cbl;
2561 userevent.data2 = NULL;
2563 event.type = SDL_USEREVENT;
2564 event.user = userevent;
2566 SDL_PushEvent (&event);
2569 static struct event_methods event_sdl_methods = {
2570 event_sdl_main_loop_run,
2571 event_sdl_main_loop_quit,
2572 event_sdl_add_watch,
2573 event_sdl_remove_watch,
2574 event_sdl_add_timeout,
2575 event_sdl_remove_timeout,
2577 event_sdl_remove_idle,
2578 event_sdl_call_callback,
2581 static struct event_priv *
2582 event_sdl_new(struct event_methods* methods)
2584 idle_tasks = g_ptr_array_new();
2585 *methods = event_sdl_methods;
2589 /* ---------- SDL Eventhandling ---------- */
2596 plugin_register_event_type("sdl", event_sdl_new);
2598 plugin_register_graphics_type("sdl", graphics_sdl_new);