aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/imgui/docs/README.md
blob: 74ec20cd20185f0e69de3ee760d87c94379011da (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
dear imgui,
=====
[![Build Status](https://travis-ci.org/ocornut/imgui.svg?branch=master)](https://travis-ci.org/ocornut/imgui)
[![Coverity Status](https://scan.coverity.com/projects/4720/badge.svg)](https://scan.coverity.com/projects/4720)

_(This library is free but needs your support to sustain its development. There are many desirable features and maintenance ahead. If you are an individual using dear imgui, please consider donating via Patreon or PayPal. If your company is using dear imgui, please consider financial support (e.g. sponsoring a few weeks/months of development). I can invoice for technical support, custom development etc. Email: omarcornut at gmail)._

Monthly donations via Patreon:
<br>[![Patreon](https://cloud.githubusercontent.com/assets/8225057/5990484/70413560-a9ab-11e4-8942-1a63607c0b00.png)](http://www.patreon.com/imgui)

One-off donations via PayPal:
<br>[![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5Q73FPZ9C526U)

Dear ImGui is a bloat-free graphical user interface library for C++. It outputs optimized vertex buffers that you can render anytime in your 3D-pipeline enabled application. It is fast, portable, renderer agnostic and self-contained (no external dependencies).

Dear ImGui is designed to enable fast iterations and to empower programmers to create content creation tools and visualization / debug tools (as opposed to UI for the average end-user). It favors simplicity and productivity toward this goal, and lacks certain features normally found in more high-level libraries.

Dear ImGui is particularly suited to integration in games engine (for tooling), real-time 3D applications, fullscreen applications, embedded applications, or any applications on consoles platforms where operating system features are non-standard. 

Dear ImGui is self-contained within a few files that you can easily copy and compile into your application/engine:
- imgui.cpp
- imgui.h
- imgui_demo.cpp
- imgui_draw.cpp
- imgui_widgets.cpp
- imgui_internal.h
- imconfig.h (empty by default, user-editable)
- imstb_rectpack.h
- imstb_textedit.h
- imstb_truetype.h

No specific build process is required. You can add the .cpp files to your project or #include them from an existing file.

### Usage

Your code passes mouse/keyboard/gamepad inputs and settings to Dear ImGui (see example applications for more details). After Dear ImGui is setup, you can use it from \_anywhere\_ in your program loop:

Code:
```cpp
ImGui::Text("Hello, world %d", 123);
if (ImGui::Button("Save"))
{
    // do stuff
}
ImGui::InputText("string", buf, IM_ARRAYSIZE(buf));
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
```
Result:
<br>![sample code output](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/code_sample_02.png)
<br>_(settings: Dark style (left), Light style (right) / Font: Roboto-Medium, 16px / Rounding: 5)_

Code:
```cpp
// Create a window called "My First Tool", with a menu bar.
ImGui::Begin("My First Tool", &my_tool_active, ImGuiWindowFlags_MenuBar);
if (ImGui::BeginMenuBar())
{
    if (ImGui::BeginMenu("File"))
    {
        if (ImGui::MenuItem("Open..", "Ctrl+O")) { /* Do stuff */ }
        if (ImGui::MenuItem("Save", "Ctrl+S"))   { /* Do stuff */ }
        if (ImGui::MenuItem("Close", "Ctrl+W"))  { my_tool_active = false; }
        ImGui::EndMenu();
    }
    ImGui::EndMenuBar();
}

// Edit a color (stored as ~4 floats)
ImGui::ColorEdit4("Color", my_color);

// Plot some values
const float my_values[] = { 0.2f, 0.1f, 1.0f, 0.5f, 0.9f, 2.2f };
ImGui::PlotLines("Frame Times", my_values, IM_ARRAYSIZE(my_values));
 
// Display contents in a scrolling region
ImGui::TextColored(ImVec4(1,1,0,1), "Important Stuff");
ImGui::BeginChild("Scrolling");
for (int n = 0; n < 50; n++)
    ImGui::Text("%04d: Some text", n);
ImGui::EndChild();
ImGui::End();
```
Result:
<br>![sample code output](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/code_sample_03_color.gif)

### How it works

Check out the References section if you want to understand the core principles behind the IMGUI paradigm. An IMGUI tries to minimize state duplication, state synchronization and state storage from the user's point of view. It is less error prone (less code and less bugs) than traditional retained-mode interfaces, and lends itself to create dynamic user interfaces. 

Dear ImGui outputs vertex buffers and command lists that you can easily render in your application. The number of draw calls and state changes is typically very small. Because it doesn't know or touch graphics state directly, you can call ImGui commands anywhere in your code (e.g. in the middle of a running algorithm, or in the middle of your own rendering process). Refer to the sample applications in the examples/ folder for instructions on how to integrate dear imgui with your existing codebase. 

_A common misunderstanding is to mistake immediate mode gui for immediate mode rendering, which usually implies hammering your driver/GPU with a bunch of inefficient draw calls and state changes as the gui functions are called. This is NOT what Dear ImGui does. Dear ImGui outputs vertex buffers and a small list of draw calls batches. It never touches your GPU directly. The draw call batches are decently optimal and you can render them later, in your app or even remotely._

Dear ImGui allows you create elaborate tools as well as very short-lived ones. On the extreme side of short-liveness: using the Edit&Continue (hot code reload) feature of modern compilers you can add a few widgets to tweaks variables while your application is running, and remove the code a minute later! Dear ImGui is not just for tweaking values. You can use it to trace a running algorithm by just emitting text commands. You can use it along with your own reflection data to browse your dataset live. You can use it to expose the internals of a subsystem in your engine, to create a logger, an inspection tool, a profiler, a debugger, an entire game making editor/framework, etc.  

Demo Binaries
-------------

You should be able to build the examples from sources (tested on Windows/Mac/Linux). If you don't, let me know! If you want to have a quick look at some Dear ImGui features, you can download Windows binaries of the demo app here:
- [imgui-demo-binaries-20181008.zip](http://www.miracleworld.net/imgui/binaries/imgui-demo-binaries-20181008.zip) (Windows binaries, Dear ImGui 1.66 WIP built 2018/10/08, master branch, 5 executables)

The demo applications are unfortunately not yet DPI aware so expect some blurriness on a 4K screen. For DPI awareness you can load/reload your font at different scale, and scale your Style with `style.ScaleAllSizes()`.

Bindings
--------

Integrating Dear ImGui within your custom engine is a matter of 1) wiring mouse/keyboard/gamepad inputs 2) uploading one texture to your GPU/render engine 3) providing a render function that can bind textures and render textured triangles. The [examples/](https://github.com/ocornut/imgui/tree/master/examples) folder is populated with applications doing just that. If you are an experienced programmer and at ease with those concepts, it should take you less than an hour to integrate Dear ImGui in your custom engine, but make sure to spend time reading the FAQ, the comments and other documentation!

_NB: those third-party bindings may be more or less maintained, more or less close to the original API (as people who create language bindings sometimes haven't used the C++ API themselves.. for the good reason that they aren't C++ users). Dear ImGui was designed with C++ in mind and some of the subtleties may be lost in translation with other languages. If your language supports it, I would suggest replicating the function overloading and default parameters used in the original, else the API may be harder to use. In doubt, please check the original C++ version first!_

Languages: (third-party bindings)
- C: [cimgui](https://github.com/cimgui/cimgui) (new 2018 auto-generated version!)
- C#/.Net: [ImGui.NET](https://github.com/mellinoe/ImGui.NET)
- ChaiScript: [imgui-chaiscript](https://github.com/JuJuBoSc/imgui-chaiscript)
- D: [DerelictImgui](https://github.com/Extrawurst/DerelictImgui)
- Go: [imgui-go](https://github.com/inkyblackness/imgui-go) or [go-imgui](https://github.com/Armored-Dragon/go-imgui)
- Haxe/hxcpp: [linc_imgui](https://github.com/Aidan63/linc_imgui)
- Java: [jimgui](https://github.com/ice1000/jimgui)
- JavaScript: [imgui-js](https://github.com/flyover/imgui-js)
- Lua: [LuaJIT-ImGui](https://github.com/sonoro1234/LuaJIT-ImGui), [imgui_lua_bindings](https://github.com/patrickriordan/imgui_lua_bindings) or [lua-ffi-bindings](https://github.com/thenumbernine/lua-ffi-bindings)
- Odin: [odin-dear_imgui](https://github.com/ThisDrunkDane/odin-dear_imgui)
- Pascal: [imgui-pas](https://github.com/dpethes/imgui-pas)
- PureBasic: [pb-cimgui](https://github.com/hippyau/pb-cimgui)
- Python [CyImGui](https://github.com/chromy/cyimgui) or [pyimgui](https://github.com/swistakm/pyimgui)
- Rust: [imgui-rs](https://github.com/Gekkio/imgui-rs)
- Swift [swift-imgui](https://github.com/mnmly/Swift-imgui)

Frameworks:
- Renderers: DirectX 9, DirectX 10, DirectX 11, DirectX 12, Metal, OpenGL2, OpenGL3+/ES2/ES3, Vulkan: [examples/](https://github.com/ocornut/imgui/tree/master/examples)
- Platform: GLFW, SDL, Win32, OSX, Freeglut: [examples/](https://github.com/ocornut/imgui/tree/master/examples)
- Framework: Allegro 5, Marmalade: [examples/](https://github.com/ocornut/imgui/tree/master/examples)
- Unmerged PR: SDL2 + OpenGLES + Emscripten: [#336](https://github.com/ocornut/imgui/pull/336)
- Unmerged PR: Android: [#421](https://github.com/ocornut/imgui/pull/421)
- Unmerged PR: ORX: [#1843](https://github.com/ocornut/imgui/pull/1843)
- Cinder: [Cinder-ImGui](https://github.com/simongeilfus/Cinder-ImGui)
- Cocos2d-x: [imguix](https://github.com/c0i/imguix), [#551](https://github.com/ocornut/imgui/issues/551)
- Flexium: [FlexGUI](https://github.com/DXsmiley/FlexGUI)
- GML/GameMakerStudio2: [ImGuiGML](https://marketplace.yoyogames.com/assets/6221/imguigml)
- Irrlicht: [IrrIMGUI](https://github.com/ZahlGraf/IrrIMGUI)
- Ogre: [ogreimgui](https://bitbucket.org/LMCrashy/ogreimgui/src)
- OpenFrameworks: [ofxImGui](https://github.com/jvcleave/ofxImGui)
- OpenSceneGraph/OSG: [gist](https://gist.github.com/fulezi/d2442ca7626bf270226014501357042c)
- LÖVE+Lua: [love-imgui](https://github.com/slages/love-imgui)
- Magnum: [magnum-imgui](https://github.com/lecopivo/magnum-imgui), [MagnumImguiPort](https://github.com/lecopivo/MagnumImguiPort)
- NanoRT: [syoyo/imgui](https://github.com/syoyo/imgui/tree/nanort)
- Qt3d: [imgui-qt3d](https://github.com/alpqr/imgui-qt3d), QOpenGLWindow [qtimgui](https://github.com/ocornut/imgui/issues/1910)
- SFML: [imgui-sfml](https://github.com/EliasD/imgui-sfml)
- Software renderer: [imgui_software_renderer](https://github.com/emilk/imgui_software_renderer)
- Unreal Engine 4: [segross/UnrealImGui](https://github.com/segross/UnrealImGui) or [sronsse/UnrealEngine_ImGui](https://github.com/sronsse/UnrealEngine_ImGui)

For other bindings: see [Bindings](https://github.com/ocornut/imgui/wiki/Bindings/). Also see [Wiki](https://github.com/ocornut/imgui/wiki) for more links and ideas.

Roadmap
-------
Some of the goals for 2018-2019 are:
- Finish work on gamepad/keyboard controls. (see [#787](https://github.com/ocornut/imgui/issues/787))
- Finish work on docking, tabs. (see [#2109](https://github.com/ocornut/imgui/issues/2109))
- Finish work on viewports and multiple OS windows management. (see [#1542](https://github.com/ocornut/imgui/issues/1542))
- Make Columns better. (they are currently pretty terrible!)
- Make the examples look better, improve styles, improve font support, make the examples hi-DPI aware.

Gallery
-------
User screenshots:
<br>[Gallery Part 1](https://github.com/ocornut/imgui/issues/123) (Feb 2015 to Feb 2016)
<br>[Gallery Part 2](https://github.com/ocornut/imgui/issues/539) (Feb 2016 to Aug 2016)
<br>[Gallery Part 3](https://github.com/ocornut/imgui/issues/772) (Aug 2016 to Jan 2017)
<br>[Gallery Part 4](https://github.com/ocornut/imgui/issues/973) (Jan 2017 to Aug 2017)
<br>[Gallery Part 5](https://github.com/ocornut/imgui/issues/1269) (Aug 2017 to Feb 2018)
<br>[Gallery Part 6](https://github.com/ocornut/imgui/issues/1607) (Feb 2018 to June 2018)
<br>[Gallery Part 7](https://github.com/ocornut/imgui/issues/1902) (June 2018 onward)
<br>Also see the [Mega screenshots](https://github.com/ocornut/imgui/issues/1273) for an idea of the available features.

Various tools
[![screenshot game](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v149/gallery_TheDragonsTrap-01-thumb.jpg)](https://cloud.githubusercontent.com/assets/8225057/20628927/33e14cac-b329-11e6-80f6-9524e93b048a.png)

[![screenshot tool](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/editor_white_preview.jpg)](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/editor_white.png)

![screenshot demo](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/v160-misc-classic.png)

[![screenshot profiler](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v148/profiler-880.jpg)](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v148/profiler.png)

Dear ImGui can load TTF/OTF fonts. UTF-8 is supported for text display and input. Here using Arial Unicode font to display Japanese. Initialize custom font with:
Code:
```cpp
ImGuiIO& io = ImGui::GetIO();
io.Fonts->AddFontFromFileTTF("NotoSansCJKjp-Medium.otf", 20.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
```
```cpp
ImGui::Text(u8"こんにちは!テスト %d", 123);
if (ImGui::Button(u8"ロード"))
{
    // do stuff
}
ImGui::InputText("string", buf, IM_ARRAYSIZE(buf));
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
```
Result:
<br>![sample code output](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v160/code_sample_02_jp.png)
<br>_(settings: Dark style (left), Light style (right) / Font: NotoSansCJKjp-Medium, 20px / Rounding: 5)_

References
----------

The Immediate Mode GUI paradigm may at first appear unusual to some users. This is mainly because "Retained Mode" GUIs have been so widespread and predominant. The following links can give you a better understanding about how Immediate Mode GUIs works. 
- [Johannes 'johno' Norneby's article](http://www.johno.se/book/imgui.html).
- [A presentation by Rickard Gustafsson and Johannes Algelind](http://www.cse.chalmers.se/edu/year/2011/course/TDA361/Advanced%20Computer%20Graphics/IMGUI.pdf).
- [Jari Komppa's tutorial on building an ImGui library](http://iki.fi/sol/imgui/).
- [Casey Muratori's original video that popularized the concept](https://mollyrocket.com/861).
- [Nicolas Guillemot's CppCon'16 flash-talk about Dear ImGui](https://www.youtube.com/watch?v=LSRJ1jZq90k).
- [Thierry Excoffier's Zero Memory Widget](http://perso.univ-lyon1.fr/thierry.excoffier/ZMW/).

See the [Wiki](https://github.com/ocornut/imgui/wiki) for more references and [Bindings](https://github.com/ocornut/imgui/wiki/Bindings) for third-party bindings to different languages and frameworks.

Support Forums
--------------

If you have issues with: compiling, linking, adding fonts, running or displaying Dear ImGui, or wiring inputs: please post on the Discourse forum: https://discourse.dearimgui.org/c/getting-started.

For any other questions, bug reports, requests, feedback, you may post on https://github.com/ocornut/imgui/issues.

Frequently Asked Question (FAQ)
-------------------------------

**Where is the documentation?**

- The documentation is at the top of imgui.cpp + effectively imgui.h. 
- Example code is in imgui_demo.cpp and particularly the ImGui::ShowDemoWindow() function. It covers most features of ImGui so you can read the code and call the function itself to see its output. 
- Standalone example applications using e.g. OpenGL/DirectX are provided in the examples/ folder. 
- We obviously needs better documentation! Consider contributing or becoming a [Patron](http://www.patreon.com/imgui) to promote this effort.
- Your programming IDE is your friend, find the type or function declaration to find comments associated to it. 

**Which version should I get?**

I occasionally tag [Releases](https://github.com/ocornut/imgui/releases) but it is generally safe and recommended to sync to master/latest. The library is fairly stable and regressions tend to be fixed fast when reported. 

You may also peak at the [Multi-Viewport](https://github.com/ocornut/imgui/issues/1542) and [Docking](https://github.com/ocornut/imgui/issues/2109) branches. Even though they are marked beta, several projects are using them and they are kept in sync with master regularly.

**Who uses Dear ImGui?**

See the [Software using dear imgui page](https://github.com/ocornut/imgui/wiki/Software-using-dear-imgui) for an (incomplete) list of games/software which are publicly known to use dear imgui. Please add yours if you can!

**Why the odd dual naming, "dear imgui" vs "ImGui"?**

The library started its life and is best known as "ImGui" only due to the fact that I didn't give it a proper name when I released it. However, the term IMGUI (immediate-mode graphical user interface) was coined before and is being used in variety of other situations. It seemed confusing and unfair to hog the name. To reduce the ambiguity without affecting existing codebases, I have decided on an alternate, longer name "dear imgui" that people can use to refer to this specific library in ambiguous situations.

**How can I tell whether to dispatch mouse/keyboard to imgui or to my application?**
<br>**How can I display an image? What is ImTextureID, how does it works?**
<br>**How can I have multiple widgets with the same label or without a label? A primer on labels and the ID Stack.**
<br>**How can I use my own math types instead of ImVec2/ImVec4?**
<br>**How can I load a different font than the default?**
<br>**How can I easily use icons in my application?**
<br>**How can I load multiple fonts?**
<br>**How can I display and input non-latin characters such as Chinese, Japanese, Korean, Cyrillic?**
<br>**How can I use the drawing facilities without an Dear ImGui window? (using ImDrawList API)**
<br>**I integrated Dear ImGui in my engine and the text or lines are blurry..**
<br>**I integrated Dear ImGui in my engine and some elements are disappearing when I move windows around..**
<br>**How can I help?**

See the FAQ in imgui.cpp for answers.

**How do you use Dear ImGui on a platform that may not have a mouse or keyboard?**

You can control Dear ImGui with a gamepad, see the explanation in imgui.cpp about how to use the navigation feature (short version: map your gamepad inputs into the `io.NavInputs[]` array and set `io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad`).

You can share your computer mouse seamlessly with your console/tablet/phone using [Synergy](http://synergy-project.org). This is the preferred solution for developer productivity. In particular, their [micro-synergy-client](https://github.com/symless/micro-synergy-client) repo there is _uSynergy.c_ sources for a small embeddable that you can use on any platform to connect to your host PC using Synergy 1.x. You may also use a third party solution such as [Remote ImGui](https://github.com/JordiRos/remoteimgui).

For touch inputs, you can increase the hit box of widgets (via the _style.TouchPadding_ setting) to accommodate a little for the lack of precision of touch inputs, but it is recommended you use a mouse or gamepad to allow optimizing for screen real-estate and precision.

**Can you create elaborate/serious tools with Dear ImGui?**

Yes. People have written game editors, data browsers, debuggers, profilers and all sort of non-trivial tools with the library. In my experience the simplicity of the API is very empowering. Your UI runs close to your live data. Make the tools always-on and everybody in the team will be inclined to create new tools (as opposed to more "offline" UI toolkits where only a fraction of your team effectively creates tools). The list of sponsors below is also an indicator that serious game teams have been using the library.

Dear ImGui is very programmer centric and the immediate-mode GUI paradigm might requires you to readjust some habits before you can realize its full potential. Dear ImGui is about making things that are simple, efficient and powerful.

**Can you reskin the look of Dear ImGui?**

You can alter the look of the interface to some degree: changing colors, sizes, padding, rounding, fonts. However, as Dear ImGui is designed and optimized to create debug tools, the amount of skinning you can apply is limited. There is only so much you can stray away from the default look and feel of the interface. Below is a screenshot from [LumixEngine](https://github.com/nem0/LumixEngine) with custom colors + a docking/tabs extension (both of which you can find in the Issues section and will eventually be merged):

![LumixEngine](https://raw.githubusercontent.com/wiki/ocornut/imgui/web/v151/lumix-201710-rearranged.png)

**Why using C++ (as opposed to C)?**

Dear ImGui takes advantage of a few C++ languages features for convenience but nothing anywhere Boost-insanity/quagmire. Dear ImGui does NOT require C++11 so it can be used with most old C++ compilers. Dear ImGui doesn't use any C++ header file. Language-wise, function overloading and default parameters are used to make the API easier to use and code more terse. Doing so I believe the API is sitting on a sweet spot and giving up on those features would make the API more cumbersome. Other features such as namespace, constructors and templates (in the case of the ImVector<> class) are also relied on as a convenience.

There is an auto-generated [c-api for Dear ImGui (cimgui)](https://github.com/cimgui/cimgui) by Sonoro1234 and Stephan Dilly. This is designed for binding other languages. If possible, I would suggest using your target language functionalities to try replicating the function overloading and default parameters used in C++ else the API may be harder to use. Also see [Bindings](https://github.com/ocornut/imgui/wiki/Bindings) for third-party bindings to other languages.

Support dear imgui
------------------

**How can I help financing further development of Dear ImGui?**

Your contributions are keeping the library alive. If you are an individual using dear imgui, please consider donating to enable me to spend more time improving the library.

Monthly donations via Patreon:
<br>[![Patreon](https://cloud.githubusercontent.com/assets/8225057/5990484/70413560-a9ab-11e4-8942-1a63607c0b00.png)](http://www.patreon.com/imgui)

One-off donations via PayPal:
<br>[![PayPal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5Q73FPZ9C526U)

Ongoing dear imgui development is financially supported on [**Patreon**](http://www.patreon.com/imgui) and by private sponsors.
If your company uses dear imgui, please consider financial support (e.g. sponsoring a few weeks/months of development. I can also invoice for private support, custom development etc. contact me for details: omarcornut at gmail). Thanks! 

**Platinum-chocolate sponsors**
- Blizzard Entertainment.

**Double-chocolate sponsors**
- Media Molecule, Mobigame, Insomniac Games, Aras Pranckevičius, Lizardcube, Greggman, DotEmu, Nadeo, Supercell, Runner, Friendly Shade.

**Salty caramel supporters**
- Jetha Chan, Wild Sheep Studio, Pastagames, Mārtiņš Možeiko, Daniel Collin, Recognition Robotics, Chris Genova, ikrima, Glenn Fiedler, Geoffrey Evans, Dakko Dakko, Mercury Labs, Singularity Demo Group, Mischa Alff, Sebastien Ronsse, Lionel Landwerlin, Nikolay Ivanov, Ron Gilbert, Brandon Townsend, Nikhil Deshpande, Cort Stratton, drudru, Harfang 3D, Jeff Roberts.

**Caramel supporters**
- Michel Courtine, César Leblic, Dale Kim, Alex Evans, Rui Figueira, Paul Patrashcu, Jerome Lanquetot, Ctrl Alt Ninja, Paul Fleming, Neil Henning, Stephan Dilly, Neil Blakey-Milner, Aleksei, NeiloGD, Justin Paver, FiniteSol, Vincent Pancaldi, James Billot, Robin Hübner, furrtek, Eric, Simon Barratt, Game Atelier, Julian Bosch, Simon Lundmark, Vincent Hamm, Farhan Wali, Matt Reyer, Colin Riley, Victor Martins, Josh Simmons, Garrett Hoofman, Sergio Gonzales, Andrew Berridge, Roy Eltham, Game Preservation Society, Kit framework, Josh Faust, Martin Donlon, Quinton, Felix, Andrew Belt, Codecat, Cort Stratton, Claudio Canepa, Doug McNabb, Emmanuel Julien, Guillaume Chereau, Jeffrey Slutter, Jeremiah Deckard, r-lyeh, Roger Clark, Nekith, Joshua Fisher, Malte Hoffmann, Mustafa Karaalioglu, Merlyn Morgan-Graham, Per Vognsen, Fabian Giesen, Jan Staubach, Matt Hargett, John Shearer, Jesse Chounard, kingcoopa, Miloš Tošić, Jonas Bernemann, Johan Andersson, Nathan Hartman, Michael Labbe, Tomasz Golebiowski, Louis Schnellbach, Felipe Alfonso, Jimmy Andrews, Bojan Endrovski, Robin Berg Pettersen, Rachel Crawford, Edsel Malasig, Andrew Johnson, Sean Hunter, Jordan Mellow, Nefarius Software Solutions, Laura Wieme, Robert Nix, Mick Honey, Astrofra, Jonas Lehmann, Steven Kah Hien Wong, Bartosz Bielecki, Oscar Penas, A M, Liam Moynihan, Artometa.

And all other supporters; THANK YOU!
(Please contact me if you would like to be added or removed from this list)

Credits
-------

Developed by [Omar Cornut](http://www.miracleworld.net) and every direct or indirect contributors to the GitHub. The early version of this library was developed with the support of [Media Molecule](http://www.mediamolecule.com) and first used internally on the game [Tearaway](http://tearaway.mediamolecule.com). 

I first discovered imgui principles at [Q-Games](http://www.q-games.com) where Atman had dropped his own simple imgui implementation in the codebase, which I spent quite some time improving and thinking about. It turned out that Atman was exposed to the concept directly by working with Casey. When I moved to Media Molecule I rewrote a new library trying to overcome the flaws and limitations of the first one I've worked with. It became this library and since then I have spent an unreasonable amount of time iterating on it. 

Embeds [ProggyClean.ttf](http://upperbounds.net) font by Tristan Grimmer (MIT license).

Embeds [stb_textedit.h, stb_truetype.h, stb_rectpack.h](https://github.com/nothings/stb/) by Sean Barrett (public domain).

Inspiration, feedback, and testing for early versions: Casey Muratori, Atman Binstock, Mikko Mononen, Emmanuel Briney, Stefan Kamoda, Anton Mikhailov, Matt Willis. And everybody posting feedback, questions and patches on the GitHub.

License
-------

Dear ImGui is licensed under the MIT License, see LICENSE for more information.
span>(mem_available_pages, MEM_AVAILABLE_PAGES); extern char builtin_cmdline[]; extern struct ns16550_defaults ns16550; #undef OF_DEBUG #undef OF_DEBUG_LOW #ifdef OF_DEBUG #define DBG(args...) of_printf(args) #else #define DBG(args...) #endif #ifdef OF_DEBUG_LOW #define DBG_LOW(args...) of_printf(args) #else #define DBG_LOW(args...) #endif #define of_panic(MSG...) \ do { of_printf(MSG); of_printf("\nHANG\n"); for (;;); } while (0) struct of_service { u32 ofs_service; u32 ofs_nargs; u32 ofs_nrets; u32 ofs_args[10]; }; static int bof_chosen; static struct of_service s; static int __init of_call( const char *service, u32 nargs, u32 nrets, s32 rets[], ...) { int rc; if (of_vec != 0) { va_list args; int i; memset(&s, 0, sizeof (s)); s.ofs_service = (ulong)service; s.ofs_nargs = nargs; s.ofs_nrets = nrets; s.ofs_nargs = nargs; /* copy all the params into the args array */ va_start(args, rets); for (i = 0; i < nargs; i++) { s.ofs_args[i] = va_arg(args, u32); } va_end(args); rc = prom_call(&s, 0, of_vec, of_msr); /* yes always to the copy, just in case */ for (i = 0; i < nrets; i++) { rets[i] = s.ofs_args[i + nargs]; } } else { rc = OF_FAILURE; } return rc; } /* popular OF methods */ static int __init _of_write(int ih, const char *addr, u32 len) { int rets[1] = { OF_FAILURE }; if (of_call("write", 3, 1, rets, ih, addr, len) == OF_FAILURE) { return OF_FAILURE; } return rets[0]; } /* popular OF methods */ static int __init of_write(int ih, const char *addr, u32 len) { int rc; int i = 0; int sum = 0; while (i < len) { if (addr[i] == '\n') { if (i > 0) { rc = _of_write(ih, addr, i); if (rc == OF_FAILURE) return rc; sum += rc; } rc = _of_write(ih, "\r\n", 2); if (rc == OF_FAILURE) return rc; sum += rc; i++; addr += i; len -= i; i = 0; continue; } i++; } if (len > 0) { rc = _of_write(ih, addr, len); if (rc == OF_FAILURE) return rc; sum += rc; } return sum; } static int of_printf(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); static int __init of_printf(const char *fmt, ...) { static char buf[1024]; va_list args; int sz; if (of_out == 0) { return OF_FAILURE; } va_start(args, fmt); sz = vsnprintf(buf, sizeof (buf), fmt, args); if (sz <= sizeof (buf)) { of_write(of_out, buf, sz); } else { static const char trunc[] = "\n(TRUNCATED)\n"; sz = sizeof (buf); of_write(of_out, buf, sz); of_write(of_out, trunc, sizeof (trunc)); } return sz; } static int __init of_finddevice(const char *devspec) { int rets[1] = { OF_FAILURE }; of_call("finddevice", 1, 1, rets, devspec); if (rets[0] == OF_FAILURE) { DBG("finddevice %s -> FAILURE %d\n",devspec,rets[0]); return OF_FAILURE; } DBG_LOW("finddevice %s -> %d\n",devspec, rets[0]); return rets[0]; } static int __init of_getprop(int ph, const char *name, void *buf, u32 buflen) { int rets[1] = { OF_FAILURE }; of_call("getprop", 4, 1, rets, ph, name, buf, buflen); if (rets[0] == OF_FAILURE) { DBG_LOW("getprop 0x%x %s -> FAILURE\n", ph, name); return OF_FAILURE; } DBG_LOW("getprop 0x%x %s -> 0x%x (%s)\n", ph, name, rets[0], (char *)buf); return rets[0]; } static int __init of_setprop( int ph, const char *name, const void *buf, u32 buflen) { int rets[1] = { OF_FAILURE }; of_call("setprop", 4, 1, rets, ph, name, buf, buflen); if (rets[0] == OF_FAILURE) { DBG("setprop 0x%x %s -> FAILURE\n", ph, name); return OF_FAILURE; } DBG_LOW("setprop 0x%x %s -> %s\n", ph, name, (char *)buf); return rets[0]; } /* * returns 0 if there are no children (of spec) */ static int __init of_getchild(int ph) { int rets[1] = { OF_FAILURE }; of_call("child", 1, 1, rets, ph); DBG_LOW("getchild 0x%x -> 0x%x\n", ph, rets[0]); return rets[0]; } /* * returns 0 is there are no peers */ static int __init of_getpeer(int ph) { int rets[1] = { OF_FAILURE }; of_call("peer", 1, 1, rets, ph); DBG_LOW("getpeer 0x%x -> 0x%x\n", ph, rets[0]); return rets[0]; } static int __init of_getproplen(int ph, const char *name) { int rets[1] = { OF_FAILURE }; of_call("getproplen", 2, 1, rets, ph, name); if (rets[0] == OF_FAILURE) { DBG("getproplen 0x%x %s -> FAILURE\n", ph, name); return OF_FAILURE; } DBG_LOW("getproplen 0x%x %s -> 0x%x\n", ph, name, rets[0]); return rets[0]; } static int __init of_package_to_path(int ph, char *buffer, u32 buflen) { int rets[1] = { OF_FAILURE }; of_call("package-to-path", 3, 1, rets, ph, buffer, buflen); if (rets[0] == OF_FAILURE) { DBG("%s 0x%x -> FAILURE\n", __func__, ph); return OF_FAILURE; } DBG_LOW("%s 0x%x %s -> 0x%x\n", __func__, ph, buffer, rets[0]); if (rets[0] <= buflen) buffer[rets[0]] = '\0'; return rets[0]; } static int __init of_nextprop(int ph, const char *name, void *buf) { int rets[1] = { OF_FAILURE }; of_call("nextprop", 3, 1, rets, ph, name, buf); if (rets[0] == OF_FAILURE) { DBG("nextprop 0x%x %s -> FAILURE\n", ph, name); return OF_FAILURE; } DBG_LOW("nextprop 0x%x %s -> %s\n", ph, name, (char *)buf); return rets[0]; } static int __init of_instance_to_path(int ih, char *buffer, u32 buflen) { int rets[1] = { OF_FAILURE }; if (of_call("instance-to-path", 3, 1, rets, ih, buffer, buflen) == OF_FAILURE) return OF_FAILURE; if (rets[0] <= buflen) buffer[rets[0]] = '\0'; return rets[0]; } static int __init of_start_cpu(int cpu, u32 pc, u32 reg) { int ret; ret = of_call("start-cpu", 3, 0, NULL, cpu, pc, reg); return ret; } static void __init of_test(const char *of_method_name) { int rets[1] = { OF_FAILURE }; of_call("test", 1, 1, rets, of_method_name); if (rets[0] == OF_FAILURE ) { of_printf("Warning: possibly no OF method %s.\n" "(Ignore this warning on PIBS.)\n", of_method_name); } } static int __init of_claim(u32 virt, u32 size, u32 align) { int rets[1] = { OF_FAILURE }; of_call("claim", 3, 1, rets, virt, size, align); if (rets[0] == OF_FAILURE) { DBG("%s 0x%08x 0x%08x 0x%08x -> FAIL\n", __func__, virt, size, align); return OF_FAILURE; } DBG_LOW("%s 0x%08x 0x%08x 0x%08x -> 0x%08x\n", __func__, virt, size, align, rets[0]); return rets[0]; } static int __init of_instance_to_package(int ih) { int rets[1] = { OF_FAILURE }; of_call("instance-to-package", 1, 1, rets, ih); if (rets[0] == OF_FAILURE) return OF_FAILURE; return rets[0]; } static int __init of_getparent(int ph) { int rets[1] = { OF_FAILURE }; of_call("parent", 1, 1, rets, ph); DBG_LOW("getparent 0x%x -> 0x%x\n", ph, rets[0]); return rets[0]; } static int __init of_open(const char *devspec) { int rets[1] = { OF_FAILURE }; of_call("open", 1, 1, rets, devspec); return rets[0]; } static void boot_of_alloc_init(int m, uint addr_cells, uint size_cells) { int rc; uint pg; uint a[64]; int tst; u64 start; u64 size; rc = of_getprop(m, "available", a, sizeof (a)); if (rc > 0) { int l = rc / sizeof(a[0]); int r = 0; #ifdef OF_DEBUG { int i; of_printf("avail:\n"); for (i = 0; i < l; i += 4) of_printf(" 0x%x%x, 0x%x%x\n", a[i], a[i + 1], a[i + 2] ,a[i + 3]); } #endif pg = 0; while (pg < MEM_AVAILABLE_PAGES && r < l) { ulong end; start = a[r++]; if (addr_cells == 2 && (r < l) ) start = (start << 32) | a[r++]; size = a[r++]; if (size_cells == 2 && (r < l) ) size = (size << 32) | a[r++]; end = ALIGN_DOWN(start + size, PAGE_SIZE); start = ALIGN_UP(start, PAGE_SIZE); DBG("%s: marking 0x%x - 0x%lx\n", __func__, pg << PAGE_SHIFT, start); start >>= PAGE_SHIFT; while (pg < MEM_AVAILABLE_PAGES && pg < start) { set_bit(pg, mem_available_pages); pg++; } pg = end >> PAGE_SHIFT; } } /* Now make sure we mark our own memory */ pg = (ulong)_start >> PAGE_SHIFT; start = (ulong)_end >> PAGE_SHIFT; DBG("%s: marking 0x%x - 0x%lx\n", __func__, pg << PAGE_SHIFT, start << PAGE_SHIFT); /* Lets try and detect if our image has stepped on something. It * is possible that FW has already subtracted our image from * available memory so we must make sure that the previous bits * are the same for the whole image */ tst = test_and_set_bit(pg, mem_available_pages); ++pg; while (pg <= start) { if (test_and_set_bit(pg, mem_available_pages) != tst) of_panic("%s: pg :0x%x of our image is different\n", __func__, pg); ++pg; } DBG("%s: marking 0x%x - 0x%x\n", __func__, 0 << PAGE_SHIFT, 3 << PAGE_SHIFT); /* First for pages (where the vectors are) should be left alone as well */ set_bit(0, mem_available_pages); set_bit(1, mem_available_pages); set_bit(2, mem_available_pages); set_bit(3, mem_available_pages); } #ifdef BOOT_OF_FREE /* this is here in case we ever need a free call at a later date */ static void boot_of_free(ulong addr, ulong size) { ulong bits; ulong pos; ulong i; size = ALIGN_UP(size, PAGE_SIZE); bits = size >> PAGE_SHIFT; pos = addr >> PAGE_SHIFT; for (i = 0; i < bits; i++) { if (!test_and_clear_bit(pos + i, mem_available_pages)) of_panic("%s: pg :0x%lx was never allocated\n", __func__, pos + i); } } #endif static ulong boot_of_alloc(ulong size) { ulong bits; ulong pos; if (size == 0) return 0; DBG("%s(0x%lx)\n", __func__, size); size = ALIGN_UP(size, PAGE_SIZE); bits = size >> PAGE_SHIFT; pos = 0; for (;;) { ulong i; pos = find_next_zero_bit(mem_available_pages, MEM_AVAILABLE_PAGES, pos); DBG("%s: found start bit at: 0x%lx\n", __func__, pos); /* found nothing */ if ((pos + bits) > MEM_AVAILABLE_PAGES) { of_printf("%s: allocation of size: 0x%lx failed\n", __func__, size); return 0; } /* find a set that fits */ DBG("%s: checking for 0x%lx bits: 0x%lx\n", __func__, bits, pos); i = find_next_bit(mem_available_pages, MEM_AVAILABLE_PAGES, pos); if (i - pos >= bits) { uint addr = pos << PAGE_SHIFT; /* make sure OF is happy with our choice */ if (of_claim(addr, size, 0) != OF_FAILURE) { for (i = 0; i < bits; i++) set_bit(pos + i, mem_available_pages); DBG("%s: 0x%lx is good returning 0x%x\n", __func__, pos, addr); return addr; } /* if OF did not like the address then simply start from * the next bit */ i = 1; } pos = pos + i; } } int boot_of_mem_avail(int pos, ulong *startpage, ulong *endpage) { ulong freebit; ulong usedbit; if (pos >= MEM_AVAILABLE_PAGES) /* Stop iterating. */ return -1; /* Find first free page. */ freebit = find_next_zero_bit(mem_available_pages, MEM_AVAILABLE_PAGES, pos); if (freebit >= MEM_AVAILABLE_PAGES) { /* We know everything after MEM_AVAILABLE_PAGES is still free. */ *startpage = MEM_AVAILABLE_PAGES << PAGE_SHIFT; *endpage = ~0UL; return freebit; } *startpage = freebit << PAGE_SHIFT; /* Now find first used page after that. */ usedbit = find_next_bit(mem_available_pages, MEM_AVAILABLE_PAGES, freebit); if (usedbit >= MEM_AVAILABLE_PAGES) { /* We know everything after MEM_AVAILABLE_PAGES is still free. */ *endpage = ~0UL; return usedbit; } *endpage = usedbit << PAGE_SHIFT; return usedbit; } static ulong boot_of_mem_init(void) { int root; int p; int rc; uint addr_cells; uint size_cells; root = of_finddevice("/"); p = of_getchild(root); /* code is writen to assume sizes of 1 */ of_getprop(root, "#address-cells", &addr_cells, sizeof (addr_cells)); of_getprop(root, "#size-cells", &size_cells, sizeof (size_cells)); DBG("%s: address_cells=%d size_cells=%d\n", __func__, addr_cells, size_cells); /* We do ream memory discovery later, for now we only want to find * the first LMB */ do { const char memory[] = "memory"; char type[32]; type[0] = '\0'; of_getprop(p, "device_type", type, sizeof (type)); if (strncmp(type, memory, sizeof (memory)) == 0) { uint reg[48]; u64 start; u64 size; int r; int l; rc = of_getprop(p, "reg", reg, sizeof (reg)); if (rc == OF_FAILURE) { of_panic("no reg property for memory node: 0x%x.\n", p); } l = rc / sizeof(reg[0]); /* number reg element */ DBG("%s: number of bytes in property 'reg' %d\n", __func__, rc); r = 0; while (r < l) { start = reg[r++]; if (addr_cells == 2 && (r < l) ) start = (start << 32) | reg[r++]; if (r >= l) break; /* partial line. Skip */ if (start > 0) { /* this is not the first LMB so we skip it */ break; } size = reg[r++]; if (size_cells == 2 && (r < l) ) size = (size << 32) | reg[r++]; if (r > l) break; /* partial line. Skip */ boot_of_alloc_init(p, addr_cells, size_cells); eomem = size; return size; } } p = of_getpeer(p); } while (p != OF_FAILURE && p != 0); return 0; } static void boot_of_bootargs(multiboot_info_t *mbi) { int rc; if (builtin_cmdline[0] == '\0') { rc = of_getprop(bof_chosen, "bootargs", builtin_cmdline, CONFIG_CMDLINE_SIZE); if (rc > CONFIG_CMDLINE_SIZE) of_panic("bootargs[] not big enough for /chosen/bootargs\n"); } mbi->flags |= MBI_CMDLINE; mbi->cmdline = (ulong)builtin_cmdline; of_printf("bootargs = %s\n", builtin_cmdline); } static int save_props(void *m, ofdn_t n, int pkg) { int ret; char name[128]; int result = 1; int found_name = 0; int found_device_type = 0; const char name_str[] = "name"; const char devtype_str[] = "device_type"; /* get first */ result = of_nextprop(pkg, 0, name); while (result > 0) { int sz; u64 obj[1024]; sz = of_getproplen(pkg, name); if (sz >= 0) { ret = OF_SUCCESS; } else { ret = OF_FAILURE; } if (ret == OF_SUCCESS) { int actual = 0; ofdn_t pos; if (sz > 0) { if (sz > sizeof (obj)) { of_panic("obj array not big enough for 0x%x\n", sz); } actual = of_getprop(pkg, name, obj, sz); if (actual > sz) of_panic("obj too small"); } if (strncmp(name, name_str, sizeof(name_str)) == 0) { found_name = 1; } if (strncmp(name, devtype_str, sizeof(devtype_str)) == 0) { found_device_type = 1; } pos = ofd_prop_add(m, n, name, obj, actual); if (pos == 0) of_panic("prop_create"); } result = of_nextprop(pkg, name, name); } return 1; } static void do_pkg(void *m, ofdn_t n, int p, char *path, size_t psz) { int pnext; ofdn_t nnext; int sz; retry: save_props(m, n, p); /* do children first */ pnext = of_getchild(p); if (pnext != 0) { sz = of_package_to_path(pnext, path, psz); if (sz == OF_FAILURE) of_panic("bad path\n"); nnext = ofd_node_child_create(m, n, path, sz); if (nnext == 0) of_panic("out of mem\n"); do_pkg(m, nnext, pnext, path, psz); } /* do peer */ pnext = of_getpeer(p); if (pnext != 0) { sz = of_package_to_path(pnext, path, psz); nnext = ofd_node_peer_create(m, n, path, sz); if (nnext <= 0) of_panic("out of space in OFD tree.\n"); n = nnext; p = pnext; goto retry; } } static long pkg_save(void *mem) { int root; char path[256]; int r; path[0]='/'; path[1]='\0'; /* get root */ root = of_getpeer(0); if (root == OF_FAILURE) of_panic("no root package\n"); do_pkg(mem, OFD_ROOT, root, path, sizeof(path)); r = ofd_size(mem); of_printf("%s: saved device tree in 0x%x bytes\n", __func__, r); return r; } static int boot_of_fixup_refs(void *mem) { static const char *fixup_props[] = { "interrupt-parent", }; int i; int count = 0; for (i = 0; i < ARRAY_SIZE(fixup_props); i++) { ofdn_t c; const char *name = fixup_props[i]; c = ofd_node_find_by_prop(mem, OFD_ROOT, name, NULL, 0); while (c > 0) { const char *path; int rp; int ref; ofdn_t dp; int rc; ofdn_t upd; char ofpath[256]; path = ofd_node_path(mem, c); if (path == NULL) of_panic("no path to found prop: %s\n", name); rp = of_finddevice(path); if (rp == OF_FAILURE) of_panic("no real device for: name %s, path %s\n", name, path); /* Note: In theory 0 is a valid node handle but it is highly * unlikely. */ if (rp == 0) { of_panic("%s: of_finddevice returns 0 for path %s\n", __func__, path); } rc = of_getprop(rp, name, &ref, sizeof(ref)); if ((rc == OF_FAILURE) || (rc == 0)) of_panic("no prop: name %s, path %s, device 0x%x\n", name, path, rp); rc = of_package_to_path(ref, ofpath, sizeof (ofpath)); if (rc == OF_FAILURE) of_panic("no package: name %s, path %s, device 0x%x,\n" "ref 0x%x\n", name, path, rp, ref); dp = ofd_node_find(mem, ofpath); if (dp <= 0) of_panic("no ofd node for OF node[0x%x]: %s\n", ref, ofpath); ref = dp; upd = ofd_prop_add(mem, c, name, &ref, sizeof(ref)); if (upd <= 0) of_panic("update failed: %s\n", name); #ifdef DEBUG of_printf("%s: %s/%s -> %s\n", __func__, path, name, ofpath); #endif ++count; c = ofd_node_find_next(mem, c); } } return count; } static int boot_of_fixup_chosen(void *mem) { int ch; ofdn_t dn; ofdn_t dc; int val; int rc; char ofpath[256]; ch = of_finddevice("/chosen"); if (ch == OF_FAILURE) of_panic("/chosen not found\n"); rc = of_getprop(ch, "cpu", &val, sizeof (val)); if (rc != OF_FAILURE) { rc = of_instance_to_path(val, ofpath, sizeof (ofpath)); if (rc > 0) { dn = ofd_node_find(mem, ofpath); if (dn <= 0) of_panic("no node for: %s\n", ofpath); ofd_boot_cpu = dn; val = dn; dn = ofd_node_find(mem, "/chosen"); if (dn <= 0) of_panic("no /chosen node\n"); dc = ofd_prop_add(mem, dn, "cpu", &val, sizeof (val)); if (dc <= 0) of_panic("could not fix /chosen/cpu\n"); rc = 1; } else { of_printf("*** can't find path to booting cpu, " "SMP is disabled\n"); ofd_boot_cpu = -1; } } return rc; } /* PIBS Version 1.05.0000 04/26/2005 has an incorrect /ht/isa/ranges * property. The values are bad, and it doesn't even have the * right number of cells. */ static void __init boot_of_fix_maple(void) { int isa; const char *ranges = "ranges"; u32 isa_ranges[3]; const u32 isa_test[] = { 0x00000001, 0xf4000000, 0x00010000 }; const u32 isa_fixed[] = { 0x00000001, 0x00000000, 0x00000000, /* 0xf4000000, matt says this */ 0x00000000, 0x00000000, 0x00010000 }; isa = of_finddevice("/ht@0/isa@4"); if (isa != OF_FAILURE) { if (of_getproplen(isa, ranges) == sizeof (isa_test)) { of_getprop(isa, ranges, isa_ranges, sizeof (isa_ranges)); if (memcmp(isa_ranges, isa_test, sizeof (isa_test)) == 0) { int rc; of_printf("OF: fixing bogus ISA range on maple\n"); rc = of_setprop(isa, ranges, isa_fixed, sizeof (isa_fixed)); if (rc == OF_FAILURE) { of_panic("of_setprop() failed\n"); } } } } } static int __init boot_of_serial(void *oft) { int n; int p; int rc; u32 val[3]; char buf[128]; n = of_instance_to_package(of_out); if (n == OF_FAILURE) { of_panic("instance-to-package of /chosen/stdout: failed\n"); } /* Prune all serial devices from the device tree, including the * one pointed to by /chosen/stdout, because a guest domain can * initialize them and in so doing corrupt our console output. */ for (p = n; p > 0; p = of_getpeer(p)) { char type[32]; rc = of_package_to_path(p, buf, sizeof(buf)); if (rc == OF_FAILURE) of_panic("package-to-path failed\n"); rc = of_getprop(p, "device_type", type, sizeof (type)); if (rc == OF_FAILURE) { of_printf("%s: fetching type of `%s' failed\n", __func__, buf); continue; } if (strcmp(type, "serial") != 0) continue; of_printf("pruning `%s' from devtree\n", buf); rc = ofd_prune_path(oft, buf); if (rc < 0) of_panic("prune of `%s' failed\n", buf); } p = of_getparent(n); if (p == OF_FAILURE) { of_panic("no parent for: 0x%x\n", n); } buf[0] = '\0'; of_getprop(p, "device_type", buf, sizeof (buf)); if (strstr(buf, "isa") == NULL) { of_panic("only ISA UARTS supported\n"); } /* should get this from devtree */ isa_io_base = 0xf4000000; of_printf("%s: ISA base: 0x%lx\n", __func__, isa_io_base); buf[0] = '\0'; of_getprop(n, "device_type", buf, sizeof (buf)); if (strstr(buf, "serial") == NULL) { of_panic("only UARTS supported\n"); } rc = of_getprop(n, "reg", val, sizeof (val)); if (rc == OF_FAILURE) { of_panic("%s: no location for serial port\n", __func__); } ns16550.baud = BAUD_AUTO; ns16550.data_bits = 8; ns16550.parity = 'n'; ns16550.stop_bits = 1; rc = of_getprop(n, "interrupts", val, sizeof (val)); if (rc == OF_FAILURE) { of_printf("%s: no ISRC, forcing poll mode\n", __func__); ns16550.irq = 0; } else { ns16550.irq = val[0]; of_printf("%s: ISRC=0x%x, but forcing poll mode\n", __func__, ns16550.irq); ns16550.irq = 0; } return 1; } static int __init boot_of_rtas(module_t *mod, multiboot_info_t *mbi) { int rtas_node; int rtas_instance; uint size = 0; int res[2]; int mem; int ret; rtas_node = of_finddevice("/rtas"); if (rtas_node <= 0) { of_printf("No RTAS, Xen has no power control\n"); return 0; } of_getprop(rtas_node, "rtas-size", &size, sizeof (size)); if (size == 0) { of_printf("RTAS, has no size\n"); return 0; } rtas_instance = of_open("/rtas"); if (rtas_instance == OF_FAILURE) { of_printf("RTAS, could not open\n"); return 0; } size = ALIGN_UP(size, PAGE_SIZE); mem = boot_of_alloc(size); if (mem == 0) of_panic("Could not allocate RTAS tree\n"); of_printf("instantiating RTAS at: 0x%x\n", mem); ret = of_call("call-method", 3, 2, res, "instantiate-rtas", rtas_instance, mem); if (ret == OF_FAILURE) { of_printf("RTAS, could not open\n"); return 0; } rtas_entry = res[1]; rtas_base = mem; rtas_end = mem + size; rtas_msr = of_msr; mod->mod_start = rtas_base; mod->mod_end = rtas_end; return 1; } static void * __init boot_of_devtree(module_t *mod, multiboot_info_t *mbi) { void *oft; ulong oft_sz = 48 * PAGE_SIZE; /* snapshot the tree */ oft = (void *)boot_of_alloc(oft_sz); if (oft == NULL) of_panic("Could not allocate OFD tree\n"); of_printf("creating oftree at: 0x%p\n", oft); of_test("package-to-path"); oft = ofd_create(oft, oft_sz); pkg_save(oft); if (ofd_size(oft) > oft_sz) of_panic("Could not fit all of native devtree\n"); boot_of_fixup_refs(oft); boot_of_fixup_chosen(oft); if (ofd_size(oft) > oft_sz) of_panic("Could not fit all devtree fixups\n"); ofd_walk(oft, __func__, OFD_ROOT, /* add_hype_props */ NULL, 2); mod->mod_start = (ulong)oft; mod->mod_end = mod->mod_start + oft_sz; of_printf("%s: devtree mod @ 0x%016x - 0x%016x\n", __func__, mod->mod_start, mod->mod_end); return oft; } static void * __init boot_of_module(ulong r3, ulong r4, multiboot_info_t *mbi) { static module_t mods[4]; ulong mod0_start; ulong mod0_size; static const char * sepr[] = {" -- ", " || "}; int sepr_index; extern char dom0_start[] __attribute__ ((weak)); extern char dom0_size[] __attribute__ ((weak)); const char *p = NULL; int mod; void *oft; if ((r3 > 0) && (r4 > 0)) { /* was it handed to us in registers ? */ mod0_start = r3; mod0_size = r4; of_printf("%s: Dom0 was loaded and found using r3/r4:" "0x%lx[size 0x%lx]\n", __func__, mod0_start, mod0_size); } else { /* see if it is in the boot params */ p = strstr((char *)((ulong)mbi->cmdline), "dom0_start="); if ( p != NULL) { p += 11; mod0_start = simple_strtoul(p, NULL, 0); p = strstr((char *)((ulong)mbi->cmdline), "dom0_size="); p += 10; mod0_size = simple_strtoul(p, NULL, 0); of_printf("%s: Dom0 was loaded and found using cmdline:" "0x%lx[size 0x%lx]\n", __func__, mod0_start, mod0_size); } else if ( ((ulong)dom0_start != 0) && ((ulong)dom0_size != 0) ) { /* was it linked in ? */ mod0_start = (ulong)dom0_start; mod0_size = (ulong)dom0_size; of_printf("%s: Dom0 is linked in: 0x%lx[size 0x%lx]\n", __func__, mod0_start, mod0_size); } else { mod0_start = (ulong)_end; mod0_size = 0; of_printf("%s: FYI Dom0 is unknown, will be caught later\n", __func__); } } if (mod0_size > 0) { const char *c = (const char *)mod0_start; of_printf("mod0: %o %c %c %c\n", c[0], c[1], c[2], c[3]); } mod = 0; mods[mod].mod_start = mod0_start; mods[mod].mod_end = mod0_start + mod0_size; of_printf("%s: dom0 mod @ 0x%016x[0x%x]\n", __func__, mods[mod].mod_start, mods[mod].mod_end); /* look for delimiter: "--" or "||" */ for (sepr_index = 0; sepr_index < ARRAY_SIZE(sepr); sepr_index++){ p = strstr((char *)(ulong)mbi->cmdline, sepr[sepr_index]); if (p != NULL) break; } if (p != NULL) { /* Xen proper should never know about the dom0 args. */ *(char *)p = '\0'; p += strlen(sepr[sepr_index]); mods[mod].string = (u32)(ulong)p; of_printf("%s: dom0 mod string: %s\n", __func__, p); } ++mod; if (boot_of_rtas(&mods[mod], mbi)) ++mod; oft = boot_of_devtree(&mods[mod], mbi); if (oft == NULL) of_panic("%s: boot_of_devtree failed\n", __func__); ++mod; mbi->flags |= MBI_MODULES; mbi->mods_count = mod; mbi->mods_addr = (u32)mods; return oft; } static int __init boot_of_cpus(void) { int cpus_node, cpu_node; int bootcpu_instance, bootcpu_node; int logical; int result; s32 cpuid; u32 cpu_clock[2]; extern uint cpu_hard_id[NR_CPUS]; u32 tbf; /* Look up which CPU we are running on right now and get all info * from there */ result = of_getprop(bof_chosen, "cpu", &bootcpu_instance, sizeof (bootcpu_instance)); if (result == OF_FAILURE) of_panic("Failed to look up boot cpu instance\n"); bootcpu_node = of_instance_to_package(bootcpu_instance); if (result == OF_FAILURE) of_panic("Failed to look up boot cpu package\n"); cpu_node = bootcpu_node; result = of_getprop(cpu_node, "timebase-frequency", &tbf, sizeof(tbf)); timebase_freq = tbf; if (result == OF_FAILURE) { of_panic("Couldn't get timebase frequency!\n"); } of_printf("OF: timebase-frequency = %ld Hz\n", timebase_freq); result = of_getprop(cpu_node, "clock-frequency", &cpu_clock, sizeof(cpu_clock)); if (result == OF_FAILURE || (result !=4 && result != 8)) { of_panic("Couldn't get clock frequency!\n"); } cpu_khz = cpu_clock[0]; if (result == 8) { cpu_khz <<= 32; cpu_khz |= cpu_clock[1]; } cpu_khz /= 1000; of_printf("OF: clock-frequency = %ld KHz\n", cpu_khz); /* We want a continuous logical cpu number space and we'll make * the booting CPU logical 0. */ cpu_set(0, cpu_present_map); cpu_set(0, cpu_online_map); cpu_set(0, cpu_possible_map); result = of_getprop(cpu_node, "reg", &cpuid, sizeof(cpuid)); cpu_hard_id[0] = cpuid; /* Spin up all CPUS, even if there are more than NR_CPUS or we are * runnign nosmp, because Open Firmware has them spinning on cache * lines which will eventually be scrubbed, which could lead to * random CPU activation. */ /* Find the base of the multi-CPU package node */ cpus_node = of_finddevice("/cpus"); if (cpus_node <= 0) { of_printf("Single Processor System\n"); return 1; } /* Start with the first child */ cpu_node = of_getchild(cpus_node); for (logical = 1; cpu_node > 0; logical++) { unsigned int ping, pong; unsigned long now, then, timeout; if (cpu_node == bootcpu_node) { /* same CPU as boot CPU shich we have already made 0 so * reduce the logical count */ --logical; } else { result = of_getprop(cpu_node, "reg", &cpuid, sizeof(cpuid)); if (result == OF_FAILURE) of_panic("cpuid lookup failed\n"); cpu_hard_id[logical] = cpuid; of_printf("spinning up secondary processor #%d: ", logical); __spin_ack = ~0x0; ping = __spin_ack; pong = __spin_ack; of_printf("ping = 0x%x: ", ping); mb(); result = of_start_cpu(cpu_node, (ulong)spin_start, logical); if (result == OF_FAILURE) of_panic("start cpu failed\n"); /* We will give the secondary processor five seconds to reply. */ then = mftb(); timeout = then + (5 * timebase_freq); do { now = mftb(); if (now >= timeout) { of_printf("BROKEN: "); break; } mb(); pong = __spin_ack; } while (pong == ping); of_printf("pong = 0x%x\n", pong); if (pong != ping) { cpu_set(logical, cpu_present_map); cpu_set(logical, cpu_possible_map); } } cpu_node = of_getpeer(cpu_node); } return 1; } multiboot_info_t __init *boot_of_init( ulong r3, ulong r4, ulong vec, ulong r6, ulong r7, ulong orig_msr) { static multiboot_info_t mbi; void *oft; int r; of_vec = vec; of_msr = orig_msr; bof_chosen = of_finddevice("/chosen"); of_getprop(bof_chosen, "stdout", &of_out, sizeof (of_out)); of_printf("%s\n", "---------------------------------------------------"); of_printf("OF: Xen/PPC version %d.%d%s (%s@%s) (%s) %s\n", xen_major_version(), xen_minor_version(), xen_extra_version(), xen_compile_by(), xen_compile_domain(), xen_compiler(), xen_compile_date()); of_printf("%s args: 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n" "boot msr: 0x%lx\n", __func__, r3, r4, vec, r6, r7, orig_msr); if (is_kernel(vec)) { of_panic("Hmm.. OF[0x%lx] seems to have stepped on our image " "that ranges: %p .. %p.\n", vec, _start, _end); } of_printf("%s: _start %p _end %p 0x%lx\n", __func__, _start, _end, r6); boot_of_fix_maple(); r = boot_of_mem_init(); if (r == 0) of_panic("failure to initialize memory allocator"); boot_of_bootargs(&mbi); oft = boot_of_module(r3, r4, &mbi); boot_of_cpus(); boot_of_serial(oft); /* end of OF */ of_printf("Quiescing Open Firmware ...\n"); of_call("quiesce", 0, 0, NULL); return &mbi; } /* * Local variables: * mode: C * c-set-style: "BSD" * c-basic-offset: 4 * tab-width: 4 * indent-tabs-mode: nil * End: */