Getting a pointer to my XDisplay (Linux) (X)? - c

I need to be able to access the X Event Loop to add clipboard support for a game API. The problem is the game API does not know which API it will use for display (It could use SDL or other). As a result, I do not have direct access to the X event loop. Is there a function in XLib to get a pointer to my display so that I can process messages and add clipboard support?
Thanks

If it runs on X11, there has to be a Display pointer in the graphics object somewhere. You can allocate a new one with XOpenDisplay(NULL); but that's not likely to achieve what you want. You'd still have to find the Windows and other info which is tricky enough when a program does it once.
You really need to dig through the existing code to find the X11 module. There's likely to be a single function that performs on iteration of the "Event Loop" as a subroutine of the real "main Processing Loop". If you can't simply add your new code there, you can at least see how the program already accesses this information.

If you're using OpenGL for graphics, you can exploit that. At some point in the program where you know, the OpenGL context is made current call glXGetCurrentDisplay. However you should be carefull not to interfere with the programs main event loop.

Related

What's the proper usage of Fibers in Windows for C?

I have been recently interested about Fibers in Windows, but I have hard time using it. The documentation involves function definitions and some example, but still some stuff are not clear to me. I see that CreateFiber definition is defined as:
LPVOID CreateFiber(
SIZE_T dwStackSize,
LPFIBER_START_ROUTINE lpStartAddress,
LPVOID lpParameter
);
So, we specify the stack size, the function for the fiber and possibly a parameter for the function. Now, my questions are:
1) Once fiber is created, I assume the provided functions execution doesn't immediately start, right? I believe one needs to call ConvertThreadToFiber first. But are there any other stuff needed to be done? I mean in the simplest case, how does defining, initiating, running and deleting a simple fiber looks like?
2) Is it possible somehow to check whether we are actually in the fiber? I mean whether fiber is executing inside some other part of the app? If yes, how?
3) Is it possible to get the memory location of the fiber's stack and the actual content of the fiber's stack at any moment we wish? If yes, how?
(Disclaimer: I've only written a few test programs that use fibers in order to verify that they were working properly while running under a performance profiler that I was working on at the time.)
1) As you say, a fiber does not run by itself. It only runs when another thread explicitly switches to it by calling SwitchToFiber. Execution then continues on that fiber until it calls SwitchToFiber and switches back to the original thread or another fiber.
2) It's unclear to me what you are asking here. If the fiber is the only one calling a particular function it can set some variable or call a function and you'll know it was there. If multiple fibers are calling the same function, maybe they could record their thread id and you'd be able to infer which fiber called the function. What's the use case here?
3) If the fiber is executing, it has access to its stack/registers in the normal way. I am not aware of a way to arbitrarily access the stack of a fiber that isn't currently scheduled to run on a thread, but I suppose you could record the address of the stack from within the fiber itself.
For what it's worth, I don't think the fiber support in Windows API is used much.

Cocoa system() progress?

