~ubuntu-branches/ubuntu/precise/hime/precise

« back to all changes in this revision

Viewing changes to src/hime-conf.c

  • Committer: Package Import Robot
  • Author(s): Yao Wei (魏銘廷)
  • Date: 2012-01-14 00:24:08 UTC
  • Revision ID: package-import@ubuntu.com-20120114002408-e79gagbeg1rt8npv
Tags: upstream-0.9.9
Import upstream version 0.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2010 Edward Der-Hua Liu, Hsin-Chu, Taiwan
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2.1 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library; if not, write to the Free Software
 
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 */
 
17
 
 
18
#include "hime.h"
 
19
#include <dirent.h>
 
20
#include <X11/Xatom.h>
 
21
 
 
22
#if !CLIENT_LIB
 
23
char *TableDir=HIME_TABLE_DIR;
 
24
 
 
25
void init_TableDir()
 
26
{
 
27
  char *dname;
 
28
  if ((dname=getenv("HIME_TABLE_DIR"))) {
 
29
    TableDir = dname;
 
30
    return;
 
31
  }
 
32
}
 
33
 
 
34
 
 
35
void get_hime_dir(char *tt)
 
36
{
 
37
    strcpy(tt,(char *)getenv("HOME"));
 
38
    strcat(tt,"/.config/hime");
 
39
}
 
40
 
 
41
 
 
42
gboolean get_hime_user_fname(char *name, char fname[])
 
43
{
 
44
  get_hime_dir(fname);
 
45
  strcat(strcat(fname,"/"),name);
 
46
  return !access(fname, R_OK);
 
47
//  dbg("get_hime_user_fname %s %s\n", name, fname);
 
48
}
 
49
 
 
50
void get_hime_conf_fname(char *name, char fname[])
 
51
{
 
52
  get_hime_dir(fname);
 
53
  strcat(strcat(fname,"/config/"),name);
 
54
}
 
55
 
 
56
void get_hime_user_or_sys_fname(char *name, char fname[])
 
57
{
 
58
  if (!getenv("HIME_TABLE_DIR")) {
 
59
    if (get_hime_user_fname(name, fname))
 
60
      return;
 
61
  }
 
62
 
 
63
  get_sys_table_file_name(name, fname);
 
64
}
 
65
 
 
66
void get_hime_conf_str(char *name, char **rstr, char *default_str)
 
67
{
 
68
  char fname[MAX_HIME_STR];
 
69
  char out[256];
 
70
 
 
71
  if (*rstr)
 
72
    free(*rstr);
 
73
 
 
74
  get_hime_conf_fname(name, fname);
 
75
 
 
76
  FILE *fp;
 
77
 
 
78
  if ((fp=fopen(fname, "rb")) == NULL) {
 
79
    *rstr = strdup(default_str);
 
80
    return;
 
81
  }
 
82
 
 
83
  myfgets(out, sizeof(out), fp);
 
84
  int len = strlen(out);
 
85
  if (len && out[len-1]=='\n')
 
86
    out[len-1] = 0;
 
87
 
 
88
  fclose(fp);
 
89
 
 
90
  *rstr = strdup(out);
 
91
}
 
92
 
 
93
void get_hime_conf_fstr(char *name, char rstr[], char *default_str)
 
94
{
 
95
  char *tt = NULL;
 
96
  get_hime_conf_str(name, &tt, default_str);
 
97
  strcpy(rstr, tt);
 
98
  free(tt);
 
99
}
 
100
 
 
101
int get_hime_conf_int(char *name, int default_value)
 
102
{
 
103
  char tt[32];
 
104
  char default_value_str[MAX_HIME_STR];
 
105
 
 
106
  sprintf(default_value_str, "%d", default_value);
 
107
  get_hime_conf_fstr(name, tt, default_value_str);
 
108
 
 
109
  return atoi(tt);
 
110
}
 
111
 
 
112
 
 
113
void save_hime_conf_str(char *name, char *str)
 
114
{
 
115
  FILE *fp;
 
116
  char fname[256];
 
117
 
 
118
  get_hime_conf_fname(name, fname);
 
119
 
 
120
  if ((fp=fopen(fname,"wb"))==NULL) {
 
121
    p_err("cannot create %s", fname);
 
122
  }
 
123
 
 
124
  fprintf(fp, "%s", str);
 
125
  fclose(fp);
 
126
}
 
127
 
 
128
 
 
129
void save_hime_conf_int(char *name, int val)
 
130
{
 
131
  char tt[16];
 
132
 
 
133
  sprintf(tt, "%d", val);
 
134
  save_hime_conf_str(name, tt);
 
135
}
 
136
 
 
137
void get_sys_table_file_name(char *name, char *fname)
 
138
{
 
139
  sprintf(fname, "%s/%s", TableDir, name);
 
140
}
 
141
#endif /* !CLIENT_LIB */
 
142
 
 
143
char *get_hime_xim_name()
 
144
{
 
145
  char *xim_name;
 
146
 
 
147
  if ((xim_name=getenv("XMODIFIERS"))) {
 
148
    static char find[] = "@im=";
 
149
    static char sstr[32];
 
150
    char *p = strstr(xim_name, find);
 
151
 
 
152
    if (p==NULL) return "hime";
 
153
 
 
154
    p += strlen(find);
 
155
    strncpy(sstr, p, sizeof(sstr));
 
156
    sstr[sizeof(sstr) - 1]=0;
 
157
 
 
158
    if ((p=strchr(sstr, '.')))
 
159
      *p=0;
 
160
 
 
161
//    dbg("Try to use name from XMODIFIERS=@im=%s\n", sstr);
 
162
    return sstr;
 
163
  }
 
164
 
 
165
  return "hime";
 
166
}
 
167
 
 
168
Atom get_hime_atom(Display *dpy)
 
169
{
 
170
  char *xim_name = get_hime_xim_name();
 
171
  char tt[128];
 
172
 
 
173
  snprintf(tt, sizeof(tt), "HIME_ATOM_%s", xim_name);
 
174
 
 
175
  Atom atom = XInternAtom(dpy, tt, False);
 
176
 
 
177
  return atom;
 
178
}