PageRenderTime 689ms CodeModel.GetById 191ms app.highlight 180ms RepoModel.GetById 313ms app.codeStats 1ms

/src/ftk.c

http://ftk.googlecode.com/
C | 456 lines | 351 code | 76 blank | 29 comment | 65 complexity | c07ceabd07717b491f77e3ee7c2dc69e MD5 | raw file
  1/*
  2 * File: ftk.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief: ftk global init, mainloop and deinit functions.  
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk.h"
 33#include "ftk_util.h"
 34#include "ftk_backend.h"
 35#include "ftk_globals.h"
 36#include "ftk_main_loop.h"
 37#include "ftk_status_item.h"
 38#include "ftk_status_panel.h"
 39#include "ftk_bitmap_factory.h"
 40#include "ftk_display_rotate.h"
 41#include "ftk_allocator_default.h"
 42#include "ftk_wnd_manager_default.h"
 43#include "ftk_input_method_preeditor_default.h"
 44
 45#ifdef FTK_MEMORY_PROFILE
 46#include "ftk_allocator_profile.h"
 47#define FTK_ALLOC_PROFILE(a) ftk_allocator_profile_create(a)
 48#else
 49#define FTK_ALLOC_PROFILE(a) a
 50#endif
 51
 52#define quit_if_fail(p, msg) \
 53	if(!(p))\
 54	{\
 55		ftk_deinit();\
 56		ftk_loge(msg);\
 57		exit(0);\
 58	}
 59
 60static void ftk_init_panel(void);
 61
 62static Ret ftk_init_bitmap_factory(void)
 63{
 64	ftk_set_bitmap_factory(ftk_bitmap_factory_create());
 65	quit_if_fail(ftk_default_bitmap_factory(), "Init bitmap factory failed.\n");
 66
 67	return RET_OK;
 68}
 69
 70static Ret ftk_init_theme(const char* theme)
 71{
 72	char filename[FTK_MAX_PATH + 1] = {0};
 73
 74	ftk_set_theme(ftk_theme_create(theme == NULL));
 75
 76	if(theme != NULL)
 77	{
 78#if !defined(ANDROID) && !defined(ANDROID_NDK)
 79		ftk_strs_cat(filename, FTK_MAX_PATH, 
 80			ftk_config_get_data_dir(ftk_default_config()), "/theme/", theme, "/theme.xml", NULL);
 81#else
 82		ftk_strs_cat(filename, FTK_MAX_PATH, 
 83			ftk_config_get_data_dir(ftk_default_config()), "/theme/", theme, "/theme.wav", NULL);
 84#endif
 85		ftk_normalize_path(filename);
 86		ftk_theme_parse_file(ftk_default_theme(), filename);
 87	}
 88	
 89	quit_if_fail(ftk_default_theme(), "Init theme failed.\n");
 90	ftk_set_animation_trigger(ftk_theme_get_animation_trigger(ftk_default_theme()));
 91
 92	return RET_OK;
 93}
 94
 95void ftk_deinit(void)
 96{
 97	if(ftk_default_input_method_preeditor() != NULL)
 98	{
 99		ftk_input_method_preeditor_destroy(ftk_default_input_method_preeditor());
100	}
101
102	if(ftk_default_wnd_manager() != NULL)
103	{
104		ftk_wnd_manager_destroy(ftk_default_wnd_manager());
105	}
106	
107	if(ftk_default_main_loop() != NULL)
108	{
109		ftk_main_loop_destroy(ftk_default_main_loop());
110	}
111
112	if(ftk_default_sources_manager() != NULL)
113	{
114		ftk_sources_manager_destroy(ftk_default_sources_manager());
115	}
116
117	if(ftk_default_bitmap_factory() != NULL)
118	{
119		ftk_bitmap_factory_destroy(ftk_default_bitmap_factory());
120	}
121
122	if(ftk_default_text_layout() != NULL)
123	{
124		ftk_text_layout_destroy(ftk_default_text_layout());
125	}
126
127	if(ftk_default_display() != NULL)
128	{
129		ftk_display_destroy(ftk_default_display());
130	}
131
132	if(ftk_default_theme() != NULL)
133	{
134		ftk_theme_destroy(ftk_default_theme());
135	}
136
137	if(ftk_shared_canvas() != NULL)
138	{
139		ftk_canvas_destroy(ftk_shared_canvas());
140	}
141
142	if(ftk_default_input_method_manager() != NULL)
143	{
144		ftk_input_method_manager_destroy(ftk_default_input_method_manager());
145	}
146
147	if(ftk_default_config() != NULL)
148	{
149		ftk_config_destroy(ftk_default_config());
150	}
151
152	ftk_platform_deinit();
153
154#ifndef USE_STD_MALLOC
155	if(ftk_default_allocator() != NULL)
156	{
157		ftk_allocator_destroy(ftk_default_allocator());
158	}
159#endif
160
161	ftk_logd("%s: ftk exit.\n", __func__);
162
163	ftk_clear_globals();
164
165	return;
166}
167
168#ifndef USE_LINUX_DFB
169static Ret ftk_move_cursor(void* ctx, void* obj)
170{
171	FtkEvent* event = (FtkEvent*)obj;
172
173	if(event->type == FTK_EVT_MOUSE_MOVE)
174	{
175		ftk_sprite_move((FtkSprite*)ctx, event->u.mouse.x, event->u.mouse.y);
176	}
177	else if(event->type == FTK_EVT_DISABLE_CURSOR)
178	{
179		ftk_sprite_show((FtkSprite*)ctx, 0);
180	}
181	else if(event->type == FTK_EVT_ENABLE_CURSOR)
182	{
183		ftk_sprite_show((FtkSprite*)ctx, 1);
184	}
185
186	return RET_OK;
187}
188
189static Ret ftk_enable_curosr(void)
190{
191	FtkSprite* sprite = ftk_sprite_create();
192	FtkBitmap* icon = ftk_theme_load_image(ftk_default_theme(), "cursor"FTK_STOCK_IMG_SUFFIX);
193	
194	ftk_sprite_set_icon(sprite, icon);
195	ftk_sprite_show(sprite, 1);
196	ftk_wnd_manager_add_global_listener(ftk_default_wnd_manager(), ftk_move_cursor, sprite);
197	
198	return RET_OK;
199}
200#endif
201
202Ret ftk_init(int argc, char* argv[])
203{
204	FtkColor bg = {0};
205	FtkConfig* config = NULL;
206	FtkDisplay* display = NULL;
207	static int ftk_inited = 0;
208
209	if(ftk_inited)
210	{
211		return RET_OK;
212	}
213	else
214	{
215		ftk_inited = 1;
216	}
217
218	ftk_clear_globals();
219	PROFILE_START();
220#ifndef USE_STD_MALLOC
221	ftk_set_allocator(FTK_ALLOC_PROFILE(ftk_allocator_default_create()));
222	quit_if_fail(ftk_default_allocator(), "Init allocator failed.\n");
223#endif
224
225	ftk_platform_init(argc, argv);
226	config = ftk_config_create();
227	ftk_set_config(config);
228	ftk_config_init(config, argc, argv);
229	quit_if_fail(ftk_default_config(), "Init config failed.\n");
230
231	PROFILE_END("config init");
232
233	ftk_set_text_layout(ftk_text_layout_create());
234	PROFILE_START();
235	ftk_set_sources_manager(ftk_sources_manager_create(64));
236	ftk_set_main_loop(ftk_main_loop_create(ftk_default_sources_manager()));
237	ftk_set_wnd_manager(ftk_wnd_manager_default_create(ftk_default_main_loop()));
238	quit_if_fail(ftk_default_wnd_manager(), "Init windows manager failed.\n");
239	PROFILE_END("source main loop wnd manager init");
240
241	ftk_init_bitmap_factory();
242	
243	PROFILE_START();
244	ftk_init_theme(ftk_config_get_theme(config));
245	ftk_backend_init(argc, argv);
246	PROFILE_END("theme and backend init");
247
248	display = ftk_display_rotate_create(ftk_default_display(), ftk_config_get_rotate(ftk_default_config()));
249	ftk_set_display(display);
250	quit_if_fail(ftk_default_display(), "Init display failed.\n");
251
252	PROFILE_START();
253	bg.a = 0xff;
254	ftk_set_shared_canvas(ftk_canvas_create(ftk_display_width(display), ftk_display_height(display), &bg));
255	ftk_logd("canvas init: %d %d\n", ftk_display_width(display), ftk_display_height(display));
256	PROFILE_END("canvas init");
257
258	PROFILE_START();
259	ftk_set_input_method_manager(ftk_input_method_manager_create());
260	ftk_set_input_method_preeditor(ftk_input_method_preeditor_default_create());
261	quit_if_fail(ftk_default_input_method_manager(), "Init input method failed.\n");
262	PROFILE_END("input method init");
263
264	PROFILE_START();
265	if(ftk_config_get_enable_status_bar(config))
266	{
267		ftk_init_panel();
268	}
269	PROFILE_END("panel init");
270
271	if(ftk_config_get_enable_cursor(config))
272	{
273#ifndef USE_LINUX_DFB
274		ftk_enable_curosr();
275#endif
276	}
277
278	return RET_OK;
279}
280
281Ret ftk_run(void)
282{
283	Ret ret = ftk_main_loop_run(ftk_default_main_loop());
284
285	ftk_deinit();
286
287	return ret;
288}
289
290void ftk_quit(void)
291{
292	if(ftk_default_main_loop() != NULL)
293	{
294		ftk_main_loop_quit(ftk_default_main_loop());
295		ftk_logd("%s\n", __func__);
296	}
297
298	return;
299}
300
301#define IDC_TITLE_ITEM 1000
302#define IDC_ICON_ITEM  1001
303#define IDC_CLOSE_ITEM 1002
304
305static Ret on_wnd_manager_global_event(void* ctx, void* obj)
306{
307	Ret ret = RET_OK;
308	FtkEvent* event = (FtkEvent*)obj;
309	FtkWidget* panel = NULL;
310	FtkWidget* top_window = NULL;
311	FtkWidget* close_widget = NULL;
312	FtkWidget* title_widget = NULL;
313
314	if(event->type != FTK_EVT_TOP_WND_CHANGED
315		&& event->type != FTK_EVT_WND_CONFIG_CHANGED)
316	{
317		return RET_OK;
318	}
319
320	if(ftk_widget_type((FtkWidget*)event->widget) != FTK_WINDOW)
321	{
322		return RET_OK;
323	}
324
325	panel = ftk_default_status_panel();
326	close_widget = ftk_widget_lookup(panel, IDC_CLOSE_ITEM);
327	title_widget = ftk_widget_lookup(panel, IDC_TITLE_ITEM);
328	switch(event->type)
329	{
330		case FTK_EVT_TOP_WND_CHANGED:
331		{
332			top_window = (FtkWidget*)event->widget;
333
334			if(top_window != NULL)
335			{
336				ftk_widget_set_text(title_widget, ftk_widget_get_text(top_window));
337				ftk_logd("top_window changed: %s\n", ftk_widget_get_text(top_window));
338			}
339			else
340			{
341				ftk_widget_set_text(title_widget, NULL);
342			}
343			ftk_widget_set_user_data(title_widget, NULL, top_window);
344			ret = RET_REMOVE;
345
346			break;
347		}
348		case FTK_EVT_WND_CONFIG_CHANGED:
349		{
350			top_window = (FtkWidget*)ftk_widget_user_data(title_widget);
351			if(top_window == event->widget)
352			{
353				ftk_widget_set_text(title_widget, ftk_widget_get_text(top_window));
354			}
355			
356			ftk_logd("%s: config changed: %p %p\n", __func__, top_window, event->widget);
357			ret = RET_REMOVE;
358			break;
359		}
360		default:break;
361	}
362
363	if(top_window != NULL && close_widget != NULL)
364	{
365		ftk_widget_show(close_widget, !ftk_widget_has_attr(top_window, FTK_ATTR_IGNORE_CLOSE));
366	}
367
368	return ret;
369}
370
371static Ret button_close_top_clicked(void* ctx, void* obj)
372{
373	FtkWidget* panel = ftk_default_status_panel();
374	FtkWidget* title_widget = ftk_widget_lookup(panel, IDC_TITLE_ITEM);
375	FtkWidget* top_window = (FtkWidget*)ftk_widget_user_data(title_widget);
376
377	if(top_window != NULL && ftk_widget_type(top_window) == FTK_WINDOW)
378	{
379		if(!ftk_widget_has_attr(top_window, FTK_ATTR_IGNORE_CLOSE))
380		{
381			ftk_widget_set_user_data(title_widget, NULL, NULL);
382			ftk_logd("%s: close window %s\n", __func__, ftk_widget_get_text(top_window));
383			ftk_widget_unref(top_window);
384		}
385		else
386		{
387			ftk_logd("%s: the top window has attribute FTK_ATTR_IGNORE_CLOSE\n", __func__);
388		}
389	}
390	else
391	{
392		ftk_logd("%s: not normal window.\n", __func__);
393	}
394
395	return RET_OK;
396}
397
398#define min(a,b) ((a) < (b) ? (a) : (b))
399
400static void ftk_init_panel(void)
401{
402	FtkGc gc;
403	FtkWidget* item = NULL;	
404	FtkWidget* panel = ftk_status_panel_create(FTK_STATUS_PANEL_HEIGHT);
405	size_t width = ftk_widget_width(panel);
406
407	ftk_set_status_panel(panel);
408	quit_if_fail(ftk_default_status_panel(), "Init status panel failed.\n");
409
410	memset(&gc, 0x00, sizeof(gc));
411	gc.mask   = FTK_GC_BITMAP;
412	gc.bitmap = ftk_theme_load_image(ftk_default_theme(), "status-bg"FTK_STOCK_IMG_SUFFIX);
413	ftk_widget_set_gc(panel, FTK_WIDGET_NORMAL, &gc);
414	ftk_widget_set_gc(panel, FTK_WIDGET_ACTIVE, &gc);
415	ftk_widget_set_gc(panel, FTK_WIDGET_FOCUSED, &gc);
416	ftk_gc_reset(&gc);
417	
418	item = ftk_status_item_create(panel, -100, 32);
419	ftk_widget_set_id(item, IDC_CLOSE_ITEM);
420	gc.bitmap = ftk_theme_load_image(ftk_default_theme(), "close-32"FTK_STOCK_IMG_SUFFIX);
421	if(gc.bitmap != NULL)
422	{
423		gc.mask = FTK_GC_BITMAP;
424		ftk_widget_set_gc(item, FTK_WIDGET_NORMAL, &gc);
425		ftk_widget_set_gc(item, FTK_WIDGET_FOCUSED, &gc);
426		ftk_gc_reset(&gc);
427		gc.mask = FTK_GC_BITMAP;
428		gc.bitmap = ftk_theme_load_image(ftk_default_theme(), "close-pressed-32"FTK_STOCK_IMG_SUFFIX);
429		ftk_widget_set_gc(item, FTK_WIDGET_ACTIVE, &gc);
430		ftk_gc_reset(&gc);
431	}
432	ftk_status_item_set_clicked_listener(item, button_close_top_clicked, NULL);
433
434	gc.bitmap = ftk_theme_load_image(ftk_default_theme(), "flag-32"FTK_STOCK_IMG_SUFFIX);
435	item = ftk_status_item_create(panel, 1, gc.bitmap ? min(ftk_bitmap_width(gc.bitmap), 100) : 32);
436	ftk_widget_set_id(item, IDC_ICON_ITEM);
437	if(gc.bitmap != NULL)
438	{
439		gc.mask = FTK_GC_BITMAP;
440		ftk_widget_set_gc(item, FTK_WIDGET_NORMAL, &gc);
441		ftk_widget_set_gc(item, FTK_WIDGET_ACTIVE, &gc);
442		ftk_widget_set_gc(item, FTK_WIDGET_FOCUSED, &gc);
443		ftk_gc_reset(&gc);
444	}
445	ftk_widget_show(item, 1);
446
447	item = ftk_status_item_create(panel, 2, width/2);
448	ftk_widget_set_id(item, IDC_TITLE_ITEM);
449	ftk_widget_show(item, 1);
450
451	ftk_wnd_manager_add_global_listener(ftk_default_wnd_manager(), on_wnd_manager_global_event, NULL);
452	ftk_widget_show(panel, 1);
453	
454	return;
455}
456