diff --git a/include/lgi/common/AudioView.h b/include/lgi/common/AudioView.h --- a/include/lgi/common/AudioView.h +++ b/include/lgi/common/AudioView.h @@ -1,1723 +1,1882 @@ /// Audio waveform display control #pragma once #include "lgi/common/Layout.h" #include "lgi/common/DisplayString.h" #include "lgi/common/Menu.h" #include "lgi/common/ClipBoard.h" #include "lgi/common/FileSelect.h" #include "lgi/common/Thread.h" #include "lgi/common/ThreadEvent.h" #include "lgi/common/DragAndDrop.h" #include "lgi/common/DropFiles.h" #include "ao.h" #define CC(code) LgiSwap32(code) +#define ERROR_ZERO_SAMPLE_COUNT 10 struct int24_t { int32_t s : 24; }; union sample_t { int8_t *i8; int16_t *i16; int24_t *i24; int32_t *i32; float *f; sample_t(void *ptr = NULL) { *this = ptr; } sample_t &operator=(void *ptr) { i8 = (int8_t*)ptr; return *this; } }; template struct Rect { T x1 = 0, y1 = 0, x2 = 0, y2 = 0; Rect &operator =(const LRect &r) { x1 = r.x1; y1 = r.y1; x2 = r.x2; y2 = r.y2; return *this; } T X() { return x2 - x1 + 1; } T Y() { return y2 - y1 + 1; } bool Valid() { return x2 >= x1 && y2 >= y1; } operator LRect() { LRect r((int)x1, (int)y1, (int)x2, (int)y2); return r; } void ZOff(T sx, T sy) { x1 = y1 = 0; x2 = sx; y2 = sy; } const char *GetStr() { static char s[96]; sprintf_s(s, sizeof(s), LPrintfInt64 "," LPrintfInt64 "," LPrintfInt64 "," LPrintfInt64, x1, y1, x2, y2); return s; } Rect &Offset(T x, T y) { x1 += x; x2 += x; y1 += y; y2 += y; return *this; } }; typedef int32_t (*ConvertFn)(sample_t &ptr); class LPopupNotification : public LWindow { constexpr static int Border = 10; // px - constexpr static int ShowMs = 3000; // milliseconds + constexpr static int ShowMs = 2300; // milliseconds LColour Back = LColour(0xf7, 0xf0, 0xd5); LColour Fore = LColour(0xd4, 0xb8, 0x62); LWindow *RefWnd = NULL; LArray Msgs; LPoint Decor = LPoint(2, 2); uint64_t HideTs = 0; LPoint CalcSize() { LPoint p; for (auto ds: Msgs) { p.x = MAX(p.x, ds->X()); p.y += ds->Y(); } p.x += Border * 2 + Decor.x; p.y += Border * 2 + Decor.y; return p; } public: static LPopupNotification *Inst() { static LAutoPtr inst; if (!inst) inst.Reset(new LPopupNotification(NULL, NULL)); return inst; } LPopupNotification(LWindow *ref, LString msg) { SetTitleBar(false); Name("Notification"); if (ref) RefWnd = ref; if (ref && msg) Add(ref, msg); } void Init() { if (!RefWnd) { LAssert(!"No reference window."); return; } auto r = RefWnd->GetPos(); auto Sz = CalcSize(); LRect pos; pos.ZOff(Sz.x - 1, Sz.y - 1); pos.Offset(r.x2 - pos.X(), r.y2 - pos.Y()); SetPos(pos); + SetAlwaysOnTop(true); SetPulse(500); if (!IsAttached()) Attach(0); Visible(true); } void Add(LWindow *ref, LString msg) { if (msg) { HideTs = LCurrentTime(); Msgs.Add(new LDisplayString(GetFont(), msg)); } if (ref && RefWnd != ref) RefWnd = ref; if (RefWnd && Msgs.Length() > 0) Init(); } void OnPaint(LSurface *pDC) { pDC->Colour(Fore); auto c = GetClient(); pDC->Box(&c); c.Inset(1, 1); pDC->Colour(Back); pDC->Rectangle(&c); auto f = GetFont(); f->Fore(Fore); f->Back(Back); f->Transparent(true); int x = c.x1 + Border; int y = c.y1 + Border; for (auto ds: Msgs) { ds->Draw(pDC, x, y); y += ds->Y(); } } void OnPulse() { if (HideTs && LCurrentTime() >= HideTs + ShowMs) { HideTs = 0; Visible(false); SetPulse(); Msgs.DeleteObjects(); } } }; class LAudioView : public LLayout, public LThread, public LCancel, public LDragDropTarget { public: enum LFileType { AudioUnknown, AudioRaw, AudioWav, }; enum LSampleType { AudioS8, AudioS16LE, AudioS16BE, AudioS24LE, AudioS24BE, AudioS32LE, AudioS32BE, AudioFloat32, }; enum Messages { M_WAVE_FORMS_FINISHED = M_USER + 100, + M_SAVE_FINISHED, }; struct LBookmark { int x = 0; bool error = false; int64_t sample; LColour colour; }; protected: enum LCmds { IDC_COPY_CURSOR, IDC_LOAD_WAV, IDC_LOAD_RAW, IDC_SAVE_WAV, IDC_SAVE_RAW, IDC_DRAW_AUTO, IDC_DRAW_LINE, IDC_SCAN_SAMPLES, IDC_SCAN_GROUPS, IDC_44K, IDC_48K, IDC_88K, IDC_96K, IDC_MONO, IDC_STEREO, IDC_2pt1_CHANNELS, IDC_5pt1_CHANNELS, }; enum LDrawMode { DrawAutoSelect, DrawSamples, ScanSamples, ScanGroups, }; enum CtrlState { CtrlNormal, CtrlLoading, CtrlBuildingWaveforms, CtrlSaving, } State = CtrlNormal; - LAutoPtr Worker; - LArray Audio; LFileType Type = AudioUnknown; LSampleType SampleType = AudioS16LE; int SampleBits = 0; int SampleRate = 0; int Channels = 0; size_t DataStart = 0; int64_t CursorSample = 0; Rect Data; LArray> ChData; double XZoom = 1.0; LString Msg, ErrorMsg; LDrawMode DrawMode = DrawAutoSelect; - LArray Bookmarks; + LArray> Bookmarks; LString FilePath; template struct Grp { T Min = 0, Max = 0; }; LArray>> IntGrps; LArray>> FloatGrps; // Libao stuff int AoDriver = -1; bool AoPlaying = false; ao_device *AoDev = NULL; LThreadEvent AoEvent; void MouseToCursor(LMouse &m) { auto idx = ViewToSample(m.x); if (idx != CursorSample) { CursorSample = idx; UpdateMsg(); Invalidate(); } } template void SetData(T &r) { Data = r; ChData.Length(Channels); auto Dy = (int)Data.Y() - 1; for (int i=0; iMsg()) { case M_WAVE_FORMS_FINISHED: OnWaveFormsFinished(); break; } return LView::OnEvent(m); } int WillAccept(LDragFormats &Formats, LPoint Pt, int KeyState) { Formats.SupportsFileDrops(); return DROPEFFECT_COPY; } int OnDrop(LArray &Data, LPoint Pt, int KeyState) { for (auto &d : Data) { if (d.IsFileDrop()) { LDropFiles df(d); auto w = GetWindow(); if (w) { w->OnReceiveFiles(df); return DROPEFFECT_COPY; } } } return DROPEFFECT_NONE; } bool Empty() { Audio.Free(); Type = AudioUnknown; SampleType = AudioS16LE; SampleRate = 0; SampleBits = 0; DataStart = 0; Channels = 0; CursorSample = 0; Data.ZOff(-1, -1); ChData.Empty(); Msg.Empty(); FilePath.Empty(); + IntGrps.Empty(); + Bookmarks.Empty(); + + if (AoPlaying) + { + AoPlaying = false; + LSleep(50); + } + if (AoDev) + { + ao_close(AoDev); + AoDev = NULL; + } Invalidate(); return false; } bool Load(const char *FileName, int rate = 0, int bitDepth = 0, int channels = 0) { LFile f(FileName, O_READ); if (!f.IsOpen()) { ErrorMsg.Printf("Can't open '%s' for reading.", FileName); return false; } + Empty(); Msg.Printf("Loading '%s'...", FileName); LPopupNotification::Inst()->Add(GetWindow(), Msg); - FilePath = FileName; if (!Audio.Length(f.GetSize())) { ErrorMsg.Printf("Can't allocate %s.", LFormatSize(f.GetSize()).Get()); return Empty(); } auto rd = f.Read(Audio.AddressOf(), f.GetSize()); if (rd > 0 && rd != f.GetSize()) Audio.Length(rd); auto Ext = LGetExtension(FileName); if (!Stricmp(Ext, "wav")) Type = AudioWav; else if (!Stricmp(Ext, "raw")) Type = AudioRaw; else { ErrorMsg.Printf("Unknown format: %s", Ext); LPopupNotification::Inst()->Add(GetWindow(), ErrorMsg); return Empty(); } if (rate) SampleRate = rate; DataStart = 0; SampleBits = bitDepth; Channels = channels; if (bitDepth == 16) { SampleType = AudioS16LE; } else if (bitDepth == 32) { SampleType = AudioS32LE; } else if (Type == AudioWav) { // Parse the wave file... LPointer p; p.s8 = Audio.AddressOf(); auto end = p.s8 + Audio.Length(); if (*p.u32++ != CC('RIFF')) return Empty(); auto ChunkSz = *p.u32++; auto Fmt = *p.u32++; while (p.s8 < end) { auto SubChunkId = *p.u32++; auto SubChunkSz = *p.u32++; auto NextChunk = p.u8 + SubChunkSz; if (SubChunkId == CC('fmt ')) { auto AudioFmt = *p.u16++; Channels = *p.u16++; SampleRate = *p.u32++; auto ByteRate = *p.u32++; auto BlockAlign = *p.u16++; SampleBits = *p.u16++; if (SampleBits == 16) SampleType = AudioS16LE; else if (SampleBits == 24) SampleType = AudioS24LE; else if (SampleBits == 32) SampleType = AudioS32LE; printf("Channels=%i\n", Channels); printf("SampleRate=%i\n", SampleRate); printf("SampleBits=%i\n", SampleBits); } else if (SubChunkId == CC('data')) { DataStart = p.s8 - Audio.AddressOf(); printf("DataStart=" LPrintfSizeT "\n", DataStart); break; } p.u8 = NextChunk; } if (!DataStart) { ErrorMsg = "No 'data' element found."; LPopupNotification::Inst()->Add(GetWindow(), ErrorMsg); return Empty(); } } else if (Type == AudioRaw) { // Assume 32bit SampleType = AudioS32LE; SampleBits = 32; } if (!SampleRate) SampleRate = 44100; if (!Channels) Channels = 2; Invalidate(); LPopupNotification::Inst()->Add(GetWindow(), "Loaded ok..."); return true; } - bool Save(const char *FileName = NULL) +private: + struct SaveThread : public LThread + { + LAudioView *view; + LString ErrorMsg; + LString FileName; + + SaveThread(LAudioView *v, const char *filename) : LThread("SaveThread", v->AddDispatch()) + { + view = v; + FileName = filename; + + LString Msg; + Msg.Printf("Saving '%s'...", FileName.Get()); + LPopupNotification::Inst()->Add(view->GetWindow(), Msg); + + Run(); + } + + ~SaveThread() + { + WaitForExit(); + } + + void OnComplete() + { + if (ErrorMsg) + LPopupNotification::Inst()->Add(view->GetWindow(), ErrorMsg); + else + LPopupNotification::Inst()->Add(view->GetWindow(), "Saved ok."); + + view->State = CtrlNormal; + view->Saving.Release(); // it doesn't own the ptr anymore + DeleteOnExit = true; + } + + int Main() + { + LFile f; + if (!f.Open(FileName, O_WRITE)) + { + ErrorMsg.Printf("Can't open '%s' for writing.", FileName); + return -1; + } + + f.SetSize(0); + auto wr = f.Write(view->Audio.AddressOf(0), view->Audio.Length()); + if (wr != view->Audio.Length()) + { + ErrorMsg.Printf("Write error: only wrote " LPrintfSizeT " of " LPrintfSizeT " bytes", wr, view->Audio.Length()); + return -1; + } + + return 0; + } + }; + + LAutoPtr Saving; + + void Save(const char *FileName = NULL) { if (!FileName) FileName = FilePath; if (!FileName) - return false; - - LFile f; - if (!f.Open(FileName, O_WRITE)) - { - ErrorMsg.Printf("Can't open '%s' for writing.", FileName); - LPopupNotification::Inst()->Add(GetWindow(), ErrorMsg); - return false; - } + return; - LString Msg; - Msg.Printf("Saving '%s'...", FileName); - LPopupNotification::Inst()->Add(GetWindow(), Msg); - f.SetSize(0); - auto wr = f.Write(Audio.AddressOf(0), Audio.Length()); - if (wr != Audio.Length()) - { - ErrorMsg.Printf("Write error: only wrote " LPrintfSizeT " of " LPrintfSizeT " bytes", wr, Audio.Length()); - LPopupNotification::Inst()->Add(GetWindow(), ErrorMsg); - return false; - } - - LPopupNotification::Inst()->Add(GetWindow(), "Saved ok."); - return true; + Saving.Reset(new SaveThread(this, FileName)); } LRect DefaultPos() { auto c = GetClient(); c.y1 += LSysFont->GetHeight(); c.Inset(10, 10); return c; } size_t GetSamples() { return (Audio.Length() - DataStart) / (SampleBits >> 3) / Channels; } int SampleToView(size_t idx) { return (int) (Data.x1 + ((idx * Data.X()) / GetSamples())); } size_t ViewToSample(int x /*px*/) { int64_t offset = (int64_t)x - (int64_t)Data.x1; int64_t samples = GetSamples(); int64_t dx = (int64_t)Data.x2 - (int64_t)Data.x1 + 1; double pos = (double) offset / dx; int64_t idx = (int64_t)(samples * pos); printf("ViewToSample(%i) data=%s offset=" LPrintfInt64 " samples=" LPrintfInt64 " idx=" LPrintfInt64 " pos=%f\n", x, Data.GetStr(), offset, samples, idx, pos); if (idx < 0) idx = 0; else if (idx >= samples) idx = samples - 1; return idx; } void OnPosChange() { auto def = DefaultPos(); LRect d = Data; d.y1 = def.y1; d.y2 = def.y2; SetData(d); } #define CheckItem(Sub, Name, Id, Chk) \ { \ auto item = Sub->AppendItem(Name, Id); \ if (item && Chk) item->Checked(true); \ } void OnMouseClick(LMouse &m) { if (m.IsContextMenu()) { LSubMenu s; s.AppendItem("Copy Cursor Address", IDC_COPY_CURSOR); s.AppendSeparator(); auto File = s.AppendSub("File"); File->AppendItem("Load Wav", IDC_LOAD_WAV); File->AppendItem("Load Raw", IDC_LOAD_RAW); File->AppendItem("Save Wav", IDC_SAVE_WAV); File->AppendItem("Save Raw", IDC_SAVE_RAW); auto Draw = s.AppendSub("Draw Mode"); CheckItem(Draw, "Auto", IDC_DRAW_AUTO, DrawMode == DrawAutoSelect); CheckItem(Draw, "Line", IDC_DRAW_LINE, DrawMode == DrawSamples); CheckItem(Draw, "Scan Samples", IDC_SCAN_SAMPLES, DrawMode == ScanSamples); CheckItem(Draw, "Scan Groups", IDC_SCAN_GROUPS, DrawMode == ScanGroups); auto Rate = s.AppendSub("Sample Rate"); CheckItem(Rate, "44.1k", IDC_44K, SampleRate == 44100); CheckItem(Rate, "48k", IDC_48K, SampleRate == 4800); CheckItem(Rate, "88.2k", IDC_88K, SampleRate == 44100*2); CheckItem(Rate, "96k", IDC_96K, SampleRate == 48000*2); auto Ch = s.AppendSub("Channels"); CheckItem(Ch, "Mono", IDC_MONO, Channels == 1); CheckItem(Ch, "Stereo", IDC_STEREO, Channels == 2); CheckItem(Ch, "2.1", IDC_2pt1_CHANNELS, Channels == 3); CheckItem(Ch, "5.1", IDC_5pt1_CHANNELS, Channels == 6); bool Update = false; switch (s.Float(this, m)) { case IDC_COPY_CURSOR: { LClipBoard clip(this); size_t addrVal = DataStart + (CursorSample * Channels * SampleBits / 8); LString addr; addr.Printf(LPrintfSizeT, addrVal); clip.Text(addr); break; } case IDC_SAVE_RAW: { auto s = new LFileSelect; s->Parent(this); s->Save([this](auto s, auto ok) { if (ok) { LFile out(s->Name(), O_WRITE); if (out) { out.SetSize(0); auto ptr = Audio.AddressOf(DataStart); out.Write(ptr, Audio.Length() - DataStart); } else LgiMsg(this, "Can't open '%s' for writing.", "Error", MB_OK, s->Name()); } delete s; }); break; } case IDC_DRAW_AUTO: DrawMode = DrawAutoSelect; Update = true; break; case IDC_DRAW_LINE: DrawMode = DrawSamples; Update = true; break; case IDC_SCAN_SAMPLES: DrawMode = ScanSamples; Update = true; break; case IDC_SCAN_GROUPS: DrawMode = ScanGroups; Update = true; break; case IDC_44K: SampleRate = 44100; Update = true; break; case IDC_48K: SampleRate = 48000; Update = true; break; case IDC_88K: SampleRate = 44100*2; Update = true; break; case IDC_96K: SampleRate = 48000*2; Update = true; break; case IDC_MONO: Channels = 1; Update = true; break; case IDC_STEREO: Channels = 2; Update = true; break; case IDC_2pt1_CHANNELS: Channels = 3; Update = true; break; case IDC_5pt1_CHANNELS: Channels = 6; Update = true; break; default: break; } if (Update) { IntGrps.Length(0); FloatGrps.Length(0); SetData(Data); Invalidate(); } } else if (m.Left()) { Focus(true); MouseToCursor(m); } } void OnMouseMove(LMouse &m) { if (m.Down()) MouseToCursor(m); } bool OnKey(LKey &k) { - k.Trace("key"); + // k.Trace("key"); if (k.IsChar) { switch (k.c16) { case 'f': case 'F': { if (k.Down()) FindErrors(); return true; } case 'r': case 'R': { if (k.Down()) RepairNext(); return true; } case 'a': case 'A': { if (k.Down()) RepairAll(); return true; } case ' ': { if (k.Down()) TogglePlay(); return true; } } } else if (k.Ctrl()) { if (k.c16 == 'S') { if (k.Down()) Save(); return true; } else if (k.c16 == 'W') { if (k.Down()) Empty(); return true; } } return false; } sample_t AddressOf(size_t Sample) { - int SampleBytes = SampleBits >> 3; - return sample_t(Audio.AddressOf(DataStart + (Sample * Channels * SampleBytes))); + int SampleBytes = Channels * (SampleBits >> 3); + return sample_t(Audio.AddressOf(DataStart + (Sample * SampleBytes))); } - void RepairBookmark(LBookmark &bm) + void RepairBookmark(int Channel, LBookmark &bm) { auto Convert = GetConvert(); auto end = Audio.AddressOf(Audio.Length()-1) + 1; + int sampleBytes = SampleBits / 8; + int skipBytes = (Channels - 1) * sampleBytes; + int channelOffset = Channel * sampleBytes; // Look at previous sample... auto repairSample = bm.sample; auto p = AddressOf(repairSample - 1); + p.i8 += channelOffset; auto lastGood = Convert(p); + p.i8 += skipBytes; // Seek over and find the end... auto nextGood = Convert(p); + p.i8 += skipBytes; while (nextGood) { nextGood = Convert(p); + p.i8 += skipBytes; repairSample++; } auto endRepair = repairSample; while (nextGood == 0) { nextGood = Convert(p); + p.i8 += skipBytes; endRepair++; } int64_t sampleDiff = nextGood - lastGood; int64_t repairSamples = endRepair - repairSample + 1; // Write the new interpolated samples for (int64_t i = repairSample; i < endRepair; i++) { p = AddressOf(i); + p.i8 += channelOffset; if (p.i8 >= end) break; switch (SampleType) { case AudioS24LE: { auto off = i - repairSample + 1; p.i24->s = lastGood + (off * sampleDiff / repairSamples); break; } default: { LAssert(!"Impl me."); break; } } } bm.colour = LColour::Green; bm.error = false; } void RepairNext() { - int64_t prev = 0; - LBookmark *Bookmark = NULL; - for (auto &bm: Bookmarks) + for (int ch = 0; ch < Channels; ch++) { - if (bm.sample >= CursorSample && - prev < CursorSample && - bm.error) + int64_t prev = 0; + LBookmark *Bookmark = NULL; + for (auto &bm: Bookmarks[ch]) { - Bookmark = &bm; - break; + if (bm.sample >= CursorSample && + prev < CursorSample && + bm.error) + { + Bookmark = &bm; + break; + } } + + if (Bookmark) + RepairBookmark(ch, *Bookmark); } - if (!Bookmark) - return; - - RepairBookmark(*Bookmark); Invalidate(); } void RepairAll() { - for (auto &bm: Bookmarks) - RepairBookmark(bm); + for (int ch = 0; ch < Bookmarks.Length(); ch++) + for (auto &bm: Bookmarks[ch]) + RepairBookmark(ch, bm); Invalidate(); } int64_t PtrToSample(sample_t &ptr) { int sampleBytes = SampleBits >> 3; auto start = Audio.AddressOf(DataStart); return (ptr.i8 - start) / (sampleBytes * Channels); } +private: + struct ErrorThread : public LThread + { + LAudioView *view; + int64_t start, end; + int channel; + LArray bookmarks; + + ErrorThread(LAudioView *v, int64_t startSample, int64_t endSample, int ch) : + LThread("ErrorThread", v->AddDispatch()) + { + view = v; + start = startSample; + end = endSample; + channel = ch; + + Run(); + } + + void OnComplete() + { + view->Bookmarks[channel].Add(bookmarks); + view->Bookmarks[channel].Sort([](auto a, auto b) + { + return (int)(a->sample - b->sample); + }); + + view->Invalidate(); + + LAssert(view->ErrorThreads.HasItem(this)); + view->ErrorThreads.Delete(this); + if (view->ErrorThreads.Length() == 0) + LPopupNotification::Inst()->Add(view->GetWindow(), "Done."); + DeleteOnExit = true; + } + + int Main() + { + auto Convert = view->GetConvert(); + int32_t prev = 0; + int sampleBytes = view->SampleBits / 8; + int byteInc = (view->Channels - 1) * sampleBytes; + int64_t zeroRunStart = -1; + + auto ptr = view->AddressOf(0); + auto pStart = ptr.i8 + (start * view->Channels * sampleBytes) + (channel * sampleBytes); + auto pEnd = ptr.i8 + (end * view->Channels * sampleBytes) + (channel * sampleBytes); + ptr.i8 = pStart; + + while (ptr.i8 < pEnd) + { + auto s = Convert(ptr); + ptr.i8 += byteInc; + + auto diff = s - prev; + if (diff < 0) + diff = -diff; + + if (s == 0) + { + if (zeroRunStart < 0) + zeroRunStart = view->PtrToSample(ptr) - 1; + } + else if (zeroRunStart >= 0) + { + auto cur = view->PtrToSample(ptr) - 1; + auto len = cur - zeroRunStart; + if (len >= ERROR_ZERO_SAMPLE_COUNT) + { + // emit book mark + auto &bm = bookmarks.New(); + bm.sample = zeroRunStart; + bm.colour = LColour::Red; + bm.error = true; + + #if 0 + if (Bookmarks.Length() >= 10000) + break; + #endif + } + + zeroRunStart = -1; + } + + prev = s; + } + + return 0; + } + }; + + LArray ErrorThreads; + +public: void FindErrors() { - auto Convert = GetConvert(); - auto ptr = AddressOf(0); - auto start = ptr.i8; - auto end = Audio.AddressOf(Audio.Length()-1) + 1; - int32_t prev = 0; - int sampleBytes = SampleBits / 8; - int byteInc = (Channels - 1) * sampleBytes; - int threshold = 180 * 1000; - int maxDiff = 0; - int64_t zeroRunStart = -1; - - while (ptr.i8 < end) + if (ErrorThreads.Length() > 0) { - auto s = Convert(ptr); - auto diff = s - prev; - if (diff < 0) - diff = -diff; - - if (s == 0) - { - if (zeroRunStart < 0) - zeroRunStart = PtrToSample(ptr) - 1; - } - else if (zeroRunStart >= 0) - { - auto cur = PtrToSample(ptr) - 1; - auto len = cur - zeroRunStart; - if (len >= 12) - { - // emit book mark - auto &bm = Bookmarks.New(); - bm.sample = zeroRunStart; - bm.colour = LColour::Red; - bm.error = true; - - if (Bookmarks.Length() >= 10000) - break; - } - - zeroRunStart = -1; - } - - /* - if (diff > threshold && s == 0) - { - // emit book mark - auto &bm = Bookmarks.New(); - bm.sample = PtrToSample(ptr) - 1; - bm.colour = LColour::Red; - bm.error = true; - - if (Bookmarks.Length() >= 5000) - break; - } - */ - - maxDiff = MAX(diff, maxDiff); - prev = s; - ptr.i8 += byteInc; + LPopupNotification::Inst()->Add(GetWindow(), "Already finding errors."); + return; } - Invalidate(); + LPopupNotification::Inst()->Add(GetWindow(), "Finding errors..."); + Bookmarks.Length(Channels); + + auto samples = GetSamples(); + auto threads = LAppInst->GetCpuCount(); + auto threadsPerCh = threads / Channels; + for (int ch=0; ch= Client.x2) d.Offset(-(d.x2 - Client.x2), 0); SetData(d); Invalidate(); } UpdateMsg(); return true; } #define PROFILE_PAINT_SAMPLES 0 #if PROFILE_PAINT_SAMPLES #define PROF(name) Prof.Add(name) #else #define PROF(name) #endif ConvertFn GetConvert() { switch (SampleType) { case AudioS8: return [](sample_t &s) -> int32_t { return *s.i8++; }; case AudioS16LE: return [](sample_t &s) -> int32_t { return *s.i16++; }; case AudioS16BE: return [](sample_t &s) -> int32_t { int16_t i = LgiSwap16(*s.i16); s.i16++; return i; }; case AudioS24LE: return [](sample_t &s) -> int32_t { int32_t i = s.i24->s; s.i8 += 3; return i; }; case AudioS24BE: return [](sample_t &s) -> int32_t { int24_t i; int8_t *p = (int8_t*)&i; p[0] = s.i8[2]; p[1] = s.i8[1]; p[2] = s.i8[0]; s.i8 += 3; return i.s; }; case AudioS32LE: return [](sample_t &s) -> int32_t { return *s.i32++; }; case AudioS32BE: return [](sample_t &s) -> int32_t { int32_t i = *s.i32++; return LgiSwap32(i); }; } return NULL; } private: constexpr static int WaveformBlockSize = 256; struct WaveFormWork { ConvertFn convert; sample_t start; int8_t *end = NULL; int stepBytes = 0; int blkBytes = 0; size_t blocks = 0; Grp *out = NULL; }; LArray WaveFormTodo; LArray WaveFormThreads; struct WaveFormThread : public LThread, public LCancel { LAudioView *view; WaveFormThread(LAudioView *v) : LThread("WaveFormThread", v->AddDispatch()) { view = v; Run(); } ~WaveFormThread() { Cancel(); } void OnComplete() { // Clean up thread array... LThread *t = this; LAssert(view->WaveFormThreads.HasItem(t)); view->WaveFormThreads.Delete(t); DeleteOnExit = true; // Last one finished? if (view->WaveFormThreads.Length() == 0) view->PostEvent(M_WAVE_FORMS_FINISHED); } int Main() { while (!IsCancelled()) { WaveFormWork *w = NULL; if (view->Lock(_FL, 500)) { // Is there work? if (view->WaveFormTodo.Length() > 0) { w = view->WaveFormTodo[0]; view->WaveFormTodo.DeleteAt(0); } view->Unlock(); if (!w) return 0; } auto s = w->start; auto block = w->out; auto convert = w->convert; // For all blocks we're assigned... for (int b = 0; b < w->blocks && !IsCancelled(); b++, s.i8 += w->blkBytes, block++) { auto ptr = s; auto blkEnd = s.i8 + w->blkBytes; auto stepBytes = w->stepBytes; // For all samples in the block... block->Min = block->Max = convert(ptr); // init min/max with first sample while (ptr.i8 < blkEnd) { auto n = convert(ptr); if (n < block->Min) block->Min = n; if (n > block->Max) block->Max = n; ptr.i8 += stepBytes; } } } return 0; } }; public: void BuildWaveForms(LArray>> *Grps) { LPopupNotification::Inst()->Add(GetWindow(), "Building waveform data..."); State = CtrlBuildingWaveforms; auto SampleBytes = SampleBits / 8; auto start = Audio.AddressOf(DataStart); size_t samples = GetSamples(); auto end = start + (SampleBytes * samples * Channels); auto BlkBytes = WaveformBlockSize * Channels * SampleBytes; auto Blocks = ((Audio.Length() - DataStart) + BlkBytes - 1) / BlkBytes; int Threads = LAppInst->GetCpuCount(); // Initialize the graph Grps->Length(Channels); for (int ch = 0; ch < Channels; ch++) { auto &GraphArr = (*Grps)[ch]; GraphArr.Length(Blocks); for (int Thread = 0; Thread < Threads; Thread++) { auto from = Thread * Blocks / Threads; auto to = (Thread + 1) * Blocks / Threads; WaveFormWork *work = new WaveFormWork; work->convert = GetConvert(); work->start.i8 = start + (from * BlkBytes) + (ch * SampleBytes); work->end = start + (to * BlkBytes) + (ch * SampleBytes); work->stepBytes = (Channels - 1) * SampleBytes; work->blkBytes = BlkBytes; work->blocks = to - from; work->out = GraphArr.AddressOf(from); WaveFormTodo.Add(work); } } for (int Thread = 0; Thread < Threads; Thread++) WaveFormThreads.Add(new WaveFormThread(this)); } void OnWaveFormsFinished() { State = CtrlNormal; LPopupNotification::Inst()->Add(GetWindow(), "Done."); Invalidate(); } void PaintSamples(LSurface *pDC, int ChannelIdx, LArray>> *Grps) { #if PROFILE_PAINT_SAMPLES LProfile Prof("PaintSamples", 10); #endif int32_t MaxT = 0; ConvertFn Convert = GetConvert(); if (SampleBits == 8) MaxT = 0x7f; else if (SampleBits == 16) MaxT = 0x7fff; else if (SampleBits == 24) MaxT = 0x7fffff; else MaxT = 0x7fffffff; auto SampleBytes = SampleBits / 8; auto Client = GetClient(); auto &c = ChData[ChannelIdx]; auto start = Audio.AddressOf(DataStart); size_t samples = GetSamples(); // printf("samples=" LPrintfSizeT "\n", samples); auto end = start + (SampleBytes * samples * Channels); auto cy = c.y1 + (c.Y() / 2); pDC->Colour(cGrid); pDC->Box(&(LRect)c); if (Grps->Length() == 0) { BuildWaveForms(Grps); return; } else if (State != CtrlNormal) { return; } PROF("PrePaint"); auto YScale = c.Y() / 2; int CursorX = SampleToView(CursorSample); double blkScale = Grps->Length() > 0 ? (double) (*Grps)[0].Length() / c.X() : 1.0; // printf("blkScale=%f blks/px\n", blkScale); LDrawMode EffectiveMode = DrawMode; auto StartSample = ViewToSample(Client.x1); auto EndSample = ViewToSample(Client.x2); auto SampleRange = EndSample - StartSample; if (EffectiveMode == DrawAutoSelect) { if (SampleRange < Client.X() * 32) EffectiveMode = DrawSamples; else if (blkScale < 1.0) EffectiveMode = ScanSamples; else EffectiveMode = ScanGroups; } // This is the bytes in channels we're not looking at int byteInc = (Channels - 1) * SampleBytes; pDC->Colour(cGrid); pDC->HLine((int)MAX(0,c.x1), (int)MIN(pDC->X(),c.x2), (int)cy); // Setup for drawing bookmarks.. - for (auto &bm: Bookmarks) + auto &BookmarkArr = Bookmarks[ChannelIdx]; + for (auto &bm: BookmarkArr) bm.x = SampleToView(bm.sample); - auto nextBookmark = Bookmarks.Length() ? Bookmarks.AddressOf(0) : NULL; - auto endBookmark = Bookmarks.Length() ? nextBookmark + Bookmarks.Length() : NULL; + auto nextBookmark = BookmarkArr.Length() ? BookmarkArr.AddressOf(0) : NULL; + auto endBookmark = BookmarkArr.Length() ? nextBookmark + BookmarkArr.Length() : NULL; if (EffectiveMode == DrawSamples) { sample_t pSample(start + (((StartSample * Channels) + ChannelIdx) * SampleBytes)); bool DrawDots = SampleRange < (Client.X() >> 2); if (CursorX >= Client.x1 && CursorX <= Client.x2) { pDC->Colour(L_BLACK); pDC->VLine(CursorX, (int)c.y1, (int)c.y2); } while ( nextBookmark && nextBookmark < endBookmark) { pDC->Colour(nextBookmark->colour); pDC->VLine(nextBookmark->x, (int)c.y1, (int)c.y2); nextBookmark++; } // For all samples in the view space... pDC->Colour(cMax); LPoint Prev(-1, -1); for (auto idx = StartSample; idx <= EndSample + 1; idx++, pSample.i8 += byteInc) { auto n = Convert(pSample); auto x = SampleToView(idx); auto y = (cy - ((int64_t)n * YScale / MaxT)); if (idx != StartSample) pDC->Line(Prev.x, Prev.y, (int)x, (int)y); Prev.Set((int)x, (int)y); if (DrawDots) pDC->Rectangle((int)x-1, (int)y-1, (int)x+1, (int)y+1); } } else { // For all x coordinates in the view space.. for (int Vx=Client.x1; Vx<=Client.x2; Vx++) { if (Vx % 100 == 0) { PROF("Pixels"); } if (Vx < c.x1 || Vx > c.x2) continue; if (nextBookmark && nextBookmark->x <= Vx) { while ( nextBookmark < endBookmark && nextBookmark->x <= Vx) { pDC->Colour(nextBookmark->colour); pDC->VLine(nextBookmark->x, (int)c.y1, (int)c.y2); nextBookmark++; } } auto isCursor = CursorX == Vx; if (isCursor) { pDC->Colour(L_BLACK); pDC->VLine(Vx, (int)c.y1, (int)c.y2); } Grp Min, Max; StartSample = ViewToSample(Vx); EndSample = ViewToSample(Vx+1); // printf("SampleIdx: " LPrintfSizeT "-" LPrintfSizeT "\n", StartSample, EndSample); if (EffectiveMode == ScanSamples) { // Scan individual samples sample_t pStart(start + ((StartSample * Channels + ChannelIdx) * SampleBytes)); auto pEnd = start + ((EndSample * Channels + ChannelIdx) * SampleBytes); #if 0 if (Vx==Client.x1) { auto PosX = SampleToView((pStart - ChannelIdx - start) / Channels); LgiTrace(" ptr=%i " LPrintfSizeT "-" LPrintfSizeT " x=%i pos=%i\n", (int)((char*)pStart-(char*)start), StartSample, EndSample, Vx, PosX); } #endif for (auto i = pStart; i.i8 < pEnd; i.i8 += byteInc) { auto n = Convert(i); Max.Min = MIN(Max.Min, n); Max.Max = MAX(Max.Max, n); } } else { // Scan the buckets auto &Graph = (*Grps)[ChannelIdx]; double blkStart = (double)StartSample * Graph.Length() / samples; double blkEnd = (double)EndSample * Graph.Length() / samples; /* if (Vx % 10 == 0) printf("BlkRange[%i]: %g-%g of:%i\n", Vx, blkStart, blkEnd, (int)Graph.Length()); */ #if 0 if (isCursor) LgiTrace("Blk %g-%g of " LPrintfSizeT "\n", blkStart, blkEnd, Graph.Length()); #endif auto iEnd = (int)ceil(blkEnd); int count = 0; for (int i=(int)floor(blkStart); iColour(isCursor ? cCursorMax : cMax); pDC->VLine(Vx, (int)y1, (int)y2); if (EffectiveMode == ScanGroups) { y1 = (cy - (Min.Max * YScale / MaxT)); y2 = (cy - (Min.Min * YScale / MaxT)); pDC->Colour(isCursor ? cCursorMin : cMin); pDC->VLine(Vx, (int)y1, (int)y2); } } } } void OnPaint(LSurface *pDC) { #ifdef WINDOWS LDoubleBuffer DblBuf(pDC); #endif pDC->Colour(L_WORKSPACE); pDC->Rectangle(); auto Fnt = GetFont(); Fnt->Transparent(true); if (!FilePath) { LDisplayString ds(Fnt, Msg); Fnt->Fore(L_LOW); auto c = GetClient().Center(); ds.Draw(pDC, c.x - (ds.X()/2), c.y - (ds.Y()/2)); return; } if (!Data.Valid()) { auto r = DefaultPos(); SetData(r); } LDisplayString ds(Fnt, ErrorMsg ? ErrorMsg : Msg); Fnt->Fore(ErrorMsg ? LColour::Red : LColour(L_LOW)); ds.Draw(pDC, 4, 4); if (ErrorMsg) return; switch (SampleType) { case AudioS16LE: case AudioS16BE: { for (int ch = 0; ch < Channels; ch++) PaintSamples(pDC, ch, &IntGrps); break; } case AudioS24LE: case AudioS24BE: { for (int ch = 0; ch < Channels; ch++) PaintSamples(pDC, ch, &IntGrps); break; } case AudioS32LE: case AudioS32BE: { for (int ch = 0; ch < Channels; ch++) PaintSamples(pDC, ch, &IntGrps); break; } case AudioFloat32: { /* for (int ch = 0; ch < Channels; ch++) PaintSamples(pDC, ch, &FloatGrps); */ break; } } } void TogglePlay() { if (AoPlaying) { + LgiTrace("stopping...\n"); AoPlaying = false; } else // Start playback { if (AoDriver < 0) { AoDriver = ao_default_driver_id(); } if (!AoDev) { ao_sample_format fmt; fmt.bits = SampleBits; fmt.rate = SampleRate; fmt.channels = Channels; fmt.byte_format = AO_FMT_LITTLE; fmt.matrix = NULL; AoDev = ao_open_live(AoDriver, &fmt, NULL); } if (AoDev) { AoPlaying = true; AoEvent.Signal(); } } } int Main() { int TimeSlice = 50; // ms - int sampleBytes = SampleBits / 8; - int sliceSamples = SampleRate * TimeSlice / 1000; - int sliceBytes = sliceSamples * Channels * sampleBytes; + int sliceBytes = 0; + int sliceSamples = 0; + int64_t playStart = 0; + int64_t playSample = 0; + bool playedSamples = false; while (!IsCancelled()) { if (AoPlaying) { // Play one timeslice of audio auto addr = AddressOf(CursorSample); + + /* + auto startTs = LMicroTime(); + auto elapsedSamples = CursorSample - playSample; + while ((LMicroTime() - playStart) < elapsedSamples) + ; + auto waitTs = LMicroTime() - startTs; + */ + ao_play(AoDev, (char*)addr.i8, sliceBytes); CursorSample += sliceSamples; + playedSamples = true; + + /* + #if 1 + LgiTrace("wait %.3fms\n", (double)waitTs / 1000.0); + #else + auto now = LMicroTime(); + auto elapsedTime = now - playStart; + double ms = (double)elapsedTime/1000.0; + elapsedSamples = CursorSample - playSample; + double sms = (double)elapsedSamples * 1000.0 / SampleRate; + LgiTrace("play ms=%.3f sms=%.3f\n", ms, sms); + #endif + */ } else { + if (playedSamples) + { + playedSamples = false; + LgiTrace("stopped.\n"); + } + AoEvent.Wait(100); + if (AoPlaying) + { + sliceSamples = SampleRate * TimeSlice / 1000; + sliceBytes = Channels * sliceSamples * (SampleBits / 8); + + playStart = LMicroTime(); + playSample = CursorSample; + } } } return 0; } bool UnitTests() { SampleType = AudioS16LE; auto c = GetConvert(); int16_t le16 = (2 << 8) | (1); sample_t ptr(&le16); auto out = c(ptr); if (out != 0x201) { LAssert(0); return false; } SampleType = AudioS16BE; c = GetConvert(); int16_t be16 = (1 << 8) | (2); ptr = &be16; out = c(ptr); if (out != 0x201) { LAssert(0); return false; } SampleType = AudioS24LE; c = GetConvert(); uint8_t le24[] = {1, 2, 3}; ptr = &le24; out = c(ptr); if (out != 0x30201) { LAssert(0); return false; } SampleType = AudioS24BE; c = GetConvert(); uint8_t be24[] = {3, 2, 1}; ptr = &be24; out = c(ptr); if (out != 0x30201) { LAssert(0); return false; } SampleType = AudioS32LE; c = GetConvert(); uint8_t le32[] = {1, 2, 3, 4}; ptr = &le32; out = c(ptr); if (out != 0x4030201) { LAssert(0); return false; } SampleType = AudioS32BE; c = GetConvert(); uint8_t be32[] = {4, 3, 2, 1}; ptr = &be32; out = c(ptr); if (out != 0x4030201) { LAssert(0); return false; } return true; } }; diff --git a/include/lgi/common/CurrentTime.h b/include/lgi/common/CurrentTime.h --- a/include/lgi/common/CurrentTime.h +++ b/include/lgi/common/CurrentTime.h @@ -1,10 +1,10 @@ #pragma once /// Gets the current clock in milli-seconds. (1,000th of a second) /// \ingroup Time LgiFunc uint64_t LCurrentTime(); /// Get the current clock in micro-seconds (1,000,000th of a second) -LgiFunc uint64_t LgiMicroTime(); +LgiFunc uint64_t LMicroTime(); diff --git a/include/lgi/common/Thread.h b/include/lgi/common/Thread.h --- a/include/lgi/common/Thread.h +++ b/include/lgi/common/Thread.h @@ -1,164 +1,164 @@ #ifndef __GTHREAD_H #define __GTHREAD_H ////////////////////////////////////////////////////////////////////////// // Thread types are defined in LMutex.h #include "lgi/common/Mutex.h" class LgiClass LThread { public: enum ThreadState { THREAD_INIT = 1, THREAD_RUNNING = 2, THREAD_ASLEEP = 3, THREAD_EXITED = 4, THREAD_ERROR = 5 }; enum ThreadPriority { ThreadPriorityIdle, ThreadPriorityNormal, ThreadPriorityHigh, ThreadPriorityRealtime, }; protected: ThreadState State; int ReturnValue; OsThread hThread; LString Name; ThreadPriority Priority; OsThreadId ThreadId; #if defined WIN32 friend uint WINAPI ThreadEntryPoint(void *i); void Create(class LThread *Thread, OsThread &hThread, OsThreadId &ThreadId); #elif defined POSIX friend void *ThreadEntryPoint(void *i); #else friend int32 ThreadEntryPoint(void *i); #endif protected: /// Auto deletes the thread after ::Main has finished. bool DeleteOnExit; /// Aka from LView::AddDispatch(). int ViewHandle; public: static const OsThread InvalidHandle; static const OsThreadId InvalidId; LThread( /// Name for the thread. const char *Name, /// [Optional] Handle from LView::AddDispatch() /// This enables the OnComplete event to be called /// from the GUI thread after the thread exits. int viewHandle = -1 ); virtual ~LThread(); // Properties OsThread Handle() { return hThread; } const char *GetName() { return Name; } OsThreadId GetId() { return ThreadId; } ThreadState GetState() { return State; } // Volatile at best... only use for 'debug' bool GetDeleteOnExit() { return DeleteOnExit; } virtual int ExitCode(); virtual bool IsExited(); // Methods virtual void Run(); virtual void Terminate(); virtual void WaitForExit(int WarnAfterMs = 2000); // Override to do something - virtual int Main(); + virtual int Main() = 0; // Events virtual void OnBeforeMain() {} virtual void OnAfterMain() {} /// This event runs after the thread has finished but /// is called from the main GUI thread. It requires a valid /// viewHandle to be passed to the constructor. Which can /// be aquired by calling LView::AddDispatch(). virtual void OnComplete() {} }; //////////////////////////////////////////////////////////////////////////////////////// class LThreadOwner; class LThreadTarget; class LThreadWorker; /// A generic threaded job parent class. class LgiClass LThreadJob { friend class LThreadWorker; LThreadTarget *Owner; public: LThreadJob(LThreadTarget *o) { Owner = o; } virtual ~LThreadJob() {} virtual void Do() {} }; /// The thread target is a virtual API to receive work units executed /// in the worker thread. class LgiClass LThreadTarget : public LMutex { friend class LThreadWorker; protected: /// The thread doing the work. LThreadWorker *Worker; public: LThreadTarget(); virtual ~LThreadTarget() {} virtual void SetWorker(LThreadWorker *w); virtual void Detach(); /// This function gets called when the job is finished virtual void OnDone(LAutoPtr j) {} }; #undef AddJob /// This parent class does the actual work of processing jobs. class LgiClass LThreadWorker : public LThread, public LMutex { LArray Owners; LArray Jobs; bool Loop; public: LThreadWorker(LThreadTarget *First, const char *ThreadName); virtual ~LThreadWorker(); void Stop(); void Attach(LThreadTarget *o); void Detach(LThreadTarget *o); virtual void AddJob(LThreadJob *j); virtual void DoJob(LThreadJob *j); int Main(); }; class LgiClass LThreadOwner : public LThreadTarget { public: virtual ~LThreadOwner(); }; #endif diff --git a/src/common/Lgi/LgiCommon.cpp b/src/common/Lgi/LgiCommon.cpp --- a/src/common/Lgi/LgiCommon.cpp +++ b/src/common/Lgi/LgiCommon.cpp @@ -1,2841 +1,2841 @@ // // Cross platform LGI functions // #if LGI_COCOA #import #endif #define _WIN32_WINNT 0x501 #include #include #include #include #ifdef WINDOWS #include #include "lgi/common/RegKey.h" #include #include #else #include #define _getcwd getcwd #endif #include "lgi/common/Lgi.h" #include "lgi/common/Capabilities.h" #if defined(LINUX) && !defined(LGI_SDL) #include "LgiWinManGlue.h" #elif defined(WINDOWS) #include "lgi/common/RegKey.h" #endif #if defined POSIX #include #include #include #include #include "lgi/common/SubProcess.h" #endif #ifdef HAIKU #include #include #else #include "SymLookup.h" #endif #include "lgi/common/Library.h" #include "lgi/common/Net.h" #if defined(__GTK_H__) namespace Gtk { #include "LgiWidget.h" } #endif ////////////////////////////////////////////////////////////////////////// // Misc stuff #if LGI_COCOA || defined(__GTK_H__) LString LgiArgsAppPath; #endif #if defined MAC #import #if defined LGI_CARBON bool _get_path_FSRef(FSRef &fs, LStringPipe &a) { HFSUniStr255 Name; ZeroObj(Name); FSRef Parent; FSCatalogInfo Cat; ZeroObj(Cat); OSErr e = FSGetCatalogInfo(&fs, kFSCatInfoVolume|kFSCatInfoNodeID, &Cat, &Name, NULL, &Parent); if (!e) { if (_get_path_FSRef(Parent, a)) { LAutoString u((char*)LNewConvertCp("utf-8", Name.unicode, "utf-16", Name.length * sizeof(Name.unicode[0]) )); // printf("CatInfo = '%s' %x %x\n", u.Get(), Cat.nodeID, Cat.volume); if (u && Cat.nodeID > 2) { a.Print("%s%s", DIR_STR, u.Get()); } } return true; } return false; } LAutoString FSRefPath(FSRef &fs) { LStringPipe a; if (_get_path_FSRef(fs, a)) { return LAutoString(a.NewStr()); } return LAutoString(); } #endif #endif bool LPostEvent(OsView Wnd, int Event, LMessage::Param a, LMessage::Param b) { #if LGI_SDL SDL_Event e; e.type = SDL_USEREVENT; e.user.code = Event; e.user.data1 = Wnd; e.user.data2 = a || b ? new LMessage::EventParams(a, b) : NULL; /* printf("LPostEvent Wnd=%p, Event=%i, a/b: %i/%i\n", Wnd, Event, (int)a, (int)b); */ return SDL_PushEvent(&e) == 0; #elif WINNATIVE return PostMessage(Wnd, Event, a, b) != 0; #elif defined(__GTK_H__) LAssert(Wnd); LViewI *View = (LViewI*) g_object_get_data(GtkCast(Wnd, g_object, GObject), "LViewI"); if (View) { LMessage m(0); m.Set(Event, a, b); return m.Send(View); } else printf("%s:%i - Error: LPostEvent can't cast OsView to LViewI\n", _FL); #elif defined(MAC) && !LGI_COCOA #if 0 int64 Now = LCurrentTime(); static int64 Last = 0; static int Count = 0; Count++; if (Now > Last + 1000) { printf("Sent %i events in the last %ims\n", Count, (int)(Now-Last)); Last = Now; Count = 0; } #endif EventRef Ev; OSStatus e = CreateEvent(NULL, kEventClassUser, kEventUser, 0, // EventTime kEventAttributeNone, &Ev); if (e) { printf("%s:%i - CreateEvent failed with %i\n", _FL, (int)e); } else { EventTargetRef t = GetControlEventTarget(Wnd); e = SetEventParameter(Ev, kEventParamLgiEvent, typeUInt32, sizeof(Event), &Event); if (e) printf("%s:%i - error %i\n", _FL, (int)e); e = SetEventParameter(Ev, kEventParamLgiA, typeUInt32, sizeof(a), &a); if (e) printf("%s:%i - error %i\n", _FL, (int)e); e = SetEventParameter(Ev, kEventParamLgiB, typeUInt32, sizeof(b), &b); if (e) printf("%s:%i - error %i\n", _FL, (int)e); bool Status = false; EventQueueRef q = GetMainEventQueue(); e = SetEventParameter(Ev, kEventParamPostTarget, typeEventTargetRef, sizeof(t), &t); if (e) printf("%s:%i - error %i\n", _FL, (int)e); e = PostEventToQueue(q, Ev, kEventPriorityStandard); if (e) printf("%s:%i - error %i\n", _FL, (int)e); else Status = true; // printf("PostEventToQueue %i,%i,%i -> %p\n", Event, a, b, q); ReleaseEvent(Ev); return Status; } #else LAssert(!"Not impl."); #endif return false; } void LExitApp() { exit(0); } ////////////////////////////////////////////////////////////////////////// #ifdef WIN32 bool RegisterActiveXControl(char *Dll) { LLibrary Lib(Dll); if (Lib.IsLoaded()) { #ifdef _MSC_VER typedef HRESULT (STDAPICALLTYPE *p_DllRegisterServer)(void); p_DllRegisterServer DllRegisterServer = (p_DllRegisterServer)Lib.GetAddress("DllRegisterServer"); if (DllRegisterServer) { return DllRegisterServer() == S_OK; } #else LAssert(!"Not impl."); #endif } return false; } #endif ////////////////////////////////////////////////////////////////////////// #ifdef WINDOWS #include #pragma comment(lib, "netapi32.lib") #endif /// \brief Returns the operating system that Lgi is running on. /// \sa Returns one of the defines starting with LGI_OS_UNKNOWN in LgiDefs.h int LGetOs ( /// Returns the version of the OS or NULL if you don't care LArray *Ver ) { #if defined(WIN32) || defined(WIN64) static int Os = LGI_OS_UNKNOWN; static int Version = 0, Revision = 0; if (Os == LGI_OS_UNKNOWN) { #if defined(WIN64) BOOL IsWow64 = TRUE; #elif defined(WIN32) BOOL IsWow64 = FALSE; IsWow64Process(GetCurrentProcess(), &IsWow64); #endif SERVER_INFO_101 *v = NULL; auto r = NetServerGetInfo(NULL, 101, (LPBYTE*)&v); if (r == NERR_Success) { Version = v->sv101_version_major; Revision = v->sv101_version_minor; Os = (v->sv101_version_major >= 6) ? #ifdef WIN32 (IsWow64 ? LGI_OS_WIN64 : LGI_OS_WIN32) #else LGI_OS_WIN64 #endif : LGI_OS_WIN9X; NetApiBufferFree(v); } else LAssert(0); } if (Ver) { Ver->Add(Version); Ver->Add(Revision); } return Os; #elif defined LINUX if (Ver) { utsname Buf; if (!uname(&Buf)) { auto t = LString(Buf.release).SplitDelimit("."); for (int i=0; iAdd(atoi(t[i])); } } } return LGI_OS_LINUX; #elif defined MAC #if !defined(__GTK_H__) if (Ver) { NSOperatingSystemVersion v = [[NSProcessInfo processInfo] operatingSystemVersion]; Ver->Add((int)v.majorVersion); Ver->Add((int)v.minorVersion); Ver->Add((int)v.patchVersion); } #endif return LGI_OS_MAC_OS_X; #elif defined HAIKU return LGI_OS_HAIKU; #else #error "Impl Me" return LGI_OS_UNKNOWN; #endif } const char *LGetOsName() { const char *Str[LGI_OS_MAX] = { "Unknown", "Win9x", "Win32", "Win64", "Haiku", "Linux", "MacOSX", }; auto Os = LGetOs(); if (Os > 0 && Os < CountOf(Str)) return Str[Os]; LAssert(!"Invalid OS index."); return "error"; } #ifdef WIN32 #define RecursiveFileSearch_Wildcard "*.*" #else // unix'ish OS #define RecursiveFileSearch_Wildcard "*" #endif bool LRecursiveFileSearch(const char *Root, LArray *Ext, LArray *Files, uint64 *Size, uint64 *Count, std::function Callback, LCancel *Cancel) { // validate args if (!Root) return false; // get directory enumerator LDirectory Dir; bool Status = true; // enumerate the directory contents for (auto Found = Dir.First(Root); Found && (!Cancel || !Cancel->IsCancelled()); Found = Dir.Next()) { char Name[300]; if (!Dir.Path(Name, sizeof(Name))) continue; if (Callback && !Callback(Name, &Dir)) continue; if (Dir.IsDir()) { // dir LRecursiveFileSearch( Name, Ext, Files, Size, Count, Callback, Cancel); } else { // process file bool Match = true; // if no Ext's then default to match if (Ext) { for (int i=0; iLength(); i++) { const char *e = (*Ext)[i]; char *RawFile = strrchr(Name, DIR_CHAR); if (RawFile && (Match = MatchStr(e, RawFile+1))) { break; } } } if (Match) { // file matched... process: if (Files) Files->Add(NewStr(Name)); if (Size) *Size += Dir.GetSize(); if (Count) (*Count)++; Status = true; } } } return Status; } #define LGI_TRACE_TO_FILE // #include #ifndef WIN32 #define _vsnprintf vsnprintf #endif static LStreamI *_LgiTraceStream = NULL; void LgiTraceSetStream(LStreamI *stream) { _LgiTraceStream = stream; } bool LgiTraceGetFilePath(char *LogPath, int BufLen) { if (!LogPath) return false; auto Exe = LGetExeFile(); if (Exe) { #ifdef MAC char *Dir = strrchr(Exe, DIR_CHAR); if (Dir) { char Part[256]; strcpy_s(Part, sizeof(Part), Dir+1); LMakePath(LogPath, BufLen, "~/Library/Logs", Dir+1); strcat_s(LogPath, BufLen, ".txt"); } else #endif { char *Dot = strrchr(Exe, '.'); if (Dot && !strchr(Dot, DIR_CHAR)) sprintf_s(LogPath, BufLen, "%.*s.txt", (int)(Dot - Exe.Get()), Exe.Get()); else sprintf_s(LogPath, BufLen, "%s.txt", Exe.Get()); } LFile f; if (f.Open(LogPath, O_WRITE)) { f.Close(); } else { char Leaf[64]; char *Dir = strrchr(LogPath, DIR_CHAR); if (Dir) { strcpy_s(Leaf, sizeof(Leaf), Dir + 1); LGetSystemPath(LSP_APP_ROOT, LogPath, BufLen); if (!LDirExists(LogPath)) FileDev->CreateFolder(LogPath); LMakePath(LogPath, BufLen, LogPath, Leaf); } else goto OnError; } #if 0 LFile tmp; if (tmp.Open("c:\\temp\\log.txt", O_WRITE)) { tmp.SetSize(0); tmp.Write(LogPath, strlen(LogPath)); } #endif } else { // Well what to do now? I give up OnError: strcpy_s(LogPath, BufLen, "trace.txt"); return false; } return true; } void LgiTrace(const char *Msg, ...) { #if defined _INC_MALLOC && WINNATIVE if (_heapchk() != _HEAPOK) return; #endif if (!Msg) return; #ifdef WIN32 static LMutex Sem("LgiTrace"); Sem.Lock(_FL, true); #endif char Buffer[2049] = ""; #ifdef LGI_TRACE_TO_FILE static LFile f; static char LogPath[MAX_PATH_LEN] = ""; if (!_LgiTraceStream && LogPath[0] == 0) LgiTraceGetFilePath(LogPath, sizeof(LogPath)); #endif va_list Arg; va_start(Arg, Msg); #ifdef LGI_TRACE_TO_FILE int Ch = #endif vsnprintf(Buffer, sizeof(Buffer)-1, Msg, Arg); va_end(Arg); #ifdef LGI_TRACE_TO_FILE LStreamI *Output = NULL; if (_LgiTraceStream) Output = _LgiTraceStream; else { if (!f.IsOpen() && f.Open(LogPath, O_WRITE)) f.Seek(0, SEEK_END); Output = &f; } if (Output && Ch > 0) { Output->ChangeThread(); Output->Write(Buffer, Ch); } if (!_LgiTraceStream) { #ifdef WINDOWS // Windows can take AGES to close a file when there is anti-virus on, like 100ms. // We can't afford to wait here so just keep the file open but flush the // buffers if we can. FlushFileBuffers(f.Handle()); #else f.Close(); #endif } #endif #if defined WIN32 OutputDebugStringA(Buffer); Sem.Unlock(); #else printf("%s", Buffer); #endif } #ifndef LGI_STATIC #define STACK_SIZE 12 void LStackTrace(const char *Msg, ...) { #ifndef HAIKU LSymLookup::Addr Stack[STACK_SIZE]; ZeroObj(Stack); LSymLookup *Lu = LAppInst ? LAppInst->GetSymLookup() : NULL; if (!Lu) { printf("%s:%i - Failed to get sym lookup object.\n", _FL); return; } int Frames = Lu ? Lu->BackTrace(0, 0, Stack, STACK_SIZE) : 0; if (Msg) { #ifdef LGI_TRACE_TO_FILE static LFile f; static char LogPath[MAX_PATH_LEN] = ""; if (!_LgiTraceStream) { if (LogPath[0] == 0) LgiTraceGetFilePath(LogPath, sizeof(LogPath)); f.Open(LogPath, O_WRITE); } #endif va_list Arg; va_start(Arg, Msg); char Buffer[2049] = ""; int Len = vsnprintf(Buffer, sizeof(Buffer)-1, Msg, Arg); va_end(Arg); Lu->Lookup(Buffer+Len, sizeof(Buffer)-Len-1, Stack, Frames); #ifdef LGI_TRACE_TO_FILE if (f.IsOpen()) { f.Seek(0, SEEK_END); f.Write(Buffer, (int)strlen(Buffer)); f.Close(); } #endif #if defined WIN32 OutputDebugStringA(Buffer); #else printf("Trace: %s", Buffer); #endif } #endif } #endif bool LTrimDir(char *Path) { if (!Path) return false; char *p = strrchr(Path, DIR_CHAR); if (!p) return false; if (p[1] == 0) // Trailing DIR_CHAR doesn't count... do it again. { *p = 0; p = strrchr(Path, DIR_CHAR); if (!p) return false; } *p = 0; return true; } LString LMakeRelativePath(const char *Base, const char *Path) { LStringPipe Status; if (Base && Path) { #ifdef WIN32 bool SameNs = strnicmp(Base, Path, 3) == 0; #else bool SameNs = true; #endif if (SameNs) { auto b = LString(Base + 1).SplitDelimit(":\\/"); auto p = LString(Path + 1).SplitDelimit(":\\/"); int Same = 0; while (Same < b.Length() && Same < p.Length() && stricmp(b[Same], p[Same]) == 0) { Same++; } for (int i = Same; i= b.Length()) { Status.Print(".%s", DIR_STR); } for (int n = Same; n Same) { Status.Push(DIR_STR); } Status.Push(p[n]); } } } return Status.NewLStr(); } bool LIsRelativePath(const char *Path) { if (!Path) return false; if (*Path == '.') return true; #ifdef WIN32 if (IsAlpha(Path[0]) && Path[1] == ':') // Drive letter path return false; #endif if (*Path == DIR_CHAR) return false; if (strstr(Path, "://")) // Protocol def return false; return true; // Correct or not??? } bool LMakePath(char *Str, int StrSize, const char *Path, const char *File) { if (!Str || StrSize <= 0 || !Path || !File) { printf("%s:%i - Invalid LMakePath(%p,%i,%s,%s) param\n", _FL, Str, StrSize, Path, File); return false; } if (StrSize <= 4) { printf("%s:%i - LgiMakeFile buf size=%i?\n", _FL, StrSize); } if (Str && Path && File) { char Dir[] = { '/', '\\', 0 }; if (Path[0] == '~') { auto Parts = LString(Path).SplitDelimit(Dir, 2); char *s = Str, *e = Str + StrSize; for (auto p: Parts) { if (p.Equals("~")) { LGetSystemPath(LSP_HOME, s, e - s); s += strlen(s); } else s += sprintf_s(s, e - s, "%s%s", DIR_STR, p.Get()); } } else if (Str != Path) { strcpy_s(Str, StrSize, Path); } #define EndStr() Str[strlen(Str)-1] #define EndDir() if (!strchr(Dir, EndStr())) strcat(Str, DIR_STR); size_t Len = strlen(Str); char *End = Str + (Len ? Len - 1 : 0); if (strchr(Dir, *End) && End > Str) { *End = 0; } auto T = LString(File).SplitDelimit(Dir); for (int i=0; i= StrSize - 1) return false; Str[Len++] = DIR_CHAR; Str[Len] = 0; } size_t SegLen = strlen(T[i]); if (Len + SegLen + 1 > StrSize) { return false; } strcpy_s(Str + Len, StrSize - Len, T[i]); } } } return true; } bool LgiGetTempPath(char *Dst, int DstSize) { return LGetSystemPath(LSP_TEMP, Dst, DstSize); } bool LGetSystemPath(LSystemPath Which, char *Dst, ssize_t DstSize) { if (!Dst || DstSize <= 0) return false; LFile::Path p; LString s = p.GetSystem(Which, 0); if (!s) return false; strcpy_s(Dst, DstSize, s); return true; } LString LGetSystemPath(LSystemPath Which, int WordSize) { LFile::Path p; return p.GetSystem(Which, WordSize); } LFile::Path::State LFile::Path::Exists() { if (Length() == 0) return TypeNone; #ifdef WINDOWS struct _stat64 s; int r = _stat64(GetFull(), &s); if (r) return TypeNone; if (s.st_mode & _S_IFDIR) return TypeFolder; if (s.st_mode & _S_IFREG) return TypeFile; #else struct stat s; int r = stat(GetFull(), &s); if (r) return TypeNone; if (S_ISDIR(s.st_mode)) return TypeFolder; if (S_ISREG(s.st_mode)) return TypeFile; #endif return TypeNone; } LString LFile::Path::PrintAll() { LStringPipe p; #define _(name) \ p.Print(#name ": '%s'\n", GetSystem(name).Get()); _(LSP_OS) _(LSP_OS_LIB) _(LSP_TEMP) _(LSP_COMMON_APP_DATA) _(LSP_USER_APP_DATA) _(LSP_LOCAL_APP_DATA) _(LSP_DESKTOP) _(LSP_HOME) _(LSP_USER_APPS) _(LSP_EXE) _(LSP_TRASH) _(LSP_APP_INSTALL) _(LSP_APP_ROOT) _(LSP_USER_DOCUMENTS) _(LSP_USER_MUSIC) _(LSP_USER_VIDEO) _(LSP_USER_DOWNLOADS) _(LSP_USER_LINKS) _(LSP_USER_PICTURES) #undef _ #if LGI_COCOA int Domains[] = {NSUserDomainMask, NSSystemDomainMask}; const char *DomainName[] = {"User", "System"}; for (int i=0; i 0) \ { \ LString s = [paths objectAtIndex:0]; \ p.Print("%s." #name ": '%s'\n", DomainName[i], s.Get()); \ } \ else p.Print("%s." #name ": null\n", DomainName[i]); \ } \ } _(NSApplicationDirectory) _(NSDemoApplicationDirectory) _(NSDeveloperApplicationDirectory) _(NSAdminApplicationDirectory) _(NSLibraryDirectory) _(NSDeveloperDirectory) _(NSUserDirectory) _(NSDocumentationDirectory) _(NSDocumentDirectory) _(NSCoreServiceDirectory) _(NSAutosavedInformationDirectory) _(NSDesktopDirectory) _(NSCachesDirectory) _(NSApplicationSupportDirectory) _(NSDownloadsDirectory) _(NSInputMethodsDirectory) _(NSMoviesDirectory) _(NSMusicDirectory) _(NSPicturesDirectory) _(NSPrinterDescriptionDirectory) _(NSSharedPublicDirectory) _(NSPreferencePanesDirectory) _(NSApplicationScriptsDirectory) _(NSItemReplacementDirectory) _(NSAllApplicationsDirectory) _(NSAllLibrariesDirectory) _(NSTrashDirectory) #undef _ } #endif return p.NewLStr(); } LString LFile::Path::GetSystem(LSystemPath Which, int WordSize) { LString Path; #if defined(WIN32) #ifndef CSIDL_PROFILE #define CSIDL_PROFILE 0x0028 #endif #if !defined(CSIDL_MYDOCUMENTS) #define CSIDL_MYDOCUMENTS 0x0005 #endif #if !defined(CSIDL_MYMUSIC) #define CSIDL_MYMUSIC 0x000d #endif #if !defined(CSIDL_MYVIDEO) #define CSIDL_MYVIDEO 0x000e #endif #if !defined(CSIDL_LOCAL_APPDATA) #define CSIDL_LOCAL_APPDATA 0x001c #endif #if !defined(CSIDL_COMMON_APPDATA) #define CSIDL_COMMON_APPDATA 0x0023 #endif #if !defined(CSIDL_APPDATA) #define CSIDL_APPDATA 0x001a #endif #endif /* #if defined(LINUX) && !defined(LGI_SDL) // Ask our window manager add-on if it knows the path LLibrary *WmLib = LAppInst ? LAppInst->GetWindowManagerLib() : NULL; if (WmLib) { Proc_LgiWmGetPath WmGetPath = (Proc_LgiWmGetPath) WmLib->GetAddress("LgiWmGetPath"); char Buf[MAX_PATH_LEN]; if (WmGetPath && WmGetPath(Which, Buf, sizeof(Buf))) { Path = Buf; return Path; } } #endif */ switch (Which) { default: break; case LSP_USER_DOWNLOADS: { #if defined(__GTK_H__) auto p = Gtk::g_get_user_special_dir(Gtk::G_USER_DIRECTORY_DOWNLOAD); Path = p; #elif defined(WIN32) && defined(_MSC_VER) // OMG!!!! Really? #ifndef REFKNOWNFOLDERID typedef GUID KNOWNFOLDERID; #define REFKNOWNFOLDERID const KNOWNFOLDERID & GUID FOLDERID_Downloads = {0x374DE290,0x123F,0x4565,{0x91,0x64,0x39,0xC4,0x92,0x5E,0x46,0x7B}}; #endif LLibrary Shell("Shell32.dll"); typedef HRESULT (STDAPICALLTYPE *pSHGetKnownFolderPath)(REFKNOWNFOLDERID rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath); pSHGetKnownFolderPath SHGetKnownFolderPath = (pSHGetKnownFolderPath)Shell.GetAddress("SHGetKnownFolderPath"); if (SHGetKnownFolderPath) { PWSTR ptr = NULL; HRESULT r = SHGetKnownFolderPath(FOLDERID_Downloads, 0, NULL, &ptr); if (SUCCEEDED(r)) { LAutoString u8(WideToUtf8(ptr)); if (u8) Path = u8; CoTaskMemFree(ptr); } } if (!Path.Get()) { LRegKey k(false, "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"); char *p = k.GetStr("{374DE290-123F-4565-9164-39C4925E467B}"); if (LDirExists(p)) Path = p; } if (!Path.Get()) { LString MyDoc = WinGetSpecialFolderPath(CSIDL_MYDOCUMENTS); if (MyDoc) { char Buf[MAX_PATH_LEN]; LMakePath(Buf, sizeof(Buf), MyDoc, "Downloads"); if (LDirExists(Buf)) Path = Buf; } } if (!Path.Get()) { LString Profile = WinGetSpecialFolderPath(CSIDL_PROFILE); if (Profile) { char Buf[MAX_PATH_LEN]; LMakePath(Buf, sizeof(Buf), Profile, "Downloads"); if (LDirExists(Buf)) Path = Buf; } } #elif LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDownloadsDirectory, NSUserDomainMask, YES); if (paths) { if ([paths count]) Path = [paths objectAtIndex:0]; } #elif defined(HAIKU) #else LAssert(!"Not implemented"); #endif break; } case LSP_USER_LINKS: { LString Home = LGetSystemPath(LSP_HOME); #if defined(WIN32) char p[MAX_PATH_LEN]; if (LMakePath(p, sizeof(p), Home, "Links")) Path = p; #elif defined(LINUX) char p[MAX_PATH_LEN]; if (LMakePath(p, sizeof(p), Home, ".config/gtk-3.0")) Path = p; #endif break; } case LSP_USER_PICTURES: { #if defined(__GTK_H__) auto p = Gtk::g_get_user_special_dir(Gtk::G_USER_DIRECTORY_DOCUMENTS); Path = p; #elif defined(WIN32) Path = WinGetSpecialFolderPath(CSIDL_MYPICTURES); if (Path) return Path; #elif defined LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSPicturesDirectory, NSUserDomainMask, YES); if (paths) { if ([paths count]) Path = [paths objectAtIndex:0]; } #endif // Default to ~/Pictures char hm[MAX_PATH_LEN]; LString Home = LGetSystemPath(LSP_HOME); if (LMakePath(hm, sizeof(hm), Home, "Pictures")) Path = hm; break; } case LSP_USER_DOCUMENTS: { char path[MAX_PATH_LEN]; #if defined(__GTK_H__) auto p = Gtk::g_get_user_special_dir(Gtk::G_USER_DIRECTORY_DOCUMENTS); if (p) Path = p; #elif defined(WIN32) && defined(_MSC_VER) Path = WinGetSpecialFolderPath(CSIDL_MYDOCUMENTS); #elif defined LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); if (paths) { if ([paths count]) Path = [paths objectAtIndex:0]; } #elif defined(HAIKU) if( find_directory ( B_SYSTEM_DOCUMENTATION_DIRECTORY, dev_for_path("/boot"), true, path, sizeof(path) ) == B_OK) Path = path; #endif if (!Path) { // Default to ~/Documents if (LMakePath(path, sizeof(path), LGetSystemPath(LSP_HOME), "Documents")) Path = path; } break; } case LSP_USER_MUSIC: { char path[MAX_PATH_LEN]; #if defined WIN32 Path = WinGetSpecialFolderPath(CSIDL_MYMUSIC); #elif defined(__GTK_H__) auto p = Gtk::g_get_user_special_dir(Gtk::G_USER_DIRECTORY_MUSIC); if (p) Path = p; #elif defined LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kUserDomain, kMusicDocumentsFolderType, kDontCreateFolder, &Ref); if (e) printf("%s:%i - FSFindFolder failed e=%i\n", _FL, e); else { LAutoString a = FSRefPath(Ref); if (a) Path = a.Get(); } #elif LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSMusicDirectory, NSUserDomainMask, YES); if (paths) { if ([paths count]) Path = [paths objectAtIndex:0]; } #elif defined(HAIKU) if( find_directory ( B_USER_SOUNDS_DIRECTORY, dev_for_path("/boot"), true, path, sizeof(path) ) == B_OK) Path = path; #endif if (!Path) { // Default to ~/Music if (LMakePath(path, sizeof(path), LGetSystemPath(LSP_HOME), "Music")) Path = path; } break; } case LSP_USER_VIDEO: { char path[MAX_PATH_LEN]; #if defined WIN32 Path = WinGetSpecialFolderPath(CSIDL_MYVIDEO); #elif defined(__GTK_H__) auto p = Gtk::g_get_user_special_dir(Gtk::G_USER_DIRECTORY_VIDEOS); if (p) Path = p; #elif defined LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kUserDomain, kMovieDocumentsFolderType, kDontCreateFolder, &Ref); if (e) printf("%s:%i - FSFindFolder failed e=%i\n", _FL, e); else { LAutoString a = FSRefPath(Ref); if (a) Path = a.Get(); } #elif LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSMoviesDirectory, NSUserDomainMask, YES); if (paths) { if ([paths count]) Path = [paths objectAtIndex:0]; } #endif if (!Path) { // Default to ~/Video if (LMakePath(path, sizeof(path), LGetSystemPath(LSP_HOME), "Video")) Path = path; } break; } case LSP_USER_APPS: { #if defined WIN32 int Id = #ifdef WIN64 CSIDL_PROGRAM_FILES #else CSIDL_PROGRAM_FILESX86 #endif ; if (WordSize == 32) Id = CSIDL_PROGRAM_FILESX86; else if (WordSize == 64) Id = CSIDL_PROGRAM_FILES; Path = WinGetSpecialFolderPath(Id); #elif defined(HAIKU) char path[MAX_PATH_LEN] = ""; if (find_directory(B_USER_APPS_DIRECTORY, dev_for_path("/boot"), true, path, sizeof(path)) == B_OK) Path = path; #elif LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSApplicationDirectory, NSSystemDomainMask, YES); if (paths) { if ([paths count]) Path = [paths objectAtIndex:0]; } #elif defined MAC Path = "/Applications"; #elif defined LINUX Path = "/usr/bin"; #else LAssert(!"Impl me."); #endif break; } case LSP_APP_INSTALL: { Path = LGetSystemPath(LSP_EXE); if (Path) { size_t Last = Path.RFind(DIR_STR); if (Last > 0) { LString s = Path(Last, -1); if ( stristr(s, #ifdef _DEBUG "Debug" #else "Release" #endif ) ) Path = Path(0, Last); } } break; } case LSP_APP_ROOT: { #ifndef LGI_STATIC const char *Name = NULL; // Try and get the configured app name: if (LAppInst) Name = LAppInst->LBase::Name(); if (!Name) { // Use the exe name? LString Exe = LGetExeFile(); char *l = LGetLeaf(Exe); if (l) { #ifdef WIN32 char *d = strrchr(l, '.'); *d = NULL; #endif Name = l; // printf("%s:%i - name '%s'\n", _FL, Name); } } if (!Name) { LAssert(0); break; } #if defined MAC #if LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); if (paths) Path = [[paths objectAtIndex:0] UTF8String]; #elif LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kUserDomain, kDomainLibraryFolderType, kDontCreateFolder, &Ref); if (e) { printf("%s:%i - FSFindFolder failed e=%i\n", _FL, e); LAssert(0); } else { LAutoString Base = FSRefPath(Ref); Path = Base.Get(); } #else struct passwd *pw = getpwuid(getuid()); if (!pw) return false; Path.Printf("%s/Library", pw->pw_dir); #endif #elif defined WIN32 Path = WinGetSpecialFolderPath(CSIDL_APPDATA); #elif defined LINUX char Dot[128]; snprintf(Dot, sizeof(Dot), ".%s", Name); Name = Dot; struct passwd *pw = getpwuid(getuid()); if (pw) Path = pw->pw_dir; else LAssert(0); #elif defined(HAIKU) dev_t volume = dev_for_path("/boot"); char path[MAX_PATH_LEN] = ""; if (find_directory(B_USER_DIRECTORY , volume, true, path, sizeof(path)) == B_OK) Path = path; #else LAssert(0); #endif if (Path) { Path += DIR_STR; Path += Name; } #endif break; } case LSP_OS: { #if defined WIN32 char16 p[MAX_PATH_LEN]; if (GetWindowsDirectoryW(p, CountOf(p)) > 0) Path = p; #elif defined(HAIKU) dev_t volume = dev_for_path("/boot"); char path[MAX_PATH_LEN] = ""; if (find_directory(B_SYSTEM_DIRECTORY, volume, true, path, sizeof(path)) == B_OK) Path = path; #elif defined LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kOnAppropriateDisk, kSystemFolderType, kDontCreateFolder, &Ref); if (e) printf("%s:%i - FSFindFolder failed e=%i\n", __FILE__, __LINE__, e); else { LAutoString u = FSRefPath(Ref); if (u) Path = u.Get(); } #elif defined LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSSystemDomainMask, YES); if (paths) { Path = [paths objectAtIndex:0]; LTrimDir(Path); } #elif defined LINUX Path = "/boot"; // it'll do for now... #endif break; } case LSP_OS_LIB: { #if defined WIN32 char16 p[MAX_PATH_LEN]; if (GetSystemDirectoryW(p, CountOf(p)) > 0) Path = p; #elif defined(HAIKU) dev_t volume = dev_for_path("/boot"); char path[MAX_PATH_LEN] = ""; if (find_directory(B_SYSTEM_LIB_DIRECTORY, volume, true, path, sizeof(path)) == B_OK) Path = path; #elif defined MAC Path = "/Library"; #elif defined LINUX Path = "/lib"; // it'll do for now... #endif break; } case LSP_TEMP: { #if defined WIN32 char16 t[MAX_PATH_LEN]; if (GetTempPathW(CountOf(t), t) > 0) { LAutoString utf(WideToUtf8(t)); if (utf) Path = utf; } #elif defined LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kUserDomain, kTemporaryFolderType, kCreateFolder, &Ref); if (e) LgiTrace("%s:%i - FSFindFolder failed e=%i\n", _FL, e); else { LAutoString u = FSRefPath(Ref); if (u) { Path = u.Get(); } else LgiTrace("%s:%i - FSRefPath failed.\n", _FL); } #elif defined LGI_COCOA NSString *tempDir = NSTemporaryDirectory(); if (tempDir) Path = tempDir; else LAssert(!"No tmp folder?"); #elif defined LINUX Path = "/tmp"; // it'll do for now... #else LAssert(!"Impl me."); #endif break; } case LSP_COMMON_APP_DATA: { #if defined WIN32 Path = WinGetSpecialFolderPath(CSIDL_COMMON_APPDATA); #elif defined LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kOnSystemDisk, kDomainLibraryFolderType, kDontCreateFolder, &Ref); if (e) printf("%s:%i - FSFindFolder failed e=%i\n", _FL, e); else { auto u = FSRefPath(Ref); if (u) Path = u.Get(); } #elif defined LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSSystemDomainMask, YES); if (paths) { Path = [paths objectAtIndex:0]; } #elif defined LINUX Path = "/usr"; #else LAssert(!"Impl me."); #endif break; } case LSP_USER_APP_DATA: { #if defined WIN32 Path = WinGetSpecialFolderPath(CSIDL_APPDATA); #elif defined LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kUserDomain, kDomainLibraryFolderType, kDontCreateFolder, &Ref); if (e) printf("%s:%i - FSFindFolder failed e=%i\n", __FILE__, __LINE__, e); else { auto u = FSRefPath(Ref); if (u) Path = u.Get(); } #elif defined LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES); if (paths) { Path = [paths objectAtIndex:0]; } #elif defined LINUX Path = "/usr"; #elif defined HAIKU dev_t volume = dev_for_path("/boot"); char path[MAX_PATH_LEN] = ""; if (find_directory(B_USER_SETTINGS_DIRECTORY, volume, true, path, sizeof(path)) == B_OK) Path = path; #else LAssert(!"Impl me."); #endif break; } case LSP_LOCAL_APP_DATA: { #if defined WIN32 Path = WinGetSpecialFolderPath(CSIDL_LOCAL_APPDATA); #elif defined LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES); if (paths) { Path = [paths objectAtIndex:0]; } #else LAssert(!"Impl me."); #endif break; } case LSP_DESKTOP: { #if defined(WINDOWS) && defined(_MSC_VER) Path = WinGetSpecialFolderPath(CSIDL_DESKTOPDIRECTORY); #elif defined(HAIKU) dev_t volume = dev_for_path("/boot"); char path[MAX_PATH_LEN] = ""; if (find_directory(B_DESKTOP_DIRECTORY, volume, true, path, sizeof(path)) == B_OK) Path = path; #elif defined(__GTK_H__) auto p = Gtk::g_get_user_special_dir(Gtk::G_USER_DIRECTORY_DESKTOP); Path = p; #elif defined LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDesktopDirectory, NSUserDomainMask, YES); if (paths) { Path = [paths objectAtIndex:0]; } #elif defined LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kOnAppropriateDisk, kDesktopFolderType, kDontCreateFolder, &Ref); if (e) printf("%s:%i - FSFindFolder failed e=%i\n", __FILE__, __LINE__, e); else { LAutoString u = FSRefPath(Ref); if (u) Path = u.Get(); } #elif defined POSIX struct passwd *pw = getpwuid(getuid()); if (pw) { #ifdef LINUX WindowManager wm = LGetWindowManager(); if (wm == WM_Gnome) { Path.Printf("%s/.gnome-desktop", pw->pw_dir); } #endif if (!LDirExists(Path)) { Path.Printf("%s/Desktop", pw->pw_dir); } } #else #error "Impl me." #endif break; } case LSP_HOME: { #if defined WIN32 Path = WinGetSpecialFolderPath(CSIDL_PROFILE); #elif defined(HAIKU) dev_t volume = dev_for_path("/boot"); char path[MAX_PATH_LEN] = ""; if (find_directory(B_USER_DIRECTORY, volume, true, path, sizeof(path)) == B_OK) Path = path; #elif defined LGI_COCOA NSString *home = NSHomeDirectory(); if (home) Path = home; else LAssert(!"No home path?"); #elif defined POSIX struct passwd *pw = getpwuid(getuid()); if (pw) Path = pw->pw_dir; #else #error "Impl me." #endif break; } case LSP_EXE: { Path = LGetExeFile(); if (!Path) break; auto p = Path.RFind(DIR_STR); if (p > 0) Path.Length(p); break; } case LSP_TRASH: { #if defined LINUX switch (LGetWindowManager()) { case WM_Kde: { static char KdeTrash[256] = ""; if (!ValidStr(KdeTrash)) { // Ask KDE where the current trash is... LStringPipe o; LSubProcess p("kde-config", "--userpath trash"); if (p.Start()) { p.Communicate(&o); char *s = o.NewStr(); if (s) { // Store it.. strcpy_s(KdeTrash, sizeof(KdeTrash), s); DeleteArray(s); // Clear out any crap at the end... char *e = KdeTrash + strlen(KdeTrash) - 1; while (e > KdeTrash && strchr(" \r\n\t/", *e)) { *e-- = 0; } } else { printf("%s:%i - No output from 'kde-config'.\n", _FL); } } else { printf("%s:%i - Run 'kde-config' failed.\n", _FL); } } if (ValidStr(KdeTrash)) Path = KdeTrash; break; } default: { LString Home = LGetSystemPath(LSP_HOME); if (!Home) { LgiTrace("%s:%i - Can't get LSP_HOME.\n", _FL); break; } char p[MAX_PATH_LEN]; if (!LMakePath(p, sizeof(p), Home, ".local/share/Trash/files") || !LDirExists(p)) { LgiTrace("%s:%i - '%s' doesn't exist.\n", _FL, p); break; } Path = p; break; } } #elif defined(HAIKU) dev_t volume = dev_for_path("/boot"); char path[MAX_PATH_LEN] = ""; if (find_directory(B_TRASH_DIRECTORY, volume, true, path, sizeof(path)) == B_OK) Path = path; #elif defined LGI_CARBON FSRef Ref; OSErr e = FSFindFolder(kUserDomain, kTrashFolderType, kDontCreateFolder, &Ref); if (e) printf("%s:%i - FSFindFolder failed e=%i\n", _FL, e); else { LAutoString u = FSRefPath(Ref); if (u) Path = u.Get(); } #elif defined LGI_COCOA NSArray *paths = NSSearchPathForDirectoriesInDomains( NSTrashDirectory, NSUserDomainMask, YES); if (paths) { Path = [paths objectAtIndex:0]; } #elif defined(WIN32) LAssert(0); #endif break; } } return Path; } LString LGetExeFile() { #if defined WIN32 char16 Exe[MAX_PATH_LEN]; if (GetModuleFileNameW(NULL, Exe, CountOf(Exe)) > 0) { LString e = Exe; if (e) { return e; } else { LgiMsg(0, "LgiFromNativeCp returned 0, ANSI CodePage=%i (%s)", "LgiGetExeFile Error", MB_OK, GetACP(), LAnsiToLgiCp()); return LString(); } } LString m; m.Printf("GetModuleFileName failed err: %08.8X", GetLastError()); MessageBoxA(0, m, "LgiGetExeFile Error", MB_OK); LExitApp(); #elif defined HAIKU // Copy the string so as to not allow callers to change it return LgiArgsAppPath.Get(); #elif defined LINUX static char ExePathCache[MAX_PATH_LEN] = ""; bool Status = false; // this is _REALLY_ lame way to do it... but hey there aren't any // other better ways to get the full path of the running executable if (!ExePathCache[0]) { // First try the self method int Len = readlink("/proc/self/exe", ExePathCache, sizeof(ExePathCache)); Status = LFileExists(ExePathCache); // printf("readlink=%i Status=%i Exe='%s'\n", Len, Status, ExePathCache); if (!Status) { ExePathCache[0] = 0; // Next try the map file method char ProcFile[256]; sprintf_s(ProcFile, sizeof(ProcFile), "/proc/%i/maps", getpid()); int fd = open(ProcFile, O_RDONLY); if (fd >= 0) { int Len = 16 << 10; // is this enough? // no better way of determining the length of proc info? char *Buf = new char[Len+1]; if (Buf) { int r = read(fd, Buf, Len); Buf[r] = 0; char *s = strchr(Buf, '/'); if (s) { char *e = strchr(s, '\n'); if (e) { *e = 0; strcpy_s(ExePathCache, sizeof(ExePathCache), s); Status = true; } } DeleteArray(Buf); } close(fd); } else { // Non proc system (like cygwin for example) // char Cmd[256]; // sprintf_s(Cmd, sizeof(Cmd), "ps | grep \"%i\"", getpid()); LStringPipe Ps; LSubProcess p("ps"); if (p.Start()) { p.Communicate(&Ps); char *PsOutput = Ps.NewStr(); if (PsOutput) { auto n = LString(PsOutput).SplitDelimit("\r\n"); for (int i=0; !Status && i 7) { int LinePid = 0; for (int i=0; iReset(NewStr(Path)); return; } #ifdef WIN32 if (PathLen < sizeof(Path) - 4) { strcat(Path, ".lnk"); if (LResolveShortcut(Path, Path, sizeof(Path))) { if (GStr) *GStr = Path; else if (AStr) AStr->Reset(NewStr(Path)); return; } } #endif } // General search fall back... LArray Ext; LArray Files; Ext.Add((char*)Name); if (LRecursiveFileSearch(Exe, &Ext, &Files) && Files.Length()) { if (GStr) *GStr = Files[0]; else { AStr->Reset(Files[0]); Files.DeleteAt(0); } } Files.DeleteArrays(); } LString LFindFile(const char *Name) { LString s; _LFindFile(Name, &s, NULL); return s; } #if defined WIN32 static LARGE_INTEGER Freq = {0}; static bool CurTimeInit = false; #endif uint64_t LCurrentTime() { #if defined WIN32 if (!CurTimeInit) { CurTimeInit = true; if (!QueryPerformanceFrequency(&Freq)) Freq.QuadPart = 0; } if (Freq.QuadPart) { // Return performance counter in ms LARGE_INTEGER i; if (QueryPerformanceCounter(&i)) { return i.QuadPart * 1000 / Freq.QuadPart; } // Now what?? Give up and go back to tick count I guess. Freq.QuadPart = 0; } // Fall back for systems without a performance counter. return GetTickCount(); #elif defined LGI_CARBON UnsignedWide t; Microseconds(&t); uint64 i = ((uint64)t.hi << 32) | t.lo; return i / 1000; #else timeval tv; gettimeofday(&tv, 0); return (tv.tv_sec * 1000) + (tv.tv_usec / 1000); #endif } -uint64_t LgiMicroTime() +uint64_t LMicroTime() { #if defined WIN32 if (!CurTimeInit) { CurTimeInit = true; if (!QueryPerformanceFrequency(&Freq)) Freq.QuadPart = 0; } if (Freq.QuadPart) { // Return performance counter in ms LARGE_INTEGER i; if (QueryPerformanceCounter(&i)) { return i.QuadPart * 1000000 / Freq.QuadPart; } } return 0; #elif defined LGI_CARBON UnsignedWide t; Microseconds(&t); return ((uint64)t.hi << 32) | t.lo; #else timeval tv; gettimeofday(&tv, 0); return (tv.tv_sec * 1000000) + tv.tv_usec; #endif } int LIsReleaseBuild() { #ifdef _DEBUG return 0; #else return 1; #endif } bool LIsVolumeRoot(const char *Path) { if (!Path) return false; #ifdef WIN32 if ( IsAlpha(Path[0]) && Path[1] == ':' && ( (Path[2] == 0) || (Path[2] == '\\' && Path[3] == 0) ) ) { return true; } #else auto t = LString(Path).SplitDelimit(DIR_STR); if (t.Length() == 0) return true; #ifdef MAC if (!stricmp(t[0], "Volumes") && t.Length() == 2) return true; #else if (!stricmp(t[0], "mnt") && t.Length() == 2) return true; #endif #endif return false; } LString LFormatSize(int64_t Size) { char Buf[64]; LFormatSize(Buf, sizeof(Buf), Size); return Buf; } void LFormatSize(char *Str, int SLen, int64_t Size) { int64_t K = 1024; int64_t M = K * K; int64_t G = K * M; int64_t T = K * G; if (Size == 1) { strcpy_s(Str, SLen, "1 byte"); } else if (Size < K) { sprintf_s(Str, SLen, "%u bytes", (int)Size); } else if (Size < 10 * K) { double d = (double)Size; sprintf_s(Str, SLen, "%.2f KiB", d / K); } else if (Size < M) { sprintf_s(Str, SLen, "%u KiB", (int) (Size / K)); } else if (Size < G) { double d = (double)Size; sprintf_s(Str, SLen, "%.2f MiB", d / M); } else if (Size < T) { double d = (double)Size; sprintf_s(Str, SLen, "%.2f GiB", d / G); } else { double d = (double)Size; sprintf_s(Str, SLen, "%.2f TiB", d / T); } } char *LTokStr(const char *&s) { char *Status = 0; if (s && *s) { // Skip whitespace static char Delim[] = ", \t\r\n"; while (*s && strchr(Delim, *s)) s++; if (*s) { if (strchr("\'\"", *s)) { char Delim = *s++; const char *e = strchr(s, Delim); if (!e) { // error, no end delimiter e = s; while (*e) e++; } Status = NewStr(s, e - s); s = *e ? e + 1 : e; } else { const char *e = s; while (*e && !strchr(Delim, *e)) e++; Status = NewStr(s, e - s); s = e; } } } return Status; } LString LGetEnv(const char *Var) { #ifdef _MSC_VER char *s = NULL; size_t sz; errno_t err = _dupenv_s(&s, &sz, Var); if (err) return LString(); LString ret(s); free(s); return ret; #else return getenv("PATH"); #endif } LString::Array LGetPath() { LString::Array Paths; #ifdef MAC // OMG, WHY?! Seriously? // // The GUI application path is NOT the same as what is configured for the terminal. // At least in 10.12. And I don't know how to make them the same. This works around // that for the time being. #if 1 LFile EctPaths("/etc/paths", O_READ); Paths = EctPaths.Read().Split("\n"); #else LFile::Path Home(LSP_HOME); Home += ".profile"; if (!Home.Exists()) { Home--; Home += ".zprofile"; } auto Profile = LFile(Home, O_READ).Read().Split("\n"); for (auto Ln : Profile) { auto p = Ln.SplitDelimit(" =", 2); if (p.Length() == 3 && p[0].Equals("export") && p[1].Equals("PATH")) { Paths = p[2].Strip("\"").SplitDelimit(LGI_PATH_SEPARATOR); Paths.SetFixedLength(false); for (auto &p : Paths) { if (p.Equals("$PATH")) { auto SysPath = LGetEnv("PATH").SplitDelimit(LGI_PATH_SEPARATOR); for (unsigned i=0; i 0) { Period = p; } } bool DoEvery::DoNow() { int64 Now = LCurrentTime(); if (LastTime + Period < Now) { LastTime = Now; return true; } return false; } ////////////////////////////////////////////////////////////////////// bool LCapabilityClient::NeedsCapability(const char *Name, const char *Param) { for (int i=0; iNeedsCapability(Name, Param); return Targets.Length() > 0; } LCapabilityClient::~LCapabilityClient() { for (int i=0; iClients.Delete(this); } void LCapabilityClient::Register(LCapabilityTarget *t) { if (t && !Targets.HasItem(t)) { Targets.Add(t); t->Clients.Add(this); } } LCapabilityTarget::~LCapabilityTarget() { for (int i=0; iTargets.Delete(this); } ///////////////////////////////////////////////////////////////////// #define BUF_SIZE (4 << 10) #define PROFILE_MICRO 1 LProfile::LProfile(const char *Name, int HideMs) { MinMs = HideMs; Used = 0; Buf = NULL; Add(Name); } LProfile::~LProfile() { Add("End"); uint64 TotalMs = s.Last().Time - s[0].Time; if (MinMs > 0) { if (TotalMs < MinMs #if PROFILE_MICRO * 1000 #endif ) { return; } } uint64 accum = 0; for (int i=0; i BUF_SIZE - 64) { LAssert(0); return; } char *Name = Buf + Used; Used += sprintf_s(Name, BUF_SIZE - Used, "%s:%i", File, Line) + 1; s.Add(Sample( #if PROFILE_MICRO - LgiMicroTime(), + LMicroTime(), #else LCurrentTime(), #endif Name)); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// bool LIsValidEmail(LString Email) { // Local part char buf[321]; char *o = buf; char *e = Email; if (!Email || *e == '.') return false; #define OutputChar() \ if (o - buf >= sizeof(buf) - 1) \ return false; \ *o++ = *e++ // Local part while (*e) { if (strchr("!#$%&\'*+-/=?^_`.{|}~", *e) || IsAlpha((uchar)*e) || IsDigit((uchar)*e)) { OutputChar(); } else if (*e == '\"') { // Quoted string OutputChar(); bool quote = false; while (*e && !quote) { quote = *e == '\"'; OutputChar(); } } else if (*e == '\\') { // Quoted character e++; if (*e < ' ' || *e >= 0x7f) return false; OutputChar(); } else if (*e == '@') { break; } else { // Illegal character return false; } } // Process the '@' if (*e != '@' || o - buf > 64) return false; OutputChar(); // Domain part... if (*e == '[') { // IP addr OutputChar(); // Initial char must by a number if (!IsDigit(*e)) return false; // Check the rest... char *Start = e; while (*e) { if (IsDigit(*e) || *e == '.') { OutputChar(); } else { return false; } } // Not a valid IP if (e - Start > 15) return false; if (*e != ']') return false; OutputChar(); } else { // Hostname, check initial char if (!IsAlpha(*e) && !IsDigit(*e)) return false; // Check the rest. while (*e) { if (IsAlpha(*e) || IsDigit(*e) || strchr(".-", *e)) { OutputChar(); } else { return false; } } } // Remove any trailing dot/dash while (strchr(".-", o[-1])) o--; // Output *o = 0; LAssert(o - buf <= sizeof(buf)); if (strcmp(Email, buf)) Email.Set(buf, o - buf); return true; } ////////////////////////////////////////////////////////////////////////// LString LGetAppForProtocol(const char *Protocol) { LString App; if (!Protocol) return App; #ifdef WINDOWS LRegKey k(false, "HKEY_CLASSES_ROOT\\%s\\shell\\open\\command", Protocol); if (k.IsOk()) { const char *p = k.GetStr(); if (p) { LAutoString a(LTokStr(p)); App = a.Get(); } } #elif defined(LINUX) const char *p = NULL; if (stricmp(Protocol, "mailto")) p = "xdg-email"; else p = "xdg-open"; LString Path = LGetEnv("PATH"); LString::Array a = Path.SplitDelimit(LGI_PATH_SEPARATOR); for (auto i : a) { LFile::Path t(i); t += p; if (t.Exists()) { App = t.GetFull(); break; } } #elif defined(__GTK_H__) LAssert(!"What to do?"); #elif defined(MAC) // Get the handler type LString s; s.Printf("%s://domain/path", Protocol); auto str = s.NsStr(); auto type = [NSURL URLWithString:str]; [str release]; auto handlerUrl = [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL:type]; [type release]; if (handlerUrl) { // Convert to app path s = [handlerUrl absoluteString]; LUri uri(s); if (uri.sProtocol.Equals("file")) App = uri.sPath.RStrip("/"); else LgiTrace("%s:%i - Error: unknown protocol '%s'\n", _FL, uri.sProtocol.Get()); } [handlerUrl release]; #else #warning "Impl me." #endif return App; } diff --git a/src/win/Lgi/Thread.cpp b/src/win/Lgi/Thread.cpp --- a/src/win/Lgi/Thread.cpp +++ b/src/win/Lgi/Thread.cpp @@ -1,250 +1,244 @@ ////////////////////////////////////////////////////////////////////////////// #include #include "lgi/common/Lgi.h" #include "lgi/common/EventTargetThread.h" const DWORD MS_VC_EXCEPTION = 0x406D1388; #pragma pack(push,8) struct THREADNAME_INFO { DWORD dwType; // Must be 0x1000. LPCSTR szName; // Pointer to name (in user addr space). DWORD dwThreadID; // Thread ID (-1=caller thread). DWORD dwFlags; // Reserved for future use, must be zero. THREADNAME_INFO() { dwType = 0x1000; szName = 0; dwThreadID = -1; dwFlags = 0; } }; #pragma pack(pop) uint WINAPI ThreadEntryPoint(void *i) { #if defined(_MT) || defined(__MINGW32__) LThread *Thread = (LThread*) i; if (Thread) { // Wait for it... int Status = 0; auto Start = LCurrentTime(); while (Thread->State == LThread::THREAD_INIT) { LSleep(5); if (LCurrentTime() - Start > 2000) { // If the thread object doesn't set the running state we're // stuffed. So timeout in that case after 2 seconds. The only // reason I can think that this could happen is if the ResumeThread // worked but returned an error. break; } } if (Thread->State == LThread::THREAD_RUNNING) { #ifdef _MSC_VER // Set the name if provided... if (Thread->Name) { THREADNAME_INFO info; info.szName = Thread->Name; info.dwThreadID = Thread->ThreadId; __try { RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info ); } __except(EXCEPTION_CONTINUE_EXECUTION) { } } #endif // Ok now we're ready to go... Thread->OnBeforeMain(); Status = Thread->ReturnValue = Thread->Main(); Thread->OnAfterMain(); } // Shutdown... Thread->State = LThread::THREAD_EXITED; bool DelayDelete = false; if (Thread->ViewHandle >= 0) { // If DeleteOnExit is set AND ViewHandle then the LView::OnEvent handle will // process the delete... don't do it here. DelayDelete = PostThreadEvent(Thread->ViewHandle, M_THREAD_COMPLETED, (LMessage::Param)Thread); // However if PostThreadEvent fails... do honour DeleteOnExit. } if (!DelayDelete && Thread->DeleteOnExit) { DeleteObj(Thread); } _endthreadex(Status); } #endif return 0; } const OsThread LThread::InvalidHandle = 0; const OsThreadId LThread::InvalidId = 0; LThread::LThread(const char *name, int viewHandle) { State = THREAD_INIT; hThread = InvalidHandle; ThreadId = InvalidId; ReturnValue = 0; DeleteOnExit = false; ViewHandle = viewHandle; Priority = ThreadPriorityNormal; Name = name; Create(this, hThread, ThreadId); } LThread::~LThread() { LAssert(State == THREAD_INIT || State == THREAD_EXITED); if (hThread) { CloseHandle(hThread); hThread = InvalidHandle; ThreadId = InvalidId; } } void LThread::Create(LThread *Thread, OsThread &hThread, OsThreadId &ThreadId) { #if defined(_MT) || defined(__MINGW32__) hThread = (HANDLE) _beginthreadex(NULL, 16<<10, (unsigned int (__stdcall *)(void *)) ThreadEntryPoint, (LPVOID) Thread, CREATE_SUSPENDED, (unsigned*) &ThreadId); #elif defined(__CYGWIN__) // Cygwin doesn't support stable threading #ifdef _DEBUG LgiTrace("%s:%i - Cygwin doesn't have stable threading support.\n", __FILE__, __LINE__); #endif #else #error "You are tryed to compile LThread support without the WIN32 multithreaded libs."; #endif } bool LThread::IsExited() { if (State == THREAD_INIT) return true; bool Alive1 = ExitCode() == STILL_ACTIVE; #ifdef _DEBUG DWORD w = ::WaitForSingleObject(hThread, 0); // LgiTrace("%p Wait=%i Alive=%i\n", hThread, w, Alive1); bool Alive2 = WAIT_OBJECT_0 != w; if (!Alive2) { State = THREAD_EXITED; return true; } #endif if (!Alive1) { State = THREAD_EXITED; return true; } return false; } void LThread::Run() { if (State == THREAD_EXITED) { Create(this, hThread, ThreadId); if (hThread != InvalidHandle) { State = THREAD_INIT; } switch (Priority) { case ThreadPriorityIdle: SetThreadPriority(hThread, THREAD_PRIORITY_IDLE); break; case ThreadPriorityHigh: SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST); break; case ThreadPriorityRealtime: SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL); break; } } if (State == THREAD_INIT) { DWORD Status = ResumeThread(hThread); if (Status) { State = THREAD_RUNNING; } else { State = THREAD_ERROR; } } } void LThread::Terminate() { if (hThread != InvalidHandle) { TerminateThread(hThread, 1); auto Start = LCurrentTime(); while (!IsExited()) { auto Now = LCurrentTime(); if (Now - Start > 2000) { LgiTrace("%s:%i - TerminateThread didn't work for '%s'\n", _FL, Name.Get()); LAssert(!"TerminateThread failure?"); break; } LSleep(10); } } } int LThread::ExitCode() { DWORD d; GetExitCodeThread(hThread, &d); ReturnValue = d; return ReturnValue; } -int LThread::Main() -{ - return 0; -} - - diff --git a/src/win/Lgi/View.cpp b/src/win/Lgi/View.cpp --- a/src/win/Lgi/View.cpp +++ b/src/win/Lgi/View.cpp @@ -1,2187 +1,2191 @@ /*hdr ** FILE: LView.cpp ** AUTHOR: Matthew Allen ** DATE: 23/4/98 ** DESCRIPTION: Win32 LView Implementation ** ** Copyright (C) 1998-2003, Matthew Allen ** fret@memecode.com */ #include #include #include "lgi/common/Lgi.h" #include "lgi/common/Base64.h" #include "lgi/common/Com.h" #include "lgi/common/DragAndDrop.h" #include "lgi/common/DropFiles.h" #include "lgi/common/GdiLeak.h" #include "lgi/common/Css.h" #include "lgi/common/Edit.h" #include "lgi/common/LgiRes.h" #include "lgi/common/Menu.h" #include "lgi/common/Thread.h" #include "ViewPriv.h" #define DEBUG_MOUSE_CLICKS 0 #define DEBUG_OVER 0 #define OLD_WM_CHAR_MODE 1 //////////////////////////////////////////////////////////////////////////////////////////////////// bool In_SetWindowPos = false; HWND LViewPrivate::hPrevCapture = 0; LViewPrivate::LViewPrivate(LView *view) : View(view) { WndStyle = WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN; IsThemed = LResources::DefaultColours; } LViewPrivate::~LViewPrivate() { while (EventTargets.Length()) delete EventTargets[0]; if (hTheme) { CloseThemeData(hTheme); hTheme = NULL; } if (FontOwnType == GV_FontOwned) { DeleteObj(Font); } } //////////////////////////////////////////////////////////////////////////////////////////////////// // Helper Stuff #include "zmouse.h" int MouseRollMsg = 0; #ifdef __GNUC__ #define MSH_WHEELMODULE_CLASS "MouseZ" #define MSH_WHEELMODULE_TITLE "Magellan MSWHEEL" #define MSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG" #endif int _lgi_mouse_wheel_lines() { UINT nScrollLines; if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, (PVOID) &nScrollLines, 0)) return nScrollLines; return 3; } #define SetKeyFlag(v, k, f) if (GetKeyState(k)&0xFF00) { v |= f; } int _lgi_get_key_flags() { int Flags = 0; if (LGetOs() == LGI_OS_WIN9X) { SetKeyFlag(Flags, VK_MENU, LGI_EF_ALT); SetKeyFlag(Flags, VK_SHIFT, LGI_EF_SHIFT); SetKeyFlag(Flags, VK_CONTROL, LGI_EF_CTRL); } else // is NT/2K/XP { SetKeyFlag(Flags, VK_LMENU, LGI_EF_LALT); SetKeyFlag(Flags, VK_RMENU, LGI_EF_RALT); SetKeyFlag(Flags, VK_LSHIFT, LGI_EF_LSHIFT); SetKeyFlag(Flags, VK_RSHIFT, LGI_EF_RSHIFT); SetKeyFlag(Flags, VK_LCONTROL, LGI_EF_LCTRL); SetKeyFlag(Flags, VK_RCONTROL, LGI_EF_RCTRL); } if (GetKeyState(VK_CAPITAL)) SetFlag(Flags, LGI_EF_CAPS_LOCK); return Flags; } //////////////////////////////////////////////////////////////////////////////////////////////////// int GetInputACP() { char16 Str[16]; LCID Lcid = (NativeInt)GetKeyboardLayout(GetCurrentThreadId()) & 0xffff; GetLocaleInfo(Lcid, LOCALE_IDEFAULTANSICODEPAGE, Str, sizeof(Str)); return _wtoi(Str); } LKey::LKey(int v, uint32_t flags) { const char *Cp = 0; vkey = v; Data = flags; c16 = 0; #if OLD_WM_CHAR_MODE c16 = vkey; #else typedef int (WINAPI *p_ToUnicode)(UINT, UINT, PBYTE, LPWSTR, int, UINT); static bool First = true; static p_ToUnicode ToUnicode = 0; if (First) { ToUnicode = (p_ToUnicode) GetProcAddress(LoadLibrary("User32.dll"), "ToUnicode"); First = false; } if (ToUnicode) { BYTE state[256]; GetKeyboardState(state); char16 w[4]; int r = ToUnicode(vkey, flags & 0x7f, state, w, CountOf(w), 0); if (r == 1) { c16 = w[0]; } } #endif } //////////////////////////////////////////////////////////////////////////////////////////////////// template bool CastHwnd(T *&Ptr, HWND hWnd) { #if _MSC_VER >= _MSC_VER_VS2005 LONG_PTR user = GetWindowLongPtr(hWnd, GWLP_USERDATA); #else LONG user = GetWindowLong(hWnd, GWL_USERDATA); #endif LONG magic = GetWindowLong(hWnd, GWL_LGI_MAGIC); if (magic != LGI_GViewMagic) { TCHAR ClsName[256] = {0}; int Ch = GetClassName(hWnd, ClsName, CountOf(ClsName)); LString Cls = ClsName; // LgiTrace("%s:%i - Error: hWnd=%p/%s, GWL_LGI_MAGIC=%i\n", _FL, hWnd, Cls.Get(), magic); return false; } Ptr = dynamic_cast((LViewI*)user); return Ptr != NULL; } bool SetLgiMagic(HWND hWnd) { SetLastError(0); LONG res = SetWindowLong(hWnd, GWL_LGI_MAGIC, LGI_GViewMagic); bool Status = res != 0; if (!Status) { DWORD err = GetLastError(); Status = err == 0; } LONG v = GetWindowLong(hWnd, GWL_LGI_MAGIC); // LgiTrace("set LGI_GViewMagic for %p, %i, %i\n", hWnd, Status, v); return Status; } LRESULT CALLBACK LWindowsClass::Redir(HWND hWnd, UINT m, WPARAM a, LPARAM b) { if (m == WM_NCCREATE) { LPCREATESTRUCT Info = (LPCREATESTRUCT) b; LViewI *ViewI = (LViewI*) Info->lpCreateParams; if (ViewI) { LView *View = ViewI->GetGView(); if (View) View->_View = hWnd; #if _MSC_VER >= _MSC_VER_VS2005 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)ViewI); #else SetWindowLong(hWnd, GWL_USERDATA, (LONG)ViewI); #endif SetLgiMagic(hWnd); } } LViewI *Wnd = (LViewI*) #if _MSC_VER >= _MSC_VER_VS2005 GetWindowLongPtr(hWnd, GWLP_USERDATA); #else GetWindowLong(hWnd, GWL_USERDATA); #endif if (Wnd) { LMessage Msg(m, a, b); Msg.hWnd = hWnd; return Wnd->OnEvent(&Msg); } return DefWindowProcW(hWnd, m, a, b); } LRESULT CALLBACK LWindowsClass::SubClassRedir(HWND hWnd, UINT m, WPARAM a, LPARAM b) { if (m == WM_NCCREATE) { LPCREATESTRUCT Info = (LPCREATESTRUCT) b; LViewI *ViewI = 0; if (Info->lpCreateParams) { if (ViewI = (LViewI*) Info->lpCreateParams) { LView *View = ViewI->GetGView(); if (View) View->_View = hWnd; } } #if _MSC_VER >= _MSC_VER_VS2005 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR) ViewI); #else SetWindowLong(hWnd, GWL_USERDATA, (LONG) ViewI); #endif SetLgiMagic(hWnd); } LViewI *Wnd = (LViewI*) #if _MSC_VER >= _MSC_VER_VS2005 GetWindowLongPtr(hWnd, GWLP_USERDATA); #else GetWindowLong(hWnd, GWL_USERDATA); #endif if (Wnd) { LMessage Msg(m, a, b); Msg.hWnd = hWnd; LMessage::Result Status = Wnd->OnEvent(&Msg); return Status; } return DefWindowProcW(hWnd, m, a, b); } LWindowsClass::LWindowsClass(const char *name) { Name(name); ZeroObj(Class); Class.lpfnWndProc = (WNDPROC) Redir; Class.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; Class.cbWndExtra = GWL_EXTRA_BYTES; Class.cbSize = sizeof(Class); ParentProc = 0; } LWindowsClass::~LWindowsClass() { UnregisterClassW(NameW(), LProcessInst()); Class.lpszClassName = NULL; } LWindowsClass *LWindowsClass::Create(const char *ClassName) { if (!LAppInst) return NULL; LApp::ClassContainer *Classes = LAppInst->GetClasses(); if (!Classes) return NULL; LWindowsClass *c = Classes->Find(ClassName); if (!c) { c = new LWindowsClass(ClassName); if (c) Classes->Add(ClassName, c); } return c; } bool LWindowsClass::IsSystem(const char *Cls) { if (!_stricmp(Cls, WC_BUTTONA) || !_stricmp(Cls, WC_COMBOBOXA) || !_stricmp(Cls, WC_STATICA)|| !_stricmp(Cls, WC_LISTBOXA)|| !_stricmp(Cls, WC_SCROLLBARA)|| !_stricmp(Cls, WC_HEADERA)|| !_stricmp(Cls, WC_LISTVIEWA)|| !_stricmp(Cls, WC_TREEVIEWA)|| !_stricmp(Cls, WC_COMBOBOXEXA)|| !_stricmp(Cls, WC_TABCONTROLA)|| !_stricmp(Cls, WC_IPADDRESSA)|| !_stricmp(Cls, WC_EDITA)) { return true; } return false; } bool LWindowsClass::Register() { bool Status = false; if (IsSystem(Name())) { ZeroObj(Class); Class.cbSize = sizeof(Class); Status = GetClassInfoExW(LProcessInst(), NameW(), &Class) != 0; LAssert(Status); } else // if (!Class.lpszClassName) { Class.hInstance = LProcessInst(); if (!Class.lpszClassName) Class.lpszClassName = NameW(); Status = RegisterClassExW(&Class) != 0; if (!Status) { auto err = GetLastError(); if (err == 1410) Status = true; else LAssert(Status); } } return Status; } bool LWindowsClass::SubClass(char *Parent) { bool Status = false; if (!Class.lpszClassName) { HBRUSH hBr = Class.hbrBackground; LAutoWString p(Utf8ToWide(Parent)); if (p) { if (GetClassInfoExW(LProcessInst(), p, &Class)) { ParentProc = Class.lpfnWndProc; if (hBr) { Class.hbrBackground = hBr; } Class.cbWndExtra = max(Class.cbWndExtra, GWL_EXTRA_BYTES); Class.hInstance = LProcessInst(); Class.lpfnWndProc = (WNDPROC) SubClassRedir; Class.lpszClassName = NameW(); Status = RegisterClassExW(&Class) != 0; LAssert(Status); } } } else Status = true; return Status; } LRESULT CALLBACK LWindowsClass::CallParent(HWND hWnd, UINT m, WPARAM a, LPARAM b) { if (!ParentProc) return 0; if (IsWindowUnicode(hWnd)) { return CallWindowProcW(ParentProc, hWnd, m, a, b); } else { return CallWindowProcA(ParentProc, hWnd, m, a, b); } } ////////////////////////////////////////////////////////////////////////////// LViewI *LWindowFromHandle(HWND hWnd) { if (hWnd) { SetLastError(0); int32 m = GetWindowLong(hWnd, GWL_LGI_MAGIC); #if 0 //def _DEBUG DWORD err = GetLastError(); if (err == 1413) { TCHAR name[256]; if (GetClassName(hWnd, name, sizeof(name))) { WNDCLASSEX cls; ZeroObj(cls); cls.cbSize = sizeof(WNDCLASSEX); if (GetClassInfoEx(LAppInst->GetInstance(), name, &cls)) { if (cls.cbWndExtra >= 8) { LAssert(!"Really?"); } } } } #endif if (m == LGI_GViewMagic) { return (LViewI*) #if _MSC_VER >= _MSC_VER_VS2005 GetWindowLongPtr(hWnd, GWLP_USERDATA); #else GetWindowLong(hWnd, GWL_USERDATA); #endif } } return 0; } ////////////////////////////////////////////////////////////////////////////// const char *LView::GetClass() { return "LView"; } void LView::_Delete() { if (_View && d->DropTarget) { RevokeDragDrop(_View); } #ifdef _DEBUG // Sanity check.. // LArray HasView; for (auto c: Children) { auto par = c->GetParent(); bool ok = ((LViewI*)par) == this || par == NULL; if (!ok) LAssert(!"heirachy error"); } #endif // Delete myself out of my parent's list if (d->Parent) { d->Parent->OnChildrenChanged(this, false); d->Parent->DelView(this); d->Parent = 0; d->ParentI = 0; } // Delete all children LViewI *c; while (c = Children[0]) { // If it has no parent, remove the pointer from the child list, // Because the child isn't going to do it... if (c->GetParent() == 0) Children.Delete(c); // Delete the child view DeleteObj(c); } // Delete the OS representation of myself if (_View && IsWindow(_View)) { WndFlags |= GWF_DESTRUCTOR; BOOL Status = DestroyWindow(_View); LAssert(Status != 0); } // NULL my handles and flags _View = 0; WndFlags = 0; // Remove static references to myself if (_Over == this) _Over = 0; if (_Capturing == this) { #if DEBUG_CAPTURE LgiTrace("%s:%i - _Capturing %p/%s -> NULL\n", _FL, this, GetClass()); #endif _Capturing = 0; } LWindow *Wnd = GetWindow(); if (Wnd) Wnd->SetFocus(this, LWindow::ViewDelete); // this should only exist in an ex-LWindow, due to the way // C++ deletes objects it needs to be here. DeleteObj(_Lock); } void LView::Quit(bool DontDelete) { if (_View) { if (!DontDelete) { WndFlags |= GWF_QUIT_WND; } DestroyWindow(_View); } } uint32_t LView::GetDlgCode() { return d->WndDlgCode; } void LView::SetDlgCode(uint32_t i) { d->WndDlgCode = i; } uint32_t LView::GetStyle() { return d->WndStyle; } void LView::SetStyle(uint32_t i) { d->WndStyle = i; } uint32_t LView::GetExStyle() { return d->WndExStyle; } void LView::SetExStyle(uint32_t i) { d->WndExStyle = i; } const char *LView::GetClassW32() { return d->WndClass; } void LView::SetClassW32(const char *c) { d->WndClass = c; } LWindowsClass *LView::CreateClassW32(const char *Class, HICON Icon, int AddStyles) { if (Class) { SetClassW32(Class); } if (GetClassW32()) { LWindowsClass *c = LWindowsClass::Create(GetClassW32()); if (c) { if (Icon) { c->Class.hIcon = Icon; } if (AddStyles) { c->Class.style |= AddStyles; } c->Register(); return c; } } return 0; } bool LView::IsAttached() { return _View && IsWindow(_View); } bool LView::Attach(LViewI *p) { bool Status = false; SetParent(p); LView *Parent = d->GetParent(); if (Parent && !_Window) _Window = Parent->_Window; const char *ClsName = GetClassW32(); if (!ClsName) ClsName = GetClass(); if (ClsName) { // Real window with HWND bool Enab = Enabled(); // Check the class is created bool IsSystemClass = LWindowsClass::IsSystem(ClsName); LWindowsClass *Cls = LWindowsClass::Create(ClsName); if (Cls) { auto r = Cls->Register(); if (!r) { LAssert(0); } } else if (!IsSystemClass) return false; LAssert(!Parent || Parent->Handle() != 0); DWORD Style = GetStyle(); DWORD ExStyle = GetExStyle() & ~WS_EX_CONTROLPARENT; if (!TestFlag(WndFlags, GWF_SYS_BORDER)) ExStyle &= ~(WS_EX_CLIENTEDGE | WS_EX_WINDOWEDGE); auto Text = LBase::NameW(); LAutoWString WCls(Utf8ToWide(ClsName)); bool hasCaption = (GetStyle() & WS_CAPTION) != 0; int Shadow = WINDOWS_SHADOW_AMOUNT; + /* LgiTrace("%p/%s::Attach %s\n", this, Name(), Pos.GetStr()); LRect r = Pos; r.x2 += Shadow; r.y2 += Shadow; LgiTrace(" %s\n", r.GetStr()); + */ _View = CreateWindowExW(ExStyle, WCls, Text, Style, Pos.x1, Pos.y1, Pos.X(), Pos.Y(), Parent ? Parent->Handle() : 0, NULL, LProcessInst(), (LViewI*) this); #ifdef _DEBUG if (!_View) { DWORD e = GetLastError(); LgiTrace("%s:%i - CreateWindowExW failed with 0x%x\n", _FL, e); LAssert(!"CreateWindowEx failed"); } #endif if (_View) { Status = (_View != NULL); if (d->Font) SendMessage(_View, WM_SETFONT, (WPARAM) d->Font->Handle(), 0); if (d->DropTarget) RegisterDragDrop(_View, d->DropTarget); if (TestFlag(WndFlags, GWF_FOCUS)) SetFocus(_View); if (d->WantsPulse > 0) { SetPulse(d->WantsPulse); d->WantsPulse = -1; } } OnAttach(); } else { // Virtual window (no HWND) Status = true; } if (Status && d->Parent) { if (!d->Parent->HasView(this)) { d->Parent->AddView(this); } d->Parent->OnChildrenChanged(this, true); } return Status; } bool LView::Detach() { bool Status = false; if (_Window) { LWindow *Wnd = dynamic_cast(_Window); if (Wnd) Wnd->SetFocus(this, LWindow::ViewDelete); _Window = NULL; } if (d->Parent) { d->Parent->DelView(this); d->Parent->OnChildrenChanged(this, false); d->Parent = 0; d->ParentI = 0; Status = true; WndFlags &= ~GWF_FOCUS; if (_Capturing == this) { if (_View) ReleaseCapture(); #if DEBUG_CAPTURE LgiTrace("%s:%i - _Capturing %p/%s -> NULL\n", _FL, this, GetClass()); #endif _Capturing = 0; } if (_View) { WndFlags &= ~GWF_QUIT_WND; BOOL Status = DestroyWindow(_View); DWORD Err = GetLastError(); LAssert(Status != 0); } } return Status; } LRect &LView::GetClient(bool InClientSpace) { static LRect Client; if (_View) { RECT rc; GetClientRect(_View, &rc); Client = rc; } else { Client.Set(0, 0, Pos.X()-1, Pos.Y()-1); if (dynamic_cast(this) || dynamic_cast(this)) { Client.x1 += GetSystemMetrics(SM_CXFRAME); Client.x2 -= GetSystemMetrics(SM_CXFRAME); Client.y1 += GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION); Client.y2 -= GetSystemMetrics(SM_CYFRAME); } else if (Sunken() || Raised()) { Client.Inset(_BorderSize, _BorderSize); } } if (InClientSpace) Client.Offset(-Client.x1, -Client.y1); return Client; } LCursor LView::GetCursor(int x, int y) { return LCUR_Normal; } #ifndef GCL_HCURSOR #define GCL_HCURSOR -12 #endif bool LgiToWindowsCursor(OsView Hnd, LCursor Cursor) { char16 *Set = 0; switch (Cursor) { case LCUR_UpArrow: Set = IDC_UPARROW; break; case LCUR_Cross: Set = IDC_CROSS; break; case LCUR_Wait: Set = IDC_WAIT; break; case LCUR_Ibeam: Set = IDC_IBEAM; break; case LCUR_SizeVer: Set = IDC_SIZENS; break; case LCUR_SizeHor: Set = IDC_SIZEWE; break; case LCUR_SizeBDiag: Set = IDC_SIZENESW; break; case LCUR_SizeFDiag: Set = IDC_SIZENWSE; break; case LCUR_SizeAll: Set = IDC_SIZEALL; break; case LCUR_PointingHand: { LArray Ver; int Os = LGetOs(&Ver); if ( ( Os == LGI_OS_WIN32 || Os == LGI_OS_WIN64 ) && Ver[0] >= 5) { #ifndef IDC_HAND #define IDC_HAND MAKEINTRESOURCE(32649) #endif Set = IDC_HAND; } // else not supported break; } case LCUR_Forbidden: Set = IDC_NO; break; // Not impl case LCUR_SplitV: break; case LCUR_SplitH: break; case LCUR_Blank: break; } HCURSOR cur = LoadCursor(0, Set ? Set : IDC_ARROW); SetCursor(cur); if (Hnd) SetWindowLongPtr(Hnd, GCL_HCURSOR, (LONG_PTR)cur); return true; } bool LView::PointToScreen(LPoint &p) { POINT pt = {p.x, p.y}; LViewI *t = this; while ( t && t->GetParent() && !t->Handle()) { pt.x += t->GetPos().x1; pt.y += t->GetPos().y1; t = t->GetParent(); } ClientToScreen(t->Handle(), &pt); p.x = pt.x; p.y = pt.y; return true; } bool LView::PointToView(LPoint &p) { POINT pt = {p.x, p.y}; LViewI *t = this; while ( t && t->GetParent() && !t->Handle()) { pt.x -= t->GetPos().x1; pt.y -= t->GetPos().y1; t = t->GetParent(); } ScreenToClient(t->Handle(), &pt); p.x = pt.x; p.y = pt.y; return true; } bool LView::GetMouse(LMouse &m, bool ScreenCoords) { // position POINT p; GetCursorPos(&p); if (!ScreenCoords) { ScreenToClient(_View, &p); } m.x = p.x; m.y = p.y; m.Target = this; // buttons m.Flags = ((GetAsyncKeyState(VK_LBUTTON)&0x8000) ? LGI_EF_LEFT : 0) | ((GetAsyncKeyState(VK_MBUTTON)&0x8000) ? LGI_EF_MIDDLE : 0) | ((GetAsyncKeyState(VK_RBUTTON)&0x8000) ? LGI_EF_RIGHT : 0) | ((GetAsyncKeyState(VK_CONTROL)&0x8000) ? LGI_EF_CTRL : 0) | ((GetAsyncKeyState(VK_MENU) &0x8000) ? LGI_EF_ALT : 0) | ((GetAsyncKeyState(VK_LWIN) &0x8000) ? LGI_EF_SYSTEM : 0) | ((GetAsyncKeyState(VK_RWIN) &0x8000) ? LGI_EF_SYSTEM : 0) | ((GetAsyncKeyState(VK_SHIFT) &0x8000) ? LGI_EF_SHIFT : 0); if (m.Flags & (LGI_EF_LEFT | LGI_EF_MIDDLE | LGI_EF_RIGHT)) { m.Flags |= LGI_EF_DOWN; } return true; } bool LView::SetPos(LRect &p, bool Repaint) { bool Status = true; LRect OldPos = Pos; if (Pos != p) { Pos = p; if (_View) { HWND hOld = GetFocus(); bool WasVis = IsWindowVisible(_View) != 0; int Shadow = WINDOWS_SHADOW_AMOUNT; In_SetWindowPos = true; Status = SetWindowPos( _View, NULL, Pos.x1, Pos.y1, Pos.X() + Shadow, Pos.Y() + Shadow, // ((Repaint) ? 0 : SWP_NOREDRAW) | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER) != 0; In_SetWindowPos = false; } else if (GetParent()) { OnPosChange(); } if (Repaint) { Invalidate(); } } return Status; } bool LView::Invalidate(LRect *r, bool Repaint, bool Frame) { if (_View) { bool Status = false; if (Frame) { RedrawWindow( _View, NULL, NULL, RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN | ((Repaint) ? RDW_UPDATENOW : 0)); } else { if (r) { Status = InvalidateRect(_View, &((RECT)*r), false) != 0; } else { RECT c = GetClient(); Status = InvalidateRect(_View, &c, false) != 0; } } if (Repaint) { UpdateWindow(_View); } return Status; } else { LRect Up; LViewI *p = this; if (r) { Up = *r; } else { Up.Set(0, 0, Pos.X()-1, Pos.Y()-1); } if (dynamic_cast(this)) return true; while (p && !p->Handle()) { LViewI *Par = p->GetParent(); LView *VPar = Par?Par->GetGView():0; LRect w = p->GetPos(); LRect c = p->GetClient(false); if (Frame && p == this) Up.Offset(w.x1, w.y1); else Up.Offset(w.x1 + c.x1, w.y1 + c.y1); p = Par; } if (p && p->Handle()) { return p->Invalidate(&Up, Repaint); } } return false; } void CALLBACK LView::TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, uint32_t dwTime) { LView *View = (LView*) idEvent; if (View) { View->OnPulse(); } } void LView::SetPulse(int Length) { if (_View) { if (Length > 0) { d->TimerId = SetTimer(_View, (UINT_PTR) this, Length, (TIMERPROC) TimerProc); } else { KillTimer(_View, d->TimerId); d->TimerId = 0; } } else { d->WantsPulse = Length; } } static int ConsumeTabKey = 0; bool SysOnKey(LView *w, LMessage *m) { if (m->a == VK_TAB && (m->m == WM_KEYDOWN || m->m == WM_SYSKEYDOWN) ) { if (!TestFlag(w->d->WndDlgCode, DLGC_WANTTAB) && !TestFlag(w->d->WndDlgCode, DLGC_WANTALLKEYS)) { // push the focus to the next control bool Shifted = (GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0; LViewI *Wnd = GetNextTabStop(w, Shifted); if (Wnd) { if (In_SetWindowPos) { assert(0); LgiTrace("%s:%i - SetFocus(%p)\\n", _FL, Wnd->Handle()); } ConsumeTabKey = 2; ::SetFocus(Wnd->Handle()); return true; } } } return false; } #ifdef _MSC_VER #include "vsstyle.h" void LView::DrawThemeBorder(LSurface *pDC, LRect &r) { if (!d->hTheme) d->hTheme = OpenThemeData(_View, VSCLASS_EDIT); if (d->hTheme) { RECT rc = r; int StateId; if (!Enabled()) StateId = EPSN_DISABLED; else if (GetFocus() == _View) StateId = EPSN_FOCUSED; else StateId = EPSN_NORMAL; // LgiTrace("ThemeDraw %s: %i\n", GetClass(), StateId); RECT clip[4]; clip[0] = LRect(r.x1, r.y1, r.x1 + 1, r.y2); // left clip[1] = LRect(r.x1 + 2, r.y1, r.x2 - 2, r.y1 + 1); // top clip[2] = LRect(r.x2 - 1, r.y1, r.x2, r.y2); // right clip[3] = LRect(r.x1 + 2, r.y2 - 1, r.x2 - 2, r.y2); // bottom LColour cols[4] = { LColour(255, 0, 0), LColour(0, 255, 0), LColour(0, 0, 255), LColour(255, 255, 0) }; for (int i=0; iColour(cols[i]); pDC->Rectangle(&tmp); #else DrawThemeBackground(d->hTheme, pDC->Handle(), EP_EDITBORDER_NOSCROLL, StateId, &rc, &clip[i]); #endif } pDC->Colour(L_MED); pDC->Set(r.x1, r.y1); pDC->Set(r.x2, r.y1); pDC->Set(r.x1, r.y2); pDC->Set(r.x2, r.y2); r.Inset(2, 2); } else { LWideBorder(pDC, r, Sunken() ? DefaultSunkenEdge : DefaultRaisedEdge); d->IsThemed = false; } } #else void LView::DrawThemeBorder(LSurface *pDC, LRect &r) { LWideBorder(pDC, r, DefaultSunkenEdge); } #endif bool IsKeyChar(LKey &k, int vk) { if (k.Ctrl() || k.Alt() || k.System()) return false; switch (vk) { case VK_BACK: case VK_TAB: case VK_RETURN: case VK_SPACE: case 0xba: // ; case 0xbb: // = case 0xbc: // , case 0xbd: // - case 0xbe: // . case 0xbf: // / case 0xc0: // ` case 0xdb: // [ case 0xdc: // | case 0xdd: // ] case 0xde: // ' return true; } if (vk >= VK_NUMPAD0 && vk <= VK_DIVIDE) return true; if (vk >= '0' && vk <= '9') return true; if (vk >= 'A' && vk <= 'Z') return true; return false; } #define KEY_FLAGS (~(MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)) LMessage::Result LView::OnEvent(LMessage *Msg) { int Status = 0; if (Msg->Msg() == MouseRollMsg) { HWND hFocus = GetFocus(); if (_View) { int Flags = ((GetKeyState(VK_SHIFT)&0xF000) ? VK_SHIFT : 0) | ((GetKeyState(VK_CONTROL)&0xF000) ? VK_CONTROL : 0); PostMessage(hFocus, WM_MOUSEWHEEL, MAKELONG(Flags, (short)Msg->a), Msg->b); } return 0; } for (auto target: d->EventTargets) { if (target->Msgs.Length() == 0 || target->Msgs.Find(Msg->Msg())) target->OnEvent(Msg); } if (_View) { switch (Msg->m) { #if 1 case WM_CTLCOLORBTN: case WM_CTLCOLOREDIT: case WM_CTLCOLORSTATIC: { HDC hdc = (HDC)Msg->A(); HWND hwnd = (HWND)Msg->B(); LViewI *v = FindControl(hwnd); LView *gv = v ? v->GetGView() : NULL; if (gv) { int Depth = dynamic_cast(gv) ? 1 : 10; LColour Fore = gv->StyleColour(LCss::PropColor, LColour(), Depth); LColour Back = gv->StyleColour(LCss::PropBackgroundColor, LColour(), Depth); if (Fore.IsValid()) { COLORREF c = RGB(Fore.r(), Fore.g(), Fore.b()); SetTextColor(hdc, c); } if (Back.IsValid()) { COLORREF c = RGB(Back.r(), Back.g(), Back.b()); SetBkColor(hdc, c); SetDCBrushColor(hdc, c); } if (Fore.IsValid() || Back.IsValid()) { #if !defined(DC_BRUSH) #define DC_BRUSH 18 #endif return (LRESULT) GetStockObject(DC_BRUSH); } } goto ReturnDefaultProc; return 0; } #endif case 5700: { // I forget what this is for... break; } case WM_ERASEBKGND: { return 1; } case WM_GETFONT: { LFont *f = GetFont(); if (!f || f == LSysFont) return (LMessage::Result) LSysFont->Handle(); return (LMessage::Result) f->Handle(); break; } case WM_MENUCHAR: case WM_MEASUREITEM: { return LMenu::_OnEvent(Msg); break; } case WM_DRAWITEM: { DRAWITEMSTRUCT *di = (DRAWITEMSTRUCT*)Msg->B(); if (di) { if (di->CtlType == ODT_MENU) { return LMenu::_OnEvent(Msg); } /* else if (di->CtlType == ODT_BUTTON) { LView *b; if (CastHwnd(b, di->hwndItem) && b->GetCss()) { LScreenDC dc(di->hDC, di->hwndItem); switch (di->itemAction) { case ODA_DRAWENTIRE: { LRect c = di->rcItem; LMemDC m(c.X(), c.Y(), GdcD->GetColourSpace()); HDC hdc = m.StartDC(); m.Colour(LColour(255, 0, 255)); m.Line(0, 0, m.X()-1, m.Y()-1); LONG s = GetWindowLong(_View, GWL_STYLE); SetWindowLong(_View, GWL_STYLE, (s & ~BS_TYPEMASK) | BS_PUSHBUTTON); SendMessage(_View, WM_PRINT, (WPARAM)hdc, PRF_ERASEBKGND|PRF_CLIENT); SetWindowLong(_View, GWL_STYLE, (s & ~BS_TYPEMASK) | BS_OWNERDRAW); m.EndDC(); dc.Blt(0, 0, &m); break; } case ODA_FOCUS: { break; } case ODA_SELECT: { break; } } return true; } } */ } if (!(WndFlags & GWF_DIALOG)) goto ReturnDefaultProc; break; } case WM_ENABLE: { Invalidate(&Pos); break; } case WM_HSCROLL: case WM_VSCROLL: { LViewI *Wnd = FindControl((HWND) Msg->b); if (Wnd) { Wnd->OnEvent(Msg); } break; } case WM_GETDLGCODE: { // we handle all tab control stuff return DLGC_WANTALLKEYS; // d->WndDlgCode | DLGC_WANTTAB; } case WM_MOUSEWHEEL: { // short fwKeys = LOWORD(Msg->a); // key flags short zDelta = (short) HIWORD(Msg->a); // wheel rotation int nScrollLines = - _lgi_mouse_wheel_lines(); double Lines = ((double)zDelta * (double)nScrollLines) / WHEEL_DELTA; if (ABS(Lines) < 1.0) Lines *= 1.0 / ABS(Lines); // LgiTrace("Lines = %g, zDelta = %i, nScrollLines = %i\n", Lines, zDelta, nScrollLines); // Try giving the event to the current window... if (!OnMouseWheel(Lines)) { // Find the window under the cursor... and try giving it the mouse wheel event short xPos = (short) LOWORD(Msg->b); // horizontal position of pointer short yPos = (short) HIWORD(Msg->b); // vertical position of pointer POINT Point = {xPos, yPos}; HWND hUnder = ::WindowFromPoint(Point); HWND hParent = ::GetParent(hUnder); if (hUnder && hUnder != _View && // Don't want to send ourselves a message... hParent != _View) // WM_MOUSEWHEEL will propagate back up to us and cause an infinite loop { // Do a post event in case the window is deleting... at least it won't crash. PostMessage(hUnder, Msg->m, Msg->a, Msg->b); } } return 0; } case M_CHANGE: { LWindow *w = GetWindow(); LAutoPtr note((LNotification*)Msg->B()); LViewI *Ctrl = w ? w->FindControl((int)Msg->a) : 0; if (Ctrl) { LAssert(note.Get() != NULL); return OnNotify(Ctrl, note ? *note : LNotifyNull); } else { LgiTrace("Ctrl %i not found.\n", Msg->a); } break; } case M_COMMAND: { // LViewI *Ci = FindControl((HWND) Msg->b); // LView *Ctrl = Ci ? Ci->GetGView() : 0; LView *Ctrl; if (Msg->b && CastHwnd(Ctrl, (HWND)Msg->b)) { short Code = HIWORD(Msg->a); switch (Code) { case CBN_CLOSEUP: { PostMessage(_View, WM_COMMAND, MAKELONG(Ctrl->GetId(), CBN_EDITCHANGE), Msg->b); break; } case CBN_EDITCHANGE: // COMBO { Ctrl->SysOnNotify(Msg->Msg(), Code); OnNotify(Ctrl, LNotifyValueChanged); break; } /* case BN_CLICKED: // BUTTON case EN_CHANGE: // EDIT */ default: { Ctrl->SysOnNotify(Msg->Msg(), Code); break; } } } break; } case WM_NCDESTROY: { #if _MSC_VER >= _MSC_VER_VS2005 SetWindowLongPtr(_View, GWLP_USERDATA, 0); #else SetWindowLong(_View, GWL_USERDATA, 0); #endif _View = NULL; if (WndFlags & GWF_QUIT_WND) { delete this; } break; } case WM_CLOSE: { if (OnRequestClose(false)) { Quit(); } break; } case WM_DESTROY: { OnDestroy(); break; } case WM_CREATE: { SetId(d->CtrlId); LWindow *w = GetWindow(); if (w && w->GetFocus() == this) { HWND hCur = GetFocus(); if (hCur != _View) { if (In_SetWindowPos) { assert(0); LgiTrace("%s:%i - SetFocus(%p) (%s)\\n", __FILE__, __LINE__, Handle(), GetClass()); } SetFocus(_View); } } if (TestFlag(GViewFlags, GWF_DROP_TARGET)) { DropTarget(true); } OnCreate(); break; } case WM_SETFOCUS: { LWindow *w = GetWindow(); if (w) { w->SetFocus(this, LWindow::GainFocus); } else { // This can happen in popup sub-trees of views. Where the focus // is tracked separately from the main LWindow. OnFocus(true); Invalidate((LRect*)NULL, false, true); } break; } case WM_KILLFOCUS: { LWindow *w = GetWindow(); if (w) { w->SetFocus(this, LWindow::LoseFocus); } else { // This can happen when the LWindow is being destroyed Invalidate((LRect*)NULL, false, true); OnFocus(false); } break; } case WM_WINDOWPOSCHANGED: { if (!IsIconic(_View)) { WINDOWPOS *Info = (LPWINDOWPOS) Msg->b; if (Info) { if (Info->x == -32000 && Info->y == -32000) { #if 0 LgiTrace("WM_WINDOWPOSCHANGED %i,%i,%i,%i (icon=%i)\\n", Info->x, Info->y, Info->cx, Info->cy, IsIconic(Handle())); #endif } else { int Shadow = WINDOWS_SHADOW_AMOUNT; LRect r; r.ZOff(Info->cx-1, Info->cy-1); r.Offset(Info->x, Info->y); if (r.Valid() && r != Pos) { Pos = r; + /* LgiTrace("%p/%s::PosChange %s\n", this, Name(), Pos.GetStr()); Pos.x2 -= Shadow; Pos.y2 -= Shadow; LgiTrace(" %s\n", Pos.GetStr()); + */ } } } OnPosChange(); } if (!(WndFlags & GWF_DIALOG)) { goto ReturnDefaultProc; } break; } case WM_CAPTURECHANGED: { LViewI *Wnd; if (Msg->B() && CastHwnd(Wnd, (HWND)Msg->B())) { if (Wnd != _Capturing) { #if DEBUG_CAPTURE LgiTrace("%s:%i - _Capturing %p/%s -> %p/%s\n", _FL, _Capturing, _Capturing?_Capturing->GetClass():0, Wnd, Wnd?Wnd->GetClass() : 0); #endif _Capturing = Wnd; } } else if (_Capturing) { #if DEBUG_CAPTURE LgiTrace("%s:%i - _Capturing %p/%s -> NULL\n", _FL, _Capturing, _Capturing?_Capturing->GetClass():0); #endif _Capturing = NULL; } break; } case M_MOUSEENTER: { LMouse Ms; Ms.Target = this; Ms.x = (short) (Msg->b&0xFFFF); Ms.y = (short) (Msg->b>>16); Ms.Flags = 0; LViewI *MouseOver = WindowFromPoint(Ms.x, Ms.y); if (MouseOver && _Over != MouseOver && !(MouseOver == this || MouseOver->Handle() == 0)) { if (_Capturing) { if (MouseOver == _Capturing) { Ms = lgi_adjust_click(Ms, _Capturing); _Capturing->OnMouseEnter(Ms); } } else { if (_Over) { LMouse m = lgi_adjust_click(Ms, _Over); _Over->OnMouseExit(m); #if DEBUG_OVER LgiTrace("Enter.LoseOver=%p/%s '%-20s'\n", _Over, _Over->GetClass(), _Over->Name()); #endif } _Over = MouseOver; if (_Over) { #if DEBUG_OVER LgiTrace("Enter.GetOver=%p/%s '%-20s'\n", _Over, _Over->GetClass(), _Over->Name()); #endif LMouse m = lgi_adjust_click(Ms, _Over); _Over->OnMouseEnter(m); } } } break; } case M_MOUSEEXIT: { if (_Over) { LMouse Ms; Ms.Target = this; Ms.x = (short) (Msg->b&0xFFFF); Ms.y = (short) (Msg->b>>16); Ms.Flags = 0; bool Mine = false; if (_Over->Handle()) { Mine = _Over == this; } else { for (LViewI *o = _Capturing ? _Capturing : _Over; o; o = o->GetParent()) { if (o == this) { Mine = true; break; } } } if (Mine) { if (_Capturing) { LMouse m = lgi_adjust_click(Ms, _Capturing); _Capturing->OnMouseExit(m); } else { #if DEBUG_OVER LgiTrace("Exit.LoseOver=%p '%-20s'\n", _Over, _Over->Name()); #endif _Over->OnMouseExit(Ms); _Over = 0; } } } break; } case WM_MOUSEMOVE: { LMouse Ms; Ms.Target = this; Ms.x = (short) (Msg->b&0xFFFF); Ms.y = (short) (Msg->b>>16); Ms.Flags = _lgi_get_key_flags(); Ms.IsMove(true); if (TestFlag(Msg->a, MK_LBUTTON)) SetFlag(Ms.Flags, LGI_EF_LEFT); if (TestFlag(Msg->a, MK_RBUTTON)) SetFlag(Ms.Flags, LGI_EF_RIGHT); if (TestFlag(Msg->a, MK_MBUTTON)) SetFlag(Ms.Flags, LGI_EF_MIDDLE); SetKeyFlag(Ms.Flags, VK_MENU, MK_ALT); Ms.Down((Msg->a & (MK_LBUTTON|MK_MBUTTON|MK_RBUTTON)) != 0); LViewI *MouseOver = WindowFromPoint(Ms.x, Ms.y); if (_Over != MouseOver) { if (_Over) { #if DEBUG_OVER LgiTrace("Move.LoseOver=%p/%s '%-20s'\n", _Over, _Over->GetClass(), _Over->Name()); #endif LMouse m = lgi_adjust_click(Ms, _Over); _Over->OnMouseExit(m); } _Over = MouseOver; if (_Over) { LMouse m = lgi_adjust_click(Ms, _Over); _Over->OnMouseEnter(m); #if DEBUG_OVER LgiTrace("Move.GetOver=%p/%s '%-20s'\n", _Over, _Over->GetClass(), _Over->Name()); #endif } } // int CurX = Ms.x, CurY = Ms.y; LCursor Cursor = (_Over ? _Over : this)->GetCursor(Ms.x, Ms.y); LgiToWindowsCursor(_View, Cursor); #if 0 LgiTrace("WM_MOUSEMOVE %i,%i target=%p/%s, over=%p/%s, cap=%p/%s\n", Ms.x, Ms.y, Ms.Target, Ms.Target?Ms.Target->GetClass():0, _Over, _Over?_Over->GetClass():0, _Capturing, _Capturing?_Capturing->GetClass():0); #endif if (_Capturing) Ms = lgi_adjust_click(Ms, _Capturing, true); else if (_Over) Ms = lgi_adjust_click(Ms, _Over); else return 0; LWindow *Wnd = GetWindow(); if (!Wnd || Wnd->HandleViewMouse(dynamic_cast(Ms.Target), Ms)) { Ms.Target->OnMouseMove(Ms); } break; } case WM_NCHITTEST: { POINT Pt = { LOWORD(Msg->b), HIWORD(Msg->b) }; ScreenToClient(_View, &Pt); int Hit = OnHitTest(Pt.x, Pt.y); if (Hit >= 0) { // LgiTrace("%I64i Hit=%i\n", LCurrentTime(), Hit); return Hit; } if (!(WndFlags & GWF_DIALOG)) { goto ReturnDefaultProc; } break; } case WM_LBUTTONDBLCLK: case WM_LBUTTONDOWN: case WM_LBUTTONUP: { LMouse Ms; Ms.x = (short) (Msg->b&0xFFFF); Ms.y = (short) (Msg->b>>16); Ms.Flags = _lgi_get_key_flags() | LGI_EF_LEFT; Ms.Down(Msg->m != WM_LBUTTONUP); Ms.Double(Msg->m == WM_LBUTTONDBLCLK); if (_Capturing) Ms = lgi_adjust_click(Ms, _Capturing, true); else if (_Over) Ms = lgi_adjust_click(Ms, _Over); else Ms.Target = this; #if DEBUG_MOUSE_CLICKS LString Msg; Msg.Printf("%s.Click", Ms.Target->GetClass()); Ms.Trace(Msg); #endif LWindow *Wnd = GetWindow(); if (!Wnd || Wnd->HandleViewMouse(dynamic_cast(Ms.Target), Ms)) Ms.Target->OnMouseClick(Ms); break; } case WM_RBUTTONDBLCLK: case WM_RBUTTONDOWN: case WM_RBUTTONUP: { LMouse Ms; Ms.x = (short) (Msg->b&0xFFFF); Ms.y = (short) (Msg->b>>16); Ms.Flags = _lgi_get_key_flags() | LGI_EF_RIGHT; Ms.Down(Msg->m != WM_RBUTTONUP); Ms.Double(Msg->m == WM_RBUTTONDBLCLK); if (_Capturing) Ms = lgi_adjust_click(Ms, _Capturing, true); else if (_Over) Ms = lgi_adjust_click(Ms, _Over); else Ms.Target = this; #if DEBUG_MOUSE_CLICKS LString Msg; Msg.Printf("%s.Click", Ms.Target->GetClass()); Ms.Trace(Msg); #endif LWindow *Wnd = GetWindow(); if (!Wnd || Wnd->HandleViewMouse(dynamic_cast(Ms.Target), Ms)) Ms.Target->OnMouseClick(Ms); break; } case WM_MBUTTONDBLCLK: case WM_MBUTTONDOWN: case WM_MBUTTONUP: { LMouse Ms; Ms.x = (short) (Msg->b&0xFFFF); Ms.y = (short) (Msg->b>>16); Ms.Flags = _lgi_get_key_flags() | LGI_EF_MIDDLE; Ms.Down(Msg->m != WM_MBUTTONUP); Ms.Double(Msg->m == WM_MBUTTONDBLCLK); if (_Capturing) Ms = lgi_adjust_click(Ms, _Capturing, true); else if (_Over) Ms = lgi_adjust_click(Ms, _Over); else Ms.Target = this; LWindow *Wnd = GetWindow(); if (!Wnd || Wnd->HandleViewMouse(dynamic_cast(Ms.Target), Ms)) Ms.Target->OnMouseClick(Ms); break; } case WM_XBUTTONDBLCLK: case WM_XBUTTONDOWN: case WM_XBUTTONUP: { LMouse Ms; int Clicked = (Msg->a >> 16) & 0xffff; Ms.x = (short) (Msg->b&0xFFFF); Ms.y = (short) (Msg->b>>16); Ms.Flags = _lgi_get_key_flags() | LGI_EF_MIDDLE; Ms.Button1(TestFlag(Clicked, XBUTTON1)); Ms.Button2(TestFlag(Clicked, XBUTTON2)); Ms.Down(Msg->m != WM_XBUTTONUP); Ms.Double(Msg->m == WM_XBUTTONDBLCLK); if (_Capturing) Ms = lgi_adjust_click(Ms, _Capturing, true); else if (_Over) Ms = lgi_adjust_click(Ms, _Over); else Ms.Target = this; LWindow *Wnd = GetWindow(); if (!Wnd || Wnd->HandleViewMouse(dynamic_cast(Ms.Target), Ms)) Ms.Target->OnMouseClick(Ms); break; } case WM_SYSKEYUP: case WM_SYSKEYDOWN: case WM_KEYDOWN: case WM_KEYUP: { static char AltCode[32]; bool IsDialog = TestFlag(WndFlags, GWF_DIALOG); bool IsDown = Msg->m == WM_KEYDOWN || Msg->m == WM_SYSKEYDOWN; int KeyFlags = _lgi_get_key_flags(); HWND hwnd = _View; if (SysOnKey(this, Msg)) { // LgiTrace("SysOnKey true, Msg=0x%x %x,%x\n", Msg->m, Msg->a, Msg->b); return 0; } else { // Key LKey Key((int)Msg->a, (int)Msg->b); Key.Flags = KeyFlags; Key.Down(IsDown); Key.IsChar = false; if (Key.Ctrl()) { Key.c16 = (char16)Msg->a; } if (Key.c16 == VK_TAB && ConsumeTabKey) { ConsumeTabKey--; } else { LWindow *Wnd = GetWindow(); if (Wnd) { if (Key.Alt() || Key.Ctrl() || (Key.c16 < 'A' || Key.c16 > 'Z')) { Wnd->HandleViewKey(this, Key); } } else { OnKey(Key); } } if (Msg->m == WM_SYSKEYUP || Msg->m == WM_SYSKEYDOWN) { if (Key.vkey >= VK_F1 && Key.vkey <= VK_F12 && Key.Alt() == false) { // So in LgiIde if you press F10 (debug next) you get a hang // sometimes in DefWindowProc. Until I figure out what's going // on this code exits before calling DefWindowProc without // breaking other WM_SYSKEY* functionality (esp Alt+F4). return 0; } } } if (!IsDialog) { // required for Alt-Key function (eg Alt-F4 closes window) goto ReturnDefaultProc; } break; } #if OLD_WM_CHAR_MODE case WM_CHAR: { LKey Key((int)Msg->a, (int)Msg->b); Key.Flags = _lgi_get_key_flags(); Key.Down(true); Key.IsChar = true; bool Shift = Key.Shift(); bool Caps = TestFlag(Key.Flags, LGI_EF_CAPS_LOCK); if (!(Shift ^ Caps)) { Key.c16 = ToLower(Key.c16); } else { Key.c16 = ToUpper(Key.c16); } if (Key.c16 == LK_TAB && ConsumeTabKey) { ConsumeTabKey--; } else { LWindow *Wnd = GetWindow(); if (Wnd) { Wnd->HandleViewKey(this, Key); } else { OnKey(Key); } } break; } #endif case M_SET_WND_STYLE: { SetWindowLong(Handle(), GWL_STYLE, (LONG)Msg->b); SetWindowPos( Handle(), 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE | SWP_FRAMECHANGED); break; } case WM_PAINT: { _Paint(); break; } case WM_NCPAINT: { if (GetWindow() != this && !TestFlag(WndFlags, GWF_SYS_BORDER)) { HDC hDC = GetWindowDC(_View); LScreenDC Dc(hDC, _View, true); LRect p(0, 0, Dc.X()-1, Dc.Y()-1); OnNcPaint(&Dc, p); } goto ReturnDefaultProc; break; } case WM_NCCALCSIZE: { LMessage::Param Status = 0; int Edge = (Sunken() || Raised()) ? _BorderSize : 0; RECT *rc = NULL; if (Msg->a) { NCCALCSIZE_PARAMS *p = (NCCALCSIZE_PARAMS*) Msg->b; rc = p->rgrc; } else { rc = (RECT*)Msg->b; } if (!(WndFlags & GWF_DIALOG)) { Status = DefWindowProcW(_View, Msg->m, Msg->a, Msg->b); } if (Edge && rc && !TestFlag(WndFlags, GWF_SYS_BORDER)) { rc->left += Edge; rc->top += Edge; rc->right -= Edge; rc->bottom -= Edge; return 0; } return Status; } case WM_NOTIFY: { NMHDR *Hdr = (NMHDR*)Msg->B(); if (Hdr) { LView *Wnd; if (CastHwnd(Wnd, Hdr->hwndFrom)) Wnd->SysOnNotify(Msg->Msg(), Hdr->code); } break; } case M_THREAD_COMPLETED: { auto Th = (LThread*)Msg->A(); if (!Th) break; Th->OnComplete(); if (Th->GetDeleteOnExit()) delete Th; return true; } default: { if (!(WndFlags & GWF_DIALOG)) goto ReturnDefaultProc; break; } } } return 0; ReturnDefaultProc: #ifdef _DEBUG uint64 start = LCurrentTime(); #endif LRESULT r = DefWindowProcW(_View, Msg->m, Msg->a, Msg->b); #ifdef _DEBUG uint64 now = LCurrentTime(); if (now - start > 1000) { LgiTrace("DefWindowProc(0x%.4x, %i, %i) took %ims\n", Msg->m, Msg->a, Msg->b, (int)(now - start)); } #endif return r; } LViewI *LView::FindControl(OsView hCtrl) { if (_View == hCtrl) { return this; } for (List::I i = Children.begin(); i.In(); i++) { LViewI *Ctrl = (*i)->FindControl(hCtrl); if (Ctrl) return Ctrl; } return 0; }