2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "sigrok-gtk.h"
41 static void prop_edited(GtkCellRendererText *cel, gchar *path, gchar *text,
46 struct sr_device *device = g_object_get_data(G_OBJECT(props), "device");
52 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(props), &iter, path);
53 gtk_tree_model_get(GTK_TREE_MODEL(props), &iter,
54 DEV_PROP_CAPABILITY, &cap,
55 DEV_PROP_TYPE, &type, -1);
59 if (sr_parse_sizestring(text, &tmp_u64) != SR_OK)
62 ret = device->plugin->set_configuration(device->plugin_index,
66 ret = device->plugin-> set_configuration(device->plugin_index,
69 /* SR_T_BOOL will be handled by prop_toggled */
73 gtk_list_store_set(props, &iter, DEV_PROP_TEXTVALUE, text, -1);
76 static void prop_toggled(GtkCellRendererToggle *cel, gchar *path,
79 struct sr_device *device = g_object_get_data(G_OBJECT(props), "device");
84 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(props), &iter, path);
85 gtk_tree_model_get(GTK_TREE_MODEL(props), &iter,
86 DEV_PROP_CAPABILITY, &cap,
87 DEV_PROP_TYPE, &type, -1);
89 val = !gtk_cell_renderer_toggle_get_active(cel);
90 ret = device->plugin-> set_configuration(device->plugin_index, cap,
91 GINT_TO_POINTER(val));
94 gtk_list_store_set(props, &iter, DEV_PROP_BOOLVALUE, val, -1);
97 void dev_prop_bool_data_func(GtkCellLayout *cell_layout,
98 GtkCellRenderer *cell,
99 GtkTreeModel *tree_model,
106 gboolean istext, val;
107 gtk_tree_model_get(tree_model, iter,
108 DEV_PROP_IS_TEXT, &istext,
109 DEV_PROP_BOOLVALUE, &val, -1);
110 g_object_set(G_OBJECT(cell), "visible", !istext, "active", val, NULL);
113 static void dev_set_options(GtkAction *action, GtkWindow *parent)
117 struct sr_device *device = g_object_get_data(G_OBJECT(parent), "device");
121 GtkWidget *dialog = gtk_dialog_new_with_buttons("Device Properties",
122 parent, GTK_DIALOG_MODAL,
123 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
125 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
126 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
127 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
128 gtk_widget_set_size_request(sw, 300, 200);
129 GtkWidget *tv = gtk_tree_view_new();
130 gtk_container_add(GTK_CONTAINER(sw), tv);
131 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), sw,
134 /* Populate list store with config options */
135 GtkListStore *props = gtk_list_store_new(MAX_DEV_PROP,
136 G_TYPE_INT, G_TYPE_INT,
137 G_TYPE_STRING, G_TYPE_STRING,
138 G_TYPE_BOOLEAN, G_TYPE_STRING,
140 gtk_tree_view_set_model(GTK_TREE_VIEW(tv), GTK_TREE_MODEL(props));
141 int *capabilities = device->plugin->get_capabilities();
144 for (cap = 0; capabilities[cap]; cap++) {
145 struct sr_hwcap_option *hwo;
146 if (!(hwo = sr_find_hwcap_option(capabilities[cap])))
148 gtk_list_store_append(props, &iter);
149 gtk_list_store_set(props, &iter,
150 DEV_PROP_CAPABILITY, capabilities[cap],
151 DEV_PROP_TYPE, hwo->type,
152 DEV_PROP_SHORTNAME, hwo->shortname,
153 DEV_PROP_DESCRIPTION, hwo->description,
154 DEV_PROP_IS_TEXT, hwo->type != SR_T_BOOL,
158 /* Popup tooltop containing description if mouse hovers */
159 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(tv),
160 DEV_PROP_DESCRIPTION);
162 /* Save device with list so that property can be set by edited
164 g_object_set_data(G_OBJECT(props), "device", device);
166 /* Add columns to the tree view */
167 GtkTreeViewColumn *col;
168 col = gtk_tree_view_column_new_with_attributes("Property",
169 gtk_cell_renderer_text_new(),
170 "text", DEV_PROP_SHORTNAME, NULL);
171 gtk_tree_view_append_column(GTK_TREE_VIEW(tv), col);
172 /* We pack both a text and toggle renderer. Only one will be visible.
175 GtkCellRenderer *cel = gtk_cell_renderer_text_new();
176 g_object_set(cel, "editable", TRUE, NULL);
177 g_signal_connect(cel, "edited", G_CALLBACK(prop_edited), props);
178 col = gtk_tree_view_column_new_with_attributes("Value",
179 cel, "text", DEV_PROP_TEXTVALUE,
180 "visible", DEV_PROP_IS_TEXT, NULL);
181 cel = gtk_cell_renderer_toggle_new();
182 g_signal_connect(cel, "toggled", G_CALLBACK(prop_toggled), props);
183 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(col), cel, TRUE);
184 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(col), cel,
185 dev_prop_bool_data_func, NULL, NULL);
186 gtk_tree_view_append_column(GTK_TREE_VIEW(tv), col);
189 gtk_widget_show_all(dialog);
190 gtk_dialog_run(GTK_DIALOG(dialog));
192 gtk_widget_destroy(dialog);
203 static void probe_toggled(GtkCellRenderer *cel, gchar *path,
204 GtkTreeModel *probes)
206 struct sr_device *device = g_object_get_data(G_OBJECT(probes), "device");
208 struct sr_probe *probe;
214 gtk_tree_model_get_iter_from_string(probes, &iter, path);
215 gtk_tree_model_get(probes, &iter, PROBE_NUMBER, &i,
216 PROBE_ENABLED, &en, -1);
217 probe = sr_device_probe_find(device, i);
218 probe->enabled = !en;
219 gtk_list_store_set(GTK_LIST_STORE(probes), &iter,
220 PROBE_ENABLED, probe->enabled, -1);
223 static void probe_named(GtkCellRendererText *cel, gchar *path, gchar *text,
224 GtkTreeModel *probes)
226 struct sr_device *device = g_object_get_data(G_OBJECT(probes), "device");
232 gtk_tree_model_get_iter_from_string(probes, &iter, path);
233 gtk_tree_model_get(probes, &iter, PROBE_NUMBER, &i, -1);
234 sr_device_probe_name(device, i, text);
235 gtk_list_store_set(GTK_LIST_STORE(probes), &iter, PROBE_NAME, text, -1);
238 static void probe_trigger_set(GtkCellRendererText *cel, gchar *path,
239 gchar *text, GtkTreeModel *probes)
241 struct sr_device *device = g_object_get_data(G_OBJECT(probes), "device");
247 gtk_tree_model_get_iter_from_string(probes, &iter, path);
248 gtk_tree_model_get(probes, &iter, PROBE_NUMBER, &i, -1);
249 sr_device_trigger_set(device, i, text);
250 gtk_list_store_set(GTK_LIST_STORE(probes), &iter,
251 PROBE_TRIGGER, text, -1);
254 static void dev_set_probes(GtkAction *action, GtkWindow *parent)
258 struct sr_device *device = g_object_get_data(G_OBJECT(parent), "device");
262 GtkWidget *dialog = gtk_dialog_new_with_buttons("Configure Probes",
263 parent, GTK_DIALOG_MODAL,
264 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
266 GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
267 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
268 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
269 gtk_widget_set_size_request(sw, 300, 200);
270 GtkWidget *tv = gtk_tree_view_new();
271 gtk_container_add(GTK_CONTAINER(sw), tv);
272 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), sw,
275 /* Populate list store with probe options */
276 GtkListStore *probes = gtk_list_store_new(MAX_PROBE,
277 G_TYPE_INT, G_TYPE_BOOLEAN,
278 G_TYPE_STRING, GTK_TYPE_STRING);
279 gtk_tree_view_set_model(GTK_TREE_VIEW(tv), GTK_TREE_MODEL(probes));
283 for (p = device->probes, i = 1; p; p = g_slist_next(p), i++) {
284 struct sr_probe *probe = p->data;
285 gtk_list_store_append(probes, &iter);
286 gtk_list_store_set(probes, &iter, PROBE_NUMBER, i,
287 PROBE_ENABLED, probe->enabled,
288 PROBE_NAME, probe->name,
289 PROBE_TRIGGER, probe->trigger,
293 /* Save device with list so that property can be set by edited
295 g_object_set_data(G_OBJECT(probes), "device", device);
297 /* Add columns to the tree view */
298 GtkTreeViewColumn *col;
299 col = gtk_tree_view_column_new_with_attributes("Probe",
300 gtk_cell_renderer_text_new(),
301 "text", PROBE_NUMBER, NULL);
302 gtk_tree_view_append_column(GTK_TREE_VIEW(tv), col);
303 GtkCellRenderer *cel = gtk_cell_renderer_toggle_new();
304 g_object_set(cel, "activatable", TRUE, NULL);
305 g_signal_connect(cel, "toggled", G_CALLBACK(probe_toggled), probes);
306 col = gtk_tree_view_column_new_with_attributes("En",
307 cel, "active", PROBE_ENABLED, NULL);
308 gtk_tree_view_append_column(GTK_TREE_VIEW(tv), col);
309 cel = gtk_cell_renderer_text_new();
310 g_object_set(cel, "editable", TRUE, NULL);
311 g_signal_connect(cel, "edited", G_CALLBACK(probe_named), probes);
312 col = gtk_tree_view_column_new_with_attributes("Signal Name", cel,
314 "sensitive", PROBE_ENABLED, NULL);
315 gtk_tree_view_column_set_resizable(col, TRUE);
316 gtk_tree_view_append_column(GTK_TREE_VIEW(tv), col);
317 cel = gtk_cell_renderer_text_new();
318 g_object_set(cel, "editable", TRUE, NULL);
319 g_signal_connect(cel, "edited", G_CALLBACK(probe_trigger_set), probes);
320 col = gtk_tree_view_column_new_with_attributes("Trigger", cel,
321 "text", PROBE_TRIGGER,
322 "sensitive", PROBE_ENABLED, NULL);
323 gtk_tree_view_append_column(GTK_TREE_VIEW(tv), col);
325 gtk_widget_show_all(dialog);
326 gtk_dialog_run(GTK_DIALOG(dialog));
328 gtk_widget_destroy(dialog);
331 static void capture_run(GtkAction *action, GObject *parent)
335 struct sr_device *device = g_object_get_data(G_OBJECT(parent), "device");
336 GtkEntry *timesamples = g_object_get_data(parent, "timesamples");
337 GtkComboBox *timeunit = g_object_get_data(parent, "timeunit");
338 gint i = gtk_combo_box_get_active(timeunit);
339 guint64 time_msec = 0;
340 guint64 limit_samples = 0;
343 case 0: /* Samples */
344 sr_parse_sizestring(gtk_entry_get_text(timesamples),
347 case 1: /* Milliseconds */
348 time_msec = strtoull(gtk_entry_get_text(timesamples), NULL, 10);
350 case 2: /* Seconds */
351 time_msec = strtoull(gtk_entry_get_text(timesamples), NULL, 10)
357 int *capabilities = device->plugin->get_capabilities();
358 if (sr_find_hwcap(capabilities, SR_HWCAP_LIMIT_MSEC)) {
359 if (device->plugin->set_configuration(device->plugin_index,
361 &time_msec) != SR_OK) {
362 g_critical("Failed to configure time limit.");
363 sr_session_destroy();
367 /* time limit set, but device doesn't support this...
368 * convert to samples based on the samplerate.
371 if (sr_device_has_hwcap(device, SR_HWCAP_SAMPLERATE)) {
373 tmp_u64 = *((uint64_t *) device->plugin->get_device_info(
374 device->plugin_index,
375 SR_DI_CUR_SAMPLERATE));
376 limit_samples = tmp_u64 * time_msec / (uint64_t) 1000;
378 if (limit_samples == 0) {
379 g_critical("Not enough time at this samplerate.");
383 if (device->plugin->set_configuration(device->plugin_index,
384 SR_HWCAP_LIMIT_SAMPLES,
385 &limit_samples) != SR_OK) {
386 g_critical("Failed to configure time-based sample limit.");
392 if (device->plugin->set_configuration(device->plugin_index,
393 SR_HWCAP_LIMIT_SAMPLES,
394 &limit_samples) != SR_OK) {
395 g_critical("Failed to configure sample limit.");
400 if (device->plugin->set_configuration(device->plugin_index,
401 SR_HWCAP_PROBECONFIG, (char *)device->probes) != SR_OK) {
402 printf("Failed to configure probes.\n");
403 sr_session_destroy();
407 if (sr_session_start() != SR_OK) {
408 g_critical("Failed to start session.");
415 static void dev_file_open(GtkAction *action, GtkWindow *parent)
418 static GtkWidget *dialog;
419 const gchar *filename;
422 dialog = gtk_file_chooser_dialog_new("Open", parent,
423 GTK_FILE_CHOOSER_ACTION_OPEN,
424 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
425 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
426 g_signal_connect(dialog, "delete-event",
427 G_CALLBACK(gtk_widget_hide_on_delete),
430 if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT) {
431 /* Dialog was cancelled or closed */
432 gtk_widget_hide(dialog);
436 gtk_widget_hide(dialog);
438 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
439 load_input_file(parent, filename);
442 void toggle_log(GtkToggleAction *action, GObject *parent)
444 GtkWidget *log = g_object_get_data(parent, "logview");
445 gtk_widget_set_visible(log, gtk_toggle_action_get_active(action));
448 void zoom_in(GtkAction *action, GObject *parent)
452 GtkWidget *sigview = g_object_get_data(parent, "sigview");
453 sigview_zoom(sigview, 1.5, 0);
456 void zoom_out(GtkAction *action, GObject *parent)
460 GtkWidget *sigview = g_object_get_data(parent, "sigview");
461 sigview_zoom(sigview, 1/1.5, 0);
464 void zoom_fit(GtkAction *action, GObject *parent)
468 GtkWidget *sigview = g_object_get_data(parent, "sigview");
469 sigview_zoom(sigview, 0, 0);
472 static const GtkActionEntry action_items[] = {
473 /* name, stock-id, label, accel, tooltip, callback */
474 {"DevMenu", NULL, "_Device", NULL, NULL, NULL},
475 {"DevOpen", GTK_STOCK_OPEN, "_Open", "<control>O",
476 "Open Session File", G_CALLBACK(dev_file_open)},
477 {"DevSelectMenu", NULL, "Select Device", NULL, NULL, NULL},
478 {"DevRescan", GTK_STOCK_REFRESH, "_Rescan", "<control>R",
479 "Rescan for LA devices", G_CALLBACK(dev_select_rescan)},
480 {"DevProperties", GTK_STOCK_PROPERTIES, "_Properties", "<control>P",
481 "Configure LA", G_CALLBACK(dev_set_options)},
482 {"DevProbes", GTK_STOCK_COLOR_PICKER, "_Probes", "<control>O",
483 "Configure Probes", G_CALLBACK(dev_set_probes)},
484 {"DevAcquire", GTK_STOCK_EXECUTE, "_Acquire", "<control>A",
485 "Acquire Samples", G_CALLBACK(capture_run)},
486 {"Exit", GTK_STOCK_QUIT, "E_xit", "<control>Q",
487 "Exit the program", G_CALLBACK(gtk_main_quit) },
489 {"ViewMenu", NULL, "_View", NULL, NULL, NULL},
490 {"ViewZoomIn", GTK_STOCK_ZOOM_IN, "Zoom _In", "<control>z", NULL,
491 G_CALLBACK(zoom_in)},
492 {"ViewZoomOut", GTK_STOCK_ZOOM_OUT, "Zoom _Out", "<control><shift>Z",
493 NULL, G_CALLBACK(zoom_out)},
494 {"ViewZoomFit", GTK_STOCK_ZOOM_FIT, NULL, NULL,
495 NULL, G_CALLBACK(zoom_fit)},
497 {"HelpMenu", NULL, "_Help", NULL, NULL, NULL},
498 {"HelpWiki", GTK_STOCK_ABOUT, "Sigrok _Wiki", NULL, NULL,
499 G_CALLBACK(help_wiki)},
500 {"HelpAbout", GTK_STOCK_ABOUT, "_About", NULL, NULL,
501 G_CALLBACK(help_about)},
504 static const GtkToggleActionEntry toggle_items[] = {
505 /* name, stock-id, label, accel, tooltip, callback, isactive */
506 {"ViewLog", GTK_STOCK_JUSTIFY_LEFT, "_Log", NULL, NULL,
507 G_CALLBACK(toggle_log), FALSE},
510 static const char ui_xml[] =
513 " <menu action='DevMenu'>"
514 " <menuitem action='DevOpen'/>"
516 " <menu action='DevSelectMenu'>"
518 " <menuitem action='DevRescan'/>"
520 " <menuitem action='DevProperties'/>"
521 " <menuitem action='DevProbes'/>"
523 " <menuitem action='DevAcquire'/>"
525 " <menuitem action='Exit'/>"
527 " <menu action='ViewMenu'>"
528 " <menuitem action='ViewZoomIn'/>"
529 " <menuitem action='ViewZoomOut'/>"
530 " <menuitem action='ViewZoomFit'/>"
532 " <menuitem action='ViewLog'/>"
534 " <menu action='HelpMenu'>"
535 " <menuitem action='HelpWiki'/>"
536 " <menuitem action='HelpAbout'/>"
540 " <placeholder name='DevSelect'/>"
541 " <toolitem action='DevRescan'/>"
542 " <toolitem action='DevProperties'/>"
543 " <toolitem action='DevProbes'/>"
545 " <placeholder name='DevSampleCount' />"
546 " <toolitem action='DevAcquire'/>"
548 " <toolitem action='ViewZoomIn'/>"
549 " <toolitem action='ViewZoomOut'/>"
550 " <toolitem action='ViewZoomFit'/>"
555 GtkWidget *toolbar_init(GtkWindow *parent)
557 GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
559 GtkActionGroup *ag = gtk_action_group_new("Actions");
560 gtk_action_group_add_actions(ag, action_items,
561 G_N_ELEMENTS(action_items), parent);
562 gtk_action_group_add_toggle_actions(ag, toggle_items,
563 G_N_ELEMENTS(toggle_items), parent);
565 GtkUIManager *ui = gtk_ui_manager_new();
566 g_object_set_data(G_OBJECT(parent), "ui_manager", ui);
567 gtk_ui_manager_insert_action_group(ui, ag, 0);
568 GtkAccelGroup *accel = gtk_ui_manager_get_accel_group(ui);
569 gtk_window_add_accel_group(parent, accel);
571 GError *error = NULL;
572 if (!gtk_ui_manager_add_ui_from_string (ui, ui_xml, -1, &error)) {
573 g_message ("building menus failed: %s", error->message);
574 g_error_free (error);
578 GtkWidget *menubar = gtk_ui_manager_get_widget(ui, "/menubar");
579 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(menubar), FALSE, TRUE, 0);
580 toolbar = GTK_TOOLBAR(gtk_ui_manager_get_widget(ui, "/toolbar"));
581 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(toolbar), FALSE, TRUE, 0);
583 /* Device selection GtkComboBox */
584 GtkToolItem *toolitem = gtk_tool_item_new();
585 GtkWidget *align = gtk_alignment_new(0.5, 0.5, 2, 0);
586 GtkWidget *dev = dev_select_combo_box_new(parent);
588 gtk_container_add(GTK_CONTAINER(align), dev);
589 gtk_container_add(GTK_CONTAINER(toolitem), align);
590 gtk_toolbar_insert(toolbar, toolitem, 0);
592 /* Time/Samples entry */
593 toolitem = gtk_tool_item_new();
594 GtkWidget *timesamples = gtk_entry_new();
595 gtk_entry_set_text(GTK_ENTRY(timesamples), "100");
596 gtk_entry_set_alignment(GTK_ENTRY(timesamples), 1.0);
597 gtk_widget_set_size_request(timesamples, 100, -1);
598 gtk_container_add(GTK_CONTAINER(toolitem), timesamples);
599 gtk_toolbar_insert(toolbar, toolitem, 7);
601 /* Time unit combo box */
602 toolitem = gtk_tool_item_new();
603 align = gtk_alignment_new(0.5, 0.5, 2, 0);
604 GtkWidget *timeunit = gtk_combo_box_new_text();
605 gtk_combo_box_append_text(GTK_COMBO_BOX(timeunit), "samples");
606 gtk_combo_box_append_text(GTK_COMBO_BOX(timeunit), "ms");
607 gtk_combo_box_append_text(GTK_COMBO_BOX(timeunit), "s");
608 gtk_combo_box_set_active(GTK_COMBO_BOX(timeunit), 0);
609 gtk_container_add(GTK_CONTAINER(align), timeunit);
610 gtk_container_add(GTK_CONTAINER(toolitem), align);
611 gtk_toolbar_insert(toolbar, toolitem, 8);
613 g_object_set_data(G_OBJECT(parent), "timesamples", timesamples);
614 g_object_set_data(G_OBJECT(parent), "timeunit", timeunit);
616 return GTK_WIDGET(vbox);