forked from akkana/pho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmain.c
432 lines (398 loc) · 12.1 KB
/
gmain.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* gmain.c: gtk main routines for pho, an image viewer.
*
* Copyright 2004 by Akkana Peck.
* You are free to use or modify this code under the Gnu Public License.
*/
#include "pho.h"
#include "dialogs.h"
#include "exif/phoexif.h"
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <stdlib.h> /* for getenv() */
#include <unistd.h>
#include <string.h>
#include <ctype.h>
char * gCapFileFormat = "Captions";
/* Toggle a variable between two modes, preferring the first.
* If it's anything but mode1 it will end up as mode1.
*/
#define ToggleBetween(val,mode1,mode2) (val != mode1 ? mode1 : mode2)
/* If we're switching into scaling mode because the user pressed - or +/=,
* which scaling mode should we switch into?
*/
static int ModeForScaling(int oldmode)
{
switch (oldmode)
{
case PHO_SCALE_IMG_RATIO:
case PHO_SCALE_FULLSIZE:
case PHO_SCALE_NORMAL:
return PHO_SCALE_IMG_RATIO;
case PHO_SCALE_FIXED:
return PHO_SCALE_FIXED;
case PHO_SCALE_FULLSCREEN:
default:
return PHO_SCALE_SCREEN_RATIO;
}
}
static void RunPhoCommand()
{
int i;
char* cmd = getenv("PHO_CMD");
if (cmd == 0) cmd = "gimp";
else if (! *cmd) {
if (gDebug)
printf("PHO_CMD set to NULL, not running anything\n");
return;
}
else if (gDebug)
printf("Calling PHO_CMD %s\n", cmd);
gint gargc;
gchar** gargv;
gchar** new_argv = 0;
GError *gerror = 0;
if (! g_shell_parse_argv (cmd, &gargc, &gargv, &gerror)) {
fprintf(stderr, "Couldn't parse PHO_CMD %s\nError was %s",
cmd, gerror->message);
return;
}
/* PHO_CMD command can have a %s in it; if it does, substitute filename. */
int added_arg = 0;
for (i=1; i<gargc; ++i) {
if (gargv[i][0] == '%' && gargv[i][1] == 's') {
gargv[i] = gCurImage->filename;
added_arg = 1;
}
}
/* If it didn't have a %s, then we have to allocate a whole new argv array
* so we can add the filename argument at the end.
* Note that glib expects the argv to be zero terminated --
* it provides an argc from g_shell_parse_args() but doesn't
* take one in g_spawn_async().
*/
if (! added_arg) {
int new_argc = gargc + 2;
gchar** new_argv = malloc(sizeof(gchar *) * new_argc);
for (i=0; i<gargc; ++i)
new_argv[i] = gargv[i];
new_argv[gargc] = gCurImage->filename;
new_argv[gargc+1] = 0;
gargv = new_argv;
}
if (! g_spawn_async(NULL, gargv,
NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &gerror))
fprintf(stderr, "Couldn't spawn %s: \"%s\"\n", cmd, gerror->message);
if (new_argv)
free(new_argv);
}
void TryScale(float times)
{
/* Save the view modes, in case this fails */
int saveScaleMode = gScaleMode;
double saveScaleRatio = gScaleRatio;
int saveDisplayMode = gDisplayMode;
if (SetViewModes(gDisplayMode, ModeForScaling(gScaleMode),
gScaleRatio * times) == 0)
return;
/* Oops! It didn't work. Try to reset back to previous settings */
SetViewModes(saveDisplayMode, saveScaleMode, saveScaleRatio);
}
gint HandleGlobalKeys(GtkWidget* widget, GdkEventKey* event)
{
if (gDebug) printf("\nKey event\n");
if (event->state) {
switch (event->keyval)
{
case GDK_f:
/* Don't respond to ctrl-F -- that might be an attempt
* to edit in a text field in the keywords dialog.
* But we don't do anything on f with any modifier key either.
*/
return FALSE;
case GDK_0:
case GDK_1:
case GDK_2:
case GDK_3:
case GDK_4:
case GDK_5:
case GDK_6:
case GDK_7:
case GDK_8:
case GDK_9:
if (event->state & GDK_MOD1_MASK) { /* alt-num: add 10 to num */
ToggleNoteFlag(gCurImage, event->keyval - GDK_0 + 10);
return TRUE;
}
return FALSE;
case GDK_equal:
if (event->state & GDK_CONTROL_MASK) {
TryScale(1.25);
return TRUE;
}
return FALSE;
case GDK_KP_Subtract:
if (event->state & GDK_CONTROL_MASK) {
TryScale(.8);
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
/* Now we know no modifier keys were down. */
switch (event->keyval)
{
case GDK_d:
DeleteImage(gCurImage);
break;
case GDK_space:
case GDK_Page_Down:
case GDK_KP_Page_Down:
/* If we're in slideshow mode, cancel the slideshow */
if (gDelayMillis > 0) {
gDelayMillis = 0;
}
else if (NextImage() != 0) {
if (Prompt("Quit pho?", "Quit", "Continue", "qx \n", "cn") != 0)
EndSession();
}
return TRUE;
case GDK_BackSpace:
case GDK_Page_Up:
case GDK_KP_Page_Up:
PrevImage();
return TRUE;
case GDK_Home:
gCurImage = 0;
NextImage();
return TRUE;
case GDK_End:
gCurImage = gFirstImage->prev;
ThisImage();
return TRUE;
case GDK_n: /* Get out of any weird display modes */
SetViewModes(PHO_DISPLAY_NORMAL, PHO_SCALE_NORMAL, 1.);
ShowImage();
return TRUE;
case GDK_f: /* Full size mode: show image bit-for-bit */
SetViewModes(gDisplayMode,
ToggleBetween(gScaleMode,
PHO_SCALE_FULLSIZE, PHO_SCALE_NORMAL),
1.);
return TRUE;
case GDK_F: /* Full screen mode: as big as possible on screen */
SetViewModes(gDisplayMode,
ToggleBetween(gScaleMode,
PHO_SCALE_FULLSCREEN, PHO_SCALE_NORMAL),
1.);
return TRUE;
case GDK_p:
SetViewModes((gDisplayMode == PHO_DISPLAY_PRESENTATION)
? PHO_DISPLAY_NORMAL
: PHO_DISPLAY_PRESENTATION,
gScaleMode, gScaleRatio);
return TRUE;
case GDK_0:
case GDK_1:
case GDK_2:
case GDK_3:
case GDK_4:
case GDK_5:
case GDK_6:
case GDK_7:
case GDK_8:
case GDK_9:
ToggleNoteFlag(gCurImage, event->keyval - GDK_0);
return TRUE;
case GDK_t: /* make life easier for xv switchers */
case GDK_r:
case GDK_Right:
case GDK_KP_Right:
ScaleAndRotate(gCurImage, 90);
return TRUE;
case GDK_T: /* make life easier for xv users */
case GDK_R:
case GDK_l:
case GDK_L:
case GDK_Left:
case GDK_KP_Left:
ScaleAndRotate(gCurImage, 270);
return TRUE;
case GDK_Up:
case GDK_Down:
ScaleAndRotate(gCurImage, 180);
return TRUE;
case GDK_plus:
case GDK_KP_Add:
case GDK_equal:
TryScale(2.);
return TRUE;
case GDK_minus:
case GDK_slash:
case GDK_KP_Subtract:
TryScale(.5);
return TRUE;
case GDK_g: /* start gimp, or some other app */
RunPhoCommand();
return TRUE;
case GDK_i:
ToggleInfo();
return TRUE;
case GDK_k:
ToggleKeywordsMode();
return TRUE;
case GDK_o:
ChangeWorkingFileSet();
return TRUE;
case GDK_Escape:
case GDK_q:
EndSession();
return TRUE;
default:
if (gDebug)
printf("Don't know key 0x%lu\n", (unsigned long)(event->keyval));
return FALSE;
}
/* Keep gcc 2.95 happy: */
return FALSE;
}
PhoImage* AddImage(char* filename)
{
PhoImage* img = NewPhoImage(filename);
if (gDebug)
printf("Adding image %s\n", filename);
if (!img) {
fprintf(stderr, "Out of memory!\n");
exit(1);
}
/* Make img the new last image in the list */
AppendItem(img);
return img;
}
static void parseGeom(char* geom, int* width, int* height)
{
int num;
if (!isdigit(*geom)) {
printf("Geometry '%s' must start with a digit\n", geom);
Usage();
}
num = atoi(geom);
if (num > 0)
*width = num;
/* Now see if there's a height */
while (*(++geom)) {
if (*geom == 'x') {
++geom;
if (!isdigit(*geom)) {
printf("Number after 'x' in geometry must start with a digit, not %s\n", geom);
Usage();
}
num = atoi(geom);
if (num > 0)
*height = num;
return;
}
}
}
/* CheckArg takes a string, like -Pvg, and sets all the relevant flags. */
static void CheckArg(char* arg)
{
for ( ; *arg != 0; ++arg)
{
if (*arg == '-')
;
else if (*arg == 'd')
gDebug = 1;
else if (*arg == 'h')
Usage();
else if (*arg == 'v')
VerboseHelp();
else if (*arg == 'n')
gMakeNewWindows = 1;
else if (*arg == 'p') {
gDisplayMode = PHO_DISPLAY_PRESENTATION;
if (arg[1] == '\0') {
/* Normal presentation mode -- center everything
* and size to the current display.
*/
gPresentationWidth = 0;
gPresentationHeight = 0;
} else {
char* geom = arg+1;
if (*geom == 'p') ++geom;
gPresentationWidth = 0;
gPresentationHeight = 0;
parseGeom(geom, &gPresentationWidth, &gPresentationHeight);
}
} else if (*arg == 'P')
gDisplayMode = PHO_DISPLAY_NORMAL;
else if (*arg == 'k') {
gDisplayMode = PHO_DISPLAY_KEYWORDS;
gScaleMode = PHO_SCALE_FIXED;
gScaleRatio = 0.0;
} else if (*arg == 's') {
/* find the slideshow delay time, from e.g. pho -s2 */
if (isdigit(arg[1]) || arg[1] == '.')
gDelayMillis = (int)(atof(arg+1) * 1000.);
else Usage();
if (gDebug)
printf("Slideshow delay %d milliseconds\n", gDelayMillis);
} else if (*arg == 'r') {
gRepeat = 1;
} else if (*arg == 'c') {
gCapFileFormat = strdup(arg+1);
if (gDebug)
printf("Format set to '%s'\n", gCapFileFormat);
/* Can't follow this with any other letters -- they're all
* part of the filename -- so return.
*/
return;
}
}
}
int main(int argc, char** argv)
{
/* Initialize some defaults from environment variables,
* before reading cmdline args.
*/
int options = 1;
char* env = getenv("PHO_ARGS");
if (env && *env)
CheckArg(env);
while (argc > 1)
{
if (argv[1][0] == '-' && options) {
if (strcmp(argv[1], "--"))
CheckArg(argv[1]);
else
options = 0;
}
else {
AddImage(argv[1]);
}
--argc;
++argv;
}
if (gFirstImage == 0)
Usage();
/* Initialize some variables associated with the notes flags */
InitNotes();
/* See http://www.gtk.org/tutorial */
gtk_init(&argc, &argv);
/* Must init rgb system explicitly, else we'll crash
* in the first gdk_pixbuf_render_to_drawable(),
* calling gdk_draw_rgb_image_dithalign():
* (Is this still true in gtk2?)
*/
gdk_rgb_init();
gPhysMonitorWidth = gMonitorWidth = gdk_screen_width();
gPhysMonitorHeight = gMonitorHeight = gdk_screen_height();
/* Load the first image */
if (NextImage() != 0)
exit(1);
gtk_main();
return 0;
}