Identify stylus barrel button (win32) - c++

I am creating an application that uses the tablet API for windows and so far I was able to capture the input of the stylus succesfully. I am able to handle the movement, stylus down and up events and pressure. What I am still missing is the ability to identify the barrel button pressed. While I am capturing the StylusButtonDown event, this is fired also when the stylus is down on the tablet.
For reference here is my (ideal) implementation:
STDMETHOD(StylusButtonDown)(IRealTimeStylus* rts, STYLUS_ID id, const GUID* guid, POINT* pt)
{
if (BARREL_BUTTON_ID == *guid) {
s_buttonDown = true;
// DEBUG
std::cout << "[StylusButtonDown]" << std::endl;
// DEBUG
}
return S_OK;
}
The problem I have is to fill in the BARREL_BUTTON_ID constant. Any expert with an idea?
Thanks a lot!

Related

Reparenting wm and BadWindow errors

I'm writing a reparenting window manager in XCB and C++:http://ix.io/3yNo
At the moment it works pretty well, but occasionally when I close a window, all the windows of that application close because the process exits with a BadWindow. For example if I have a couple xfce4-terminal windows open, all managed by one process,and I close one, occasionally the application will close and I will get a BadWindow (invalid window parameter) error (in the app, not my wm). The very interesting thing is that this is not reproducible but kind of rare, probably a race condition between reporting the error and closing the window due to X11's asynchoronous nature.I have no clue where to begin debugging this, any tips?I kind of suspect it might be something in the Unmap O
Your link contains almost 500 lines of code. I am not going to try to fully understand that. Instead, I'll just randomly guess.
auto window_manager::handle_unmap_notify(xcb_unmap_notify_event_t *ev) -> void {
if (unmap_ignore > 0) {
unmap_ignore--;
return;
}
client *cl = nullptr;
size_t idx = 0;
for (client &c : clients) {
if (c.window == ev->window) {
cl = &c;
break;
}
idx++;
}
if (not cl)
return;
xcb_destroy_window(conn, cl->frame);
clients.erase(clients.begin() + idx);
}
You are destroying windows that are not yours. When the owner of the window accesses it the next time, it will get a BadWindow error.
Instead, you should check a window's WM_PROTOCOLS property and check for WM_DELETE_WINDOW. If that is present, you are supposed to send a WM_DELETE_WINDOW message to the window. See ICCCM ยง 4.2.8.1: https://tronche.com/gui/x/icccm/sec-4.html#s-4.2.8.1

Exeception in application when Threading used to saveImage simultaneously from multiple IP Camera

I am working on application where I connect the application and display multiple video feed from IP cameras. I am able to get the video feed which is too laggy(working to get solution to remove the lag). And in the application I have Button which when clicked it takes 50 pictures from all the IP cameras connected. But the code I have implemented gives an exception when threading is implemented. When used without threading it works fine.Heres the code for the Button Event.
void CDialogBasedVideoDlg::OnBnClickedButtonTakePic()
{
int nIndex = m_CamListBox.GetCurSel();
CStaticEx *objStaticEx = (CStaticEx*)m_StaticArray.GetAt(nIndex);
objStaticEx->StartThreadToCaptureUSBCam();//threading implementation gives exception.
//objStaticEx->CapturePicture();//this func works fine(without threading)
// TODO: Add your control notification handler code here
}
I have overrriden Static class which dynamically creates a picture control and displays the live video feed, threading is implemented in this class where the images are saved. Here's the code for capturing images and threading function.
void CStaticEx::CapturePicture(void)
{
CString csFileDir;
CString csFileName;
csFileDir.Format(DIR_USB_CAM_NAME,m_IpAddr);
if(IsDirExist(csFileDir)== false){
CreateDirectory(csFileDir, NULL);
}
CString csStr = csFileDir;
csStr += RANDOM_FILE_SEARCH;
int nNoOfFile = CountFileNumInDir((char*)csStr.GetBuffer());
csFileDir += DBL_SLASH;
int i = 0;
do{
csFileName.Format(FILE_NAME, csFileDir, (m_nCamID+1));
CString csCount;
csCount.Format(_T("%d"),(nNoOfFile+1));
csFileName += csCount;
csFileName += JPG;
m_pFrameImg = cvQueryFrame( m_pCamera ); //<----Exception come at this point
if(m_pFrameImg){
cvSaveImage(csFileName, m_pFrameImg);
i++;
nNoOfFile++;
csFileName = _T("");
}
}while(i < 50);
}
Threading Control function.
void CStaticEx::StartThreadToCaptureUSBCam(){
THREADSTRUCT *_param = new THREADSTRUCT;
_param->_this = this;
AfxBeginThread(StartThread,_param);
}
UINT CStaticEx::StartThread (LPVOID param)
{
THREADSTRUCT* ts = (THREADSTRUCT*)param;
//AfxMessageBox("Thread Started");
ts->_this->CapturePicture();
return 1;
}
Exception thrown is as follows.
Windows has treggered a breakpoint in DialogBasedVideo.exe.
This may be due to a corruption of heap, which indicates a bug in DialogBasedVideo.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while dialogbasedvideo.exe has focus.
The output window may have more diagnostic information. How do i get rid of this exception.
All the experts out their please help me.I am using VS2010 and Windows7, OpenCv2.4.6 Thanks in advance.

