summaryrefslogtreecommitdiff
path: root/gui/gui.c
blob: defd125057e97a89f0ad9b065c70879ee555062b (plain)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
 * gui/gui.c - GUI for eeshow
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/*
 * Resources:
 *
 * http://zetcode.com/gfx/cairo/cairobackends/
 * https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html
 */

#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <gdk/gdkx.h>
#include <gtk/gtk.h>

#include "version.h"
#include "misc/util.h"
#include "misc/diag.h"
#include "file/git-hist.h"
#include "kicad/ext.h"
#include "kicad/pl.h"
#include "kicad/lib.h"
#include "kicad/sch.h"
#include "kicad/pro.h"
#include "kicad/delta.h"
#include "gui/aoi.h"
#include "gui/input.h"
#include "gui/common.h"
#include "gui/icons.h"
#include "gui/gui.h"


/* ----- Helper functions -------------------------------------------------- */


struct gui_sheet *find_corresponding_sheet(struct gui_sheet *pick_from,
    struct gui_sheet *ref_in, const struct gui_sheet *ref)
{
	struct gui_sheet *sheet, *plan_b;
	const char *title = ref->sch->title;

	/* plan A: try to find sheet with same name */

	if (title)
		for (sheet = pick_from; sheet; sheet = sheet->next)
			if (sheet->sch->title &&
			    !strcmp(title, sheet->sch->title))
				return sheet;

	/* plan B: use sheet in same position in sheet sequence */

	plan_b = ref_in;
	for (sheet = pick_from; sheet; sheet = sheet->next) {
		if (plan_b == ref)
			return sheet;
		plan_b = plan_b->next;
	}

	/* plan C: just go to the top */
	return pick_from;
}


/* ----- AoIs -------------------------------------------------------------- */


struct sheet_aoi_ctx {
	struct gui *gui;
	const struct sch_obj *obj;
};


static void select_subsheet(void *user)
{
	const struct sheet_aoi_ctx *aoi_ctx = user;
	struct gui *gui = aoi_ctx->gui;
	const struct sch_obj *obj = aoi_ctx->obj;
	struct gui_sheet *sheet;

	if (!obj->u.sheet.sheet)
		return;

	if (!gui->old_hist || gui->diff_mode != diff_old) {
		for (sheet = gui->new_hist->sheets; sheet; sheet = sheet->next)
			if (sheet->sch == obj->u.sheet.sheet) {
				go_to_sheet(gui, sheet);
				return;
			}
		BUG("new sheet not found");
	}

	for (sheet = gui->old_hist->sheets; sheet; sheet = sheet->next)
		if (sheet->sch == obj->u.sheet.sheet)
			goto found;
	BUG("old sheet not found");

found:
	sheet = find_corresponding_sheet(gui->new_hist->sheets,
	    gui->old_hist->sheets, sheet);
	go_to_sheet(gui, sheet);
}


static void add_sheet_aoi(struct gui *gui, struct gui_sheet *parent,
    const struct sch_obj *obj)
{
	struct sheet_aoi_ctx *aoi_ctx = alloc_type(struct sheet_aoi_ctx);

	aoi_ctx->gui = gui;
	aoi_ctx->obj = obj;

	struct aoi aoi = {
		.x	= obj->x,
		.y	= obj->y,
		.w	= obj->u.sheet.w,
		.h	= obj->u.sheet.h,
		.click	= select_subsheet,
		.user	= aoi_ctx,
	};

	aoi_add(&parent->aois, &aoi);
}


/* ----- Load revisions ---------------------------------------------------- */


void mark_aois(struct gui *gui, struct gui_sheet *sheet)
{
	const struct sch_obj *obj;

	sheet->aois = NULL;
	for (obj = sheet->sch->objs; obj; obj = obj->next)
		switch (obj->type) {
		case sch_obj_sheet:
			add_sheet_aoi(gui, sheet, obj);
			break;
		case sch_obj_glabel:
			add_glabel_aoi(sheet, obj);
			break;
		case sch_obj_comp:
			add_comp_aoi(sheet, obj);
			break;
		default:
			break;
		}
}


static struct gui_sheet *get_sheets(struct gui *gui, struct gui_hist *hist,
    const struct sheet *sheets)
{
	const struct sheet *sch;
	struct gui_sheet *gui_sheets = NULL;
	struct gui_sheet **next = &gui_sheets;
	struct gui_sheet *new;

	for (sch = sheets; sch; sch = sch->next) {
		new = alloc_type(struct gui_sheet);
		new->sch = sch;
		new->gui = gui;
		new->hist = hist;
		new->gfx_thumb = NULL;
		new->thumb_surf = NULL;
		new->rendered = 0;

		new->over = NULL;
		new->aois = NULL;

		*next = new;
		next = &new->next;
	}
	*next = NULL;
	return gui_sheets;
}


