Get window title with XCB - c++

I am trying to get information about the window in focus. It seems that I get a correct window id from xcb_get_input_focus_reply_t->focus: it stays the same for my Eclipse IDE (56623164) and is another for any other window in focus. However, the value length is always 0 for XCB_ATOM_WM_NAME.
shortened code
cookie = xcb_get_property(c, 0, fr->focus, XCB_ATOM_WM_NAME,
XCB_ATOM_STRING, 0, 0);
if ((reply = xcb_get_property_reply(c, cookie, NULL))) {
int len = xcb_get_property_value_length(reply);
if (len == 0) {
printf("Zero Length\n");
free(reply);
return;
}
printf("WM_NAME is %.*s\n", len, (char*) xcb_get_property_value(reply));
}
Eclipse Debugger
reply xcb_get_property_reply_t * 0x60bd40
response_type uint8_t 1 '\001'
format uint8_t 0 '\0'
sequence uint16_t 2
length uint32_t 0
type xcb_atom_t 0
bytes_after uint32_t 0
value_len uint32_t 0
pad0 unsigned char [12] 0x60bd54
There is no error (I passed and inspected a xcb_generic_error_t). Do you have any idea what could go wrong? Maybe I should use Xlib instead...

This code works for me, it is in js-ctypes but you can ignore that part and see this for API use:
var win = aXcbWindowT;
// console.log('win:', win);
var req_title = ostypes.API('xcb_get_property')(ostypes.HELPER.cachedXCBConn(), 0, win, ostypes.CONST.XCB_ATOM_WM_NAME, ostypes.CONST.XCB_ATOM_STRING, 0, 100); // `100` means it will get 100*4 so 400 bytes, so that 400 char, so `rez_title.bytes_after` should be `0` but i can loop till it comes out to be 0
var rez_title = ostypes.API('xcb_get_property_reply')(ostypes.HELPER.cachedXCBConn(), req_title, null);
// console.log('rez_title:', rez_title);
var title_len = ostypes.API('xcb_get_property_value_length')(rez_title); // length is not null terminated so "Console - chrome://nativeshot/content/resources/scripts/MainWorker.js?0.01966718940939427" will be length of `88`, this matches `rez_title.length` but the docs recommend to use this call to get the value, i dont know why
console.log('title_len:', title_len, 'rez_title.contents.length:', rez_title.contents.length); // i think `rez_title.contents.length` is the actual length DIVIDED by 4, and rez_title_len is not dividied by 4
var title_buf = ostypes.API('xcb_get_property_value')(rez_title); // "title_len: 89 rez_title.contents.length: 23" for test case of "Console - chrome://nativeshot/content/resources/scripts/MainWorker.js?0.01966718940939427"
// console.log('title_buf:', title_buf);
var title = ctypes.cast(title_buf, ctypes.char.array(title_len).ptr).contents.readString();
console.log('title:', title);
ostypes.API('free')(rez_title);
return title;
Sometimes though what is returned by xcb_get_input_focus_reply_t->focus is not the window to act on. I have found that sometimes it doesn't have a title, but if you use xcb_query_tree you can find its parent window probaby has a title:
var req_query = ostypes.API('xcb_query_tree')(ostypes.HELPER.cachedXCBConn(), win);
var rez_query = ostypes.API('xcb_query_tree_reply')(ostypes.HELPER.cachedXCBConn(), req_query, null);
console.log('rez_query.contents:', rez_query.contents);
if (root === -1) {
root = rez_query.contents.root;
}
win = rez_query.contents.parent; // this win should have the title

Related

C++/libscreen fails to update visibility