Clipboard Shortcut/Hotkey binding with Qt outside of application

I've tried googling, and searching on this site about this but to no avail.
I am building a Clipboard related application for Windows using Qt, and one of the requirements for it to work right is to be able to register for keyboard events outside my Qt application, like ctrl + c, ctrl + v. (copy/paste). The only thing that I have found online is using an external plugin for Qt but the entire concept was not explained properly, so I hit a dead end.
Does anyone have an idea how I can do this? Again, I want to register shortcuts to my application that will occur outside the application itself.
Thanks in advance!
Binding clipboard shortcuts and binding shortcuts in general are as I have discovered, two different things. Related to Clipboard events, Qt provides access to a dataChanged() signal through its QClipboard class. Using that, you are able to know when the clipboard data has changed, and act accordingly, and should eliminate the need to perform a system-wide binding of the Copy/Paste shortcuts.
In order to register a global shortcut (in this case the need for ctrl + v), and this is platform specific like in my needs, one can use the RegisterHotKey function under Windows. The HWND requested as the first parameter can be obtained from the winId function that is provided by QWidget.
In order to accept the WM_HOTKEY event, one would have to implement the winEvent virtual protected function under Qt <= 5.0, and nativeEvent on >= 5.0.
This depends on the Desktopenvironment the application runs in and it is OS-specific. If you intend to run the application within KDE you can register global hotkeys easyly by deploying a .desktop File with your Application or by adding things to /usr/share/kde4/apps/khotkeys .
Within Windows, the easiest way would probably be adding a registry key to the registry in order to register a global Hotkey. See MSDN
I think you're just confused about how the clipboard works. You never need to register clipboard-related hotkeys outside of your application. Those are handled by other applications. What those applications do is interact with the system-wide clipboard. Your application needs to interact with the same clipboard and get notifications about new clipboard data being available etc.
You might get more helpful answers if you tell us what you mean by a "clipboard related" application. Is it used to sell wooden clipboards? Or to calibrate clipboard springs? Or to manage inventory of clipboards? Or does it run on a digital clipboard? Sigh.
This should get you up and running in Windows+Qt and HotKeys pretty quickly.
I haven't tried the Qt eXTension library with QxtGlobalShortcut, but it sounds like it may be a more elegant complete solution for more platforms. (like in #TimMeyer's comment to your question)
https://stackoverflow.com/a/3154652/808151
I wrote up this function to listen for a single system wide hotkey in windows.
#ifndef HOTKEYTHREAD_H
#define HOTKEYTHREAD_H
#include <QThread>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
class HotKeyThread : public QThread
{
Q_OBJECT
public:
HotKeyThread(QObject *parent);
~HotKeyThread();
signals:
void hot_key_event(int);
public slots:
void run();
void stop();
private:
volatile bool m_stopped;
DWORD m_thread_id;
};
#endif // HOTKEYTHREAD_H
.cpp file
#include "hotkeythread.h"
#include <QDebug>
#include <process.h>
#define WM_END_THREAD (WM_USER+2)
HotKeyThread::HotKeyThread(QObject *parent)
: QThread(parent)
{
this->m_thread_id = 0;
}
HotKeyThread::~HotKeyThread()
{
}
void HotKeyThread::stop()
{
if(this->m_thread_id != 0)
::PostThreadMessage(this->m_thread_id, WM_END_THREAD, 0, 0);
}
//
void HotKeyThread::run()
{
// store a thread id so we can exit later
m_thread_id = ::GetCurrentThreadId();
qDebug() << "ThreadIDs" << QString::number(m_thread_id, 16) << QString::number((int) this->currentThreadId(), 16);
// register an atom, and a hotkey
BOOL retVal;
int counter = 0;
int magic_num = 1128;
ATOM id = ::GlobalAddAtom(MAKEINTATOM(magic_num + counter++));
// http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
int modifier = 0x0;// modify this line
int key = VK_NUMPAD0;// modify this line
if(QSysInfo::windowsVersion() > QSysInfo::WV_VISTA)
{
retVal = ::RegisterHotKey(NULL, id, modifier | MOD_NOREPEAT, key);
}
else
{
// No repeat is only supported in 7 and later
retVal = ::RegisterHotKey(NULL, id, modifier, key);
}
if(retVal)
{
qDebug() << "Successfully added a HotKey!";
}
else
{
qDebug() << "Failed to add a hotkey!";
return;
}
// wait on hotkeys
MSG msg = {0};
while (0 < ::GetMessage(&msg, NULL, 0, 0))
{
if(msg.message == WM_HOTKEY)
{
bool control = LOWORD(msg.lParam) & MOD_CONTROL;
bool shift = LOWORD(msg.lParam) & MOD_SHIFT;
bool alt = LOWORD(msg.lParam) & MOD_ALT;
bool win = LOWORD(msg.lParam) & MOD_WIN;
qDebug() << "HotKey!" << (control ? "Ctrl +": "")
<< (alt ? "Alt +": "")
<< (shift ? "Shift +":"")
<< (win ? "Win +":"") << QString::number(HIWORD(msg.lParam),16);
// TODO Notify MainWindow of the event
emit hot_key_event(msg.lParam);
}
else if(msg.message == WM_END_THREAD)
{
// exit
break;
}
}
// Clean up Hotkey
::UnregisterHotKey(NULL, id);
::GlobalDeleteAtom(id);
}
Usage in your GUI
// Start HotKey Thread!
m_hot_key_thread = new HotKeyThread(this);
QObject::connect(m_hot_key_thread, SIGNAL(hot_key_event(int)),
this, SLOT(handle_hot_key_event(int)), Qt::QueuedConnection);
m_hot_key_thread->start();
and when your program is closing use
m_hot_key_thread->stop();
Hope that helps.

