void* gr_loadFont(const char* fontName) { int fd; GRFont *font = 0; GGLSurface *ftex; unsigned char *bits, *rle; unsigned char *in, data; unsigned width, height; unsigned element; fd = open(fontName, O_RDONLY); if (fd == -1) { char tmp[128]; #ifndef TW_HAS_LANDSCAPE sprintf(tmp, "/res/fonts/%s.dat", fontName); #else if(gr_get_rotation()%180 == 0) sprintf(tmp, "/res/fonts/%s.dat", fontName); else sprintf(tmp, "/res/landscape/fonts/%s.dat", fontName); #endif fd = open(tmp, O_RDONLY); if (fd == -1) return NULL; } font = calloc(sizeof(*font), 1); ftex = &font->texture; read(fd, &width, sizeof(unsigned)); read(fd, &height, sizeof(unsigned)); read(fd, font->offset, sizeof(unsigned) * 96); font->offset[96] = width; bits = malloc(width * height); memset(bits, 0, width * height); unsigned pos = 0; while (pos < width * height) { int bit; read(fd, &data, 1); for (bit = 0; bit < 8; bit++) { if (data & (1 << (7-bit))) bits[pos++] = 255; else bits[pos++] = 0; if (pos == width * height) break; } } close(fd); ftex->version = sizeof(*ftex); ftex->width = width; ftex->height = height; ftex->stride = width; ftex->data = (void*) bits; ftex->format = GGL_PIXEL_FORMAT_A_8; font->cheight = height; font->ascent = height - 2; return (void*) font; }
int res_create_surface_png(const char* name, gr_surface* pSurface) { GGLSurface* surface = NULL; int result = 0; unsigned char header[8]; png_structp png_ptr = NULL; png_infop info_ptr = NULL; FILE* fp = fopen(name, "rb"); if (fp == NULL) { char resPath[256]; if (gr_get_rotation() % 180 == 0) snprintf(resPath, sizeof(resPath)-1, "/res/portrait/%s.png", name); else snprintf(resPath, sizeof(resPath)-1, "/res/landscape/%s.png", name); resPath[sizeof(resPath)-1] = '\0'; fp = fopen(resPath, "rb"); if (fp == NULL) { result = -1; goto exit; } } size_t bytesRead = fread(header, 1, sizeof(header), fp); if (bytesRead != sizeof(header)) { result = -2; goto exit; } if (png_sig_cmp(header, 0, sizeof(header))) { result = -3; goto exit; } png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { result = -4; goto exit; } info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { result = -5; goto exit; } if (setjmp(png_jmpbuf(png_ptr))) { result = -6; goto exit; } png_set_packing(png_ptr); png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, sizeof(header)); png_read_info(png_ptr, info_ptr); size_t width = info_ptr->width; size_t height = info_ptr->height; size_t stride = 4 * width; size_t pixelSize = stride * height; int color_type = info_ptr->color_type; int bit_depth = info_ptr->bit_depth; int channels = info_ptr->channels; if (!(bit_depth == 8 && ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) || (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) || (channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE)))) { result = -7; goto exit; } surface = malloc(sizeof(GGLSurface) + pixelSize); if (surface == NULL) { result = -8; goto exit; } unsigned char* pData = (unsigned char*) (surface + 1); surface->version = sizeof(GGLSurface); surface->width = width; surface->height = height; surface->stride = width; /* Yes, pixels, not bytes */ surface->data = pData; surface->format = (channels == 3) ? GGL_PIXEL_FORMAT_RGBX_8888 : GGL_PIXEL_FORMAT_RGBA_8888; if (color_type == PNG_COLOR_TYPE_PALETTE) { png_set_palette_to_rgb(png_ptr); } int y; if (channels < 4) { for (y = 0; y < (int) height; ++y) { unsigned char* pRow = pData + y * stride; png_read_row(png_ptr, pRow, NULL); int x; for(x = width - 1; x >= 0; x--) { int sx = x * 3; int dx = x * 4; unsigned char r = pRow[sx]; unsigned char g = pRow[sx + 1]; unsigned char b = pRow[sx + 2]; unsigned char a = 0xff; pRow[dx ] = r; // r pRow[dx + 1] = g; // g pRow[dx + 2] = b; // b pRow[dx + 3] = a; } } } else { for (y = 0; y < (int) height; ++y) { unsigned char* pRow = pData + y * stride; png_read_row(png_ptr, pRow, NULL); } } *pSurface = (gr_surface) surface; exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); if (fp != NULL) { fclose(fp); } if (result < 0) { if (surface) { free(surface); } } return result; }
/* Returns non-zero when the event should be consumed */ static int vk_modify(struct ev *e, struct input_event *ev) { static int downX = -1, downY = -1; static int discard = 0; static int last_virt_key = 0; static int lastWasSynReport = 0; static int touchReleaseOnNextSynReport = 0; static int use_tracking_id_negative_as_touch_release = 0; // On some devices, type: 3 code: 39 value: -1, aka EV_ABS ABS_MT_TRACKING_ID -1 indicates a true touch release int i; int x, y; // This is used to ditch useless event handlers, like an accelerometer if (e->ignored) return 1; if (ev->type == EV_REL && ev->code == REL_Z) { // This appears to be an accelerometer or another strange input device. It's not the touchscreen. #ifdef _EVENT_LOGGING printf("EV: Device disabled due to non-touchscreen messages.\n"); #endif e->ignored = 1; return 1; } #ifdef _EVENT_LOGGING printf("EV: %s => type: %x code: %x value: %d\n", e->deviceName, ev->type, ev->code, ev->value); #endif // Handle keyboard events, value of 1 indicates key down, 0 indicates key up if (ev->type == EV_KEY) { return 0; } if (ev->type == EV_ABS) { switch (ev->code) { case ABS_X: //00 e->p.synced |= 0x01; e->p.x = ev->value; #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value); #endif break; case ABS_Y: //01 e->p.synced |= 0x02; e->p.y = ev->value; #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value); #endif break; case ABS_MT_POSITION: //2a e->mt_p.synced = 0x03; if (ev->value == (1 << 31)) { e->mt_p.x = 0; e->mt_p.y = 0; lastWasSynReport = 1; } else { lastWasSynReport = 0; e->mt_p.x = (ev->value & 0x7FFF0000) >> 16; e->mt_p.y = (ev->value & 0xFFFF); } break; case ABS_MT_TOUCH_MAJOR: //30 if (ev->value == 0) { #ifndef TW_IGNORE_MAJOR_AXIS_0 // We're in a touch release, although some devices will still send positions as well e->mt_p.x = 0; e->mt_p.y = 0; touchReleaseOnNextSynReport = 1; #endif } #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value); #endif break; case ABS_MT_PRESSURE: //3a if (ev->value == 0) { // We're in a touch release, although some devices will still send positions as well e->mt_p.x = 0; e->mt_p.y = 0; touchReleaseOnNextSynReport = 1; } #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value); #endif break; case ABS_MT_POSITION_X: //35 e->mt_p.synced |= 0x01; e->mt_p.x = ev->value; #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value); #endif break; case ABS_MT_POSITION_Y: //36 e->mt_p.synced |= 0x02; e->mt_p.y = ev->value; #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value); #endif break; case ABS_MT_TOUCH_MINOR: //31 #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value); #endif break; case ABS_MT_WIDTH_MAJOR: //32 #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value); #endif break; case ABS_MT_WIDTH_MINOR: //33 #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value); #endif break; case ABS_MT_TRACKING_ID: //39 if (ev->value < 0) { e->mt_p.x = 0; e->mt_p.y = 0; touchReleaseOnNextSynReport = 2; use_tracking_id_negative_as_touch_release = 1; #ifdef _EVENT_LOGGING if (use_tracking_id_negative_as_touch_release) printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n"); #endif } #ifdef _EVENT_LOGGING printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value); #endif break; #ifdef _EVENT_LOGGING // These are for touch logging purposes only case ABS_MT_ORIENTATION: //34 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value); return 1; break; case ABS_MT_TOOL_TYPE: //37 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value); return 1; break; case ABS_MT_BLOB_ID: //38 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value); return 1; break; case ABS_MT_DISTANCE: //3b printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value); return 1; break; #endif default: // This is an unhandled message, just skip it return 1; } if (ev->code != ABS_MT_POSITION) { lastWasSynReport = 0; return 1; } } // Check if we should ignore the message if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT))) { lastWasSynReport = 0; return 0; } #ifdef _EVENT_LOGGING if (ev->type == EV_SYN && ev->code == SYN_REPORT) printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName); if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT) printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName); #endif // Discard the MT versions if (ev->code == SYN_MT_REPORT) return 0; if (((lastWasSynReport == 1 || touchReleaseOnNextSynReport == 1) && !use_tracking_id_negative_as_touch_release) || (use_tracking_id_negative_as_touch_release && touchReleaseOnNextSynReport == 2)) { // Reset the value touchReleaseOnNextSynReport = 0; // We are a finger-up state if (!discard) { // Report the key up ev->type = EV_ABS; ev->code = 0; ev->value = (downX << 16) | downY; } downX = -1; downY = -1; if (discard) { discard = 0; // Send the keyUp event ev->type = EV_KEY; ev->code = last_virt_key; ev->value = 0; } return 0; } lastWasSynReport = 1; // Retrieve where the x,y position is if (e->p.synced & 0x03) { vk_tp_to_screen(&e->p, &x, &y); } else if (e->mt_p.synced & 0x03) { vk_tp_to_screen(&e->mt_p, &x, &y); } else { // We don't have useful information to convey return 1; } #ifdef RECOVERY_TOUCHSCREEN_SWAP_XY x ^= y; y ^= x; x ^= y; #endif #ifdef RECOVERY_TOUCHSCREEN_FLIP_X x = gr_fb_width() - x; #endif #ifdef RECOVERY_TOUCHSCREEN_FLIP_Y y = gr_fb_height() - y; #endif #ifdef TW_HAS_LANDSCAPE switch(gr_get_rotation()) { case 0: default: break; case 90: #ifdef RECOVERY_TOUCHSCREEN_FLIP_X x = gr_fb_width() - x; #endif #ifdef RECOVERY_TOUCHSCREEN_FLIP_Y y = gr_fb_height() - y; #endif x ^= y; y ^= x; x ^= y; #ifndef RECOVERY_TOUCHSCREEN_FLIP_X y = gr_fb_height() - y; #endif break; case 180: y = gr_fb_height() - y; x = gr_fb_width() - x; break; case 270: #ifdef RECOVERY_TOUCHSCREEN_FLIP_X x = gr_fb_width() - x; #endif #ifdef RECOVERY_TOUCHSCREEN_FLIP_Y y = gr_fb_height() - y; #endif x ^= y; y ^= x; x ^= y; #ifdef RECOVERY_TOUCHSCREEN_FLIP_X y = gr_fb_height() - y; #endif #ifndef RECOVERY_TOUCHSCREEN_FLIP_Y x = gr_fb_width() - x; #endif break; } #endif #ifdef _EVENT_LOGGING printf("EV: x: %d y: %d\n", x, y); #endif // Clear the current sync states e->p.synced = e->mt_p.synced = 0; // If we have nothing useful to report, skip it if (x == -1 || y == -1) return 1; // On first touch, see if we're at a virtual key if (downX == -1) { // Attempt mapping to virtual key for (i = 0; i < e->vk_count; ++i) { int xd = ABS(e->vks[i].centerx - x); int yd = ABS(e->vks[i].centery - y); if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2) { ev->type = EV_KEY; ev->code = e->vks[i].scancode; ev->value = 1; last_virt_key = e->vks[i].scancode; vibrate(VIBRATOR_TIME_MS); // Mark that all further movement until lift is discard, // and make sure we don't come back into this area discard = 1; downX = 0; return 0; } } } // If we were originally a button press, discard this event if (discard) { return 1; } // Record where we started the touch for deciding if this is a key or a scroll downX = x; downY = y; ev->type = EV_ABS; ev->code = 1; ev->value = (x << 16) | y; return 0; }
int res_create_surface_jpg(const char* name, gr_surface* pSurface) { GGLSurface* surface = NULL; int result = 0; struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE* fp = fopen(name, "rb"); if (fp == NULL) { char resPath[256]; if (gr_get_rotation() % 180 == 0) snprintf(resPath, sizeof(resPath)-1, "/res/portrait/%s", name); else snprintf(resPath, sizeof(resPath)-1, "/res/landscape/%s", name); resPath[sizeof(resPath)-1] = '\0'; fp = fopen(resPath, "rb"); if (fp == NULL) { result = -1; goto exit; } } cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); /* Specify data source for decompression */ jpeg_stdio_src(&cinfo, fp); /* Read file header, set default decompression parameters */ if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) goto exit; /* Start decompressor */ (void) jpeg_start_decompress(&cinfo); size_t width = cinfo.image_width; size_t height = cinfo.image_height; size_t stride = 4 * width; size_t pixelSize = stride * height; surface = malloc(sizeof(GGLSurface) + pixelSize); if (surface == NULL) { result = -8; goto exit; } unsigned char* pData = (unsigned char*) (surface + 1); surface->version = sizeof(GGLSurface); surface->width = width; surface->height = height; surface->stride = width; /* Yes, pixels, not bytes */ surface->data = pData; surface->format = GGL_PIXEL_FORMAT_RGBX_8888; int y; for (y = 0; y < (int) height; ++y) { unsigned char* pRow = pData + y * stride; jpeg_read_scanlines(&cinfo, &pRow, 1); int x; for(x = width - 1; x >= 0; x--) { int sx = x * 3; int dx = x * 4; unsigned char r = pRow[sx]; unsigned char g = pRow[sx + 1]; unsigned char b = pRow[sx + 2]; unsigned char a = 0xff; pRow[dx ] = r; // r pRow[dx + 1] = g; // g pRow[dx + 2] = b; // b pRow[dx + 3] = a; } } *pSurface = (gr_surface) surface; exit: if (fp != NULL) { if (surface) { (void) jpeg_finish_decompress(&cinfo); if (result < 0) { free(surface); } } jpeg_destroy_decompress(&cinfo); fclose(fp); } return result; }