/*
 * Library caching:
 *
 * We reuse previous components if all libraries are identical
 *
 * Future optimizations:
 * - don't parse into single list of components, so that we can share
 *   libraries that are the same, even if there are others that have changed.
 * - maybe put components into tree, so that they can be replaced individually
 *   (this would also help to identify sheets that don't need parsing)
 *
 * Sheet caching:
 *
 * We reuse previous sheets if
 * - all libraries are identical (whether a given sheet uses them or not),
 * - they have no sub-sheets, and
 * - the objects IDs (hashes) are identical.
 *
 * Note that we only compare with the immediately preceding (newer) revision,
 * so branches and merges can disrupt caching.
 *
 * Possible optimizations:
 * - if we record which child sheets a sheet has, we could also clone it,
 *   without having to parse it. However, this is somewhat complex and may
 *   not save all that much time.
 * - we could record what libraries a sheet uses, and parse only if one of
 *   these has changed (benefits scenarios with many library files),
 * - we could record what components a sheet uses, and parse only if one of
 *   these has changed (benefits scenarios with few big libraries),
 * - we could postpone library lookups to render time.
 * - we could record IDs globally, which would help to avoid tripping over
 *   branches and merges.
 */

static const struct sheet *parse_files(struct gui_hist *hist,
    const struct file_names *fn, bool recurse, struct gui_hist *prev)
{
	char *rev = NULL;
	struct file pro_file, sch_file;
	const struct file *leader = NULL;
	unsigned libs_open, i;
	bool libs_cached = 0;
	bool ok;

	if (hist->vcs_hist && hist->vcs_hist->commit)
		rev = vcs_git_get_rev(hist->vcs_hist);

	if (fn->pro) {
		if (file_open_revision(&pro_file, rev, fn->pro, NULL)) {
			free(rev);
			rev = NULL; /* thus sch_file opens as with file_open */
			fn = pro_parse_file(&pro_file, fn);
			if (!fn)
				return NULL;
			leader = &pro_file;
		} else {
			/*
			 * If we happen to have a top sheet name, we may as
			 * well try to use it.
			 */
			if (!fn->sch) {
				free(rev);
				return NULL;
			}
		}
	}
	sch_init(&hist->sch_ctx, recurse);
	ok = file_open_revision(&sch_file, rev, fn->sch, leader);

	free(rev);
	if (!ok) {
		sch_free(&hist->sch_ctx);
		return NULL;
	}

	if (!leader)
		leader = &sch_file;

	struct file lib_files[fn->n_libs];

	lib_init(&hist->lib);
	libs_open = 0;
	for (i = 0; i != fn->n_libs; i++)
		if (lib_find_file(lib_files + libs_open, fn->libs[libs_open],
		    fn, leader))
			libs_open++;

	hist->pl = pl_parse_search(fn->pl, leader);
	/*
	 * We treat failing to load or parse the page layout as a "minor"
	 * failure and don't reject the revision just because of it.
	 */

	if (hist->vcs_hist) {
		hist->oids = alloc_type_n(void *, libs_open);
		hist->libs_open = libs_open;
		for (i = 0; i != libs_open; i++)
			hist->oids[i] = file_oid(lib_files + i);
		if (prev && prev->vcs_hist && prev->libs_open == libs_open) {
			for (i = 0; i != libs_open; i++)
				if (!file_oid_eq(hist->oids[i], prev->oids[i]))
					break;
			if (i == libs_open) {
				hist->lib.comps = prev->lib.comps;
				libs_cached = 1;
			}
		}
	}

	if (!libs_cached)
		for (i = 0; i != libs_open; i++)
			if (!lib_parse_file(&hist->lib, lib_files +i))
				goto fail;

	if (!sch_parse(&hist->sch_ctx, &sch_file, &hist->lib,
	    libs_cached ? &prev->sch_ctx : NULL))
		goto fail;

	for (i = 0; i != libs_open; i++)
		file_close(lib_files + i);
	file_close(&sch_file);
	// @@@ close pro_file

	/*
	 * sheet_eq recurses into sub-sheets, so we compare all sheets, even if
	 * it may look like it.
	 */

	if (prev && prev->sheets &&
	    sheet_eq(prev->sch_ctx.sheets, hist->sch_ctx.sheets, 1))
		prev->identical = 1;

	/*
	 * @@@ we have a major memory leak for the component library.
	 * We should record parsed schematics and libraries separately, so
	 * that we can clean them up, without having to rely on the history,
	 * with - when sharing unchanged item - possibly many duplicate
	 * pointers.
	 */
	return hist->sch_ctx.sheets;

fail:
	while (libs_open--)
		file_close(lib_files + libs_open);
	sch_free(&hist->sch_ctx);
	lib_free(&hist->lib);
	file_close(&sch_file);
	// @@@ close pro_file
	return NULL;
}


struct add_hist_ctx {
	struct gui *gui;
	const struct file_names *fn;
	bool recurse;
	unsigned limit;
};