C++ Finding Index for Font temporarily added to System Font Table with AddFontResource() to use in Console

I am trying to temporarily install a font to use in the win32 console with
int AddFontResource(LPCTSTR lpszFilename);
and
BOOL WINAPI SetConsoleFont(HANDLE hOutput, DWORD fontIndex)
I got hold of this function from this site.
Although both functions seem to work fine I have no idea how to find the added font index to use with SetConsoleFont.
AddFontResource returns no index value or key to the temporary font.
Here is my relevant code:
#include "Level.h"
#include "ConsoleFont.h" //acquired from above mentioned site
#include <Windows.h>
//-------------------------------------------------------------------------------
void init();
void cleanup();
int main()
{
FileManager *pFileManager = new FileManager(); //unrelated
Level *lvl1 = new Level("filename",pFileManager); //unrelated
///TEMPORARY PLANNING
// using add font resource. how can i get this fonts index value?
int err = AddFontResource(L"Files/gamefont.fnt");
if (err == 0)
{
MessageBox(NULL,L"loading font failed",L"Error",0);
}
else
{
wchar_t message[100];
swprintf_s(message,100,L"AddFontResource returned: %d",err);
MessageBox(NULL,LPTSTR(message),L"error",0);
}
SendMessage(HWND_BROADCAST, WM_FONTCHANGE,0,0);
//acquiring handle to current active screen buffer
HANDLE tempHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (tempHandle == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,L"Failed to aquire Screen Buffer handle",L"Error",0);
}
//I dont know what to set this to. this is the crux of the problem.
DWORD fontIndex = 1;
if (FALSE == SetConsoleFont(tempHandle,fontIndex))
{
MessageBox(NULL,L"loading console font failed",L"Error",0);
}
//draws a house when in correct font
std::cout<<"!!!!!!!!#\n"
<<"!!!!!!!!!\n"
<<"! !! !! !\n"
<<"!!!!!!!!!\n"
<<"! !! !! !\n"
<<"!!!!!!!!!\n"
<<"! !! !! !\n"
<<"!!!!!!!!!\n"
<<"! !! !! !#\n"
<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<std::endl;
///PLANNING OVERS
bool quit = false;
while(!quit)
{
//still to be implemented
}
err = RemoveFontResource(L"Files/gamefont.fnt");
if (err==0)
{
MessageBox(NULL,L"removing font failed",L"Error",0);
}
return 0;
}
I don't know how to go about finding my new font's index value or even if this is possible using my current method.
If someone knows or has a better method please help me out.
any help or hints are appreciated. It must possible to use a custom font in the win32 Console without fiddling with the registry. I'm sure of it :)
Unfortunately you entered the dark world on Win APIs. There is no documentation (or atleast I could never find it) for a console font table lookup. You can try the method "GetNumberOfConsoleFonts()" to see what is returned. I think the font at index 10 is Lucida Console. You'll have to play around a little. Also, this may not work for the OS version you have. Worked for me on XP. Never had to try on anything else. And honestly, never got it fully working on XP too.
For the registry,
Fonts registries are here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts
Console registries are here:
HKEY_CURRENT_USER\Console
If you end up modifying the registry, the changes may not be reflected immediately. You need to either restart the console or send a special WM_* message (sorry don't remember the name).
Will be great if you can find a solution/workaround :)
int err = AddFontResource(L"Files/gamefont.fnt");
if (err == 0)
{
MessageBox(NULL,L"loading font failed",L"Error",0);
}
else
{
wchar_t message[100];
swprintf_s(message,100,L"AddFontResource returned: %d",err);
MessageBox(NULL,LPTSTR(message),L"error",0);
}
this is wrong AddFontResource returns the number of fonts loaded, so the code in the ELSE doesn't make sense.