I'm writing a GUI in C++ (qcc) for Neutrino 6.5.0 with QNX CAR 2.0 using the screen windowing system, following the documentation tutorial. No visible output is generated, and /dev/screen/{pid}/win-{n}/win-{n} has status = WIN_STATUS_INVISIBLE.
(Using {n} and {pid} to indicate substitutions of runtime/dynamic variables' values)
I've tried following the tutorial to the letter, but had the same results. In particular, the value of window_group_name (mainwindowgroup or veryuniquewgn) makes no difference; the former being the value suggested by the tutorial, and the latter my own.
I've constructed an MCVE:
#include <cstdlib> // for EXIT_ constants
#include <stdio.h> // for printf(), getchar()
#include <process.h> // for getpid()
#include <screen.h>
#define screen_err(desc) \
if(screen_res != 0) { \
printf("ERR screen: failed to %s\n", desc); \
return EXIT_FAILURE; \
}
int main_withwindow(screen_context_t scr_ctx, screen_window_t scr_win) {
static const char *window_group_name = "veryuniquewgn";
// Dummy to pass as ptr to enum values
int prop;
int screen_res;
screen_res = screen_create_window_group(scr_win, window_group_name);
screen_err("create window group");
prop = SCREEN_FORMAT_RGBA8888;
screen_res = screen_set_window_property_iv(scr_win, SCREEN_PROPERTY_FORMAT,
&prop);
screen_err("set window property: format -> RGBA");
prop = SCREEN_USAGE_NATIVE;
screen_res = screen_set_window_property_iv(scr_win, SCREEN_PROPERTY_USAGE,
&prop);
screen_err("set window property: usage -> native");
screen_res = screen_create_window_buffers(scr_win, 1);
screen_err("create window buffers");
int win_buf_rect[4] = { 0, 0 };
screen_res = screen_get_window_property_iv(scr_win,
SCREEN_PROPERTY_BUFFER_SIZE, win_buf_rect + 2);
screen_err("get window property: buffer_size");
// Array type to easily support multi-buffering in future
screen_buffer_t scr_buf[1];
screen_res = screen_get_window_property_pv(scr_win,
SCREEN_PROPERTY_RENDER_BUFFERS, (void **)scr_buf);
screen_err("get window property: render_buffers");
int bg[] = { SCREEN_BLIT_COLOR, 0xffffff00, SCREEN_BLIT_END };
screen_res = screen_fill(scr_ctx, scr_buf[0], bg);
screen_err("fill buffer with yellow");
screen_res = screen_post_window(scr_win, scr_buf[0], 1, win_buf_rect, 0);
screen_err("post window");
prop = 255;
screen_res = screen_set_window_property_iv(scr_win,
SCREEN_PROPERTY_ZORDER, &prop);
screen_err("set window property: zorder -> 255");
prop = 1;
screen_res = screen_set_window_property_iv(scr_win,
SCREEN_PROPERTY_VISIBLE, &prop);
screen_err("set window property: visible -> true");
screen_res = screen_flush_context(scr_ctx, SCREEN_WAIT_IDLE);
screen_err("flush context to idle");
getchar();
return EXIT_SUCCESS;
}
int main_withcontext(screen_context_t scr_ctx) {
screen_window_t scr_win;
int ret;
int screen_res;
screen_res = screen_create_window(&scr_win, scr_ctx);
screen_err("create window");
ret = main_withwindow(scr_ctx, scr_win);
screen_res = screen_destroy_window(scr_win);
screen_err("destroy window");
return ret;
}
int main(int argc, char *argv[]) {
printf("%d\n", getpid());
screen_context_t scr_ctx;
int ret;
int screen_res;
screen_res = screen_create_context(&scr_ctx, SCREEN_APPLICATION_CONTEXT);
screen_err("create context");
ret = main_withcontext(scr_ctx);
screen_res = screen_destroy_context(scr_ctx);
screen_err("destroy context");
return ret;
}
Given the use of the screen_err() define, no output - except the first printf for the pid - indicates no errors from screen_...() calls. That's exactly what I see when I run this.
Looking in /dev/screen/{pid}, I see a ctx-{n}/ctx-{n} and a win-{n}/win-{n} file, as expected. The former is not human-readable, but the latter, and its differences to its counterpart in a working nto 6.5.0 + CAR 2.0 + libscreen application, yield some insight.
The working application is an HMI for which I do not have the source, and is launched from /etc/ro as root, and mine is launched from ksh, logged in as root.
They're both 98-line files, so I've produced a diff - the win-{n} from the working application on the left, and mine on the right - excluding metrics:
(lines over 39 characters compared vertically)
autonomous = 0 autonomous = 1
status = WIN_STATUS_FULLSCREEN status = WIN_STATUS_INVISIBLE
id string = DPY_HMI id string =
insert id = 1 insert id = 3
reclip = 0 reclip = 1
flags = WIN_FLAG_VISIBLE WIN_FLAG_FLOATING
flags = WIN_FLAG_FLOATING
usage = SCREEN_USAGE_OPENGL_ES2 usage = SCREEN_USAGE_NATIVE
order = 240 order = 0
regions = (0,0;800,480) regions = (none)
clipped source viewport = (0,0;800,480 800x480)
clipped source viewport = (0,0;0,0 0x0)
clipped destination rectangle = (0,0;800,480 800x480)
clipped destination rectangle = (0,0;0,0 0x0)
transform = [[1 0 0],[0 1 0],[0 0 1]] transform = [[0 0 0],[0 0 0],[0 0 0]]
Of these differences, usage is the only one with the value I expect, considering that it reflects the parameter of the relevant call to screen_set_window_property_iv(...). For all the rest, especially regions and flags, I do not understand why their values differ from those of the working application.
The target's display's native resolution is 800x480.
As it turns out, that code was completely valid and correct. The reason for its failure was a window manager daemon I wasn't aware of suppressing the window. Turning this off solved the problem.

Clearing the stm32f4-discovery flash programatically

I'm having a problem clearing the stm32f429 discovery board flash after creating a window using stemwin library. If I use the code below, flash will be cleared correctly:
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector = FLASH_SECTOR_22;
EraseInitStruct.NbSectors = 1;
HAL_FLASH_Unlock();
uint32_t sectorError = 0;
if(HAL_FLASHEx_Erase(&EraseInitStruct,&sectorError)!=HAL_OK)
{
return HAL_FLASH_GetError();
}
HAL_FLASH_Lock();
CreateWindow();
But if I bring the CreateWindow() function to top of the code, flash will not be cleared. Here is CreateWindow() function:
WM_HWIN CreateWindow(void)
{
hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);
hMultiPage = MULTIPAGE_CreateEx(0, -20, 320, 260, WM_GetClientWindow(hWin), WM_CF_SHOW, 0, 0);
MULTIPAGE_SetSkin(hMultiPage,MULTIPAGE_SKIN_FLEX);
hPage1 = GUI_CreateDialogBox(_aDialogCreate2, GUI_COUNTOF(_aDialogCreate2), _cbDialogPage1, WM_UNATTACHED, 0, 0);
MULTIPAGE_AddPage(hMultiPage, hPage1, "Settings");
hPage2 = GUI_CreateDialogBox(_aDialogCreate3, GUI_COUNTOF(_aDialogCreate3), _cbDialogPage2, WM_UNATTACHED, 0, 0);
MULTIPAGE_AddPage(hMultiPage, hPage2, "Run");
MULTIPAGE_SelectPage(hMultiPage,0);
return hWin;
}
These 2 things has nothing to do between.
There could be a problem, however, that you want to delete sector where data are located (maybe images or similar) for your GUI.
Technically, there is no other option here.
Also, check which value "SectorError" variables has after function completes?

