收藏 分销(赏)

Android原生C开发.doc

上传人:仙人****88 文档编号:12006646 上传时间:2025-08-26 格式:DOC 页数:36 大小:427KB 下载积分:10 金币
下载 相关 举报
Android原生C开发.doc_第1页
第1页 / 共36页
Android原生C开发.doc_第2页
第2页 / 共36页


点击查看更多>>
资源描述
Android原生(Native)C开发之一:环境搭建篇 (转)(2009-08-28 16:53:24) 标签:it  原文: Android是基于Linux的操作系统,处理器是ARM的,所以要在Linux或Windows等x86系统上编译Android能运行的程序,你需要一个交叉编译器。 在Linux下面,你可以自已编译一个交叉编译环境,但Windows下面,就比较复杂(也可以在cygwin中编译一个),但你可以选择下载一个现成的交叉编译环境: Windows: Linux: 安装好了之后,将 CodeSourcery编译器的bin目录 (我的是D:\Program Files\CodeSourcery\Sourcery G++ Lite\bin)加入你的PATH环境变量中,就可以开始你的Android Native C开发之旅了,写好一个简单的C程序: #include <stdlib.h> int main(int argc, char** argv) {  printf("hello android!\nI'm %s!\nI like android very much!!!\n", "Martin Foo");    return 0; } 另存成hello.c,进入命令行模式,确保交叉编译器的bin目录,及Android SDK的tools目录在你的系统环境变量的path里面,用如下命令编译: arm-none-linux-gnueabi-gcc -static hello.c -o hello 注意,一定要加上static参数,否则编译好的可能会在Android上不能运行。 启动Android模拟器,用如下命令将文件push到Android模拟器上: adb shell mkdir /dev/sample adb push hello /dev/sample/hello adb shell chmod 777 /dev/sample/hello 先创建 /dev/sample目录,再将编译好的hello上传上去,最后将hello改成可执行的。 再进入命令行模式,进入Android的shell环境: adb shell #cd /dev/sample #./hello 进入 /dev/sample目录,执行hello,运行结果如下图: Android原生(Native)C开发之二:framebuffer篇 (转)(2009-08-28 16:54:02) 标签:it   原文: 如对Android原生(Natvie)C开发还任何疑问,请参阅《Android原生(Native)C开发之一:环境搭建篇》: 虽然现在能通过交叉环境编译程序,并push到Android上执行,但那只是console台程序,是不是有些单调呢?下面就要看如何通过Linux的 framebuffer 技术在Android上画图形,关于Linux的framebuffer技术,这里就不再详细讲解了,请大家google一下。 操作framebuffer的主要步骤如下: 1、打开一个可用的FrameBuffer设备; 2、通过mmap调用把显卡的物理内存空间映射到用户空间; 3、更改内存空间里的像素数据并显示; 4、退出时关闭framebuffer设备。 下面的这个例子简单地用framebuffer画了一个渐变的进度条,代码 framebuf.c 如下: #include <unistd.h> #include <stdio.h> #include <fcntl.h> #include <linux/fb.h> #include <sys/mman.h> inline static unsigned short int make16color(unsigned char r, unsigned char g, unsigned char b) {     return (  (((r >> 3) & 31) << 11) |  (((g >> 2) & 63) << 5)  |   ((b >> 3) & 31)        ); } int main() {     int fbfd = 0;     struct fb_var_screeninfo vinfo;     struct fb_fix_screeninfo finfo;     long int screensize = 0;  char *fbp = 0;     int x = 0, y = 0;     int guage_height = 20, step = 10;     long int location = 0;     // Open the file for reading and writing     fbfd = open("/dev/graphics/fb0", O_RDWR);     if (!fbfd) {         printf("Error: cannot open framebuffer device.\n");         exit(1);     }     printf("The framebuffer device was opened successfully.\n");     // Get fixed screen information     if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {         printf("Error reading fixed information.\n");         exit(2);     }     // Get variable screen information     if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {         printf("Error reading variable information.\n");         exit(3);     }  printf("sizeof(unsigned short) = %d\n", sizeof(unsigned short));     printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );  printf("xoffset:%d, yoffset:%d, line_length: %d\n", vinfo.xoffset, vinfo.yoffset, finfo.line_length );     // Figure out the size of the screen in bytes     screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;;     // Map the device to memory     fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,                        fbfd, 0);     if ((int)fbp == -1) {         printf("Error: failed to map framebuffer device to memory.\n");         exit(4);     }     printf("The framebuffer device was mapped to memory successfully.\n");  //set to black color first  memset(fbp, 0, screensize);     //draw rectangle     y = (vinfo.yres - guage_height) / 2 - 2;       // Where we are going to put the pixel     for (x = step - 2; x < vinfo.xres - step + 2; x++) {         location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +                        (y+vinfo.yoffset) * finfo.line_length;         *((unsigned short int*)(fbp + location)) = 255;     }     y = (vinfo.yres + guage_height) / 2 + 2;       // Where we are going to put the pixel     for (x = step - 2; x < vinfo.xres - step + 2; x++) {         location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +                        (y+vinfo.yoffset) * finfo.line_length;         *((unsigned short int*)(fbp + location)) = 255;     }     x = step - 2;     for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {         location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +                        (y+vinfo.yoffset) * finfo.line_length;         *((unsigned short int*)(fbp + location)) = 255;     }     x = vinfo.xres - step + 2;     for (y = (vinfo.yres - guage_height) / 2 - 2; y < (vinfo.yres + guage_height) / 2 + 2; y++) {         location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +                        (y+vinfo.yoffset) * finfo.line_length;         *((unsigned short int*)(fbp + location)) = 255;     }     // Figure out where in memory to put the pixel     for ( x = step; x < vinfo.xres - step; x++ ) {         for ( y = (vinfo.yres - guage_height) / 2; y < (vinfo.yres + guage_height) / 2; y++ ) {             location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +                        (y+vinfo.yoffset) * finfo.line_length;             if ( vinfo.bits_per_pixel == 32 ) {                 *(fbp + location) = 100;        // Some blue                 *(fbp + location + 1) = 15+(x-100)/2;     // A little green                 *(fbp + location + 2) = 200-(y-100)/5;    // A lot of red                 *(fbp + location + 3) = 0;      // No transparency             } else { //assume 16bpp                 unsigned char b = 255 * x / (vinfo.xres - step);                 unsigned char g = 255;     // (x - 100)/6 A little green                 unsigned char r = 255;    // A lot of red                 unsigned short int t = make16color(r, g, b);                 *((unsigned short int*)(fbp + location)) = t;             }         }   //printf("x = %d, temp = %d\n", x, temp);         //sleep to see it         usleep(200);     }     //clean framebuffer     munmap(fbp, screensize);     close(fbfd);     return 0; } 注意,在Android环境,framebuffer设备不是象linux一样的 /dev/fb0,而是 /dev/graphics/fb0 , fbfd = open("/dev/graphics/fb0", O_RDWR); 打开framebuffer设备,     fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,                        fbfd, 0); 将设备map到一块内存,然后就可以操作这块内存空间来显示你想画的图形了。 最后别忘了关闭设备:     munmap(fbp, screensize);     close(fbfd); 效果图如下: Android原生(Native)C开发之三:鼠标事件篇(捕鼠记)(2009-08-28 16:55:10) 标签:it   原文: 在做SDL至Android的移植时,键盘事件是能正常捕获到,看了SLD的源码,发现用的device是 /dev/tty0,但是鼠标叫是不能成功捕获,总是得到 0,运行命令查看devices时,显示如下:  # cat /proc/bus/input/devices cat /proc/bus/input/devices I: Bus=0000 Vendor=0000 Product=0000 Version=0000 N: Name="qwerty" P: Phys= S: Sysfs=/class/input/input0 U: Uniq= H: Handlers=kbd mouse0 event0 B: EV=2f B: KEY=ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff f fffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe B: REL=3 B: ABS=7 B: SW=1 进入 /dev/input 目录,发现在3个device文件:mice,mouse0,event0,分别 cat这3个文件,发现只有 event0 有反应,如下图: 而且不管是点击鼠标还是按键,都有反应,但显示的是一堆乱码,而且点击鼠标出来的东西要多一点,难道这就是传说是的 touchscreen ?! 为了分析 event0 的返回值,写了一段代码 testmice.c,如下:  #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <linux/input.h> static int event0_fd = -1; struct input_event ev0[64]; //for handling event0, mouse/key/ts static int handle_event0() {     int button = 0, realx = 0, realy = 0, i, rd;     rd = read(event0_fd, ev0, sizeof(struct input_event) * 64);     if ( rd < sizeof(struct input_event) ) return 0;     for (i = 0; i < rd / sizeof(struct input_event); i++) {         printf("", ev0[i].type, ev0[i].code, ev0[i].value);         if (ev0[i].type == 3 && ev0[i].code == 0)             realx = ev0[i].value;         else if (ev0[i].type == 3 && ev0[i].code == 1)             realy = ev0[i].value;         else if (ev0[i].type == 1) {             if (ev0[i].code == 158) {                 //if key esc then exit                 return 0;             }         } else if (ev0[i].type == 0 && ev0[i].code == 0 && ev0[i].value == 0) {             realx = 0, realy = 0;         }         printf("event(%d): type: %d; code: %3d; value: %3d; realx: %3d; realy: %3d\n", i,    ev0[i].type, ev0[i].code, ev0[i].value, realx, realy);     }     return 1; } int main(void) {  int done = 1;     printf("sizeof(struct input_event) = %d\n", sizeof(struct input_event));     event0_fd = open("/dev/input/event0", O_RDWR);     if ( event0_fd < 0 )         return -1;     while ( done ) {   printf("begin handel_event0...\n");         done = handle_event0();   printf("end handel_event0...\n");     }     if ( event0_fd > 0 ) {         close(event0_fd);         event0_fd = -1;     }     return 0; } 用交叉编译器编译好后(编译过程就不再详述,请参见 blog:Android原生(Native)C开发之一:环境搭建篇),push至 emulator后执行后,切换到android 模拟器,在模拟器上点几下mouse,程序就会打出你点击的信息,效果如下,果然能正确得到点击的 mouse pos,如下图: 分析上面的返回值,发现当按下 mouse left button 时,会得到4个事件,2个 type = 3 的事件返回了 pos x, pos y 的值,即mouse click pos, 另外1个 type = 1 的事件是按键事件(keydown),value就是按下的键的key,为0的应该就是 key的release事件,当松开 mouse时,也会得到两个 type = 1, 0 的事件,没有仔细去看它们的返回值,反正已经正确得到了 mosue的事件,下一步就是改SDL的事件驱动源码了... Android原生(Native)C开发之四:SDL移植笔记  (转)(2009-08-28 16:56:38) 标签:it   原文: SDL(Simple DirectMedia Layer)是一套开放源码的跨平台多媒体开发库,使用C语言写成。SDL提供了多种图像、声音、键盘等的实现,可配置性与移植性非常高,开发者可以开发出跨多个平台(Linux、Windows、Mac OS X、Symbian、Widnows Mobiel等嵌入式系统,当然也包括今天要移植的平台:Android)的应用,目前SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用。 目前,SDL的稳定版本是 1.2.13,1.3还在开发中,可以通过SVN得到最新的代码,本次移植以 1.2.13为准,没有测试 1.3版的源码。请从 SDL 的官方网站下载 1.2.13 的源码,文件名为:SDL-1.2.13.zip,并解压,将得到一个 SDL-1.2.13 目录。 在Native编译SDL之前,要先装 Code Sourcery公司的arm交叉编译器,如果是用Windows操作系统,则一定要装 Cygwin(一个在windows上模拟linux的软件),因为在编译时要用到一些 linux命令,具体的步骤请参见:Port SDL/TinySDGL to android with native C,或自已在网上搜一些资料。 因为SDL是用纯C写的一套类库,所以移植性非常好,官方支持的系统有:Linux, Windows, Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX,非官方支持的有:AmigaOS, Dreamcast, Atari, AIX, OSF/Tru64, RISC OS, SymbianOS, and OS/2,而且网上也有人将SDL移植到很多其他嵌入式系统,甚至有人将SDL移植到 Moto A1200,如此强大的可移植性,其架构真是值得好好学习。 现在切入正题,如何为Android量身定做一个 SDL,下面就从视频,音频,输入事件,定时器(video,audio,events[key,mouse],timer),多线程等几个方面来分析: 1.首先讲视频方面,Android是一个定制的Linux操作系统,Linux显示要么用 X11,要么用framebuffer技术,很显然Android并没有集成 X11(也许Android的软件界面是基于X11的?!),那只有唯一的选择:framebuffer! 打开$SDL/src/video目录,可以发现SDL支持多达30多种的视频显示技术,其中包括我们想要的fbcon及directfb,directfb我没有做测试,也许显示效果会比linux自带的fbcon好,有兴趣的朋友可以试一下,成功了别忘了告诉我; 2.再来谈音频,记得一个广告词:没有声音,再好的戏也出不来!可见音频对多媒体应用的重要性。 这次用的是OSS的driver,但用的是dsp及dma的实现,但在打开Android指定的音频文件 /dev/eac 时有误,所以音频这一块只是能编译通过,不能正常运行,正在考虑用ALSA (Advanced Linux Sound Architecture) 替代; 关于OSS大家可以参看IBM的文章: OSS--跨平台的音频接口简介,写得比较详细。 3.输入事件(键盘,鼠标)中的键盘事件不需要任何更改,就能正常处理,用的设备文件是 /dev/tty0, 但鼠标事件却不能正常处理,加上DEBUG_MOUSE发现用的是PS2的鼠标,但其实Android用的不是PS2的鼠标,用的应该是触摸屏(TouchScreen)鼠标,设备文件是 /dev/input/event0,详情请参见本人的blog: Android原生(Native)C开发之三:鼠标事件篇(捕鼠记),经过改动后,基本能实现鼠标的处理; 4.定时器用的是unix的实现; 5.多线程用的是pthread的实现,unix系统都是用pthread来实现多线程的,在 ln demo时别忘了加 -lpthread; 6.加载动态库用的是unix 的 dl库,同样,在ln demo时别忘了加 -ldl。 SDL提供了一个最小化的Makefile:Makefile.minimal,所有的实现都是 dummy,就是一个空的实现,编译能通过,但运行时什么都不能做,根据上面的分析,将 Makefile.minimal 的内容改成如下:  # Makefile to build the SDL library INCLUDE = -I./include CFLAGS  = -g -s -O2 $(INCLUDE) CC  = arm-none-linux-gnueabi-gcc AR  = arm-none-linux-gnueabi-ar RANLIB = arm-none-linux-gnueabi-ranlib CONFIG_H = include/SDL_config.h TARGET  = libSDL.a SOURCES = \  src/*.c \  src/audio/*.c \  src/cdrom/*.c \  src/cpuinfo/*.c \  src/events/*.c \  src/file/*.c \  src/joystick/*.c \  src/stdlib/*.c \  src/thread/*.c \  src/timer/*.c \  src/video/*.c \  src/audio/dsp/*.c \  src/audio/dma/*.c \  src/video/fbcon/*.c \  src/joystick/dummy/*.c \  src/cdrom/dummy/*.c \  src/thread/pthread/*.c \  src/timer/unix/*.c \  src/loadso/dlopen/*.c \ OBJECTS = $(shell echo $(SOURCES) | sed -e 's,\.c,\.o,g') all: $(TARGET) $(TARGET): $(CONFIG_H) $(OBJECTS)  $(AR) crv $@ $^  $(RANLIB) $@ $(CONFIG_H):  cp $(CONFIG_H).default $(CONFIG_H) clean:  rm -f $(TARGET) $(OBJECTS) 最后将$SDL\include\SDL_config_minimal.h的内容改成如下: #ifndef _SDL_config_minimal_h #define _SDL_config_minimal_h #include "SDL_platform.h" #include <stdarg.h> typedef signed char int8_t; typedef unsigned char uint8_t; typedef signed short int16_t; typedef unsigned short uint16_t; typedef signed int int32_t; typedef unsigned int uint32_t; typedef unsigned int size_t; //typedef unsigned long uintptr_t; #define HAVE_LIBC 1 #ifdef  HAVE_LIBC    #define HAVE_ALLOCA_H  1  #define HAVE_SYS_TYPES_H 1  #define HAVE_STDIO_H  1  #define STDC_HEADERS  1  #define HAVE_STDLIB_H  1  #define HAVE_STDARG_H  1  #define HAVE_MALLOC_H  1  #define HAVE_MEMORY_H  1  //#define HAVE_STRING_H   1  //#define HAVE_STRINGS_H  1  #define HAVE_INTTYPES_H  1  #define HAVE_STDINT_H  1  #define HAVE_CTYPE_H  1  #define HAVE_MATH_H   1  //#define HAVE_ICONV_H   1  #define HAVE_SIGNAL_H  1  #define HAVE_ALTIVEC_H  1    #define HAVE_MALLOC   1  #define HAVE_CALLOC   1  #define HAVE_REALLOC  1  #define HAVE_FREE   1  #define HAVE_ALLOCA   1  #define HAVE_GETENV   1  #define HAVE_PUTENV   1  #define HAVE_UNSETENV  1  #define HAVE_QSORT   1  #define HAVE_ABS   1  //#define HAVE_BCOPY   1  //#define HAVE_MEMSET   1  //#define HAVE_MEMCPY   1  //#define HAVE_MEMMOVE   1  //#define HAVE_MEMCMP   1  //#define HAVE_STRLEN   1  //#define HAVE_STRLCPY   1  //#define HAVE_STRLCAT   1  //#define HAVE_STRDUP   1  #define HAVE__STRREV  1  #define HAVE__STRUPR  1  #define HAVE__STRLWR  1  //#define HAVE_INDEX   1  #define HAVE_RINDEX   1  //#define HAVE_STRCHR   1  #define HAVE_STRRCHR  1  #define HAVE_STRSTR   1  #define HAVE_ITOA   1  #define HAVE__LTOA   1  #define HAVE__UITOA   1  #define HAVE__ULTOA   1  #define HAVE_STRTOL   1  #define HAVE_STRTOUL  1  #define HAVE__I64TOA  1  #define HAVE__UI64TOA  1  #define HAVE_STRTOLL  1  #define HAVE_STRTOULL  1  #define HAVE_STRTOD   1  #define HAVE_ATOI   1  #define HAVE_ATOF   1  #define HAVE_STRCMP   1  #define HAVE_STRNCMP  1  #define HAVE__STRICMP  1  #define HAVE_STRCASECMP  1  #define HAVE__STRNICMP  1  #define HAVE_STRNCASECMP 1  #define HAVE_SSCANF   1  #define HAVE_SNPRINTF  1  #define HAVE_VSNPRINTF  1  //#define HAVE_ICONV  #define HAVE_SIGACTION  1  #define HAVE_SETJMP   1  #define HAVE_NANOSLEEP  1  //#define HAVE_CLOCK_GETTIME 1  #define HAVE_DLVSYM   1  #def
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服