<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>芷菁博客 &#187; GTK</title>
	<atom:link href="http://www.stars625.com/tag/gtk/feed" rel="self" type="application/rss+xml" />
	<link>http://www.stars625.com</link>
	<description>记录生活点滴，分享学习体会，专注微嵌开发。</description>
	<lastBuildDate>Sun, 18 Jul 2010 02:55:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>安装GTK+/DirectFB图形环境</title>
		<link>http://www.stars625.com/gtkdfb.html</link>
		<comments>http://www.stars625.com/gtkdfb.html#comments</comments>
		<pubDate>Wed, 26 Aug 2009 15:45:42 +0000</pubDate>
		<dc:creator>stars_625</dc:creator>
				<category><![CDATA[学习分享]]></category>
		<category><![CDATA[DirectFB]]></category>
		<category><![CDATA[GTK]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.stars625.com/?p=155</guid>
		<description><![CDATA[在学习了一段时间的GTK+图形编程后，考虑是否可以将该环境移植到嵌入式系统上，在网上查阅了相关资料后，确定了可行性，本文就是介绍了在虚拟机X86平台Linux系统下配置GTK+图形显示环境的过程。]]></description>
			<content:encoded><![CDATA[<p>一般的嵌入式系统使用MiniGUI、Qt/E、uC/GUI等图形环境，它们都是专业的面向嵌入式系统，经过专门裁剪后的集成包，相对比较成熟、安装方便，不过大部份都是商业版，在网上查阅了相关资料后，发现在嵌入式系统上构建GTK+的图形环境是完全可行的，但是整个环境由很多个源码包组成，依赖复杂，难度相对较大。</p>
<p>Linux系统常用的图形接口是FrameBuffer帧缓冲，实现了显存映射，生成了对应的/dev/fbX文件供用户操作，这需要内核打开相关选项。</p>
<p>本次实验在虚拟机上X86平台进行，可以为我们的第一次尝试去除交叉编译等不必要的麻烦，抓住主要矛盾。这里使用VirtualBox虚拟机，VirtualBox是目前个人感觉最好的虚拟机，比较简洁，不会给系统安装一大撮的服务；在上面安装了Red Hat Enterprise Linux AS4.0，使用AS4企业版，只因为一直使用的这个版本，感觉比较稳定，只安装基本的字符模式和开发包，差不多十分钟就能安装完成。</p>
<p>此时的系统只有字符界面，可以进行正常的编译、安装等开发工作，过会我们就要在上面实现基本的图形界面。</p>
<p>首先要确保内核打开了FrameBuffer支持选项，并且需要在系统启动参数中添加“vga=0&#215;0314”，代表使用800*600分辨率，如果启动时能看到小企鹅图标，表明内核已经支持FrameBuffer，启动后查看是否有/dev/fb0等设备文件。</p>
<p>在这里首先说一下Linux系统编译安装源码包的注意事项，解开源码包有，首先查看README，看看基本功能介绍等，一般它会诉你如果要安装的话要查看哪个说明文件，一般都是INSTALL，在这个里面我们可以了解这个源码包依赖哪些包，以及编译安装方法，一般都是./configure、make、make install，当然要先安装它所依赖的源码包。</p>
<p>我以前一直不了解./configure里面的“&#8211;prefix”参数有什么大的用处，直接安装到默认路径不就行了吗，这也就给我的这次移植埋下了第一大隐患，在安装包的时候，系统中可能已经有一些早期版本，而早期版本一般都是安装在/usr目录下的，而我们的新版本，由于没有指定安装路径，一般都会安装到/usr/local下，导致版本冲突，调用的时候往往使用的还是老版本的可执行文件或库文件，所以安装一定要注意版本冲突问题。</p>
<p>因为Linux操作系统统一使用pkg-config工具来统一管理头文件路径和库路径，在新安装一个开发包后，会将一个开发包信息脚本放到pkg-config的目录下供其调用，而当系统中安装了不同版本的pkg-config，或者其它原因引起的，会同时存在两个pkg-config目录，一般都是/usr/lib/pkgconfig和/usr/local/lib/pkgconfig目录，而一般情况下系统只能搜寻前面的这个目录，所以有时候会导致，明明安装了那个开发包，却提示说pkg-config找不到，此时需要设置一个环境变量“export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig”就能解决，另外将“/usr/lib”和“/usr/local/lib”这两个路径加入到“/etc/ld.so.conf”并且执行“ldconfig”更新。</p>
<p>好了，如果在安装过程中出现开发包没安装，或库找不到的情况请对照以上两条解决。</p>
<p>首先进行编译安装DirectFB-1.4.1.tar.gz，没什么问题，顺利通过。编写以下测试程序并编译运行，如果能看到一条横线，那就成功了。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;unistd.h&gt;</span>
<span style="color: #339933;">#include &lt;directfb.h&gt;</span>
&nbsp;
<span style="color: #993333;">static</span> IDirectFB <span style="color: #339933;">*</span>dfb <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
<span style="color: #993333;">static</span> IDirectFBSurface <span style="color: #339933;">*</span>primary <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> screen_width  <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #993333;">static</span> <span style="color: #993333;">int</span> screen_height <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">#define DFBCHECK(x...)                                         \
{                                                      	   \
    DFBResult err = x;                                         \
                                                               \
    if (err != DFB_OK)                                         \
    {                                                          \
        fprintf( stderr, &quot;%s &lt;%d&gt;:\n\t&quot;, __FILE__, __LINE__ ); \
        DirectFBErrorFatal( #x, err );                         \
    }                                                          \
}</span>
&nbsp;
<span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	DFBSurfaceDescription dsc<span style="color: #339933;">;</span>
	DFBCHECK <span style="color: #009900;">&#40;</span>DirectFBInit <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	DFBCHECK <span style="color: #009900;">&#40;</span>DirectFBCreate <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>dfb<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	DFBCHECK <span style="color: #009900;">&#40;</span>dfb<span style="color: #339933;">-&gt;</span>SetCooperativeLevel <span style="color: #009900;">&#40;</span>dfb<span style="color: #339933;">,</span> DFSCL_FULLSCREEN<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	dsc.<span style="color: #202020;">flags</span> <span style="color: #339933;">=</span> DSDESC_CAPS<span style="color: #339933;">;</span>
	dsc.<span style="color: #202020;">caps</span>  <span style="color: #339933;">=</span> DSCAPS_PRIMARY <span style="color: #339933;">|</span> DSCAPS_FLIPPING<span style="color: #339933;">;</span>
&nbsp;
	DFBCHECK <span style="color: #009900;">&#40;</span>dfb<span style="color: #339933;">-&gt;</span>CreateSurface<span style="color: #009900;">&#40;</span> dfb<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>dsc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>primary <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	DFBCHECK <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">-&gt;</span>GetSize <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>screen_width<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>screen_height<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	DFBCHECK <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">-&gt;</span>FillRectangle <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> screen_width<span style="color: #339933;">,</span> screen_height<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	DFBCHECK <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">-&gt;</span>SetColor <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">,</span> <span style="color: #208080;">0x80</span><span style="color: #339933;">,</span> <span style="color: #208080;">0x80</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xff</span><span style="color: #339933;">,</span> <span style="color: #208080;">0xff</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	DFBCHECK <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">-&gt;</span>DrawLine <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">,</span>
			                      <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> screen_height <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span>
			       screen_width <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> screen_height <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	DFBCHECK <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">-&gt;</span>Flip <span style="color: #009900;">&#40;</span>primary<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	sleep <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	primary<span style="color: #339933;">-&gt;</span>Release<span style="color: #009900;">&#40;</span> primary <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	dfb<span style="color: #339933;">-&gt;</span>Release<span style="color: #009900;">&#40;</span> dfb <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">23</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>在进行GTK+环境安装的时候出了很多问题，包括开发包的依赖问题，库找不到，undefined symbol: FcDirCacheLoad，mandatory image surface backend feature could not be enabled，提示重新安装FreeType和FontConfig等问题请参考上文方法解决，因为经历过磨难，这里就直接将正确的包的安装顺利和配置参数给出：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">pkg-config-0.23.tar.gz
.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
glib-2.20.4.tar.bz2 require pkg-config
.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
注：此包依赖新版本的pkg-config工具，因此在此之前对系统原有程序升级。
&nbsp;
libpng-1.2.39.tar.bz2
.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
pixman-0.15.20.tar.gz
.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
freetype-2.3.9.tar.bz2
.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
&nbsp;
fontconfig-2.7.1.tar.gz
.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
注：此包依赖FreeType，如果已经安装TreeType但还提示找不到，请根据前文修改。
&nbsp;
cairo-1.8.8.tar.gz
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--enable-directfb</span>=<span style="color: #c20cb9; font-weight: bold;">yes</span> <span style="color: #660033;">--enable-ft</span>=<span style="color: #c20cb9; font-weight: bold;">yes</span> <span style="color: #660033;">--enable-ps</span>=no <span style="color: #660033;">--enable-svg</span>=no
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
注：此包依赖libpng pixman fontconfig，注意Enable DirectFB。
&nbsp;
pango-1.24.5.tar.bz2
.<span style="color: #000000; font-weight: bold;">/</span>configure
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
注：依赖cairo glib
&nbsp;
atk-1.27.90.tar.bz2
.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--with-gtktarget-directfb</span> <span style="color: #660033;">--without-libtiff</span> <span style="color: #660033;">--without-libjpeg</span> <span style="color: #660033;">--without-libjasper</span>
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
注：注意GtkTarget DirectFB。</pre></td></tr></table></div>

<p>至此，开发包都已安装完毕，如果没有出错的话，那么我们就可以写一个GTK+代码进行测试，这里我写了一个最简单的窗体，包括一个按钮，单击按钮退出程序，代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;gtk/gtk.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	GtkWidget <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
	GtkWidget <span style="color: #339933;">*</span>button<span style="color: #339933;">;</span>
&nbsp;
	gtk_init<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>argc<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	window <span style="color: #339933;">=</span> gtk_window_new<span style="color: #009900;">&#40;</span>GTK_WINDOW_TOPLEVEL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	gtk_widget_set_size_request<span style="color: #009900;">&#40;</span>GTK_WIDGET<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">640</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">480</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	button <span style="color: #339933;">=</span> gtk_button_new_with_label<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Click Here&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	g_signal_connect<span style="color: #009900;">&#40;</span>window<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;destroy&quot;</span><span style="color: #339933;">,</span> G_CALLBACK<span style="color: #009900;">&#40;</span>gtk_main_quit<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	g_signal_connect<span style="color: #009900;">&#40;</span>button<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;clicked&quot;</span><span style="color: #339933;">,</span> G_CALLBACK<span style="color: #009900;">&#40;</span>gtk_main_quit<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	gtk_container_add<span style="color: #009900;">&#40;</span>GTK_CONTAINER<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> GTK_WIDGET<span style="color: #009900;">&#40;</span>button<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	gtk_widget_show_all<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	gtk_main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>一切正常的话就会出现如下图，能看到鼠标，但标题栏没有，样子也很难后，颜色也许可以通过RC文件修改，但是在我的虚拟机上显示出的整</p>
<p>个图像好你宽度都变成的1/2，前面的DirectFB测试也是如此。</p>
<div id="attachment_160" class="wp-caption aligncenter" style="width: 586px"><a href="http://www.stars625.com/gtkdfb.html/gtkdfb-2" rel="attachment wp-att-160"><img src="http://www.stars625.com/wp-content/uploads/gtkdfb.jpg" alt="GTK+ DirectFB Pic" title="GTK+ DirectFB" width="576" height="474" class="size-full wp-image-160" /></a><p class="wp-caption-text">GTK+ DirectFB Pic</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.stars625.com/gtkdfb.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