how to change Text to Speech voice and how to insert characters into char array

I need to change the voice of the Text To Speech engine. When a menu is selected (ID_SPEAK_PLAY), I get the text of an edit box and simply read it.
My situation can be solved in two ways :
insert the XML code at the begining of ptrData without using strncat or other functions that involve creating other wchar_t* buffers (memory issues ). StringCchPrintf is not working.
change the voice in some other way that i don't know.
Here is my code :
case ID_SPEAK_PLAY:
text_size = SendMessage(h_edit, WM_GETTEXTLENGTH, 0, 0);
text_size += 100;
ptrData = new wchar_t[text_size];
SendMessage(h_edit, WM_GETTEXT, text_size, (LPARAM)ptrData);
StringCchPrintf(ptrData, text_size, L"<voice required = \"Gender=Female;Age=Teen\"> %s", ptrData);
pVoice->Speak(ptrData, SPF_ASYNC | SPF_IS_XML, NULL);
delete [] ptrData;
break;
StringCchPrintf is not working.
That is because you ignored the warning in the documentation:
Behavior is undefined if the strings pointed to by pszDest, pszFormat, or any argument strings overlap.
You are specifying ptrData as both pszDest and an argument string, so your code has undefined behavior. You must use separate buffers when using StringCchPrintf():
case ID_SPEAK_PLAY:
text_size = SendMessage(h_edit, WM_GETTEXTLENGTHW, 0, 0) + 1;
ptrData = new wchar_t[text_size];
SendMessage(h_edit, WM_GETTEXTW, text_size, (LPARAM)ptrData);
speak_size = text_size + 100;
speakData = new wchar_t[speak_size];
StringCchPrintf(speakData, speak_size, L"<voice required = \"Gender=Female;Age=Teen\"> %s", ptrData);
pVoice->Speak(speakData, SPF_ASYNC | SPF_IS_XML, NULL);
delete [] speakData;
delete [] ptrData;
break;
Alternatively, just skip StringCchPrintf() and let WM_GETTEXT populate your single buffer directly:
case ID_SPEAK_PLAY:
{
const wchar_t *xml = L"<voice required = \"Gender=Female;Age=Teen\"> ";
const int xml_size = lstrlenW(xml);
text_size = SendMessage(h_edit, WM_GETTEXTLENGTHW, 0, 0);
ptrData = new wchar_t[text_size + xml_size + 1];
lstrcpyW(ptrData, xml);
SendMessage(h_edit, WM_GETTEXTW, text_size+1, (LPARAM)(ptrData+xml_size));
pVoice->Speak(ptrData, SPF_ASYNC | SPF_IS_XML, NULL);
delete [] ptrData;
break;
}
change the voice in some other way that i don't know.
Instead of inserting XML in front of your text, you can call the ISpVoice::SetVoice() method before calling ISpVoice::Speak(). Use SpEnumTokens() to know which voices are installed, or use SpFindBestToken() to search for a voice that matches the criteria you need.

