summaryrefslogtreecommitdiff
path: root/src/main.vala
blob: 091da5d8d44b66362e72666fbc841b63c4d48f71 (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
/*
 * Copyright 2010 Joakim Sindholt <opensource@zhasha.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE. */

using Emulation;
using Gtk;

namespace GUI
{
    public class MainWindow : Window
    {
        private static 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="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="stock_id">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="stock_id">gtk-add</property>
                  </object>
                </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="stock_id">gtk-convert</property>
                  </object>
                </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="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="ExportCS" name="ExportCS"/>
                <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="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 ListStore cs_store;
        private TreeView cs_list;
        private Menu edit_menu;

        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_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_importcs.activate.connect((source) => { var d = new CSImport(); d.run(); });
            action_exportcs.activate.connect((source) => { open_csexport(); });
            action_removecs.activate.connect((source) => { remove_cs(); });

            /* 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 */ }
        }

        private void edit_set_sensitive(bool 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;
        }

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

        public void remove_cs()
        {
            TreeIter iter;

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

            cs_store.remove(iter);
        }

        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();
            }
        }
    }

    public static MainWindow main;

    static void init(ref weak string[] args)
    {
        Gtk.init(ref args);

        specs = new SpecEntry[2];
        specs[0].spec = new Spec("/usr/share/rsim/r300reg.xml", "r300");
        specs[0].name = "r300";
        specs[1].spec = new Spec("/usr/share/rsim/r300reg.xml", "r500");
        specs[1].name = "r500";

        main = new MainWindow();
        main.show_all();

        Gtk.main();
    }
}

namespace Emulation
{
    public struct SpecEntry
    {
        Spec spec;
        string name;
    }

    public SpecEntry[] specs;
    public uint spec_default = 1;
}

public static int main(string[] args)
{
    if (!Thread.supported()) {
        stderr.printf("Cannot run without threads.\n");
        return 1;
    }

    GUI.init(ref args);

    /* vala doesn't unref globals */
    for (uint i = 0; i < specs.length; i++) {
        specs[i].spec.unref();
    }
    GUI.main.unref();

    return 0;
}