00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016 #include <limits.h>
00017 #include <err.h>
00018
00019 #include "util.h"
00020 #include "data.h"
00021 #include "i3.h"
00022 #include "config.h"
00023 #include "xcb.h"
00024 #include "table.h"
00025 #include "randr.h"
00026 #include "layout.h"
00027 #include "workspace.h"
00028 #include "client.h"
00029 #include "log.h"
00030 #include "ewmh.h"
00031 #include "ipc.h"
00032
00033
00034
00035
00036
00037
00038
00039 Workspace *workspace_get(int number) {
00040 Workspace *ws = NULL;
00041 TAILQ_FOREACH(ws, workspaces, workspaces)
00042 if (ws->num == number)
00043 return ws;
00044
00045
00046 int last_ws = TAILQ_LAST(workspaces, workspaces_head)->num;
00047
00048 DLOG("We need to initialize that one, last ws = %d\n", last_ws);
00049
00050 for (int c = last_ws; c < number; c++) {
00051 DLOG("Creating new ws\n");
00052
00053 ws = scalloc(sizeof(Workspace));
00054 ws->num = c+1;
00055 TAILQ_INIT(&(ws->floating_clients));
00056 expand_table_cols(ws);
00057 expand_table_rows(ws);
00058 workspace_set_name(ws, NULL);
00059
00060 TAILQ_INSERT_TAIL(workspaces, ws, workspaces);
00061
00062 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
00063 }
00064 DLOG("done\n");
00065
00066 ewmh_update_workarea();
00067
00068 return ws;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078 void workspace_set_name(Workspace *ws, const char *name) {
00079 char *label;
00080 int ret;
00081
00082 if (name != NULL)
00083 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
00084 else ret = asprintf(&label, "%d", ws->num + 1);
00085
00086 if (ret == -1)
00087 errx(1, "asprintf() failed");
00088
00089 FREE(ws->name);
00090 FREE(ws->utf8_name);
00091
00092 ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
00093 if (config.font != NULL)
00094 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
00095 else ws->text_width = 0;
00096 ws->utf8_name = label;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105 bool workspace_is_visible(Workspace *ws) {
00106 return (ws->output != NULL && ws->output->current_workspace == ws);
00107 }
00108
00109
00110
00111
00112
00113 void workspace_show(xcb_connection_t *conn, int workspace) {
00114 bool need_warp = false;
00115 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
00116
00117 Workspace *t_ws = workspace_get(workspace-1);
00118
00119 DLOG("show_workspace(%d)\n", workspace);
00120
00121
00122 c_ws->current_row = current_row;
00123 c_ws->current_col = current_col;
00124
00125
00126 workspace_initialize(t_ws, c_ws->output, false);
00127
00128 if (c_ws->output != t_ws->output) {
00129
00130 DLOG("moving over to other output.\n");
00131
00132
00133 Client *old_client = CUR_CELL->currently_focused;
00134
00135 c_ws = t_ws->output->current_workspace;
00136 current_col = c_ws->current_col;
00137 current_row = c_ws->current_row;
00138 if (CUR_CELL->currently_focused != NULL)
00139 need_warp = true;
00140 else {
00141 Rect *dims = &(c_ws->output->rect);
00142 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
00143 dims->x + (dims->width / 2), dims->y + (dims->height / 2));
00144 }
00145
00146
00147 if ((old_client != NULL) && !old_client->dock)
00148 redecorate_window(conn, old_client);
00149 else xcb_flush(conn);
00150
00151
00152
00153 Client* client = c_ws->fullscreen_client;
00154 if (client != NULL && client->workspace != c_ws) {
00155 if (c_ws->fullscreen_client->workspace != c_ws)
00156 c_ws->fullscreen_client = NULL;
00157 client_enter_fullscreen(conn, client, false);
00158 }
00159 }
00160
00161
00162 if (c_ws->output->current_workspace->num == (workspace-1)) {
00163 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
00164 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
00165 set_focus(conn, last_focused, true);
00166 if (need_warp) {
00167 client_warp_pointer_into(conn, last_focused);
00168 xcb_flush(conn);
00169 }
00170
00171 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
00172
00173 return;
00174 }
00175
00176 Workspace *old_workspace = c_ws;
00177 c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
00178
00179
00180 workspace_unmap_clients(conn, old_workspace);
00181
00182 current_row = c_ws->current_row;
00183 current_col = c_ws->current_col;
00184 DLOG("new current row = %d, current col = %d\n", current_row, current_col);
00185
00186 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
00187
00188 workspace_map_clients(conn, c_ws);
00189
00190
00191
00192
00193
00194
00195
00196
00197 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
00198 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
00199 set_focus(conn, last_focused, true);
00200 else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
00201
00202 render_layout(conn);
00203
00204
00205
00206
00207
00208 if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
00209 client_warp_pointer_into(conn, last_focused);
00210 xcb_flush(conn);
00211 }
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
00224 Client *client;
00225 bool empty = true;
00226 bool visible = workspace_is_visible(ws);
00227
00228 ws->output = output;
00229
00230
00231 memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
00232
00233 ewmh_update_workarea();
00234
00235
00236 SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
00237 client->force_reconfigure = true;
00238 empty = false;
00239 }
00240
00241 if (empty)
00242 return;
00243
00244
00245 render_workspace(global_conn, output, ws);
00246
00247
00248 if (visible && !hide_it)
00249 return;
00250
00251
00252
00253
00254 if (c_ws == ws) {
00255 DLOG("Need to adjust output->current_workspace...\n");
00256 output->current_workspace = c_ws;
00257 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
00258 return;
00259 }
00260
00261 workspace_unmap_clients(global_conn, ws);
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
00272 Output *old_output;
00273
00274 if (ws->output != NULL && !recheck) {
00275 DLOG("Workspace already initialized\n");
00276 return;
00277 }
00278
00279 old_output = ws->output;
00280
00281
00282
00283
00284 if (ws->preferred_output == NULL ||
00285 (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
00286 ws->output = output;
00287
00288 DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
00289
00290 if (old_output != NULL && ws->output == old_output)
00291 return;
00292
00293 workspace_assign_to(ws, ws->output, false);
00294 }
00295
00296
00297
00298
00299
00300
00301 Workspace *get_first_workspace_for_output(Output *output) {
00302 Workspace *result = NULL;
00303
00304 Workspace *ws;
00305 TAILQ_FOREACH(ws, workspaces, workspaces) {
00306 if (ws->preferred_output == NULL ||
00307 get_output_by_name(ws->preferred_output) != output)
00308 continue;
00309
00310 result = ws;
00311 break;
00312 }
00313
00314 if (result == NULL) {
00315
00316 TAILQ_FOREACH(ws, workspaces, workspaces) {
00317 if (ws->output != NULL)
00318 continue;
00319
00320 result = ws;
00321 break;
00322 }
00323 }
00324
00325 if (result == NULL) {
00326 DLOG("No existing free workspace found to assign, creating a new one\n");
00327
00328 int last_ws = 0;
00329 TAILQ_FOREACH(ws, workspaces, workspaces)
00330 last_ws = ws->num;
00331 result = workspace_get(last_ws + 1);
00332 }
00333
00334 workspace_initialize(result, output, false);
00335 return result;
00336 }
00337
00338
00339
00340
00341
00342 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
00343 Client *client;
00344
00345 ignore_enter_notify_forall(conn, ws, true);
00346
00347
00348 FOR_TABLE(ws)
00349 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
00350 client_map(conn, client);
00351
00352
00353 if (!ws->floating_hidden)
00354 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
00355 client_map(conn, client);
00356
00357
00358 struct Stack_Window *stack_win;
00359 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
00360 if (stack_win->container->workspace == ws && stack_win->rect.height > 0)
00361 xcb_map_window(conn, stack_win->window);
00362
00363 ignore_enter_notify_forall(conn, ws, false);
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
00376 Client *client;
00377 struct Stack_Window *stack_win;
00378
00379
00380 ignore_enter_notify_forall(conn, u_ws, true);
00381
00382
00383 int unmapped_clients = 0;
00384 FOR_TABLE(u_ws)
00385 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
00386 DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
00387 client_unmap(conn, client);
00388 unmapped_clients++;
00389 }
00390
00391
00392 SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
00393 if (!client_is_floating(client))
00394 continue;
00395
00396 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
00397
00398 client_unmap(conn, client);
00399 unmapped_clients++;
00400 }
00401
00402
00403
00404 if (unmapped_clients == 0 && u_ws != c_ws) {
00405
00406 Client *dock;
00407 DLOG("workspace %p is empty\n", u_ws);
00408 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
00409 if (dock->workspace != u_ws)
00410 continue;
00411
00412 DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
00413 dock->workspace = c_ws;
00414 }
00415 u_ws->output = NULL;
00416 }
00417
00418
00419 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
00420 if (stack_win->container->workspace == u_ws)
00421 xcb_unmap_window(conn, stack_win->window);
00422
00423 ignore_enter_notify_forall(conn, u_ws, false);
00424 }
00425
00426
00427
00428
00429
00430
00431 void workspace_update_urgent_flag(Workspace *ws) {
00432 Client *current;
00433 bool old_flag = ws->urgent;
00434 bool urgent = false;
00435
00436 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
00437 if (!current->urgent)
00438 continue;
00439
00440 urgent = true;
00441 break;
00442 }
00443
00444 ws->urgent = urgent;
00445
00446 if (old_flag != urgent)
00447 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
00448 }
00449
00450
00451
00452
00453
00454 int workspace_width(Workspace *ws) {
00455 return ws->rect.width;
00456 }
00457
00458
00459
00460
00461
00462
00463 int workspace_height(Workspace *ws) {
00464 int height = ws->rect.height;
00465 i3Font *font = load_font(global_conn, config.font);
00466
00467
00468 Client *client;
00469 SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
00470 height -= client->desired_height;
00471
00472
00473 if (!config.disable_workspace_bar)
00474 height -= (font->height + 6);
00475
00476 return height;
00477 }