1.窗口管理器常用数据结构:
Glib库里面的双向链表,用来管理窗口和控件等数据,定义如下:
struct GList
{
gpointer data;
GList *next;
GList *prev;
};
转载网上的一段示例代码(http://keyewan.blog.163.com/blog/static/189827233201110844628516/):
/*
* file: g_list.c
* desc: 这个文件用于演示glib库里双向链表GList的用法
* compile: gcc -o g_list g_list.c `pkg-config --cflags --libs glib-2.0`
*/
#include <glib.h>
void display_list(GList *list)
{
GList *it = NULL;
/* 遍历链表 */
for (it = list; it; it = it->next) {
printf("%s", it->data);
}
printf("/n");
}
int main(int argc, char *argv[])
{
GList *list = NULL;
/*
* 向链表尾部追加节点
*/
list = g_list_append(list, "one ");
list = g_list_append(list, "two ");
list = g_list_append(list, "three ");
list = g_list_append(list, "four ");
list = g_list_append(list, "five ");
display_list(list);
/* 在链表头部插入*/
list = g_list_prepend(list, "zero ");
display_list(list);
GList *it = NULL;
/* 查找链表中的节点 */
it = g_list_find(list, "two ");
printf("Find data "two ": %s/n", it->data);
int index = 0;
/* 确定链表指针指向链表中的第几个节点 */
index = g_list_position(list, it);
printf("index of "two " is: %d/n", index);
it = g_list_nth(list, 1);
printf("%s/n", it->data);
/* 从链表里删除一个节点 */
printf("After remove data "three ", the list:/n");
list = g_list_remove(list, "three ");
display_list(list);
/* 向链表里插入一个节点 */
printf("After insert string "INSERT " at index 3, the list:/n");
list = g_list_insert(list, "INSERT ", 3);
display_list(list);
/* 采用内循环遍历链表节点 */
g_list_foreach(list, (GFunc)printf, NULL);
printf("/n");
GList *last = NULL;
/* 取链表的最后一个节点 */
last = g_list_last(list);
/* 从最后一个节点开始遍历链表 */
for (it = last; it; it = it->prev) {
printf("%s", it->data);
}
printf("/n");
/* 销毁链表 */
g_list_free(list);
return 0;
}
在linux下使用下面的命令编译:gcc -o g_list g_list.c `pkg-config --cflags --libs glib-2.0`
编译后程序运行的结果如下:
$ ./g_list
one two three four five
zero one two three four five
Find data "two ": two
index of "two " is: 2
one
After remove data "three ", the list:
zero one two four five
After insert string "INSERT " at index 3, the list:
zero one two INSERT four five
zero one two INSERT four five
five four INSERT two one zero
现在你应该知道我为什么在每个字符串后面加一个空格了吧。
总结:在glib库中,双向链表的用法跟单向链表基本上是一致的,只是多了一个访问上一个节点的指针prev。对于你的应用如果单向链表够用,就没有必要使用双向链表,因为这里要增加维护一个指针的开销。
---------------------------------------------------------------------------------------------------------------------------------
2.窗口管理器对“窗口”的数据结构定义:
struct _MetaWindow
{
MetaDisplay *display; /*关于窗口的显示,隐藏,选中操作,有点复杂,待续*/
MetaScreen *screen;
MetaWorkspace *workspace;
Window xwindow;