1 /*
2  * Copyright (C) 2020 The HIME team, Taiwan
3  * Copyright (C) 2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation version 2.1
8  * of the License.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "hime.h"
21 
22 #include "pho.h"
23 
24 gboolean b_use_full_space = TRUE;
25 
26 static char text_pho[6][CH_SZ];
27 
28 // initialized in hime.c
29 Display *dpy;
30 
bell(void)31 void bell (void) {
32     if (hime_bell_off) {
33         return;
34     }
35 
36     XBell (dpy, hime_bell_volume);
37 }
38 
case_inverse(KeySym * xkey,int shift_m)39 void case_inverse (KeySym *xkey, int shift_m) {
40     if (*xkey > 0x7e) {
41         return;
42     }
43 
44     if (shift_m) {
45         if (islower (*xkey)) {
46             *xkey -= 0x20;
47         }
48     } else if (isupper (*xkey)) {
49         *xkey += 0x20;
50     }
51 }
52 
current_time(void)53 gint64 current_time (void) {
54     struct timeval tval;
55 
56     gettimeofday (&tval, NULL);
57     return (gint64) tval.tv_sec * 1000000 + tval.tv_usec;
58 }
59 
disp_pho_sub(GtkWidget * label,int index,char * pho)60 void disp_pho_sub (GtkWidget *label, int index, char *pho) {
61     if (!label) {
62         return;
63     }
64 
65     if (index >= text_pho_N) {
66         return;
67     }
68 
69     if (pho[0] == ' ' && !pin_juyin) {
70         u8cpy (text_pho[index], " "); /* Full width space */
71     } else {
72         u8cpy (text_pho[index], pho);
73     }
74     char s[text_pho_N * CH_SZ + 1];
75 
76     int tn = 0;
77     for (int i = 0; i < text_pho_N; i++) {
78         int n = utf8cpy (s + tn, text_pho[i]);
79         tn += n;
80     }
81 
82     gtk_label_set_text (GTK_LABEL (label), s);
83 }
84 
exec_hime_setup(void)85 void exec_hime_setup (void) {
86     dbg ("exec hime\n");
87     if (geteuid () < 100 || getegid () < 100) {
88         return;
89     }
90 
91     system (HIME_BIN_DIR "/hime-setup &");
92 }
93 
set_label_font_size(GtkWidget * label,int size)94 void set_label_font_size (GtkWidget *label, int size) {
95     if (!GTK_IS_WIDGET (label)) {
96         return;
97     }
98 
99     PangoContext *pango_context = gtk_widget_get_pango_context (label);
100     PangoFontDescription *font = pango_context_get_font_description (pango_context);
101 
102     char tt[256];
103     snprintf (tt, sizeof (tt), "%s %d", hime_font_name, size);
104 
105     PangoFontDescription *nfont = pango_font_description_from_string (tt);
106 
107     pango_font_description_merge (font, nfont, TRUE);
108     pango_font_description_free (nfont);
109 
110     // XXX(xatier): deprecated function, find alternatives
111     gtk_widget_override_font (label, font);
112 }
113 
114 // the width of ascii space in firefly song
set_label_space(GtkWidget * label)115 void set_label_space (GtkWidget *label) {
116     gtk_label_set_text (GTK_LABEL (label), "\xe3\x80\x80");
117 }
118 
set_no_focus(GtkWidget * win)119 void set_no_focus (GtkWidget *win) {
120     gdk_window_set_override_redirect (gtk_widget_get_window (win), TRUE);
121     gtk_window_set_accept_focus (GTK_WINDOW (win), FALSE);
122     gtk_window_set_focus_on_map (GTK_WINDOW (win), FALSE);
123     gtk_window_set_resizable (GTK_WINDOW (win), FALSE);
124 }
125 
get_default_display(void)126 GdkDisplay *get_default_display (void) {
127     GdkDisplay *display = gdk_display_get_default ();
128     if (!display) {
129         dbg ("gdk_display_get_default returned NULL\n");
130     }
131     return display;
132 }
133 
134 // GTK+ 3.22 introduced GdkMonitor APIs, GdkScreen APIs were deprecated
135 #if GTK_CHECK_VERSION(3, 0, 0)
get_primary_monitor(void)136 GdkMonitor *get_primary_monitor (void) {
137     GdkMonitor *primary_monitor = gdk_display_get_primary_monitor (
138         get_default_display ());
139     if (!primary_monitor) {
140         dbg ("gdk_display_get_primary_monitor returned NULL\n");
141     }
142     return primary_monitor;
143 }
144 #endif
145 
get_keymap(void)146 GdkKeymap *get_keymap (void) {
147     GdkKeymap *keymap = gdk_keymap_get_for_display (get_default_display ());
148     if (!keymap) {
149         dbg ("gdk_keymap_get_for_display returned NULL\n");
150     }
151     return keymap;
152 }
153 
get_caps_lock_state(void)154 gboolean get_caps_lock_state (void) {
155     return gdk_keymap_get_caps_lock_state (get_keymap ());
156 }
157 
get_atom_by_name(Display * display,const char * name)158 Atom get_atom_by_name (Display *display, const char *name) {
159     if (!display) {
160         dbg ("display is null\n");
161         return 0;
162     }
163 
164     const char *xim_name = get_hime_xim_name ();
165     static const int ATOM_NAME_SIZE = 128;
166     char atom_name[ATOM_NAME_SIZE];
167 
168     snprintf (atom_name, sizeof (atom_name), name, xim_name);
169 
170     // get the atom identifier
171     // the atom is created if it does not exist
172     return XInternAtom (display, atom_name, False);
173 }
174 
175 #if !USE_TSIN
change_tsin_color()176 void change_tsin_color () {}
change_tsin_font_size()177 void change_tsin_font_size () {}
change_win0_style()178 void change_win0_style () {}
destroy_win0()179 void destroy_win0 () {}
destroy_win1()180 void destroy_win1 () {}
free_tsin()181 void free_tsin () {}
load_tsin_db()182 void load_tsin_db () {}
tsin_reset_in_pho()183 void tsin_reset_in_pho () {}
tsin_reset_in_pho0()184 void tsin_reset_in_pho0 () {}
tsin_toggle_half_full()185 void tsin_toggle_half_full () {}
186 #endif
187