forked from bfabiszewski/kterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kindle.c
294 lines (272 loc) · 9.39 KB
/
kindle.c
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* kindle.c
*
* This file is part of kterm
*
* Copyright(C) 2016 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include "kindle.h"
#include "config.h"
/** Global config */
extern KTconf *conf;
#ifdef HAVE_DBUS
#include <dbus/dbus.h>
/**
* Send dbus message, get string response if requested
* @param destination Name that the message should be sent to
* @param method Method to invoke
* @param param Optional param do append to message
* @param reply_msg Location to store reply message or NULL to ignore response
* @return True on success, false otherwise
*/
static gboolean dbus_send(const gchar *dest, const gchar *method, const gchar *param, gchar **reply_msg) {
gboolean ret = TRUE;
DBusError error;
DBusBusType type = DBUS_BUS_SYSTEM;
gchar iface[256];
snprintf(iface, sizeof(iface), "%s.%s", dest, method);
const gchar *path = "/default";
dbus_error_init(&error);
DBusConnection *connection = dbus_bus_get(type, &error);
if G_UNLIKELY(!connection) {
D printf("Dbus connection failed: %s\n", error.message);
dbus_error_free(&error);
return FALSE;
}
DBusMessage *message = dbus_message_new_method_call(dest, path, iface, method);
if G_UNLIKELY(!message) {
D printf("Dbus message allocation failed\n");
dbus_connection_unref(connection);
return FALSE;
}
if (param && !dbus_message_append_args(message, DBUS_TYPE_STRING, ¶m, DBUS_TYPE_INVALID)) {
D printf("Dbus append args failed\n");
dbus_message_unref(message);
dbus_connection_unref(connection);
return FALSE;
}
if (reply_msg) {
dbus_error_init(&error);
gint timeout_ms = 500;
DBusMessage *reply = dbus_connection_send_with_reply_and_block(connection, message, timeout_ms, &error);
if G_UNLIKELY(!reply) {
D printf("Dbus reply error: %s: %s\n", error.name, error.message);
dbus_error_free(&error);
ret = FALSE;
} else {
DBusMessageIter args;
dbus_message_iter_init(reply, &args);
do {
if (dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_STRING) {
dbus_message_iter_get_basic(&args, reply_msg);
break;
}
} while (dbus_message_iter_next(&args));
dbus_message_unref(reply);
}
} else {
dbus_connection_send(connection, message, NULL);
dbus_connection_flush(connection);
}
dbus_message_unref(message);
dbus_connection_unref(connection);
return ret;
}
/**
* Get screen orientation
* @return 'U', 'L', 'R' or zero on failure
*/
gchar get_orientation(void) {
const gchar *dest = "com.lab126.winmgr";
const gchar *reply_method = "getorientationLockStr";
gchar *reply = NULL;
gboolean ret = dbus_send(dest, reply_method, NULL, &reply);
if G_LIKELY(ret && reply) {
D printf("screen orientation: %s\n", reply);
gchar orientation = reply[0];
if (orientation == 'U' || orientation == 'L' || orientation == 'R') {
return orientation;
}
}
return 0;
}
/**
* Set screen orientation
* @param orientation One of 'U', 'L', 'R'
* @return True on success, false otherwise
*/
gboolean set_orientation(gchar orientation) {
if (orientation != 'U' && orientation != 'L' && orientation != 'R') {
return FALSE;
}
gchar param[2] = { 0 };
param[0] = orientation;
const gchar *dest = "com.lab126.winmgr";
const gchar *query_method = "setorientationLockStr";
gboolean ret = dbus_send(dest, query_method, param, NULL);
if G_LIKELY(ret) {
D printf("screen orientation set to %c\n", orientation);
}
return ret;
}
#else
gchar get_orientation(void) {
D printf("dbus support not compiled\n");
return 0;
}
gboolean set_orientation(gchar orientation) {
UNUSED(orientation);
D printf("dbus support not compiled\n");
return FALSE;
}
#endif
/**
* Initialize orientation settings, set orientation if needed
*/
void orientation_init(void) {
// save current screen orientation
conf->orientation_saved = get_orientation();
// set orientation from config if any
if (conf->orientation > 0) {
if (conf->orientation != conf->orientation_saved) {
set_orientation(conf->orientation);
}
} else {
conf->orientation = conf->orientation_saved;
}
}
/**
* Restore initial screen orientation
*/
void orientation_restore(void) {
if (conf->orientation_saved > 0 && conf->orientation_saved != conf->orientation) {
set_orientation(conf->orientation_saved);
}
}
/**
* Grab/ungrab keyboard
* @param window Gdk window
* @param grab Grab if true, ungrab if false
* @return True on success, false otherwise
*/
gboolean keyboard_grab(GdkWindow *window, gboolean grab) {
GdkGrabStatus ret = GDK_GRAB_SUCCESS;
#if GTK_CHECK_VERSION(3,20,0)
GdkDisplay *display = gdk_display_get_default();
GdkSeat *seat = gdk_display_get_default_seat(display);
if (!seat) {
D printf("Getting default seat failed\n");
return FALSE;
}
if (grab) {
ret = gdk_seat_grab(seat, window, GDK_SEAT_CAPABILITY_KEYBOARD, TRUE, NULL, NULL, NULL, NULL);
} else {
gdk_seat_ungrab(seat);
}
#elif GTK_CHECK_VERSION(3,0,0)
GdkDisplay *display = gdk_display_get_default();
GdkDeviceManager *manager = gdk_display_get_device_manager(display);
if (!manager) {
D printf("Getting display manager failed\n");
return FALSE;
}
GdkDevice *pointer = gdk_device_manager_get_client_pointer(manager);
if (!pointer) {
D printf("Getting pointer failed\n");
return FALSE;
}
GdkDevice *keyboard = gdk_device_get_associated_device(pointer);
if (!keyboard || gdk_device_get_source(keyboard) != GDK_SOURCE_KEYBOARD) {
D printf("Getting keyboard failed\n");
return FALSE;
}
if (grab) {
ret = gdk_device_grab(keyboard, window, GDK_OWNERSHIP_NONE, TRUE, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK, NULL, GDK_CURRENT_TIME);
} else {
gdk_device_ungrab(keyboard, GDK_CURRENT_TIME);
}
#else
if (grab) {
ret = gdk_keyboard_grab(window, FALSE, GDK_CURRENT_TIME);
} else {
gdk_keyboard_ungrab(GDK_CURRENT_TIME);
}
#endif
if (ret != GDK_GRAB_SUCCESS) {
D printf("Keyboard grabbing failed (%d)\n", ret);
return FALSE;
}
return TRUE;
}
/**
* Grab keyboard callback
* @param widget Calling widget
* @param event Gdk event
* @return Always false to propagate event
*/
gboolean grab_keyboard_cb(GtkWidget *widget, GdkEvent *event, gpointer data) {
UNUSED(event);
UNUSED(data);
keyboard_grab(gtk_widget_get_window(widget), TRUE);
return FALSE;
}
/**
* Inject custom styles (css)
* Reason: Kindle users have limited access to style customizations.
* This scheme may however be overriden by local styles.
*/
void inject_styles(void) {
#if GTK_CHECK_VERSION(3,0,0)
GError *error = NULL;
GtkCssProvider *provider = gtk_css_provider_new();
if (!provider) { return; }
gtk_css_provider_load_from_data(provider,
"@define-color gray #f0f0f0;"
"#ktermKbButton { background: @gray; }"
"#ktermKbButton:hover { background: @gray; }"
"#ktermKbButton:selected { background: @gray; }"
"#ktermKbButton:disabled { background: @gray; }"
"#ktermKbButton:active { background: @gray; }"
, -1, &error);
if G_UNLIKELY(error) {
D printf("Style injection failed: %s\n", error->message);
g_error_free(error);
} else {
GdkScreen *screen = gdk_screen_get_default();
gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
g_object_unref(provider);
#endif
}
/**
* Inject custom styles (gtkrc)
* Reason: Kindle users have limited access to style customizations.
* This scheme may however be overriden by local styles.
*/
void inject_gtkrc(void) {
#if !GTK_CHECK_VERSION(3,0,0)
gtk_rc_parse_string("gtk_color_scheme = \"white: #ffffff\ngray: #f0f0f0\""
"style \"kterm-style\" {"
" bg[NORMAL] = @gray"
" bg[PRELIGHT] = @gray"
" bg[INSENSITIVE] = @gray"
" bg[ACTIVE] = @gray"
" bg[SELECTED] = @gray"
"}"
"widget \"*ktermKbButton\" style : lowest \"kterm-style\"");
#endif
}