How do I read the pressure value from a graphics tablet stylus in Linux?

I am trying to add pressure sensitivity support to Synergy on Linux. I
believe the first step should be to detect the pressure value on the
server side. The stylus movement comes in as a MotionNotify event when
XNextEvent is called. However, this line does not output a pressure
value when the stylus is used:
case MotionNotify:
XDeviceMotionEvent* motionEvent = reinterpret_cast<XDeviceMotionEvent*>(xevent);
LOG((CLOG_INFO "tablet event: pressure=%d", motionEvent->axis_data[2]));
To solve this, I guessed that I might not be "subscribed" to such
info, so following some examples I found on the web, I have attempted
to open the Wacom device:
void
CXWindowsScreen::openWacom()
{
// init tablet (e.g. wacom)
int deviceCount;
XDeviceInfo* deviceInfo = XListInputDevices(m_display, &deviceCount);
for (int i = 0; i < deviceCount; ++i) {
if (CString(deviceInfo[i].name).find("stylus") != CString::npos) {
LOG((CLOG_INFO "tablet device: name='%s', id=%d",
deviceInfo[i].name, deviceInfo[i].id));
XDevice* tabletStylusDevice = XOpenDevice(m_display, deviceInfo[i].id);
if (tabletStylusDevice == NULL) {
LOG((CLOG_ERR "failed to open tablet device"));
return;
}
XEventClass eventClass;
DeviceMotionNotify(tabletStylusDevice, m_tabletMotionEvent, eventClass);
XSelectExtensionEvent(m_display, m_window, &eventClass, 1);
LOG((CLOG_INFO "tablet motion event=%d class=%d",
m_tabletMotionEvent, eventClass));
}
}
XFreeDeviceList(deviceInfo);
}
This does indeed detect a Wacom device...
2012-01-30T11:15:59 INFO: tablet device: name='Wacom Intuos4 6x9 stylus', id=8
/home/nick/Projects/synergy/1.4/src/lib/platform/CXWindowsScreen.cpp,1144
2012-01-30T11:15:59 INFO: tablet motion event=105 class=2153
/home/nick/Projects/synergy/1.4/src/lib/platform/CXWindowsScreen.cpp,1157
But I have so far never received the stylus event 105
(m_tabletMotionEvent) from XNextEvent...
if (xevent->type == m_tabletMotionEvent) {
XDeviceMotionEvent* motionEvent = reinterpret_cast<XDeviceMotionEvent*>(xevent);
LOG((CLOG_INFO "tablet event: pressure=%d", motionEvent->axis_data[2]));
return;
}
In other words, the above if never evaluates to true.
I hope someone can help me with this, I've been trying to solve it for weeks.
The next challenge will be to fake the pressure level on the Synergy
client so that programs like GIMP will receive it (and I'm not even sure where to
start with that).