1//
2// CannaIM - Canna-based Input Method Add-on for BeOS R4
3//
4
5#include <List.h>
6#include <Looper.h>
7#include <FindDirectory.h>
8#include <Path.h>
9#include <File.h>
10
11#include "CannaCommon.h"
12#include "CannaMethod.h"
13#include "CannaLooper.h"
14
15#include "Bitmaps.h"
16
17#ifdef DEBUG
18#include <Debug.h>
19#endif
20
21Preferences gSettings;
22
23BInputServerMethod*
24instantiate_input_method()
25{
26	return new CannaMethod();
27}
28
29
30//	#pragma mark -
31
32
33CannaMethod::CannaMethod()
34	: BInputServerMethod("Canna", (const uchar*)kCannaIcon)
35{
36#ifdef DEBUG
37	SERIAL_PRINT(( "CannaIM:Constructor called.\n" ));
38#endif
39	ReadSettings();
40}
41
42CannaMethod::~CannaMethod()
43{
44	BLooper *looper = NULL;
45	cannaLooper.Target( &looper );
46	if ( looper != NULL )
47	{
48#ifdef DEBUG
49	SERIAL_PRINT(( "CannaIM:Locking CannaLooper...\n" ));
50#endif
51		if ( looper->Lock() )
52#ifdef DEBUG
53	SERIAL_PRINT(( "CannaIM:CannaLooper locked. Calling Quit().\n" ));
54#endif
55			looper->Quit();
56	}
57	WriteSettings();
58}
59
60status_t
61CannaMethod::MethodActivated( bool active )
62{
63	BMessage msg( CANNA_METHOD_ACTIVATED );
64
65	if ( active )
66		msg.AddBool( "active", true );
67
68	cannaLooper.SendMessage( &msg );
69
70	return B_OK;
71}
72
73filter_result
74CannaMethod::Filter( BMessage *msg, BList *outList )
75{
76	if ( msg->what != B_KEY_DOWN )
77		return B_DISPATCH_MESSAGE;
78
79	cannaLooper.SendMessage( msg );
80	return B_SKIP_MESSAGE;
81}
82
83status_t
84CannaMethod::InitCheck()
85{
86#ifdef DEBUG
87	SERIAL_PRINT(( "CannaIM:InitCheck() called.\n" ));
88#endif
89	CannaLooper *looper;
90	status_t err;
91	looper = new CannaLooper( this );
92	looper->Lock();
93	err = looper->Init();
94	looper->Unlock();
95	cannaLooper = BMessenger( NULL, looper );
96#ifdef DEBUG
97	if ( err != B_NO_ERROR )
98	SERIAL_PRINT(( "CannaLooper::InitCheck() failed.\n" ));
99	else
100	SERIAL_PRINT(( "CannaLooper::InitCheck() success.\n" ));
101#endif
102
103	return err;
104}
105
106void
107CannaMethod::ReadSettings()
108{
109	BMessage pref;
110	BFile preffile( CANNAIM_SETTINGS_FILE, B_READ_ONLY );
111	if ( preffile.InitCheck() == B_NO_ERROR && pref.Unflatten( &preffile ) == B_OK )
112	{
113		pref.FindBool( "arrowkey", &gSettings.convert_arrowkey );
114		pref.FindPoint( "palette", &gSettings.palette_loc );
115		pref.FindPoint( "standalone", &gSettings.standalone_loc );
116#ifdef DEBUG
117SERIAL_PRINT(( "CannaMethod: ReadSettings() success. arrowkey=%d, palette_loc=%d,%d standalone_loc=%d, %d\n", gSettings.convert_arrowkey, gSettings.palette_loc.x, gSettings.palette_loc.y, gSettings.standalone_loc.x, gSettings.standalone_loc.y ));
118#endif
119		return;
120	}
121
122	//set default values
123#ifdef DEBUG
124SERIAL_PRINT(( "CannaMethod: ReadSettings() failed.\n" ));
125#endif
126	gSettings.convert_arrowkey = true;
127	gSettings.palette_loc.Set( 800, 720 );
128	gSettings.standalone_loc.Set( 256, 576 );
129}
130
131void CannaMethod::WriteSettings()
132{
133	BMessage pref;
134	BFile preffile( CANNAIM_SETTINGS_FILE,
135			B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE );
136
137	if ( preffile.InitCheck() == B_NO_ERROR )
138	{
139		pref.AddBool( "arrowkey", gSettings.convert_arrowkey );
140		pref.AddPoint( "palette", gSettings.palette_loc );
141		pref.AddPoint( "standalone", gSettings.standalone_loc );
142		pref.Flatten( &preffile );
143#ifdef DEBUG
144SERIAL_PRINT(( "CannaMethod: WriteSettings() success. arrowkey=%d, palette_loc=%d,%d standalone_loc=%d, %d\n", gSettings.convert_arrowkey, gSettings.palette_loc.x, gSettings.palette_loc.y, gSettings.standalone_loc.x, gSettings.standalone_loc.y ));
145#endif
146	}
147}
148