Skip to content

Commit

Permalink
Fix: when switching window, the window thumbnail is pure black.
Browse files Browse the repository at this point in the history
* Use the new method to get the window thumbnail in the front end.
* Remove redundant debugging to win information.
* Adjust the indentation format.
  • Loading branch information
droiing committed Feb 2, 2018
1 parent 24889b7 commit 9aeedb0
Show file tree
Hide file tree
Showing 12 changed files with 551 additions and 464 deletions.
27 changes: 10 additions & 17 deletions alt_tab_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ bool InitUkwmPluginDBusComm(void)
/** Second step: try to get a connection to the given bus.*/
if (pProxy == NULL)
pProxy = ukwm_plugin_proxy_new_sync(pConnection,
G_DBUS_PROXY_FLAGS_NONE,
UKUI_PLUGIN_BUS_NAME,
UKUI_PLUGIN_OBJECT_PATH,
NULL,
&pProxyError);
G_DBUS_PROXY_FLAGS_NONE,
UKUI_PLUGIN_BUS_NAME,
UKUI_PLUGIN_OBJECT_PATH,
NULL,
&pProxyError);
if (pProxy == NULL)
{
g_print("InitDBusCommunication: Failed to create proxy. Reason: %s.\n", pProxyError->message);
Expand All @@ -137,7 +137,6 @@ bool InitUkwmPluginDBusComm(void)

void FinishUkwmPluginDBusComm(void)
{
//printf("DBus Debug: %s [%d]\n", __FUNCTION__, __LINE__);
g_object_ref(pConnection);
g_object_ref(pProxy);
pConnection = NULL;
Expand All @@ -154,13 +153,12 @@ GList *DBusGetAltTabList(void)
GError *error = NULL;

bRet = ukwm_plugin_call_get_alt_tab_list_sync(pProxy, &out_count,
&out_tab_list_gva, NULL, &error);
&out_tab_list_gva, NULL, &error);
if (bRet == FALSE)
{
//printf("Can't get anything.\n");
printf("Can't get anything.\n");
return NULL;
}
//printf("out_count = %d\n", out_count);

char *title_name = NULL;

Expand All @@ -173,18 +171,14 @@ GList *DBusGetAltTabList(void)
{
alt_tab_item *ati = new_alt_tab_item();

g_variant_get(_item, "(siiii)", &title_name,
g_variant_get(_item, "(siiiii)", &title_name,
&ati->xid,
&ati->width,
&ati->height,
&ati->x,
&ati->y);
ati->title_name = newstr(title_name);
tab_list = g_list_append(tab_list, ati);
// printf("Win[%02d]: %s (%03d, %03d) -> (%03d, %03d)\n",
// i + 1, ati->title_name,
// ati->x, ati->y,
// ati->x + ati->width,
// ati->y + ati->height);

g_variant_unref(_item);
i++;
Expand All @@ -199,10 +193,9 @@ void DBusActivateWindowByTabListIndex(int index)
{
gboolean bRet;

//printf("DBus Debug: %s [%d], index = %d\n", __FUNCTION__, __LINE__, index);
bRet = ukwm_plugin_call_activate_window_by_tab_list_index_sync(pProxy, index, NULL, NULL);
if (bRet == FALSE)
{
//printf("Can't activate window: [%d]\n", index);
printf("Can't activate window: [%d]\n", index);
}
}
150 changes: 75 additions & 75 deletions keyeventmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,108 +5,108 @@

KeyEventMonitor::KeyEventMonitor(QObject *parent) : QThread(parent)
{
//Whether the mouse is pressed, used to identify the drag and drop operation of the mouse
isRelease = false;
//Whether the mouse is pressed, used to identify the drag and drop operation of the mouse
isRelease = false;
}

void KeyEventMonitor::run()
{
// Open a connection to the X server that controls a display.
Display *display = XOpenDisplay(0);
// Open a connection to the X server that controls a display.
Display *display = XOpenDisplay(0);

if (display == 0)
{
fprintf(stderr, "unable to open display\n");
return;
}
if (display == 0)
{
fprintf(stderr, "unable to open display\n");
return;
}

// Initialize the parameters for the function of XRecordCreateContext.
// XRecordAllClients means recording all events of X Client.
XRecordClientSpec clients = XRecordAllClients;
// Initialize the parameters for the function of XRecordCreateContext.
// XRecordAllClients means recording all events of X Client.
XRecordClientSpec clients = XRecordAllClients;

// Control the range of recording events.
XRecordRange *range = XRecordAllocRange();
if (range == 0)
{
fprintf(stderr, "unable to allocate XRecordRange\n");
return;
}
// Control the range of recording events.
XRecordRange *range = XRecordAllocRange();
if (range == 0)
{
fprintf(stderr, "unable to allocate XRecordRange\n");
return;
}

// Initialize range.
// Only five types of events: KeyPress, KeyRelase, ButtonPress, ButtonRelease, MotionNotify.
memset(range, 0, sizeof(XRecordRange));
range->device_events.first = KeyPress;
range->device_events.last = MotionNotify;
// Initialize range.
// Only five types of events: KeyPress, KeyRelase, ButtonPress, ButtonRelease, MotionNotify.
memset(range, 0, sizeof(XRecordRange));
range->device_events.first = KeyPress;
range->device_events.last = MotionNotify;

// Create the record context by clients and range.
// The variable is as the paratemer of XRecordEnableContext for event loop.
XRecordContext context = XRecordCreateContext(display, 0, &clients, 1, &range, 1);
if (context == 0)
{
fprintf(stderr, "XRecordCreateContext failed\n");
return;
}
XFree(range);
// Create the record context by clients and range.
// The variable is as the paratemer of XRecordEnableContext for event loop.
XRecordContext context = XRecordCreateContext(display, 0, &clients, 1, &range, 1);
if (context == 0)
{
fprintf(stderr, "XRecordCreateContext failed\n");
return;
}
XFree(range);

// Flush the output buffer and then waits until all requests have been received and processed by the X server.
XSync(display, True);
// Flush the output buffer and then waits until all requests have been received and processed by the X server.
XSync(display, True);

// Returns a Display structure that serves as the connection to the X server and that contains all the information about that X server.
Display *display_datalink = XOpenDisplay(0);
if (display_datalink == 0)
{
fprintf(stderr, "unable to open second display\n");
return;
}
// Returns a Display structure that serves as the connection to the X server and that contains all the information about that X server.
Display *display_datalink = XOpenDisplay(0);
if (display_datalink == 0)
{
fprintf(stderr, "unable to open second display\n");
return;
}

// Call XRecordEnableContext to build the XRecord context and enter the event loop.
// The callback function will be evoked on receiving an event from X Server.
if (!XRecordEnableContext(display_datalink, context, callback, (XPointer)this))
{
fprintf(stderr, "XRecordEnableContext() failed\n");
return;
}
// Call XRecordEnableContext to build the XRecord context and enter the event loop.
// The callback function will be evoked on receiving an event from X Server.
if (!XRecordEnableContext(display_datalink, context, callback, (XPointer)this))
{
fprintf(stderr, "XRecordEnableContext() failed\n");
return;
}
}

// To wrap the handleRecordEvent function to avoid that the code about XRecord can not be compiled.
void KeyEventMonitor::callback(XPointer ptr, XRecordInterceptData *data)
{
((KeyEventMonitor *)ptr)->handleRecordEvent(data);
((KeyEventMonitor *)ptr)->handleRecordEvent(data);
}

// The callback function to handle the event from X Server.
void KeyEventMonitor::handleRecordEvent(XRecordInterceptData *data)
{
if (data->category == XRecordFromServer)
{
xEvent *event = (xEvent *)data->data;
switch (event->u.u.type)
if (data->category == XRecordFromServer)
{
case ButtonPress:
//printf("X11 ButtonPress\n");
break;
case ButtonRelease:
//printf("X11 ButtonRelease\n");
break;
case KeyPress:
//printf("X11 KeyPress\n");
break;
case KeyRelease:
//printf("X11 KeyRelease\n");
this->isReleaseAlt(((unsigned char *)data->data)[1]);
break;
default:
break;
xEvent *event = (xEvent *)data->data;
switch (event->u.u.type)
{
case ButtonPress:
//printf("X11 ButtonPress\n");
break;
case ButtonRelease:
//printf("X11 ButtonRelease\n");
break;
case KeyPress:
//printf("X11 KeyPress\n");
break;
case KeyRelease:
//printf("X11 KeyRelease\n");
this->isReleaseAlt(((unsigned char *)data->data)[1]);
break;
default:
break;
}
}
}

fflush(stdout);
XRecordFreeData(data);
fflush(stdout);
XRecordFreeData(data);
}

void KeyEventMonitor::isReleaseAlt(int code)
{
if (code == 64) {
emit KeyAltRelease();
}
if (code == 64) {
emit KeyAltRelease();
}
}
10 changes: 5 additions & 5 deletions keyeventmonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ class KeyEventMonitor : public QThread
{
Q_OBJECT

public:
public:
KeyEventMonitor(QObject *parent = 0);

Q_SIGNALS:
Q_SIGNALS:
void KeyAltRelease();

public Q_SLOTS:
public Q_SLOTS:
void isReleaseAlt(int code);

protected:
protected:
static void callback(XPointer trash, XRecordInterceptData *data);
void handleRecordEvent(XRecordInterceptData *);
void run();

private:
private:
bool isRelease;
};

Expand Down
80 changes: 40 additions & 40 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,64 +36,64 @@

/*
* Check if another process is running.
* Input: program name
* Input: program name
* Return:
* 0 No another process is running.
* 1 Another process is running.
* <0 Exception.
*/
int checkProcessRunning(const char *processName)
{
int uid = getuid();
int pid = getpid();
int uid = getuid();
int pid = getpid();

char pid_file[PATH_MAX_LEN] = {0};
char pid_string[PID_STRING_LEN] = {0};
char pid_file[PATH_MAX_LEN] = {0};
char pid_string[PID_STRING_LEN] = {0};

snprintf(pid_file, PATH_MAX_LEN, "/run/user/%d/%s.pid", uid, processName);
int pid_file_fd = open(pid_file, O_CREAT | O_TRUNC | O_RDWR, 0666);
if (pid_file_fd < 0)
{
fprintf(stderr, "Can not open pid file: %s\n", pid_file);
return -1;
}
snprintf(pid_file, PATH_MAX_LEN, "/run/user/%d/%s.pid", uid, processName);
int pid_file_fd = open(pid_file, O_CREAT | O_TRUNC | O_RDWR, 0666);
if (pid_file_fd < 0)
{
fprintf(stderr, "Can not open pid file: %s\n", pid_file);
return -1;
}

int lock_ret = flock(pid_file_fd, LOCK_EX | LOCK_NB);
if (lock_ret < 0)
{
struct passwd *pwd = getpwuid(uid);
fprintf(stdout, "User %s[%d] has run %s, the current program exits.\n",
pwd->pw_name, uid, processName);
return 1;
}
int lock_ret = flock(pid_file_fd, LOCK_EX | LOCK_NB);
if (lock_ret < 0)
{
struct passwd *pwd = getpwuid(uid);
fprintf(stdout, "User %s[%d] has run %s, the current program exits.\n",
pwd->pw_name, uid, processName);
return 1;
}

snprintf(pid_string, PID_STRING_LEN, "%d\n", pid);
write(pid_file_fd, pid_string, strlen(pid_string));
fsync(pid_file_fd);
snprintf(pid_string, PID_STRING_LEN, "%d\n", pid);
write(pid_file_fd, pid_string, strlen(pid_string));
fsync(pid_file_fd);

return 0;
return 0;
}

int main(int argc, char *argv[])
{
//Check if another process is running.
int check_ret = checkProcessRunning(PROGRAM_NAME);
if (check_ret != 0)
{
return check_ret;
}
//Check if another process is running.
int check_ret = checkProcessRunning(PROGRAM_NAME);
if (check_ret != 0)
{
return check_ret;
}

//sleep(SHORTCAT_REG_DELAY);
//sleep(SHORTCAT_REG_DELAY);

QApplication a(argc, argv);
QFile qss(":/style/global");
QString locale = QLocale::system().name();
QApplication a(argc, argv);
QFile qss(":/style/global");
QString locale = QLocale::system().name();

MainWindow w;
w.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
w.setWindowState(Qt::WindowActive);
//w.setAttribute(Qt::WA_TranslucentBackground, false);
//w.setWindowOpacity(0.5);
MainWindow w;
w.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
w.setWindowState(Qt::WindowActive);
//w.setAttribute(Qt::WA_TranslucentBackground, false);
//w.setWindowOpacity(0.5);

return a.exec();
return a.exec();
}
Loading

0 comments on commit 9aeedb0

Please sign in to comment.