Sleeping display using IOKit on Lion - objective-c

I have the following code that is suppose to sleep the display on a Mac. I've tried it on Lion but it doesn't seem to do anything. I tested the code by creating a barebones window Mac app with a button in the window and an IBAction method. When the button is pressed, the function below is called, however nothing happens.
Any suggestions as to why it doesn't work?
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
int display_sleep(void)
{
io_registry_entry_t reg = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");
if (reg)
{
IORegistryEntrySetCFProperty(reg, CFSTR("IORequestIdle"), kCFBooleanTrue);
IOObjectRelease(reg);
}
else
{
return 1;
}
return 0;
}

Related

MSP430: Trying to learn interrupts using button and LED blinking

I'm learning the MSP430 for the first time, and trying to teach myself interrupts.
I'm trying to follow these examples 1 2 3 4.
I'm using an MSP430FR6989 eval board and writing the code in Code Composer Studio.
I'm trying to have the REDLED on the board toggle when I push the P1.1 button (ie, using an interrupt).
I'm able to blink the LEDs using separate code, so I know the board works.
This is the code that i'm trying to get to work.
#include <msp430.h>
#include "driverlib.h"
int main(void) //Main program
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= BIT0; // Set P1.0 to output and P1.3 to input direction
P1OUT &= ~BIT0; // set P1.0 to Off
P1IE |= BIT3; // P1.3 interrupt enabled
P1IFG &= ~BIT3; // P1.3 interrupt flag cleared
__bis_SR_register(GIE); // Enable all interrupts
while(1) //Loop forever, we'll do our job in the interrupt routine...
{}
}
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1OUT ^= BIT0; // Toggle P1.0
P1IFG &= ~BIT3; // P1.3 interrupt flag cleared
}
When I press the button, the LED doesn't turn on and I'm not sure why.
I'd appreciate any help!
To show a working LED Blink program as requested by user #CL
#include <msp430.h>
#include "driverlib.h"
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Disables the watchdog
PM5CTL0 &= ~LOCKLPM5; // allows output pins to be set... turning off pullups
P1DIR = BIT0; // Make a pin an output... RED LED
long x = 0; // Will be used to slow down blinking
while(1) // Continuously repeat everything below
{
for(x=0 ; x < 30000 ; x=x+1); // Count from 0 to 30,000 for a delay
P1OUT = BIT0; // Turn red LED light on
for(x=0 ; x < 30000 ; x=x+1); // Count from 0 to 30,000 for a delay
P1OUT ^= BIT0; // Turn off the red LED light
}
}
On the MSP430FR6989 LaunchPad, P1.3 is not connected to a button. Use P1.1 instead.
The button requires a pull-up resistor, so you have to configure it in P1REN and P1OUT.
It might be a good idea to configure the signal edge for the interrupt in P1IES.
You have to clear LOCKLPM5 to activate the port settings.
All this can be seen in the msp430fr69xx_p1_03.c example program.

SDL left mouse button event without press on startup

On the start-up of my SDL program a left mouse button event is registered without me pressing the physical left mouse button. The next physical left button press is not registered. All other keys and buttons work normal. When clicking the .exe the mouse is not located within the location of the window.
Here are the specifications of that event (taken from the SDL_Event struct):
type=1025 (SDL_MOUSEBUTTONDOWN)
timestamp=24
windowID=1
which=0
button=1 (SDL_BUTTON_LEFT)
state=1
clicks=1
x=0
y=0
I am on windows 8.1 compiling with MinGW (gcc version 4.8.1).
I have SDL version 2.0.3
This is my source code:
#include <stdio.h>
#include <SDL.h>
int main(int argc, char* argv[])
{
SDL_Window* window = NULL;
SDL_Surface* screen = NULL;
SDL_Event ev;
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
screen = SDL_GetWindowSurface(window);
while (1) {
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
return 0;
else {
if (ev.type == SDL_MOUSEBUTTONDOWN) {
if (ev.button.button == SDL_BUTTON_LEFT) {
printf("MOUSE DOWN\n");
printf("type=%d\n", ev.button.type);
printf("timestamp=%d\n", ev.button.timestamp);
printf("windowID=%d\n", ev.button.windowID);
printf("which=%d\n", ev.button.which);
printf("button=%d\n", ev.button.button);
printf("state=%d\n", ev.button.state);
printf("clicks=%d\n", ev.button.clicks);
printf("x=%d\n", ev.button.x);
printf("y=%d\n\n", ev.button.y);
}
}
}
}
SDL_UpdateWindowSurface(window);
SDL_Delay(5);
}
SDL_FreeSurface(screen);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I have tried to solve it by calling SDL_PumpEvents() and SDL_FlushEvents() and while this removed the first (erroneous) event, the second press was still not registered.
Something strange I noticed was that it worked as expected when I opened the program .exe by right clicking and then pressing 'open'.
It'd be very grateful if somebody could shed light on this issue.

Print complete WebKitWebView (not only visible part) into PDF with gtk3 and cairo