static void add_hist(void *user, struct vcs_hist *h,
    const struct vcs_hist *next)
{
	struct add_hist_ctx *ahc = user;
	struct gui *gui = ahc->gui;
	struct gui_hist **anchor, *hist, *prev;
	const struct sheet *sch;
	unsigned age = 0;

	if (!ahc->limit)
		return;
	if (ahc->limit > 0)
		ahc->limit--;

	prev = NULL;
	for (anchor = &gui->hist; *anchor; anchor = &(*anchor)->next) {
		prev = *anchor;
		age++;
	}

	hist = alloc_type(struct gui_hist);
	hist->gui = gui;
	hist->vcs_hist = h;
	hist->libs_open = 0;
	hist->identical = 0;
	hist->pl = NULL;
	sch = parse_files(hist, ahc->fn, ahc->recurse, prev);
	hist->sheets = sch ? get_sheets(gui, hist, sch) : NULL;
	hist->age = age;

	hist->next = NULL;
	*anchor = hist;

	if (gui->hist_size)
		progress_update(gui);
}


static void get_revisions(struct gui *gui, const struct file_names *fn,
    bool recurse, int limit)
{
	struct add_hist_ctx add_hist_ctx = {
		.gui		= gui,
		.fn		= fn,
		.recurse	= recurse,
		.limit		= limit ? limit < 0 ? -limit : limit : -1,
	};

	if (gui->vcs_history)
		hist_iterate(gui->vcs_history, add_hist, &add_hist_ctx);
	else
		add_hist(&add_hist_ctx, NULL, NULL);
}


/* ----- Retrieve and count history ---------------------------------------- */


static void count_history(void *user, struct vcs_hist *h,
    const struct vcs_hist *next)
{
	struct gui *gui = user;

	gui->hist_size++;
}


static void get_history(struct gui *gui, const char *name, int limit)
{
	if (!vcs_git_try(name)) {
		gui->vcs_history = NULL;
		return;
	}

	if (limit < 0)
		limit = -limit;
	gui->vcs_history = vcs_git_history(name, limit);
	hist_iterate(gui->vcs_history, count_history, gui);
	if (limit && gui->hist_size > limit)
		gui->hist_size = limit;
}


/* ----- Resizing ---------------------------------------------------------- */


static void size_allocate_event(GtkWidget *widget, GdkRectangle *allocation,
    gpointer data)
{
	struct gui *gui = data;

	zoom_to_extents(gui);
	if (gui->mode == showing_index)
		index_resize(gui);
}


/* ----- Initialization ---------------------------------------------------- */


int run_gui(const struct file_names *fn, bool recurse, int limit,
    const char **commands, unsigned n_commands)
{
	GtkWidget *window;
	char *title;
	GtkSettings *settings = gtk_settings_get_default();
	struct gui gui = {
		.scale		= 1 / 16.0,
		.hist		= NULL,
		.vcs_history	= NULL,
		.mode		= showing_sheet,
		.sheet_overlays	= NULL,
		.hist_overlays	= NULL,
		.thumb_overlays	= NULL,
		.pop_overlays	= NULL,
		.pop_underlays	= NULL,
		.pop_origin	= NULL,
		.glabel		= NULL,
		.aois		= NULL,
		.diff_mode	= diff_delta,
		.old_hist	= NULL,
		.hist_y_offset	= 0,
		.hist_size	= 0,
	};

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gui.da = gtk_drawing_area_new();
	gtk_container_add(GTK_CONTAINER(window), gui.da);

	gtk_window_set_default_size(GTK_WINDOW(window), 640, 480);
	alloc_printf(&title, "eeshow (rev %s)", version);
	gtk_window_set_title(GTK_WINDOW(window), title);

	gtk_widget_set_events(gui.da,
	    GDK_EXPOSE | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);

	input_setup(gui.da);

	gtk_widget_show_all(window);

	get_history(&gui, fn->pro ? fn->pro : fn->sch, limit);
	if (gui.hist_size)
		setup_progress_bar(&gui, window);

	get_revisions(&gui, fn, recurse, limit);
	for (gui.new_hist = gui.hist; gui.new_hist && !gui.new_hist->sheets;
	    gui.new_hist = gui.new_hist->next);
	if (!gui.new_hist)
		fatal("no valid sheets\n");

	g_signal_connect(G_OBJECT(gui.da), "size_allocate",
	    G_CALLBACK(size_allocate_event), &gui);
	g_signal_connect(window, "destroy",
	    G_CALLBACK(gtk_main_quit), NULL);

	icons_init();
	sheet_setup(&gui);
	render_setup(&gui);

//	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

	/* http://gtk.10911.n7.nabble.com/Question-on-gtk-label-set-selectable-td9717.html */
	if (settings)
		g_object_set(settings, "gtk-label-select-on-focus",
		    FALSE, NULL);

	go_to_sheet(&gui, gui.new_hist->sheets);
	gtk_widget_show_all(window);

	if (n_commands) {
		long xid =
		    gdk_x11_window_get_xid(gtk_widget_get_window(window));
		char *s;

		alloc_printf(&s, "0x%lx", xid);
		setenv("EESHOW_WINDOW_ID", s, 1);

		while (n_commands--) {
			while (gtk_events_pending())
				gtk_main_iteration();

			system(*commands);
			commands++;
		}
	}

	/* for performance testing, use -N-depth */
	if (limit >= 0)
		gtk_main();

	return 0;
}