I am creating an application that must use the system(const char*) function to do some "heavy lifting", and I need to be able to give the user a rough progress percentage. For example, if the OS is moving files for you, it gives you a progress bar with the amount of data moved and the amount of data to move displayed on the window. I need something like that. How can this be done?
Edit: Basically, I give the user the option to save files in a compressed format. If they do so, it saves normally then runs this code:
char* command = (char*)[[NSString stringWithFormat:#"tar -jcvf %#.tar.bz2 %#", saveurl.path, filename] cStringUsingEncoding:NSUTF8StringEncoding];
system(command);
Sometimes this takes a little while (the app deals with video files), so I want to be able to give them an estimated completion time.
I am creating an application that must use the system(const char*)
function to do some "heavy lifting"
No, it doesn't have to use system() as such. In fact, it shouldn't. There are plenty of other APIs for running subprocesses, almost all of which will be better. In Cocoa, the most obvious better option is NSTask.
In any case, there's nothing that can tell how much progress a subprocess is making except that subprocess itself. If the program you're running doesn't provide a means for reporting progress, there's little hope. Nothing else can even divine what the purpose or goal of the subprocess is, let alone judge how far along it is to meeting that goal.
Even if the program does report progress, you'll need a means to receive that information. system() doesn't allow for that. NSTask does, as would popen() or manually forking and execing the program.
You would need a command line program that has a way of communicating progress information back to your application (or perhaps simply write progress info to a log file that you parse in your cocoa app). Are you sure you really need to do this?
For your edited example, you might consider just putting up some sort of spinner or hourglass type UI indicator to show them that the write is in progress, while allowing them to continue with other work. You can't predict archive creation time, especially when you add compression to it.

How to trap read write system calls?

Whenever i attempt to write anything on my pendrive, a write system call is generated. What i want to do is, this write call should be trapped and and the user should be requested to input predecided password( which i can define during coding itself).
Please tell me whether this is possible or not? and if yes than how should i do it?
The windows DDK has an example of hooking the file reads/writes/copies in filesys\minifilter, with both pre and post op callbacks, that should have you set for the kernel side of things. For the gui part you'll need something to do a non-blocking spin till the drives signals an event, you'll probably also want a pipe or mapped memory view to pass data around
EasyHook is supposed to give you the ability to hook kernel functions. I have not tried it, so your mileage may vary. Be sure to hook functions cautiously - you may degrade the performance of your machine to a point where it's unusable. What you want is to interact with the user, meaning that you must put the hooked function on hold, and issue a callback into user space. This is probably not an exercise for mere mortals.
At any rate, good luck!

force a blue screen (from a simple driver bug?) in C code [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Simulating a BlueScreen
Hello SO,
I'm trying to induce a BSOD somehow inline in my C code. My main background is Java but have been fortunate to have been tutored by some coworkers and am helping out with a simple C utility.
There's two sections:
1) write to a hard drive (I finished this, wasn't too bad)
2) Force a blue screen immediately after sending the last SCSI write command
You can probably tell the intent of the program easily now.
I've tried two things so far:
1) Externally calling pskill.exe (windows utility) to manually crash csrss.exe which forces a blue screen every time since csrss.exe is a required service of windows. This doesn't work because it's not fast enough. The call to the external utility takes too long so we need inline code to compile with the write to disk section in order to crash the computer fast enough.
2) Use the windows.h API to call TerminateProcess: http://msdn.microsoft.com/en-us/library/ms686714%28v=vs.85%29.aspx
The problem is this function cannot end system related tasks so it can't close csrss.exe
This has left me short on options. I need a clever way to kill csrss.exe in our own native code without an external call or a clever way to force a blue screen in internal code OR I need a very simple driver I can load and call which will blue screen the machine immediately. Could be as short as 1 line calling KeBugCheck http://msdn.microsoft.com/en-us/library/ff551948.aspx
Thanks for your time and input.
Your best bet is to write a trivial driver that calls KeBugCheck() as you yourself suggest. You can take the most simple example from the Windows Driver Kit and cut it down to the barebones.
I recomment Not My Fault from sysinternals.
Here are two ways to get a blue screen when running in kernel mode:
Dereference a null pointer, or
Divide by zero

How to grab 'key-press-event' when the window is not focused?

I am writing a program using gtk. What the program does is monitor the keystroke the user entered and play a sound. My question is that how do I catch the key-press-event when the window is not focused? I'm planning to let my program stay in tray icon, so I wonder how I can grab any key-press-event from there. Thanks
Edit:
I finally find a way to do it - The XTest extension, I found the a piece of code snippet from the program 'xmacro'.
You can see my implementation here:
http://github.com/Aitjcize/Qwertickle/blob/master/src/qwertickle.c
btw, it's still quite buggy, maybe someone can help me out? :)
As Matt Joiner said,
This kind of thing isn't as easy in Linux.
and unfortunately GTK+ can't do this kind of magic.
You should take a look at XEvIE - X Event Interception Extension - it will make your job easier.
XEvIE is a X extension providing functionalities to allow users intercept keyboard/mouse events.
And as suggested by this guy, another way to go would be to use XGrabKey()/XUngrabKey() from X11. I believe that tinywm shows how to use it correctly.
There is a program called xbindkeys that can bind mouse and keyboard keys in X to launch shell commands. You can either utilize this to send commands to your program, or look at the sourcecode to see how its done: xbindkeys
You can also directly open /dev/input/eventX and read() from it to an input_event struct, but thats a little nasty, because you need to have the proper rights (normally root, or change it with chmod)

Resources