summaryrefslogtreecommitdiff
path: root/src/project.vala
blob: e7fab6191e3fa327c1c9e86995cc61ce6a9a85e8 (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
using Emulation;
using Gtk;
using GUI;

namespace Project
{
    public interface Manager : GLib.Object
    {
        public struct CSEntry
        {
            string name;
            CS cs;

            public CSEntry(string name, CS cs)
            {
                this.name = name;
                this.cs = cs;
            }
        }

        public abstract class Iterator
        {
            public abstract bool next();
            public new abstract CSEntry get();
        }

        public abstract Iterator iterator();
        public new abstract CSEntry get(uint index);
        public abstract uint length { get; }

        public abstract Iterator add_cs(string name, CS cs);
        public abstract void remove_cs(Iterator iter);
        public abstract void clear();

        public bool save(string filename)
        {
            return false;
        }

        public bool load(string filename)
        {
            return false;
        }
    }

    public class Window : Gtk.Window, Manager
    {
        private static const string xml_ui = """
        <?xml version="1.0"?>
        <interface>
          <requires lib="gtk+" version="2.16"/>
          <!-- interface-naming-policy project-wide -->
          <object class="GtkUIManager" id="uimanager1">
            <child>
              <object class="GtkActionGroup" id="actiongroup1">
                <child>
                  <object class="GtkAction" id="New">
                    <property name="name">New</property>
                    <property name="stock_id">gtk-new</property>
                  </object>
                </child>
                <child>
                  <object class="GtkAction" id="Open">
                    <property name="name">Open</property>
                    <property name="stock_id">gtk-open</property>
                  </object>
                </child>
                <child>
                  <object class="GtkAction" id="Save">
                    <property name="name">Save</property>
                    <property name="stock_id">gtk-save</property>
                  </object>
                </child>
                <child>
                  <object class="GtkAction" id="SaveAs">
                    <property name="name">SaveAs</property>
                    <property name="stock_id">gtk-save-as</property>
                  </object>
                </child>
                <child>
                  <object class="GtkAction" id="Quit">
                    <property name="name">Quit</property>
                    <property name="stock_id">gtk-quit</property>
                  </object>
                </child>
                <child>
                  <object class="GtkAction" id="ViewCS">
                    <property name="name">ViewCS</property>
                    <property name="label">_View CS</property>
                    <property name="tooltip">View the selected command stream.</property>
                    <property name="icon_name">gtk-open</property>
                  </object>
                </child>
                <child>
                  <object class="GtkAction" id="RemoveCS">
                    <property name="name">RemoveCS</property>
                    <property name="label">_Remove CS</property>
                    <property name="tooltip">Remove the selected command stream from the current project.</property>
                    <property name="icon_name">gtk-delete</property>
                  </object>
                  <accelerator key="Delete"/>
                </child>
                <child>
                  <object class="GtkAction" id="ImportCS">
                    <property name="name">ImportCS</property>
                    <property name="label">_Import CS</property>
                    <property name="tooltip">Import a new command stream to the current project.</property>
                    <property name="icon_name">gtk-add</property>
                  </object>
                  <accelerator key="i" modifiers="GDK_CONTROL_MASK"/>
                </child>
                <child>
                  <object class="GtkAction" id="ExportCS">
                    <property name="name">ExportCS</property>
                    <property name="label">_Export CS</property>
                    <property name="tooltip">Export the selected command stream to a file.</property>
                    <property name="icon_name">gtk-convert</property>
                  </object>
                  <accelerator key="e" modifiers="GDK_CONTROL_MASK"/>
                </child>
                <child>
                  <object class="GtkAction" id="About">
                    <property name="name">About</property>
                    <property name="stock_id">gtk-about</property>
                  </object>
                  <accelerator key="F1"/>
                </child>
                <child>
                  <object class="GtkAction" id="FileMenu">
                    <property name="name">FileMenu</property>
                    <property name="label" translatable="yes">_File</property>
                  </object>
                </child>
                <child>
                  <object class="GtkAction" id="EditMenu">
                    <property name="name">EditMenu</property>
                    <property name="label" translatable="yes">_Edit</property>
                  </object>
                </child>
                <child>
                  <object class="GtkAction" id="HelpMenu">
                    <property name="name">HelpMenu</property>
                    <property name="label" translatable="yes">_Help</property>
                  </object>
                </child>
              </object>
            </child>
            <ui>
              <menubar name="menubar1">
                <menu action="FileMenu" name="FileMenu">
                  <menuitem action="New" name="New"/>
                  <menuitem action="Open" name="Open"/>
                  <menuitem action="Save" name="Save"/>
                  <menuitem action="SaveAs" name="SaveAs"/>
                  <separator/>
                  <menuitem action="Quit" name="Quit"/>
                </menu>
                <menu action="EditMenu" name="EditMenu">
                  <menuitem action="ViewCS" name="ViewCS"/>
                  <menuitem action="ImportCS" name="ImportCS"/>
                  <menuitem action="ExportCS" name="ExportCS"/>
                  <separator/>
                  <menuitem action="RemoveCS" name="RemoveCS"/>
                </menu>
                <menu action="HelpMenu" name="HelpMenu">
                  <menuitem action="About" name="About"/>
                </menu>
              </menubar>
              <toolbar name="toolbar1">
                <toolitem action="New" name="New"/>
                <toolitem action="Open" name="Open"/>
                <toolitem action="Save" name="Save"/>
                <separator/>
                <toolitem action="ImportCS" name="ImportCS"/>
                <toolitem action="ExportCS" name="ExportCS"/>
                <separator/>
                <toolitem action="RemoveCS" name="RemoveCS"/>
              </toolbar>
              <popup name="popup1">
                <menuitem action="ViewCS" name="ViewCS"/>
                <menuitem action="ExportCS" name="ExportCS"/>
                <separator/>
                <menuitem action="RemoveCS" name="RemoveCS"/>
              </popup>
            </ui>
          </object>
          <object class="GtkVBox" id="mainbox">
            <property name="visible">True</property>
            <property name="orientation">vertical</property>
            <child>
              <object constructor="uimanager1" class="GtkMenuBar" id="menubar1">
                <property name="visible">True</property>
                <child internal-child="accessible">
                  <object class="AtkObject" id="a11y-menubar">
                    <property name="AtkObject::accessible-name">The menubar</property>
                  </object>
                </child>
              </object>
              <packing>
                <property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object constructor="uimanager1" class="GtkToolbar" id="toolbar1">
                  <property name="visible">True</property>
                  <child internal-child="accessible">
                      <object class="AtkObject" id="a11y-toolbar">
                          <property name="AtkObject::accessible-name">The toolbar</property>
                      </object>
                  </child>
              </object>
              <packing>
                  <property name="expand">False</property>
                  <property name="position">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkScrolledWindow" id="scrolledwindow1">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="hscrollbar_policy">automatic</property>
                <property name="vscrollbar_policy">automatic</property>
                <child>
                  <object class="GtkTreeView" id="cs_list">
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="reorderable">True</property>
                    <property name="model">cs_store</property>
                    <child>
                      <object class="GtkTreeViewColumn" id="pkts_column">
                        <property name="title">Packets</property>
                        <child>
                          <object class="GtkCellRendererText" id="pkts_cell"/>
                        </child>
                      </object>
                    </child>
                    <child>
                      <object class="GtkTreeViewColumn" id="treeviewcolumn1">
                        <property name="title">Name</property>
                        <child>
                          <object class="GtkCellRendererText" id="cellrenderertext1"/>
                          <attributes>
                            <attribute name="text">0</attribute>
                          </attributes>
                        </child>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
              <packing>
                <property name="position">2</property>
              </packing>
            </child>
          </object>
          <object class="GtkListStore" id="cs_store">
            <columns>
              <!-- column-name Name -->
              <column type="gchararray"/>
              <!-- column-name CS -->
              <column type="GObject"/>
            </columns>
          </object>
          <object constructor="uimanager1" class="GtkMenu" id="popup1">
            <child internal-child="accessible">
              <object class="AtkObject" id="a11y-popup">
                <property name="AtkObject::accessible-name">The popup menu</property>
              </object>
            </child>
          </object>
        </interface>
        """;

        private static const string about_xml_ui = """
        <?xml version="1.0"?>
        <interface>
          <requires lib="gtk+" version="2.16"/>
          <!-- interface-naming-policy project-wide -->
          <object class="GtkAboutDialog" id="about_dialog">
            <property name="border_width">5</property>
            <property name="type_hint">normal</property>
            <property name="skip_taskbar_hint">True</property>
            <property name="has_separator">False</property>
            <property name="program_name">Radeon Simulator</property>
            <property name="copyright" translatable="yes">Copyright &#xA9; 2010 Joakim Sindholt</property>
            <property name="comments" translatable="yes">A simulator for Radeon GPUs</property>
            <property name="website">http://cgit.freedesktop.org/~jsindholt/rsim</property>
            <property name="license" translatable="yes">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 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 General Public License along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</property>
            <property name="authors">Joakim Sindholt &lt;opensource@zhasha.com&gt;</property>
            <!-- This path shouldn't be hardcoded -->
            <property name="logo">/usr/share/pixmaps/rsim.png</property>
            <property name="wrap_license">True</property>
            <child internal-child="vbox">
              <object class="GtkVBox" id="dialog-vbox1">
                <property name="visible">True</property>
                <property name="orientation">vertical</property>
                <property name="spacing">2</property>
                <child internal-child="action_area">
                  <object class="GtkHButtonBox" id="dialog-action_area1">
                    <property name="visible">True</property>
                    <property name="layout_style">end</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="pack_type">end</property>
                    <property name="position">0</property>
                  </packing>
                </child>
              </object>
            </child>
          </object>
        </interface>
        """;

        private ListStore cs_store;
        private TreeView cs_list;
        private Menu edit_menu;

        private Action action_viewcs;
        private Action action_exportcs;
        private Action action_removecs;

        construct {
            var builder = new Builder();
            try {
                builder.add_from_string(xml_ui, xml_ui.length);
            } catch (Error e) {
                stderr.printf("Unable to construct main window: %s.\n", e.message);
                assert(false);
            }
            set_size_request(400, 200);
            title = "Radeon Simulator";

            /* Packets column */
            var col = builder.get_object("pkts_column") as TreeViewColumn;
            var cell = builder.get_object("pkts_cell") as CellRendererText;
            col.set_cell_data_func(cell, (col, cell, model, iter) => {
                CS cs;
                model.get(iter, 1, out cs, -1);
                assert(cs != null);
                (cell as CellRendererText).text = cs.length.to_string();
            });

            /* connect actions */
            action_viewcs = builder.get_object("ViewCS") as Action;
            action_exportcs = builder.get_object("ExportCS") as Action;
            action_removecs = builder.get_object("RemoveCS") as Action;
            var action_importcs = builder.get_object("ImportCS") as Action;

            action_viewcs.activate.connect((source) => { open_csview(); });
            action_importcs.activate.connect((source) => { var d = new CSImport(this); d.run(); });
            action_exportcs.activate.connect((source) => { open_csexport(); });
            action_removecs.activate.connect((source) => { remove_selected_cs(); });

            /* about dialog is kind of a special case. */
            (builder.get_object("About") as Action).activate.connect((source) => {
                var about = new Builder();
                try {
                    about.add_from_string(about_xml_ui, about_xml_ui.length);
                    var dlg = about.get_object("about_dialog") as AboutDialog;
                    dlg.run();
                    dlg.destroy();
                } catch (Error e) { }
            });

            /* menus */
            cs_store = builder.get_object("cs_store") as ListStore;
            cs_list = builder.get_object("cs_list") as TreeView;
            edit_menu = builder.get_object("popup1") as Menu;
            /* double clicking a row */
            cs_list.row_activated.connect((path, column) => { open_csview(); });
            /* right clicking */
            cs_list.button_press_event.connect((event) => {
                CS? cs = get_selected_cs();
                if (cs != null && event.type == Gdk.EventType.BUTTON_PRESS && event.button == 3) {
                    edit_menu.popup(null, null, null, event.button, event.time);
                }
            });
            /* selecting something */
            cs_list.cursor_changed.connect((source) => {
                edit_set_sensitive(cs_list.get_selection().get_selected(null, null));
            });
            edit_set_sensitive(false);

            add(builder.get_object("mainbox") as Widget);
            destroy.connect(main_quit);

            try {
                /* XXX This shouldn't be hardcoded */
                set_icon_from_file("/usr/share/pixmaps/rsim.png");
            } catch (Error e) { /* don't care */ }
        }

        /* helpers */
        private void edit_set_sensitive(bool setting)
        {
            action_viewcs.sensitive = setting;
            action_exportcs.sensitive = setting;
            action_removecs.sensitive = setting;
        }

        private CS? get_selected_cs()
        {
            TreeIter iter;
            CS cs;

            var selection = cs_list.get_selection();
            if (!selection.get_selected(null, out iter)) { return null; }

            cs_store.get(iter, 1, out cs, -1);
            return cs;
        }

        private void open_csview()
        {
            CS? cs = get_selected_cs();
            if (cs != null) {
                var csv = new CSView(cs);
                csv.show_all();
            }
        }

        private void open_csexport()
        {
            CS? cs = get_selected_cs();
            if (cs != null) {
                var d = new CSExport(cs);
                d.run();
            }
        }

        private void remove_selected_cs()
        {
            TreeIter iter;

            var selection = cs_list.get_selection();
            if (!selection.get_selected(null, out iter)) { return; }

            remove_cs(new Iterator(cs_store, iter, true));
        }

        /*** BELOW: interface Manager ***/
        public class Iterator : Manager.Iterator
        {
            private ListStore model;
            internal TreeIter iter;
            private bool first;
            private bool valid;

            public Iterator(ListStore model, TreeIter iter, bool valid = true)
            {
                this.model = model;
                this.iter = iter;
                this.first = true;
                this.valid = valid;
            }

            public override bool next()
            {
                if (first) {
                    first = false;
                    return valid;
                }
                return model.iter_next(ref iter);
            }

            public new override Manager.CSEntry get()
            {
                string name;
                CS cs;
                model.get(iter, 0, out name, 1, out cs, -1);
                return Manager.CSEntry(name, cs);
            }
        }

        public Manager.Iterator iterator()
        {
            TreeIter iter;
            var valid = cs_store.get_iter_first(out iter);
            return new Iterator(cs_store, iter, valid);
        }

        public new Manager.CSEntry get(uint index)
        {
            TreeIter iter;
            TreePath path = new TreePath.from_indices((int)index, -1);
            var valid = cs_store.get_iter(out iter, path);
            return (new Iterator(cs_store, iter, valid)).get();
        }

        public uint length { get { return (uint)cs_store.iter_n_children(null); } }

        public Manager.Iterator add_cs(string name, CS cs)
        {
            TreeIter iter;
            cs_store.append(out iter);
            cs_store.set(iter, 0, name, 1, cs, -1);
            return new Iterator(cs_store, iter);
        }

        public void remove_cs(Manager.Iterator iter)
        {
            TreeIter it = (iter as Iterator).iter;

            string name;
            cs_store.get(it, 0, out name, -1);

            var d = new MessageDialog(this, DialogFlags.DESTROY_WITH_PARENT,
                                      MessageType.QUESTION, ButtonsType.YES_NO,
                                      "Are you sure you want to delete the command stream '%s'", name);
            int result = d.run();
            d.destroy();
            if (result != ResponseType.YES) { return; }

            cs_store.remove(it);
        }

        public void clear()
        {
            var d = new MessageDialog(this, DialogFlags.DESTROY_WITH_PARENT,
                                      MessageType.QUESTION, ButtonsType.YES_NO,
                                      "Are you sure you want to clear all command streams?");
            int result = d.run();
            d.destroy();
            if (result != ResponseType.YES) { return; }

            cs_store.clear();
        }
    }
}