summaryrefslogtreecommitdiff
path: root/src/main.vala
blob: 177a198ab6f891a0f6af86ffb07c50e4ba0e75cf (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
using Emulation;
using Gtk;

namespace GUI
{
    public class CActions : GLib.Object
    {
        public Action New;
        public Action Open;
        public Action Save;
        public Action SaveAs;
        public Action RemoveCS;
        public Action ImportCS;
        public Action ExportCS;
        public Action Quit;

        construct {
            New = new Action("New", null, null, "gtk-new");
            Open = new Action("Open", null, null, "gtk-open");
            Save = new Action("Save", null, null, "gtk-save");
            SaveAs = new Action("SaveAs", null, null, "gtk-save-as");
            RemoveCS = new Action("RemoveCS", "Remove", "Remove selected CS' from the list", "gtk-remove");
            ImportCS = new Action("ImportCS", "Import", "Import a command stream to the list", "gtk-add");
            ExportCS = new Action("ExportCS", "Export", "Export a CS to a file", "gtk-convert");
            Quit = new Action("Quit", null, null, "gtk-quit");

            ImportCS.activate.connect((source) => { var d = new CSImport(); d.run(); });
        }
    }

    public class CMenus
    {
        public Menu file;
        public Menu edit;
    }

    public class CMain : Window
    {
        public CMenus menu;
        public MenuBar menubar;
        public Toolbar toolbar;
        public ListStore cs_store;

        construct {
            menu = new CMenus();

            toolbar = new Toolbar();
            add_toolitem(actions.New);
            add_toolitem(actions.Open);
            add_toolitem(actions.Save);
            toolbar.insert(new SeparatorToolItem() as ToolItem, -1);
            add_toolitem(actions.RemoveCS);
            add_toolitem(actions.ImportCS);
            add_toolitem(actions.ExportCS);

            menu.file = new Menu();
            menu.file.append(actions.New.create_menu_item() as MenuItem);
            menu.file.append(actions.Open.create_menu_item() as MenuItem);
            menu.file.append(actions.Save.create_menu_item() as MenuItem);
            menu.file.append(actions.SaveAs.create_menu_item() as MenuItem);
            menu.file.append(new SeparatorMenuItem() as MenuItem);
            menu.file.append(actions.Quit.create_menu_item() as MenuItem);
            var menu_file_item = new MenuItem.with_label("File");
            menu_file_item.submenu = menu.file;

            menu.edit = new Menu();
            menu.edit.append(actions.RemoveCS.create_menu_item() as MenuItem);
            menu.edit.append(actions.ImportCS.create_menu_item() as MenuItem);
            menu.edit.append(actions.ExportCS.create_menu_item() as MenuItem);
            var menu_edit_item = new MenuItem.with_label("Edit");
            menu_edit_item.submenu = menu.edit;

            menubar = new MenuBar();
            menubar.append(menu_file_item);
            menubar.append(menu_edit_item);

            cs_store = new ListStore(2, typeof(string), typeof(CS));
            var cslist = new TreeView();
            cslist.insert_column_with_data_func(-1, "DWORDs", new CellRendererText(), dwords_cellrenderer);
            cslist.insert_column_with_attributes(-1, "Name", new CellRendererText(), "text", 0, null);
            cslist.set_model(cs_store);
            cslist.row_activated.connect(open_csview);

            var sw = new ScrolledWindow(null, null);
            sw.hscrollbar_policy = PolicyType.AUTOMATIC;
            sw.vscrollbar_policy = PolicyType.AUTOMATIC;
            sw.add(cslist);

            var mainbox = new VBox(false, 0);
            mainbox.pack_start(menubar, false, true, 0);
            mainbox.pack_start(toolbar, false, true, 0);
            mainbox.pack_start(sw, true, true, 0);

            set_size_request(400, 200);
            title = "Radeon Simulator";
            add(mainbox);
            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 */ }

            show_all();
        }

        private void add_toolitem(Action action)
        {
            ToolItem titem = action.create_tool_item() as ToolItem;
            titem.set_homogeneous(false);
            toolbar.insert(titem, -1);
        }

        private void dwords_cellrenderer(TreeViewColumn tree_column, CellRenderer cell, TreeModel model, TreeIter iter)
        {
            CS cs;
            model.get(iter, 1, out cs, -1);
            assert(cs != null);
            (cell as CellRendererText).text = cs.dwords.length.to_string();
        }

        private void open_csview(TreePath path, TreeViewColumn column)
        {
            TreeIter iter;
            CS cs;
            cs_store.get_iter(out iter, path);
            cs_store.get(iter, 1, out cs, -1);

            var csv = new CSView(cs);
            csv.show_all();
        }
    }

    public static CActions actions;
    public static CMain main;

    static void init(ref weak string[] args)
    {
        Gtk.init(ref args);
        actions = new CActions();
        main = new CMain();

        {
            Gdk.threads_init();

            var specload = new SpecLoadDialog(main);
            try {
                Thread.create(specload.run, false);
            } catch (ThreadError e) {
                stderr.printf("Unable to start loading thread.\n");
                assert(false);
            }
        }

        Gtk.main();
    }
}

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

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

    internal class SpecLoadDialog : Window
    {
        private Label loading;
        private ProgressBar progress;
        private bool done;

        public SpecLoadDialog(Window? parent = null)
        {
            if (parent != null) {
                set_transient_for(parent);
                modal = true;
                destroy_with_parent = true;
            }
            deletable = false;

            specs = new SpecEntry[2];
            loading = new Label("Loading...");

            progress = new ProgressBar();
            progress.pulse_fraction = 0.1;

            var box = new VBox(false, 5);
            box.pack_start(loading, false, true, 0);
            box.pack_start(progress, false, true, 0);

            border_width = 10;
            set_default_size(300, 10);

            add(box);
        }

        public void *run()
        {
            Gdk.threads_enter();
                show_all();
            Gdk.threads_leave();

            done = false;
            weak Thread load;
            weak Thread pulse;

            try {
                pulse = Thread.create(pulse_thread, true);
                load = Thread.create(specload_thread, true);
            } catch (ThreadError e) {
                stderr.printf("Unable to start loading thread.\n");
                assert(false);
                return null; /* appease vala */
            }

            void *result = load.join();
            done = true;
            pulse.join();

            Gdk.threads_enter();
                hide_all();
            Gdk.threads_leave();

            return result;
        }

        private void *pulse_thread()
        {
            while (!done) {
                Gdk.threads_enter();
                    progress.pulse();
                Gdk.threads_leave();

                Thread.usleep(50000);
            }
            return null;
        }

        private void *specload_thread()
        {
            /* XXX This shouldn't be hardcoded */
            Gdk.threads_enter();
                loading.label = "Loading r300 registers...";
            Gdk.threads_leave();
            var r300_registers = new Spec("/usr/share/rsim/r300reg.xml", "r300");

            Gdk.threads_enter();
                loading.label = "Loading r500 registers...";
            Gdk.threads_leave();
            var r500_registers = new Spec("/usr/share/rsim/r300reg.xml", "r500");

            Gdk.threads_enter();
                loading.label = "Done";
            Gdk.threads_leave();

            specs[0].spec = r300_registers;
            specs[0].name = "r300";
            specs[1].spec = r500_registers;
            specs[1].name = "r500";

            return (void *)123;
        }
    }
}

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.actions.unref();
    GUI.main.unref();

    return 0;
}