aboutsummaryrefslogtreecommitdiffstats
path: root/mbinputmgr.c
blob: 12a84eac7b1b2d604247fa214687892cc08900ae (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "mbinputmgr.h"

static void 
fork_exec(MBInpmgrState *inpmgr, char *cmd)
{
  switch (inpmgr->PidCurrent = fork())
    {
    case 0: // Child process
      mb_exec(cmd); 		/* XXX handle for gnome */
      fprintf(stderr, "Can't exec \"%s\"\n", cmd);
      exit(1);
    case -1:
      fprintf(stderr, "Couldn't fork\n");
      /* XXX msg bubble error */
      break;
    }
}

static void 
selected_method_save(MBInpmgrState *inpmgr)
{
  FILE *fp;
  char *sessionfile = alloca(sizeof(char)*255);

  if (inpmgr->MethodSelected == NULL) 
    return;

  if (getenv("HOME") == NULL)
    {
      fprintf(stderr, "mbinputmgr: unable to get home directory, is HOME set?\n");
      return;
    }

  snprintf(sessionfile, 255, "%s/.mbinputmgr", getenv("HOME"));   
  
  if ((fp = fopen(sessionfile, "w")) == NULL)
    { 
      fprintf(stderr,"mbinputmgr: Unable to create Session file ( %s )\n", 
	      sessionfile); 
      return; 
    }

  fprintf(fp, "%s", inpmgr->MethodSelected->exec);  

  fclose(fp);
}

static void 
selected_method_load(MBInpmgrState *inpmgr)
{
  FILE *fp;
  char *sessionfile = alloca(sizeof(char)*255);
  char *selected_exec_str = alloca(sizeof(char)*255);

  int i = 0;

  if (getenv("HOME") == NULL)
    {
      fprintf(stderr, 
	      "mbinputmgr: unable to get home directory, is HOME set?\n");
      return;
    }

  snprintf(sessionfile, 255, "%s/.mbinputmgr", getenv("HOME"));   
  
  if ((fp = fopen(sessionfile, "r")) == NULL)
    {
      fprintf(stderr, "mbinputmgr: no session data found.");
      return;
    }
  
  if (fgets(selected_exec_str, 255, fp) != NULL)
    {
      for (i = 0; i < inpmgr->NMethods; i++)
	if (!strcmp(selected_exec_str, inpmgr->Methods[i].exec))
	  {
	    inpmgr->MethodSelected = &inpmgr->Methods[i];
	    break;
	  }
    }
 
  fclose(fp);
  return;
}

#define N_SEARCH_DIRS 2

MBInpmgrState*
mbinpmgr_init(void)
{
  MBInpmgrState *inpmgr = NULL;

  char app_paths[N_SEARCH_DIRS][256];
  int cur_entry_count = 0;
  DIR *dp;
  struct dirent *dir_entry;
  struct stat stat_info;
  char orig_wd[256];
  int i;

  inpmgr = malloc(sizeof(MBInpmgrState));
  memset(inpmgr,0,sizeof(MBInpmgrState));

  inpmgr->MethodSelected = NULL;
  inpmgr->NMethods       = 0;
  inpmgr->PidCurrent     = -1;

  if (getcwd(orig_wd, 255) == (char *)NULL)
    {
      fprintf(stderr, "mbinputmgr: cant get current directory\n");
      exit(0);
    }
  
  snprintf(app_paths[0], 256, DATADIR "/applications/inputmethods");
  snprintf(app_paths[1], 256, "%s/.applications/inputmethods", getenv("HOME"));

  for (i = 0; i < N_SEARCH_DIRS; i++)
  {
    if ((dp = opendir(app_paths[i])) == NULL)
      {
	fprintf(stderr, "mbinputmgr: failed to open %s\n", app_paths[i]);
	continue;
      }
  
    chdir(app_paths[i]);
    while((dir_entry = readdir(dp)) != NULL)
      {
	lstat(dir_entry->d_name, &stat_info);
	if (!(S_ISDIR(stat_info.st_mode)))
	  {

	    /* 
	       XXX should use libgnome/gnome-dentry.[hc]
	           for gnome .desktop parsing - use ifdefs or wrapper? 
	    */

	    MBDotDesktop *dd;
	    int flags = 0;
	    dd = mb_dotdesktop_new_from_file(dir_entry->d_name);
	    if (dd /* && mb_dotdesktop_get(dd, "X-MB-INPUT-MECHANSIM") */)   
	      {
		if (mb_dotdesktop_get(dd, "Icon")
		    && mb_dotdesktop_get(dd, "Name")
		    && mb_dotdesktop_get(dd, "Exec")
		    && cur_entry_count < MAX_METHODS)
		  {
		    MBMenuItem      *new_item = NULL;
		    
		    MBPixbufImage *img_tmp;
		    
		    inpmgr->Methods[cur_entry_count].name 
		      = strdup(mb_dotdesktop_get(dd,"Name"));
		    inpmgr->Methods[cur_entry_count].icon_name 
		      = strdup(mb_dotdesktop_get(dd,"Icon"));
		    inpmgr->Methods[cur_entry_count].exec 
		      = strdup(mb_dotdesktop_get(dd,"Exec"));

		    /* XXX Should be done elsewhere		    
		    load_icon(&inpmgr->Methods[cur_entry_count]);

		    inpmgr->Methods[cur_entry_count].item 
		      = mb_menu_new_item(PopupMenu, 
					 mb_menu_get_root_menu(PopupMenu), 
					 Methods[cur_entry_count].name, 
					 menu_item_activated_callback,
					 (void *)&Methods[cur_entry_count],
					 0 );
		    
		    mb_menu_item_icon_set (PopupMenu, 
					   Methods[cur_entry_count].item, 
					   Methods[cur_entry_count].icon );
		    */

		    if (inpmgr->MethodSelected == NULL) 
		      inpmgr->MethodSelected = &inpmgr->Methods[cur_entry_count];
		    
		    cur_entry_count++;
		  }
		mb_dotdesktop_free(dd);
	      }
#ifdef DEBUG
	    else
	      fprintf(stderr, "mbinputmgr: failed to parse %s :( \n", 
		      dir_entry->d_name);
#endif
	  }
      }
    
    closedir(dp);
  }

  chdir(orig_wd);

  if (cur_entry_count == 0)
    {
      /* XXX Msg bubble error */
      fprintf(stderr, "mbinputmgr: Failed to find any input mechanisms\n");
      exit(1);
    }

  inpmgr->NMethods = cur_entry_count;

  selected_method_load(inpmgr);
  
  return inpmgr;
}

void
mbinpmgr_change_selected_method (MBInpmgrState *inpmgr, 
				 InputMethod   *new_method)
{
  if (inpmgr->MethodSelected != new_method)
    {
      inpmgr->MethodSelected = new_method;

      /* If the old method executing, kill it and start a new one */
      if (inpmgr->PidCurrent != -1) 
	{
	  kill(inpmgr->PidCurrent, 15);
	  inpmgr->PidCurrent = -1;
	  fork_exec(inpmgr, inpmgr->MethodSelected->exec);
	}

      selected_method_save (inpmgr);
    }
}

void
mbinputmgr_toggle_selected_method (MBInpmgrState *inpmgr)
{
  /* Start/stop current sleected method */

  if ( (inpmgr->PidCurrent != -1) /* Something running */
       &&  (kill(inpmgr->PidCurrent, 0) != -1) )
    {
      kill(inpmgr->PidCurrent, 15); /* kill it */
      inpmgr->PidCurrent = -1;
    }
  else fork_exec(inpmgr, inpmgr->MethodSelected->exec);
}

int
mbinputmgr_method_active (MBInpmgrState *inpmgr)
{
  return (inpmgr->PidCurrent != -1);
}