I want to save a webpage using webkit, gtk3 and cairo into a pdf.
What works: The visible part (visible in window) gets correctly printed into the pdf
What doesn't work, but should work: The invisible part (the part when you scroll down) should be printed into that pdf, too. Any ideas?
That's my code:
#include <gtk/gtk.h>
#include <webkit/webkit.h>
#include <cairo-pdf.h>
static void save_as_pdf (GtkWidget *widget, const char *filename)
{
GtkAllocation allocation;
printf("Saving PDF to file %s\n", filename);
gtk_widget_get_allocation(GTK_WIDGET(widget), &allocation);
printf("height: %d width: %d\n", allocation.height, allocation.width);
cairo_surface_t *surface = cairo_pdf_surface_create( filename, allocation.width, allocation.height);
cairo_t *cr = cairo_create(surface);
gtk_widget_draw(widget, cr);
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
static void notifyProgressCb(WebKitWebView* webView, GParamSpec* pspec, GtkWidget* window)
{
float progress = webkit_web_view_get_progress(webView);
printf("\x1b[1G\t\x1b[1G%f", progress * 100); fflush(stdout);
if (progress == 1.0)
save_as_pdf(window, "test.pdf");
}
int main (int argc, char *argv[])
{
GtkWidget *window;
WebKitWebView *webView;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 1024, 768);
webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
gtk_container_set_resize_mode(GTK_CONTAINER(webView), GTK_RESIZE_PARENT);
gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(webView));
g_signal_connect(webView, "notify::progress", G_CALLBACK(notifyProgressCb), webView);
webkit_web_view_load_uri(webView, "http://www.heise.de");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main();
return 0;
}
compile with:
gcc pkg-config --cflags --libs gtk+-3.0 pkg-config --libs --cflags webkitgtk-3.0 yourfile.c
Supposedly the cairo_create() method only draws the part of the WebView widget, which is visible inside the window. In this case, I would completely circumvent the WebkitGtk objects and include some code which downloads the web page, renders it and saves the output into a PDF file; HTML2PDF, for instance. It may be a dirty low-level approach, but it's more robust and easier to implement.
You are looking for this:
Save a page as a PDF:
screenshot.pl --output cpan.pdf http://search.cpan.org/

How can I make a screen blink in TSR program?

I have written a program which acts as a screensaver i.e after 10 seconds all the command prompt screen is cleared if no keyboard button is pressed. In short I have hooked Timer and Keyboard interrupt. But I want the program to show blinking screen i.e alternatively show all the text of command prompt and blank screen. But my current situation is that it keeps on showing a blank screen after 10 seconds if no button is pressed. How can I modify my program to show blinking screen alternating between cleared screen and textual data instead of just cleared screen. Here is my program:
#include <dos.h>
#include <conio.h>
void interrupt (*oldTimer)();
void interrupt (*oldKey)();
void interrupt newTimer();
void interrupt newKey();
char far *scr = (char far*) 0xB8000000;
int i, t=0, m=0;
char charscr[4000];
void main()
{
oldTimer=getvect(8);
oldKey=getvect(9);
setvect(8,newTimer);
setvect(9,newKey);
getch();
keep (0,1000);
}
void interrupt newTimer()
{
t++;
if((t>=182)&&(m==0))
{
for (i=0;i<4000;i++)
charscr [i]=*(scr+i);
for (i=0;i<=4000;i+=2)
{
*(scr+i)=0x20;
*(scr+i+1)=0x07;
}
t=0; m=1;
}
(*oldTimer)();
}
void interrupt newKey()
{
int w;
if(m==1)
{
for (w=0; w<4000;w++)
*(scr+w)=charscr[w];
m=0;
}
(*oldKey)();
}
Sorry for my poor indentation. I find it very hard on this site to indent the code.

CGEventPostToPSN() not working for mouse clicking

I need to send mouse click events to an arbitrary process (not necessarily the front one) without bringing that process's window to the front.
This code works for sending a mouse click and letting the window server send it to whatever process it decides has focus:
#include <ApplicationServices/ApplicationServices.h>
int
main()
{
CGEventRef down, up;
down = CGEventCreateMouseEvent(
NULL,
kCGEventLeftMouseDown,
CGPointMake(16, 36),
kCGMouseButtonLeft
);
up = CGEventCreateMouseEvent(
NULL,
kCGEventLeftMouseUp,
CGPointMake(16, 36),
kCGMouseButtonLeft
);
CGEventPost(kCGHIDEventTap, down);
CGEventPost(kCGHIDEventTap, up);
CFRelease(down);
CFRelease(up);
return 0;
}
I can send mouse click events via CGEventPost() just fine, but that requires the target process to have focus (which I am trying to avoid).
I can send keyboard events via CGEventPostToPSN() just fine, and as far as I can tell, mouse move events work too, what I'm having trouble with is mouse down/up events.
This code (pretty much the above, the only difference being that I specify myself which process to send the events to) does not work and I don't even know how to find out where it breaks down.
#include <ApplicationServices/ApplicationServices.h>
#include <Carbon/Carbon.h> /* for ProcessSerialNumber stuff */
#include <stdio.h>
int
main()
{
ProcessSerialNumber psn;
CGEventRef down, up;
if (GetFrontProcess(&psn) != noErr) {
printf("Unable to get front process\n");
return 1;
}
down = CGEventCreateMouseEvent(
NULL,
kCGEventLeftMouseDown,
CGPointMake(16, 36),
kCGMouseButtonLeft
);
up = CGEventCreateMouseEvent(
NULL,
kCGEventLeftMouseUp,
CGPointMake(16, 36),
kCGMouseButtonLeft
);
CGEventPostToPSN(&psn, down);
CGEventPostToPSN(&psn, up);
CFRelease(down);
CFRelease(up);
return 0;
}
I've been stuck on this for a few days now, and I can't seem to figure it out. According to the documentation this is (as far as I can tell) exactly how it is supposed to be done.
I have tested this code on Snow Leopard and Lion, with the same results.
Could somebody who has had success with clicking via CGEventPostToPSN() please shed some insight on the proper way to accomplish this?