How to save specific part of a TRichEdit text RTF (c++ codegear)

I'm creating a search features in an existing c++ CodeGear project.
When you double-click a word, the background of all occurrences of the word is paint in green like in notepad++.
Before color is applied, I'm saving the original TRichEDit text in a TMemoryStream to be able to get the original text back. I reset the color back to normal on click event in the TRichEdit.
I would like to know if there is a way to save each occurrence of the search word in a TMemoryStream or maybe by using a Message like EM_STREAMOUT?
Right now everything works fine, but when the TRichEdit text is too big, it takes forever to reload the big memo of all the text. I think it would be better to only remember the color of the word that changed rather then reload all the text.
I'm really a beginner in programming, any help is appreciate. Tell me if it's not clear enough.
Here is my code that is working and putting background color to occurrences of word:
`
void SearchInText::searchWordInText(TRichEdit* reTextToSearch, AnsiString strWordToFind)
{
lstOccurrences->Clear(); //reset lst
strTextToParse = AnsiReplaceText(strTextToParse, "\r\n", "\n");
int nPrevTagPos = 0;
int nTagPos = strTextToParse.AnsiPos(strWordToFind);
while (nTagPos != 0)
{
int nPosMin = nPrevTagPos + nTagPos - 1;
//List of all the occurrence in the TRichEdit with their position in the text
//It's not a list of int, but it POINT to adresses of INT so it's the same result =)
lstOccurrences->Add((TObject*) nPosMin);
//Change color of background when there is an occurrence
changeBgColor(reTextToSearch, strWordToFind, nPosMin +1, 155, 255, 155); //lime
bColorWasApplied = true;
nPrevTagPos = nPosMin + strWordToFind.Length();
strTextToParse = strTextToParse.SubString(nTagPos + strWordToFind.Length(), strTextToParse.Length());
nTagPos = strTextToParse.AnsiPos(strWordToFind);
}
}
`
Try something like this:
#include <vector>
struct WordOccurrence
{
CHARRANGE Range;
CHARFORMAT2 OriginalFormat;
};
std::vector<WordOccurrence> WordOccurrences;
void TMyForm::HighlightWords(const String &WordToFind)
{
// disable the RichEdit's notification messages
int OriginalEventMask = RichEdit1->Perform(EM_SETEVENTMASK, 0, 0);
// disable the RichEdit's painting
RichEdit1->Perform(WM_SETREDRAW, FALSE, 0);
// save the RichEdit's current selection
CHARRANGE OriginalSelection;
RichEdit1->Perform(EM_EXGETSEL, 0, (LPARAM)&OriginalSelection);
// assign values to use while searching
int WordLen = WordToFind.Length();
int TextLen = RichEdit1->GetTextLen();
TSearchTypes SearchTypes = TSearchTypes() << stWholeWord << stMatchCase;
// find the first occurrence of the word
int StartPos = RichEdit1->FindText(WordToFind, 0, TextLen, SearchTypes);
while (StartPos != -1)
{
WordOccurrence Occurrence;
Occurrence.Range.cpMin = StartPos;
Occurrence.Range.cpMax = StartPos + WordLen;
// select the word
RichEdit1->Perform(EM_EXSETSEL, 0, (LPARAM)&Occurrence.Range);
// get the word's current formatting
Occurrence.OriginalFormat.cbSize = sizeof(CHARFORMAT2);
RichEdit1->Perform(EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&Occurrence.OriginalFormat);
// save it for later
WordOccurrences.push_back(Occurrence);
// set the word's new formatting
CHARFORMAT2 NewFormat = Occurrence.OriginalFormat;
NewFormat.dwMask |= (CFM_COLOR | CFM_BACKCOLOR);
NewFormat.crTextColor = ...;
newFormat.crBackColor = ...;
RichEdit1->Perform(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&NewFormat);
// find the next occurrence of the word
StartPos = RichEdit1->FindText(WordToFind, Occurrence.Range.cpMax, TextLen - Occurence.Range.cpMax, SearchTypes);
}
// restore the RichEdit's original selection
RichEdit1->Perform(EM_EXSETSEL, 0, (LPARAM)&OriginalSelection);
// re-enable the RichEdit's painting
RichEdit1->Perform(WM_SETREDRAW, TRUE, 0);
RichEdit1->Invalidate();
// re-enable the RichEdit's notification messages
RichEdit1->Perform(EM_SETEVENTMASK, 0, OriginalEventMask);
}
void TMyForm::RestoreHighlightedWords()
{
// are there any occurrences to restore?
if (WordOccurances.empty())
return;
// disable the RichEdit's notification messages
int OriginalEventMask = RichEdit1->Perform(EM_SETEVENTMASK, 0, 0);
// disable the RichEdit's painting
RichEdit1->Perform(WM_SETREDRAW, FALSE, 0);
// save the RichEdit's current selection
CHARRANGE OriginalSelection;
RichEdit1->Perform(EM_EXGETSEL, 0, (LPARAM)&OriginalSelection);
// restore the formatting of each occurrence
for (std::vector<WordOccurrence>::iterator iter = WordOccurrences.begin();
iter != WordOccurrences.end();
++iter)
{
WordOccurrence &occurrence = *iter;
// select the word
RichEdit1->Perform(EM_EXSETSEL, 0, (LPARAM)&occurrence.Range);
// restore the word's original formatting
RichEdit1->Perform(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&occurrence.OriginalFormat);
}
// clear the list
WordOccurances.clear();
// restore the RichEdit's original selection
RichEdit1->Perform(EM_EXSETSEL, 0, (LPARAM)&OriginalSelection);
// re-enable the RichEdit's painting
RichEdit1->Perform(WM_SETREDRAW, TRUE, 0);
RichEdit1->Invalidate();
// re-enable the RichEdit's notification messages
RichEdit1->Perform(EM_SETEVENTMASK, 0, OriginalEventMask);
}
Ok, so I finally get it!
I add a struct in my .h
Inside it, I store:
-the position in the TRichEdit of the word found (nStart)
-the length of the word (nLength)
-the actual text and his RTF
struct sSelectedWord : public TObject
{
public:
__fastcall ~sSelectedWord(); //destructor
int nStart;
int nLength;
TMemoryStream* memoRTF;
};
Here is the code that save the RTF of my TRichEdit in my struct I just created in the .h.
void SearchInText::searchWordInText(TRichEdit* reTextToSearch, AnsiString strWordToFind)
{
lstOccurrences->Clear(); //reset lst
lstOccWithRTFMemo->Clear();
nCountOccurrence = 0;
strTextToParse = AnsiReplaceText(strTextToParse, "\r\n", "\n");
int nPrevTagPos = 0;
int nTagPos = strTextToParse.AnsiPos(strWordToFind);
while (nTagPos != 0)
{
int nPosMin = nPrevTagPos + nTagPos - 1;
//List of all the occurrence in the TRichEdit with their position in the text
//It's not a list of int, but it POINT to adresses of INT so it's the same result =)
lstOccurrences->Add((TObject*) nPosMin);
nCountOccurrence++;
//selected the word in the TRichEdit to save it with is RTF
reTextToSearch->SelStart = nPosMin;
reTextToSearch->SelLength = strWordToFind.Length();
TMemoryStream* memo = new TMemoryStream;
//important part!
rtfSaveStream(reTextToSearch,memo);
sSelectedWord* currentWord = new sSelectedWord;
currentWord->nStart = nPosMin;
currentWord->nLength = strWordToFind.Length();
currentWord->memoRTF = memo;
//Here we go, we add our new object in a list to be able to loop through it when we will want to reset the color
lstOccWithRTFMemo->Add(currentWord);
lstOccWithRTFMemo->Count;
//Change color of background when there is an occurrence
changeBgColor(reTextToSearch, strWordToFind, nPosMin +1, 155, 255, 155); //lime
bColorWasApplied = true;
nPrevTagPos = nPosMin + strWordToFind.Length();
strTextToParse = strTextToParse.SubString(nTagPos + strWordToFind.Length(), strTextToParse.Length());
nTagPos = strTextToParse.AnsiPos(strWordToFind);
}
}
The important part was done with the EM_STREAMOUT message.
void SearchInText::rtfSaveStream(TRichEdit* re, TMemoryStream* memo)
{
// Create an instance of an EDITSTREAM that will contain:
// - The detail of our callback (StreamInCallback)
// - The TMemoryStream that contains the text to paste
EDITSTREAM es = {0};
ZeroMemory(&es, sizeof(es));
es.dwCookie = (DWORD_PTR) memo;
es.dwError = 0;
es.pfnCallback = &StreamSaveInCallback ; //pointer to function callBack
//To save the selected word of the TRichEdit, use STREAMOUT
re->Perform(EM_STREAMOUT, SF_RTF | SFF_SELECTION, (LPARAM)&es);
}
DWORD CALLBACK SearchInText::StreamSaveInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
TMemoryStream *memo = (TMemoryStream*)dwCookie;
memo->Write(pbBuff, cb);
*pcb = cb;
return 0;
}
In the pbuff, you can see the word that you selected in your TRichEdit with his RTF!
Hope it will help others with the same issue! Thanks for those who suggested code =)

Windows API Combobox data not being inserted

I'm trying to assign an int to each item in a ComboBox.
Here's my code:
int itemIndex = ComboBox_InsertString(hComboBox, -1, strData);
if (itemIndex > -1)
{
// idataIndex is always 0
// and ComboBox_GetItemData() always returns 0 regardless of what intData is
int idataIndex = ComboBox_InsertItemData(hComboBox, itemIndex, intData);
}
Am I doing something wrong? This seems pretty straightforward.
EDIT:
Figured it out, ComboBox_InsertItemData sends CB_INSERTSTRING instead of CB_SETITEMDATA. I needed to send CB_SETITEMDATA to set the item data.
Have you tried to use ComboBox_SetItemData instead? This macro sends the CB_SETITEMDATA