forked from terrop/fbcanvas
/
commands.c
357 lines (308 loc) · 7.42 KB
/
commands.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* commands.c - 13.6.2008 - 14.7.2008 Ari & Tero Roponen */
#include <linux/input.h>
#include <cairo/cairo.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "commands.h"
#include "document.h"
#include "extcmd.h"
#include "keymap.h"
#include "util.h"
extern char *fb_read_line (struct document *doc, char *prompt); /* In readline.c */
jmp_buf exit_loop;
static int this_command;
static int last_command;
static void cmd_unbound (struct document *doc)
{
printf ("\a"); /* bell */
fflush (stdout);
}
static void cmd_quit (struct document *doc)
{
longjmp (exit_loop, 1);
}
static void cmd_redraw (struct document *doc)
{
/* Nothing to do */
}
static void cmd_first_page (struct document *doc)
{
if (doc->pagenum > 0)
{
doc->pagenum = 0;
doc->update(doc);
}
}
static void cmd_next_page (struct document *doc)
{
if (doc->pagenum < doc->pagecount - 1)
{
doc->pagenum++;
doc->update(doc);
}
}
static void cmd_prev_page (struct document *doc)
{
if (doc->pagenum > 0)
{
doc->pagenum--;
doc->update(doc);
}
}
static void cmd_last_page (struct document *doc)
{
if (doc->pagenum < doc->pagecount - 1)
{
doc->pagenum = doc->pagecount - 1;
doc->update(doc);
}
}
static void cmd_down (struct document *doc)
{
int delta = doc->backend->height / 20;
doc->scroll(doc, 0, delta);
}
static void cmd_up (struct document *doc)
{
int delta = -(doc->backend->height / 20);
doc->scroll(doc, 0, delta);
}
static void cmd_left (struct document *doc)
{
int delta = -(doc->backend->width / 20);
doc->scroll(doc, delta, 0);
}
static void cmd_right (struct document *doc)
{
int delta = doc->backend->width / 20;
doc->scroll(doc, delta, 0);
}
static void cmd_set_zoom (struct document *doc)
{
double scale = 1.0;
if (this_command != KEY_0)
scale += 0.1 * (this_command - KEY_1 + 1);
execute_extended_command (doc, "scale %2f", scale);
}
static void cmd_zoom_in (struct document *doc)
{
execute_extended_command (doc, "scale more");
}
static void cmd_zoom_out (struct document *doc)
{
execute_extended_command (doc, "scale less");
}
static void ecmd_save (struct document *doc, int argc, char *argv[])
{
if (argc != 2)
{
doc->set_message (doc, "Usage: save file.png");
return;
}
char *ext = strrchr (argv[1], '.');
if (! ext || strcmp (ext, ".png"))
{
doc->set_message (doc, "File name must end with \".png\".");
return;
}
convert_surface_argb_to_abgr (doc->cairo);
int ret = cairo_surface_write_to_png(doc->cairo, argv[1]);
if (ret)
fprintf (stderr, "%s", cairo_status_to_string(ret));
convert_surface_argb_to_abgr (doc->cairo);
}
static void cmd_dump_text (struct document *doc)
{
char filename[256];
sprintf(filename, "%s-pg-%d.txt", basename(doc->filename), doc->pagenum + 1);
doc->dump_text(doc, filename);
}
static void cmd_flip_x (struct document *doc)
{
int width = (doc->flags & NO_GENERIC_SCALE) ?
doc->width : ceil(doc->scale * doc->width);
cairo_matrix_t flipx;
cairo_matrix_init (&flipx, -1, 0, 0, 1, width, 0);
/*
* .y y.
* │ -> │
* └─x x─┘
*/
cairo_matrix_multiply (&doc->transform, &doc->transform, &flipx);
}
static void cmd_flip_y (struct document *doc)
{
int height = (doc->flags & NO_GENERIC_SCALE) ?
doc->height : ceil(doc->scale * doc->height);
cairo_matrix_t flipy;
cairo_matrix_init (&flipy, 1, 0, 0, -1, 0, height);
/* .y .
* │
* └─x ->
* ┌─x
* │
* y
*/
cairo_matrix_multiply (&doc->transform, &doc->transform, &flipy);
}
static void cmd_flip_z (struct document *doc)
{
int width = (doc->flags & NO_GENERIC_SCALE) ?
doc->width : ceil(doc->scale * doc->width);
int height = (doc->flags & NO_GENERIC_SCALE) ?
doc->height : ceil(doc->scale * doc->height);
int dir = (this_command == (KEY_Z | SHIFT)) ? 1 : -1;
cairo_matrix_t flipz;
cairo_matrix_init (&flipz, 0, dir, -dir, 0,
(dir == 1) ? height : 0,
(dir == -1) ? width : 0);
/* TODO: fix images
* dir = 1 dir = -1
*
* .y x .y .┌─y
* │ -> │ │ -> │
* └─x .y─┘ └─x x
*/
cairo_matrix_multiply (&doc->transform, &doc->transform, &flipz);
int tmp = doc->height;
doc->height = doc->width;
doc->width = tmp;
}
static void cmd_goto_top (struct document *doc)
{
static int last_y;
if (last_command == this_command)
{
int tmp = doc->yoffset;
doc->yoffset = last_y;
last_y = tmp;
} else {
last_y = doc->yoffset;
doc->yoffset = 0;
}
}
static void cmd_goto_bottom (struct document *doc)
{
static int last_y;
if (last_command == this_command)
{
int tmp = doc->yoffset;
doc->yoffset = last_y;
last_y = tmp;
} else {
last_y = doc->yoffset;
doc->yoffset = doc->height - doc->backend->height;
}
}
static void cmd_display_current_page (struct document *doc)
{
if (last_command == this_command)
{
this_command = KEY_L | CONTROL;
return;
}
doc->set_message(doc, "%d/%d", doc->pagenum + 1, doc->pagecount);
}
static void cmd_display_info(struct document *doc)
{
if (last_command == this_command)
{
this_command = KEY_L | CONTROL;
return;
}
doc->set_message (doc, "File: %s\nPage: %d/%d",
basename(doc->filename),
doc->pagenum + 1,
doc->pagecount);
}
static void cmd_reset (struct document *doc)
{
execute_extended_command (doc, "scale reset");
}
static void cmd_full_screen (struct document *doc)
{
execute_extended_command (doc, "scale full");
}
static void cmd_full_width (struct document *doc)
{
execute_extended_command (doc, "scale width");
}
static void cmd_full_height (struct document *doc)
{
execute_extended_command (doc, "scale height");
}
void cmd_extended_command (struct document *doc)
{
char *prompt = "C:\\> ";
char *cmd = fb_read_line (doc, prompt);
if (cmd)
execute_extended_command (doc, cmd);
free (cmd);
}
void setup_keys (void)
{
struct
{
unsigned int code;
void (*cmd)(struct document *doc);
} keys[] = {
{KEY_ESC, cmd_quit},
{KEY_F, cmd_full_screen},
{KEY_G, cmd_reset},
{KEY_H, cmd_full_height},
{KEY_I, cmd_display_info},
{KEY_L | CONTROL, cmd_redraw},
{KEY_N | CONTROL, cmd_down},
{KEY_P, cmd_display_current_page},
{KEY_P | CONTROL, cmd_up},
{KEY_Q, cmd_quit},
{KEY_ENTER, cmd_extended_command}, /* RET */
{KEY_T, cmd_dump_text},
{KEY_W, cmd_full_width},
{KEY_X, cmd_flip_x},
{KEY_Y, cmd_flip_y},
{KEY_Z, cmd_flip_z},
{KEY_Z | SHIFT, cmd_flip_z},
{KEY_HOME, cmd_goto_top},
{KEY_HOME | CONTROL, cmd_first_page},
{KEY_END, cmd_goto_bottom},
{KEY_END | CONTROL, cmd_last_page},
{KEY_PAGEDOWN, cmd_next_page},
{KEY_PAGEUP, cmd_prev_page},
{KEY_DOWN, cmd_down},
{KEY_UP, cmd_up},
{KEY_LEFT, cmd_left},
{KEY_RIGHT, cmd_right},
{KEY_0, cmd_set_zoom},
{KEY_1, cmd_set_zoom},
{KEY_2, cmd_set_zoom},
{KEY_3, cmd_set_zoom},
{KEY_4, cmd_set_zoom},
{KEY_5, cmd_set_zoom},
{KEY_6, cmd_set_zoom},
{KEY_7, cmd_set_zoom},
{KEY_8, cmd_set_zoom},
{KEY_9, cmd_set_zoom},
{KEY_MINUS, cmd_zoom_in}, /* +/- tulevat englanninkielisen */
{KEY_SLASH, cmd_zoom_out}, /* näppäismistöasettelun mukaisesti */
{0, NULL}
};
use_keymap (NULL); /* Global keymap */
for (int i = 0; keys[i].cmd; i++)
set_key(keys[i].code, keys[i].cmd);
register_extended_commands ();
set_extcmd ("save", ecmd_save);
}
command_t lookup_command (int character)
{
void *cmd = lookup_key (character);
if (! cmd)
cmd = cmd_unbound;
last_command = this_command;
this_command = character;
return (command_t) cmd;
}